Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | Jul 2017 |
Last Sign in: | 214 Weeks Ago |
Questions Answered: | 15833 |
Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Creating a Programmer-Defined Class in Java
Summary
In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter.
Instructions
Make sure the class file named Rectangle.java is open.
In the Rectangle class, create two private attributes named length and width. Both length and width should be data type double.
Write public set methods to set the values for length and width.
Write public get methods to retrieve the values for length and width.
Write a public calculateArea() method and a public calculatePerimeter() method to calculate and return the area of the rectangle and the perimeter of the rectangle.
Open the file named MyRectangleClassProgram.java.
In the MyRectangleClassProgram class, create two Rectangle objects named rectangle1 and rectangle2.
Set the length of rectangle1 to 10.0 and the width to 5.0. Set the length of ectangle2 to 7.0 and the width to 3.0.
Print the value of rectangle1’s perimeter and area, and then print the value of rectangle2’s perimeter and area.
Execute the program.
MyRectangleClassProgram.java
// This program uses the programmer-defined Rectangle class.
public class MyRectangleClassProgram
{
public static void main(String args[])
{
// Create rectangle1 and rectangle2 objects here.
// Set the length of rectangle1 to 10.0 here.
// Set the width of rectangle1 to 5.0 here.
// Print the area and perimeter of rectangle1 here.
// Set the length of rectangle2 to 7.0 here.
// Set the width of rectangle2 to 3.0 here.
// Print the area and perimeter of rectangle2 here.
System.exit(0);
}
}
MyRectangleClassProgramsST.java
// This program uses the programmer-defined Rectangle class.
public class MyRectangleClassProgram
{
public static void main(String args[])
{
Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle();
rectangle1.setLength(10.0);
rectangle1.setWidth(5.0);
System.out.println("Rectangle 1's perimeter is: " + rectangle1.calculatePerimeter());
System.out.println("Rectangle 1's area is: " + rectangle1.calculateArea());
rectangle2.setLength(7.0);
rectangle2.setWidth(3.0);
System.exit(0);
}
}
Rectangle.java
class Rectangle
{
// Length of this rectangle.
// Width of this rectangle.
// Write set methods here.
// Write get methods here.
// Write the calculatePerimeter() and
// calculateArea() methods here.
}
RectangleST.java class Rectangle { private double length; // Length of this rectangle private double width; // Width of this rectangle public void setLength(double len) { length = len; } public void setWidth(double wid) { width = wid; } public double getLength() { return length; } public double getWidth() { return width; } public double calculatePerimeter() { return (2 * (length + length)); } public double calculateArea() { return(length * width); } }
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly