Exercise 10 Solution

advertisement
44-141 Exercises 10
In these exercises, you will use BlueJ to define and test a definition for class Square,
which models the mathematical concept of a square; Rectangle, that models a rectangle,
and Circle, that (well, guess).
Part 1: Defining and Compiling the Square Class
1.
In BlueJ, create a new project. Then click on New Class, choose Class, and
type Square for the name of the class.
2.
Right-click on the Square class icon and open the editor.
3.
In the JavaDoc comment at the beginning of the class definition,
(a)
replace the text
"write a description of class Square here"
with
"This class models the mathematical concept of a square."
(b)
replace
"yourCourse-sectionNumber"
"yourName"
"currentDate"
with the appropriate information.
4.
Squares have just one attribute—the length of a side. In the "instance variables"
section, declare an instance variable sideLength of type double and having
private visibility.
5.
In the "constructor(s)" section, define:
(a)
A constructor with no parameters that initializes sideLength to 0.0:
public Square()
{
sideLength = 0.0;
}
(b)
A constructor with one parameter sideLengthInit that initializes
sideLength to the specified value:
public Square( double sideLengthInit )
{
sideLength = sideLengthInit;
}
6.
In the "other methods" section, define:
(a)
A method getSideLength() that returns the value of sideLength:
public double getSideLength()
{
return sideLength;
}
(b)
A method setSideLength() that sets sideLength to the specified
value:
public void setSideLength( double length )
{
sideLength = length;
}
(c)
A method area() that returns the area of the square:
public double area()
{
return sideLength * sideLength;
}
(d)
A method perimeter() that returns the perimeter of the square:
public double perimeter()
{
return 4.0 * sideLength;
}
7.
Compile your class definition by clicking on the Compile button in the upper left
portion of the editor window. Correct any syntax errors reported by the compiler.
8.
Close the editor window.
Part 2: Testing the Constructors and Methods
9.
Right click on the Square class icon.
10.
Click on new Square() and then click OK. This calls the constructor with no
parameters to create a new Square object. An icon for the Square object
square1 will appear at the bottom left of the window.
11.
Right click on the square1 object icon, and then click Inspect.
What is the value of the sideLength instance variable of square1?
Close the Object Inspector window.
12.
Right click on the Square class icon. Click on
new Square(double sideLengthInit),
enter 5.0 in the box provided for the argument value, and then click Ok. This
calls the constructor with one parameter to create a new Square object. An icon
for the Square object square2 will appear at the bottom left of the window.
13.
Test the getSideLength() method as follows:
Right click on the square2 object icon.
Click on double getSideLength().
What value is returned by this method? 5 1 point
Close the Method Result window.
14.
Use a similar procedure to test the area() method.
What value is returned by this method? 25 1 point
15.
Use a similar procedure to test the perimeter() method.
What value is returned by this method? 20 1 point
16.
Use the setSideLength() method to change the side length of the square1
object to 10.0.
17.
Use Inspect to verify that the side length is now 10.0.
18.
Use the area() method to find the area of square2.
Create a class Rectangle and test your constructors and methods.
1.
In BlueJ, create a new project.
2.
Click the New Class button.
3.
Enter Rectangle in the Class Name field, and click OK.
4.
Open the editor for the Rectangle class.
5.
Fill in the required information in the header section (as you did with the Square).
/**
* This class models the mathematical concept of a rectangle.
* Computer Programming I - Section 3
* Name
* November 19, 2010
*/
6.
The attributes of a Rectangle object will be its width and height.
In the instance variables section, declare two instance variables width and
height, both private and of type double.
private double width; 2 points: 1 for “private” 1 for type
private double height; 2 points: 1 for “private” 1 for type
7.
In the constructors section, define a constructor with no parameters. It sets both
the width and height to the default values of 1.0. Refer back to the first
constructor for the Square for help.
public Rectangle() {
width = 1.0;
height = 1.0;
}
3 points: 1 for a valid constructor with no parameters, 1 for initializing each field to 1.0
NOTE: The name of the constructor must be the name of the class you are
defining.
8.
In the constructors section, define a constructor with two parameters theWidth
and theHeight, both of type double. This constructor will set the attributes
of the Rectangle object to the specified values.
public Rectangle(double theWidth, double theHeight) {
width = theWidth;
height = theHeight;
}
3 points: 1 for a valid constructor with two parameters, 1 for initializing each field using
the formal parameters
9.
Define a method getWidth() that returns the width of a Rectangle object.
public double getWidth() {
return width;
}
2 points: 1 for the return type, 1 for returning width
10.
Define a method getHeight() that returns the height of a Rectangle object.
public double getHeight() {
return height;
}
2 points: 1 for the return type, 1 for returning height
11.
Define a method perimeter() that returns the perimeter of a Rectangle
object.
public double perimeter() {
return 2 * (width + height);
}
3 points: 1 for the return type, 2 for formula
12.
Define a method area() that returns the area of a Rectangle object.
public double area() {
return height * width;
}
3 points: 1 for the return type, 2 for formula
13.
Compile the Rectangle class and correct any syntax errors.
14.
Test your Rectangle constructors and methods in the same way you tested the
Square class. There is no need to answer your questions as you did in the
Square class, but you should include the complete code for the Entire Rectangle
Class (the entire file, not just the methods):
/**
* This class models the mathematical concept of a rectangle.
* Computer Programming I - Section 3
* Name
* November 19, 2010
*/
public class Rectangle
{
// instance variables
private double width;
private double height;
/**
* Constructor for objects of class Rectangle
*/
public Rectangle()
{
width = 1.0;
height = 1.0;
}
public Rectangle(double theWidth, double theHeight)
{
width = theWidth;
height = theHeight;
}
public double getWidth()
{
return width;
}
public double getHeight()
{
return height;
}
public double perimeter()
{
return 2.0 * (width + height);
}
public double area()
{
return width * height;
}
}
(points listed above)
In these exercises, you will create a class Circle and test your constructors and
methods.
1.
In BlueJ, create a new project.
2.
Click the New Class button.
3.
Enter Circle in the Class Name field, and click OK.
4.
Open the editor for the Circle class.
5.
Fill in the required information in the header section.
/**
* This class models the mathematical concept of a circle.
* Computer Programming I - Section 3
* Name
* November 19, 2010
*/
1 point
6.
The only attribute of a Circle object will be its radius.
In the instance variables section, declare an instance variable radius of type
double with private visibility.
private double radius; 2 points: 1 for type, 1 for private
7.
In the constructors section, define a constructor with no parameters. It sets the
radius to the default value of 1.0.
public Circle() {
radius = 1.0;
}
2 points: 1 for a constructor with no args, 1 for setting radius to 1
8.
In the constructors section, define a constructor with one parameter
radiusInit of type double. This constructor will set the radius of the
Circle object to the specified value.
public Circle(double radiusInIt) {
radius = radiusInIt;
}
2 points: 1 for the constructor with 1 arguemtn, 1 for setting the radius
9.
Define a method getRadius() that returns the radius of a Circle object.
public double getRadius() {
return radius;
}
2 points: 1 for the getter, 1 for the return
10.
Define a method area() that returns the area of a Circle object.
Recall: the area of a circle is r2, where r is the radius.
public double area() {
return Math.PI * Math.pow(radius, 2);
}
4 points: 1 for the method, 2 for the calculation, 1 point for Math.PI vs typing in 3.141…
(raidus*radius is fine rather than Math.pow())
11.
Define a method circumference() that returns the circumference of a
Circle object.
Recall: the circumference of a circle is 2r, where r is the radius.
public double circumference() {
return 2 * Math.PI * radius;
}
4 points: 1 for the method, 2 for the calculation, 1 point for Math.PI vs typing in 3.141…
(raidus*radius is fine rather than Math.pow())
12.
Compile the Circle class and correct any syntax errors.
13.
Test your Circle constructors and methods in the same way you tested the
Square and Rectangle. Again, there is no need to answer your questions as
you did in the Square class, but you should include the complete code for the
Entire Circle Class (the entire file, not just the methods):
/**
* This class models the mathematical concept of a circle.
* Computer Programming I - Section 3
* Name
* November 19, 2010
*/
public class Circle
{
// instance variables
private double radius;
/**
* Constructor for objects of class Circle
*/
public Circle()
{
radius = 1.0;
}
public Circle(double radiusInit)
{
radius = radiusInit;
}
public double getRadius()
{
return radius;
}
public double area()
{
return Math.PI * Math.pow(radius, 2);
}
public double circumference()
{
return 2 * Math.PI * radius;
}
}
(points described above)
Download