This lab contains sample solutions to the exercise set.
Open your working shapes project located in workspaceBlueJ/labs/session01 folder.
Check that the Circle class compiles.
Change diameter
We are asked to change the diameter of a circle object.
In order to do this we require a setter method.
Here is the code for a setter that facilitates changing the diameter:
public void setDiameter(int diameter)
{
this.diameter = diameter;
}
Retrieve area modified circle object
Create a Circle object on the object bench.
Create a new default Circle.
Invoke setDiameter on the circle object, providing, for example, 100, as the new diameter.
Invoke getArea on the circle object.
The answer returned should be 7853.98 to two decimal places.
Check answer
Calculator check: Area circle diameter 100 is pi*r*r = 3.141592654*50*50 = 7853.98
Method to return circumference
public double getCircumference()
{
return Math.PI*diameter;
}
Invoke method on default Circle object
Inspect state
Open the inspector window (Figure 2).
Change diameter and recheck circumference
Change the diameter of the default Circle object to, for example, 100.
Method to return calculate area triangle
public double getArea()
{
return (height*width)/2;
}
Invoke getArea()
Instantiate a default Triangle on the object bench.
Exercise 4
We recommend you implement the setter method public void setCapacity(int capacity) before commencing the solution so as to avoid the necessity to recompile the project during the exercise.
Here is a (naive) setter method which you should add to the LabClass source code.
public void setCapacity(int capacity)
{
this.capacity = capacity;
}
Create LabClass object named labClass1
Invoke numberStudents method on labClass1
Invoke enrollStudent on Student object, student1
Create a further 3 student objects
Open LabClass inspector
The solution provided does not contain any validation. Validation will be the subject of later labs.
Create a new class called Cone to represent a right circular cone (Figure 1).
The fields of the Cone class are
public Cone(double height, Circle base);
Right cick on the BlueJ shapes window and in the drop-down menu that appears, invoked new Class and name it Cone.
Open Cone in the editor and delete all except the following:
public class Cone
{
}
Check that this compiles.
We are obliged to comply with the specification for the class' fields which are a Circle object reference and a primitive type, a double, representing the cone height.
Add these fields and recompile.
public class Cone
{
private double height;
private Circle base;
}
A constructor signature (including modifier and return type) is also specified: here is the constructor code:
public Cone(double height, Circle base)
{
this.height = height;
this.base = base;
}
Add the constructor to the class and recompile.
Here is the complete source code for the Cone class as specified in the exercise:
public class Cone
{
private double height;
private Circle base;
public Cone(double height, Circle base)
{
this.height = height;
this.base = base;
}
}
Write and test a method to calculate and return the volume of the cone.
Method to calculate volume of cone
The volume of a right circular cone is 1/3 height area base which is equivalent to PI times diameter squared divided by 12.
See here for further information.
This may be expressed in Java as follows:
Math.PI*diameter*diameter*height/12;
Notice that a class Math is used to access pi.
We could have simply inserted a numberic value, for example, 3.141592654.
However, the use of number values throughout code, pejoratively referred to as magic numbers, is strongly discouraged because
The Math class is contained with the java.lang package. This is a fundamental package and does not require an import statement.
We can incorporate the above statement into a the volume method as follows:
public double volume()
{
double diameter = base.getDiameter();
return Math.PI*diameter*diameter*height/12;
}
Add this method to the Cone class and attempt to compile.
This may generate a compile-time error with the following message output at the bottom of the Cone-shapes window as illustrated in Figure 1.
cannot find symbol - method getDiameter()
The message is informing us that our attempt to invoke a method in the Circle class has failed because no such method is accessible
To resolve this issue open the Circle source code in the editor and insert a getter method for the diameter field as follows:
public double getDiameter()
{
return diameter;
}
Observe that we have returned diameter as a double despite the variable being declared as an int in Circle. Clearly, the integer value is being transparently cast to a double by the compiler.
Add getDiameter to Circle and press the compile button on the BlueJ: shapes window.
You should see a message at the bottom of the window: Compiling... Done, indicating that the Cone and associated classes are free of syntactic errors.
Testing method to calculate volume of cone
We shall use 3 methods to test the method we have just written - public double volume().
Right click on the Cone object
Check the answer as follows:
In this case an anonymous Circle object is created as an argument in the Cone constructor
Construct a new class called TestCone.
public class TestCone
{
}
Add the following method:
public double volume()
{
double height = 100;
Circle base = new Circle();
Cone cone = new Cone(height, base);
return cone.volume();
}
This method creates a Cone object with similar dimensions to these created in method 1 and 2 above and so we would expect to have the same volume returned by the volume method.
The method volume in TestClass is quite verbose: we have written it so to illustrate clearly the steps being taken.
Here are two progressively more terse versions of the same method.
public double volume2()
{
Cone cone = new Cone(100, new Circle());
return cone.volume();
}
public double volume3()
{
return new Cone(100, new Circle()).volume();
}
Add these method to TestClass and invoke each in turn, verifying that all three return similar values as illustrated in Figure 6.
Here is the complete source for TestClass
public class TestClass
{
public double volume()
{
double height = 100;
Circle base = new Circle();
Cone cone = new Cone(height, base);
return cone.volume();
}
public double volume2()
{
Cone cone = new Cone(100, new Circle());
return cone.volume();
}
public double volume3()
{
return new Cone(100, new Circle()).volume();
}
}
Write and test a method to calculate and return the surface area of the cone including the base.
Derivation of the volume of a cone is elegantly dealt with here.
Here is the code for a method surfaceArea that uses this formula:
public double surfaceArea()
{
double radius = base.getDiameter()/2.0;
double slantHeight = Math.hypot(radius, height);
double areaTop = Math.PI*radius*slantHeight;
double areaBase = base.getArea();
return areaTop + areaBase;
}
Add this method to the Cone class and compile.
You will encounter a compile-time error:
We can fix this be adding a method, getArea to the Circle class.
Open the Circle class code in the editor and add this method:
public double getArea()
{
return Math.PI*diameter*diameter/4;
}
The project shapes should now compile.
Use the three methods employed in the previous Exercise 6 to test the surfaceArea method.
Here, in Figure 2, is the outcome of a test using a default Circle object from the object bench.