For problems 1 through 4 explain why the code as shown is al

For problems 1 through 4, explain why the code as shown is almost certainly not what the programmer intended, and how it should be fixed to work the way the programmer probably had in mind.

Number your answers to match with the questions asked.

Upload your answers as a single Microsoft Word document. You can include the Java code snippet (copy and paste from your .java file) along with explanation in the Word document. Upload only a single Word document as your answer.

1. (10 pts) What is wrong with the following program and how should it be fixed?

1 public class MyClassA {
2    int v = 12;
3
4    public MyClassA (int pV) {
5      v = pV;
6 }
7
8    public static void main (String args []) {
9    MyClassA m = new MyClassA ();
10 } // end main
11 } // end class MyClassA

2. (10 pts) What is wrong with the following program and how should it be fixed?

1 public class MyClassB {
2    int v = 12;
3
4    public void MyClassB (int pV) {
5      v = pV;
6    }
7
8    public static void main (String args []) {
9      MyClassB m = new MyClassB (23);
10   } // end main
11 } // end class MyClassB

3. (10 pts) What is wrong with the following program and how should it be fixed?

1   public class MyClassD {
2 public static void main (String args []) {
3    MyClassC m = new MyClassC (23);
4 } // end main
5   } // end class MyClassD
6
7   class MyClassC {
8     int v = 12;
9
10    public MyClassC (int pV) {
11      int v = pV;
12    }
13
14 } // end class MyClassC


4. (10 pts) What is wrong with the following program and how should it be fixed?

1   public class MyClassE {
2     public static void main (String args []) {
3       MyClassF m = new MyClassF (23);
4     } // end main
5   } // end class MyClassE
6
7   class MyClassF {
8     int v = 12;
9
10    private MyClassF (int pV) {
11      v = pV;
12    }
13
14 } // end class MyClassF

5. (10 pts) Given all the problems identified in problems 1 through 4, explain in detail why the following code works, ie, compiles without errors or warnings.

1 public class MyClassG {
2    public static void main (String args []) {
3      MyClassH m = new MyClassH (23, true);
4    } // end main
5 } // end class MyClassG
6
7 class MyClassH {
8    int v = 12;
9
10   public MyClassH (int x, boolean b) {
11     this (x);
12   }
13
14   private MyClassH (int pV) {
15     v = pV;
16 }
17
18 } // end class MyClassH

6. (10 pts) Explain why the following class hierarchy is not reasonable:

DefenseDepartmentGeneral

Private

7. (10 pts) Give at least one example of a reasonable field for each of the following classes in the following class hierarchy. Be sure that the field is at the right level in the hierarchy.

Vehicle

Car

Airplane

Passenger

Fighter

Bomber

SpaceShip

8. (10 pts) Give at least one example of a reasonable method for each of the following classes in the following class hierarchy. Be sure that the method is at the right level in the hierarchy. Constructors, getters and setters don\'t count for this problem.

Vehicle

Car

Airplane

Passenger

Fighter

Bomber

SpaceShip

9. (10 pts) Are a Private and a Platoon in an encapsulation or an inheritance relationship? Explain

10. (10 pts) Present reasonable parent and child classes for the class Tree (the biological kind). Give a short explanation for why the classes you are proposing are in good parent-child relationships.

Solution

General Note : The part in the code which needed to be fixed is highlighted in bold.

Q1)

the class MyClassA would compile as there is no constructor without argument. To fix the code you would need to add a parameter to the place where you create the object.

public class MyClassA {
int v = 12;

public MyClassA (int pV) {
v = pV;
}

public static void main (String args []) {
MyClassA m = new MyClassA (1); // add the parameter of type int to the instantition. any int value would do

} // end main
} // end class MyClassA

Q2) There is no constructor with int argument defined for MyClass B. So the code for ClassB wouldn\'t compile, To fix that we would need to remove the \" void \" from the function signature to convert it to constructor.

public class MyClassB {

int v = 12;

//removed the return type \"void \" from method signature to make it a constructor

public MyClassB (int pV) {

v = pV;

}

public static void main (String args []) {

MyClassB m = new MyClassB(23);

} // end main

} // end class MyClassB

Q3) The value for instance variable v is alwaus 12, the value we pass from the constructor is never applied. To fix the code use this implementation :

public class MyClassD {

   public static void main (String args []) {

   MyClassC m = new MyClassC(23);

System.out.println(m.v);

   } // end main

} // end class MyClassD

class MyClassC {

   int v = 12;

   public MyClassC (int pV) {

   v = pV; // remove the int from start of variable v

   }

} // end class C

Q4) The constructor for class F is not visible in Class E. We need to change the visibility to default or public for it to be visible outside Class F. Fixed Code below :

public class MyClassE {

public static void main (String args []) {

MyClassF m = new MyClassF (23);

} // end main

} // end class MyClassE

class MyClassF {

int v = 12;

public MyClassF (int pV) { //removed the private access modifier from the constructor declaration

v = pV;

}

} // end

Q5)
The code above compiles fine beacuse all the statements inside the class are syntactically valid.
There is a call to two arg constructor while instantiating m which exists is MyClassH so this part is valid.
This two arg constructor internally calls the single arg constructor in MyClassH like this : this(x) which is also valid.
  

Q6)

DefenseDepartmentGeneral - is very specific to Defense domain. Not every object in this world can fit in to DefenseDepartmentGeneral category.
where as \"Private\" is very generic and can be fit to any domain. So this hierarchy makes no sense.We use hierarchy to define reltionships. A \"Private\" can be related to anything in the world but that is not the case with \"DefenseDepartmentGeneral\".


Q7)
Vehicle - int capacity

Car - String vin

Airplane - String airlineCarrier

Passenger - List<Boggies> boggieList // Assuming this is talking about passengerTrains

Fighter - List<Weapon> weaponList

Bomber - float bombingCapacityTonnes

SpaceShip - String spaceStation
  
Q8)
Vehicle - public void displaySpeedOnSpeedo()

Car - public void autoShiftGears()

Airplane - public void upgradeTravellerClass()

Passenger - public List<Passenger> getPassengerList()

Fighter - public void fireArms(Weapon weapon)

Bomber - public void dropBomb()

SpaceShip - public void transmitSignalToSpaceStation()

q9) Not clear what do you mean by \"Private\" here.

q10)

Tree
|-Hybrid
| |-Fruit Bearing
| | |- Human Consumable
| | |
| | |- Non Cosumable
| |
|   |-Non Fruit Bearing
|
|
|-Natural
| |-Fruit Bearing
| | |- Human Consumable
| | |
| | |- Non Cosumable
| |-Non Fruit Bearing

For problems 1 through 4, explain why the code as shown is almost certainly not what the programmer intended, and how it should be fixed to work the way the pro
For problems 1 through 4, explain why the code as shown is almost certainly not what the programmer intended, and how it should be fixed to work the way the pro
For problems 1 through 4, explain why the code as shown is almost certainly not what the programmer intended, and how it should be fixed to work the way the pro
For problems 1 through 4, explain why the code as shown is almost certainly not what the programmer intended, and how it should be fixed to work the way the pro
For problems 1 through 4, explain why the code as shown is almost certainly not what the programmer intended, and how it should be fixed to work the way the pro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site