Java OOP

 

SHORT ANSWER QUESTIONS

AGGREGATION

 

QUESTIONS


Fill the blanks and short answer questions.  Solutions are at the bottom of this document.

 

a)    ______________ is the concept of having objects be instance variables inside other objects.  So, objects can be made up of different objects.

b)    For each of the following classes showing their instance variables, state whether there is Aggregation or not.

A)
public class Point
{
   public int x;
   public int y;

   //Nothing else shown.
}

B)
public class Address
{
   private int number;

   private String street;
   private int apt;

   //Nothing else shown.
}

C)

public class Ingredient

{

   public String name

   public double amount;

   public String unit;

   //Nothing else shown.

}

D)

public class Trio

{

   private byte n1;

   private byte n2;

   private byte n3;

   //Nothing else shown.

}

E)
public class Recipe

{

   public Ingredient ing1;

   public Ingredient ing2;

   public Ingredient ing3;

   public Ingredient ing4;

   public Ingredient ing5;


   //Nothing else shown.

}

F)
public class Light
{
   public boolean on
   public int brightness;

   //Nothing else shown.
}

 

c)     How can we easily tell if an instance variable (or any variable really) is a primitive data type or an object?

 

 

 

 

 

 

d)    Complete the following Line constructors given the Point class on the right.

 

public class Line
{
   public Point p1;
   public Point p2;

   public Line(Point tp1, Point tp2)

   {   }

   public Line(int x1, int y1, int x2, int y2

   {   }
}

public class Point
{
   public int x;

   public int y;

 

   public Point(int tx, int ty)

   {
      x = tx;

      y = ty;
   }
}

 

e)    Consider the Name and Address classes below and complete the Person class’ constructors. 

 

public class Name

{

   public String first;

   public String mid;

   public String last;

   public Name(String f, String m, String l)

   {

      first = f;

      mid = m;

      last = l;

   }

}

public class Address

{

   public int number;

   public String street;

   public String town;

   public String province;

   public String postalCode;

 

   public Address(int n, String s, String t, String p, String pc)

   {

      number = n;

      street = s;

      town = t;

      province = p;

      postalCode = pc;

}

 

public class Person

{

   public Name n;

   public Address a;

 

   public Person(Name n, Address a)
   {   }

 

   public Person(String f, String m, String l, int n, String s, String t, String p, String pc)

   {   }

  

}

 

f)      Assume the above Name and Address classes are changed so that all instance variables are private and that there is a get method for each one.

1) Show the entire implementation of the getProvince() method in Address.

 

2) Assume the Person class’ instance variables are set to private.  Show the entire implementation of the getAddress() method.

3) If we wanted to have a getPostalCode() method in the Person class, how would you implement that?

 

 

SOLUTIONS

 

Click here.