Java
OOP GUIDE / WORK
ROUNDTABLE CLASS
SOLUTIONS
TASK –
PART 1 – ROUNDTABLE CLASS
Here is my solution:
public class
RoundTable
{
public double diameter;
public double height;
public
String material;
public RoundTable(double td, double th, String tm)
{
diameter = td;
height = th;
material = tm;
}
public
String toString()
{
return "RoundTable:Diameter="+diameter+"
Height="+height+"
Material="+material;
}
public int seatingSpots()
{
if (diameter
>= 10)
return 15;
else if (diameter
>= 9)
return 14;
else if (diameter
>= 8)
return 12;
else if (diameter
>= 7)
return 11;
else if (diameter
>= 6)
return 9;
else if (diameter
>= 5)
return 8;
else if (diameter
>= 4)
return 6;
else if (diameter
>= 3)
return 3;
else //not
in chart
return 1;
}
}
|
Note: The implementation of the seatingSpot()
method could be different. The coder
could have assumed that the exact diameters would be provided. In my implementation, there are issues. If a table would have a diameter of 20, it
still would only have 15 seating spots.
Also, I give 1 seating spot to tables under 3 diameter
TASK –
PART 2 – TESTING YOUR ROUNDTABLE CLASS
Here is my solution:
public class RoundTableTester
{
public static void
main(String[] args)
{
RoundTable rt = new RoundTable(4.1, 3.0, "wood");
System.out.println(rt);
System.out.println(rt.seatingSpots());
}
}
|
TASK –
PART 3 – SHORT ANSWER QUESTION
Here is the statement:
int totalSeating = t1.seatingSpots() + t2.seatingSpots() + t3.seatingSpots()
+ t4.seatingSpots();
|
|