Lab2 - Class and Objects
Lab2 - Class and Objects
Objective
===========================================================
Lab Activities
Activity #1
This programming task defines the Circle class and uses it to create objects. The program constructs three
circle objects with radius 1, 25, and 125 and displays the radius and area of each of the three circles. It
then changes the radius of the second object to 100 and displays its new radius and area.
The program contains two classes. The first of these, TestCircle, is the main class. Its sole purpose is to
test the second class, Circle. Such a program that uses the class is often referred to as a client of the class.
When you run the program, the Java runtime system invokes the main method in the main class.
==============================================================
Activity #2
In this programming task the two classes of programming task 1 are put into one file, but only one class
in the file can be a public class. Furthermore, the public class must have the same name as the file name.
Therefore, the file name is TestCircle.java, since TestCircle is public. Each class in the source code is
compiled into a .class file. When you compile TestCircle.java, two class files TestCircle.class and
Circle.class are generated as shown in the following figure
public class Circle {
/** Main method */
public static void main(String[] args) {
// Create a circle with radius 1
Circle circle1 = new Circle();
System.out.println("The area of the circle of radius " + circle1.radius + " is " + circle1.getArea());
// Create a circle with radius 25
Circle circle2 = new Circle(25);
System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());
// Create a circle with radius 125
Circle circle3 = new Circle(125);
System.out.println("The area of the circle of radius " + circle3.radius + " is " + circle3.getArea());
// Modify circle radius
circle2.radius = 100;
System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());
}
double radius;
/** Construct a circle with radius 1 */
Circle() {
radius = 1;
}
/** Construct a circle with a specified radius */
Circle(double newRadius) {
radius = newRadius;
}
/** Return the area of this circle */
double getArea() {
return radius * radius * Math.PI;
}
/** Return the perimeter of this circle */
double getPerimeter() {
return 2 * radius * Math.PI;
}
/** Set a new radius for this circle */
void setRadius(double newRadius) {
radius = newRadius;
}
}
==============================================================
Activity #3
consider television sets. Each TV is an object with states (current channel, current volume level, and
power on or off) and behaviors (change channels, adjust volume, and turn on/off). You can use a class to
model TV sets. The UML diagram for the class is shown in the following figure.
public class TV {
int channel = 1; // Default channel is 1
int volumeLevel = 1; // Default volume level is 1
boolean on = false; // TV is off
public TV() {
}
public void turnOn() {
on = true;
}
public void turnOff() {
on = false;
}
public void setChannel(int newChannel) {
if (on && newChannel >= 1 && newChannel <= 120)
channel = newChannel;
}
public void setVolume(int newVolumeLevel) {
if (on && newVolumeLevel >= 1 && newVolumeLevel <= 7)
volumeLevel = newVolumeLevel;
}
public void channelUp() {
if (on && channel < 120)
channel++;
}
public void channelDown() {
if (on && channel > 1)
channel—–;
}
public void volumeUp() {
if (on && volumeLevel < 7)
volumeLevel++;
}
public void volumeDown() {
if (on && volumeLevel > 1)
volumeLevel—–;
}
}
Exercise # 1: following the Circle class in programming task #1, develop a class named Rectangle to represent a
rectangle. The class contains:
■ Two double data fields named width and height that specify the width and height of the rectangle. The
default values are 1 for both width and height.
■ A no-arg constructor that creates a default rectangle.
■ A constructor that creates a rectangle with the specified width and height.
■ A method named getArea() that returns the area of this rectangle.
■ A method named getPerimeter() that returns the perimeter.
Write a test program that creates two Rectangle objects—one with width 4 and height 40, and the other with
width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle in this order.