Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Quiz: Casting Objects in Java

DZone's Guide to

Java Quiz: Casting Objects in Java

A new quiz on working with an array of objects in Java.

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

First, let's look at the answer to last week's quiz.

if(row <= 4 && col < 3)

Status = RESERVED;

The above statement changes some of the seat status from free to reserved as shown below

if(row >= 3 && col != 3)

Status = OCCUPIED;

The above statement changes some of the seat status from (free and reserved) to occupied as shown below

// First Row

hall[1][1] //changed from Free to Res

hall[1][2] //changed from Free to Res

hall[1][3]

hall[1][4]
// First Row

hall[1][1] Reserved

hall[1][2] Reserved

hall[1][3] Free

hall[1][4] Free
// Second Row

hall[2][1] //changed from Free to Res

hall[2][2] //changed from Free to Res

hall[2][3]

hall[2][4]
// Second Row

hall[2][1] Reserved

hall[2][2] Reserved

hall[2][3] Free

hall[2][4] Free
// Third Row

hall[3][1] //changed from Free to Res

hall[3][2] //changed from Free to Res

hall[3][3]

hall[3][4]
// Third Row

hall[3][1] //changed from Res to Occ

hall[3][2] //changed from Res to Occ

hall[3][3] Free

hall[3][4] //changed from Free to Occ
// Fourth Row

hall[4][1] //changed from Free to Res

hall[4][2] //changed from Free to Res

hall[4][3]

hall[4][4]
// Fourth Row

hall[4][1] //changed from Res to Occ

hall[4][2] //changed from Res to Occ

hall[4][3] Free

hall[4][4] //changed from Free to Occ
// Fifth Row

hall[5][1]

hall[5][2]

hall[5][3]

hall[5][4]
// Fifth Row

hall[5][1] //changed from Free to Occ

hall[5][2] //changed from Free to Occ

hall[5][3] Free

hall[5][4] //changed from Free to Occ

Answer B is correct!

The result is: free = 7, reserved = 4 and occupied = 9

Here's another method that may help clarify the above answer:

public void getSeatStatus(){
int nrFr = 0;
int nrOC = 0;
int nrRS = 0;
for(int row = 1; row <= NUMBER_ROWS; row ++ ) {
for(int col = 1; col <= NUMBER_COLS; col ++ ) {
if(hall[row][col].status == Status.RESERVED) {
nrRS ++ ;
}else if(hall[row][col].status == Status.OCCUPIED) {
nrOC ++ ;
}else {
nrFr ++ ;
}
System.out.println(hall[row][col].status);
}
}
System.out.print("nr free: " + nrFr + " nr reserved: " + nrRS + " nr occupied: " + nrOC);
}
public static void main(String[] args){
Seat seat = new Seat(0,0);
seat.initHall();
seat.getSeatStatus();
}

You can find this week's quiz below. This quiz is aimed at intermediate to advanced Java programmers.

Purpose of the Quiz

  1. Learn some tricks and behaviors about casting objects.

  2. Find out whether it is possible to cast an object and add it with the object to a single Java ArrayList.

  3. Imagine the changes of the elements in an ArrayList.

  4. Write a piece of code to check your expectations.

The following program adds 12 objects to a list:

public class MySuper {
   public int id = 20;
    public int getNumber(){
        return 12;
    }
}

public class MySub extends MySuper {
    int id = 40;
    public int getNumber(){
        return 15;
    }
}

import java.util.ArrayList;
import java.util.List;

public class MyApp {

  private List<MySuper> listMySub = new ArrayList<MySuper>();

    void method(){
        for(int i = 0; i < 12; i ++ ) {
            MySuper myS = new MySub();
            MySuper mySuper = new MySuper();
            if(i < 6 || i == 10) {
                listMySub.add(myS);
            }else if(i >= 7) {
                listMySub.add(mySuper);
            }else {
                listMySub.add(myS);
            }
        }
    }
    public static void main(String[] args){
        MyApp ma = new MyApp();
        ma.method();
    }
}
  1. Assuming that the index of the twelve objects starts from 0 to 11, which one of the following statements is true about the sixth and the eighth objects in the ArrayList?

  2. Add a piece of code to this program to check out your answer!

As always, you can find Sar at his site. Let us know your thoughts in the comments!

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:
class ,java ,objects

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}