SEN 964 OOD in JavaAssignment #6

Name: ______ID: ______

Section 1: (15 points) Answer the following questions using words, NO source code examples. A sentence or two is sufficient.

  1. Describe the four main characteristics of OOD (A PIE).
  2. What is an instance method? What is a class method?
  3. Define overloading a method. Describe how this is done.
  4. What is an overridden method in Java?
  5. Give an example when you can not overload a method in Java.
  6. Give an example when you can not override a method in Java.
  7. List the different access levels for fields and methods in Java.
  8. Explain the differences between super and this in Java.
  9. Explain one way to achieve polymorphism in Java.
  10. What is a constructor? When are they called?
  11. What are the differences between an abstract class and an interface in Java?
  12. An object is composed of identity, state, and behaviors. Give an example of an identity, state and behavior.
  13. What is meant by Information Hiding in OOD?
  14. Describe the advantages of using Inheritance for a program.
  15. Do we have a Deconstructor in java? Why or Why not?

Section 2: (4 points) Answer the following questions withJava source code.

  1. Show with Java source code how to inherit one class from another.
  2. Show with Java source code how to create and use an Abstract class.
  3. Show with Java source code how to create and use an Interface class.
  4. Show with Java source code how to create a subclass and overload aconstructor of the superclass.

Section 3: (6 points) Miscellaneous Java Concept Questions.

1.What will be the output of the following Java program? Explain why. You can cut-n-paste the code, compile and run it and then see the output but still explain why you get what you see.

class Parent
{
static
{
System.out.println( "Parent static" );
}
}
class Child extends Parent
{
{
System.out.println( "Child non-static" );
}
public Child()
{
System.out.println( "In child constructor" );
}
static
{
System.out.println( "Child static" );
}
publicstaticvoid main( String[] arguments )
{
new Child();
new Child(); //Yes I mean to repeat it here
}
}

2. You can assume that the Weather class is stored in a file named Weather.java and the ProbTwo class is stored in a file named

ProbTwo.java. This code compiles and runs.

publicclass Weather {
privatedouble humidity;
public Weather(double theHumidity) {
humidity = theHumidity;
}
publicvoid setHumidity(double theHumidity) {
humidity = theHumidity;
}
publicdouble getHumidity() {
return humidity;
}
}

publicclass ProbTwo {
publicvoid one(int x, int y) {
System.out.println(x * y);
System.out.println(x % y);
System.out.println(x / y);
System.out.println(x + (y / 5));
System.out.println(x == y);
}

publicvoid two(double f, double g, double k) {
if ((f <= g) || (f == 0.0)) {
if (!(g > k) || false) {
System.out.println("Case 1");
} else {
System.out.println("Case 2");
}
} else {
System.out.println("Case 3");
}
}

publicvoid three(int b) {
int i = 1;
while (i <= b) {
if (i == 2) {
i = i + 1;
}
System.out.println(i);
i = i + 1;
}
System.out.println(i);
}

publicvoid four(Weather mon, Weather fri, double maxTemp) {
mon = new Weather(100.00);
fri.setHumidity(90);

fri.setHumidity(70);
maxTemp = 110;
}
}

a.What is the output produced by executing the following code segment:

ProbTwo prob2 = new ProbTwo();
prob2.one(8, 4);

b.What is the output produced by executing the following code segment:

ProbTwo prob2 = new ProbTwo();
prob2.two(5.0, 10.0, 7.0);
prob2.two(5.0, 8.0, 9.0);

c.What is the output produced by executing the following code segment:

ProbTwo prob2 = new ProbTwo();
prob2.three(3);

3. Identify the classes in the following description of a refrigerator. List at least one responsibility for each class. You do NOT need to create the classes in Java. Just describe in words or any way that you want what set of classes you would create for the example description below.

A refrigerator has a motor, a temperature sensor, a light and a door. The motor turns on and off primarily as prescribed by the temperature sensor. However, the motor stops when the door is opened. The motor restarts when the door is closed if the temperature is too high. The light is turned on when the door opens and is turned off when the door is closed.

1