Java

OOP GUIDE / WORK

 

PRISM CLASS

 

Topics

  • Simple classes (instance variables, constructors, methods)

 

 

TASK – PART 1 – SETUP

 

Copy and paste the following incomplete class into your IDE:

 

 

public class Prism

{

     public double width;

     public double height;

     public double depth;

 

     public Prism(double width, double height, double depth)

     {

           //more here

     }

 

     public Prism()

     {

           //more here

     }

}

 

 

 

TASK – PART 2 – COMPLETE THE CLASS

 

Make the following completions/additions:

 

a) Write the statements required to implement the first constructor.

b) Write the statements required to implement the 2nd constructor.  It should give random values between 0 and 10 for each length.


c) Write a method called volume that calculates and returns the volume of the Prism object.

d) Write a method called maxSide that returns the length of the side with the maximum length.  For example, if a prism has a width of 8, a height of 6 and a depth of 9, then this method will return 9.

 

e) Write a toString() method that returns a String of the form:

          [W8.2 x H8.1 x D9.2]

 

 

TASK – PART 3 – TESTING THE CLASS

 

a) Create a tester class with a main function. 

 

b) Write a statement needed to create a Prism with constructor #1.  Output the object to the screen (using toString()).

 

c) Write a statement needed to create a Prism with constructor #2.  Output the object to the screen (using toString()).

 

d) Test both the volume method and the maxSide method.