0% found this document useful (0 votes)
29 views

Lab2 - Class and Objects

The document describes three activities in an object-oriented programming lab about classes and objects: 1. Defines a Circle class with radius and methods to get area and change radius, and tests it by creating Circle objects. 2. Puts the Circle class and test class from activity 1 into one file called TestCircle, compiling it to generate two class files. 3. Defines a TV class with attributes like channel and volume and methods to change them, and a test class to create TV objects and change their attributes. The document also provides two exercises: 1) Create a Rectangle class with width, height and methods to get area and perimeter. 2) Create a Stock class with name, prices and a

Uploaded by

Ghala Aldosari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Lab2 - Class and Objects

The document describes three activities in an object-oriented programming lab about classes and objects: 1. Defines a Circle class with radius and methods to get area and change radius, and tests it by creating Circle objects. 2. Puts the Circle class and test class from activity 1 into one file called TestCircle, compiling it to generate two class files. 3. Defines a TV class with attributes like channel and volume and methods to change them, and a test class to create TV objects and change their attributes. The document also provides two exercises: 1) Create a Rectangle class with width, height and methods to get area and perimeter. 2) Create a Stock class with name, prices and a

Uploaded by

Ghala Aldosari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

113CSS-4 OOP LAB #2

Classes and Objects

Objective

 Understanding the concept of Object-Oriented programming.


 Introducing classes and objects and differences between them.
 Understanding class members and their properties.

===========================================================

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.

public class TestCircle {


/** 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; // or circle2.setRadius(100)
System.out.println("The area of the circle of radius "+ circle2.radius + " is " + circle2.getArea());
}
}
// Define the circle class with two constructors
class Circle {
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 #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—–;
}
}

public class TestTV {


public static void main(String[] args) {
TV tv1 = new TV();
tv1.turnOn();
tv1.setChannel(30);
tv1.setVolume(3);
TV tv2 = new TV();
tv2.turnOn();
tv2.channelUp();
tv2.channelUp();
tv2.volumeUp();
System.out.println("tv1's channel is " + tv1.channel + " and volume level is " + tv1.volumeLevel);
System.out.println("tv2's channel is " + tv2.channel + " and volume level is " + tv2.volumeLevel);
}
}
Lab Exercise

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.

Exercise 2: Create a class named Stock that contains:


■ A string data field named symbol for the stock’s symbol.
■ A string data field named name for the stock’s name.
■ A double data field named previousClosingPrice that stores the stock price for the previous day.
■ A double data field named currentPrice that stores the stock price for the current time.
■ A constructor that creates a stock with the specified symbol and name.
■ A method named getChangePercent() that returns the percentage changed from previousClosingPrice to
currentPrice.
Write a test program that creates a Stock object with the stock symbol ORCL, the name Oracle Corporation, and
the previous closing price of 34.5. Set a new current price to 34.35 and display the price-change percentage.

You might also like