Java

OOP GUIDE / WORK

 

PRISM CLASS SOLUTIONS

 

 

TASK – PART 1 – SETUP

 

No solution required

 

 

TASK – PART 2 – PRISM CLASS

 

Here is my solution:

 


public
class Prism

{

     public double width;

     public double height;

     public double depth;

 

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

     {

           this.width = width;

           this.height = height;

           this.depth = depth;

     }

 

     public Prism()

     {

           width = Math.random() * 10;

           height = Math.random() * 10;

           depth = Math.random() * 10;

     }

    

     public double volume()

     {

           return width * height * depth;

     }

    

     public double maxSide()

     {

           return Math.max(width, Math.max(height, depth));

     }

    

     public String toString()

     {

           return "[W" + width + " x H" + height + " x D" + depth + "]";

     }

}

 

 

 

TASK – PART 3 – TESTING THE CLASS

 

Here is my solution:

 

 

public class PrismTester

{

     public static void main(String[] args)

     {

           Prism p1 = new Prism(4.2, 8.0, 7.1);

           System.out.println(p1);

          

           Prism p2 = new Prism();

           System.out.println(p2);

          

           System.out.println(p1.volume());

          

           System.out.println(p1.maxSide());

     }

}