From the last week Rectangle class Rewrite the same class In
From the last week Rectangle class, Rewrite the same class.
Instance variables: (private)
length (double)
width (double
Methods: (public)Method initialize
Parameters: a width and a length.
Initializes all data fields to the given values passed by the parameters
Method getWidth
Returns the width of the calling object.
Method getLength
Returns the length of the calling object.
Method setWidth
Parameter: A width
Re-sets the value of the object\'s width to the value passed by the parameter.
Method setLength
Parameter: A length
Re-sets the value of the object\'s length to the value passed by the parameter.
Method calculateArea
Returns the area of the object (does NOT print; it only performs a calculation).
Method calculatePerimeter
Returns the perimeter of the object (does NOT print; it only performs a calculation).
Method printDimensions
Prints out all the data related to the object up to 2 decimal points (only length and width)
RectangleDemo class:
Perform the following actions in the main method:
Create an object named r1 of the Rectangle class.
Using the set methods, initialize r1 to have width 3.0 and length 5.2.
Use the printDimensions method to output all the data related to r1 (length, width).
Create another object named r2 of the Rectangle class with width 2.6 and length 5.4 using the initialize method.
Print out r2 using the get methods ( width, length ).
Create another object r3 of the. Ask the user to enter the length and width( use set method)
Print out r3 using the get methods ( width, length )
Print out the area and the perimeter of r1 using the calculateArea and calculatePerimeter methods.
Print out the area and the perimeter of r2 using the calculateArea and calculatePerimeter methods.
Print out the area and the perimeter of r3 using the calculateArea and calculatePerimeter methods
Example:
Width: 3.00
Length: 5.20
Width: 2.60
Length: 5.40
Width: user input
Length: user input
R1 Perimeter: 16.40
R1 Area: 15.60
R2 Perimeter: 16.00
R2 Area: 14.04
R3 Perimeter: calculated
R3 Area: calculated
====================================\\
IT\'S THE LAST WEEK WE DID FROM HERE...
package week05;
public class Rectangle
{
double width;
double length;
public double getWidth()
{
return width;
}
public double getLength()
{
return length;
}
public void setWidth(double newWidth)
{
width = newWidth;
}
public void setLength(double newLength)
{
length = newLength;
}
public void newInitial (double newLength, double newWidth)
{
length = newLength;
width = newWidth;
}
public void printOutput()
{
System.out.printf(\"Width: %.2f\", width);
System.out.println();
System.out.printf(\"Length : %.2f\", length);
System.out.println();
System.out.printf(\"Area : %.2f\", this.calculateArea());
System.out.println();
System.out.printf(\"Perimeter : %.2f\", this.perimeter());
System.out.println();
}
public double calculateArea()
{
return (length*width);
}
public double perimeter()
{
return((length+width)*2);
}
}
package week05;
import java.util.*;
public class RectangleDemo
{
public static void main(String[] args)
{
System.out.println(\"The first RECTANGLE\");
Rectangle r4 = new Rectangle ();
r4.newInitial(10, 3);
r4.printOutput();
System.out.println(\"--------------------------------------\");
System.out.println(\"The second RECTANGLE\");
Rectangle r5 = new Rectangle ();
r5.newInitial(12.45, 4.25);
System.out.printf(\"length : %.2f\" ,r5.length);
System.out.println();
System.out.printf(\"width : %.2f\" ,r5.width);
System.out.println();
System.out.printf(\"Area : %.2f\" ,r5.calculateArea());
System.out.println();
System.out.printf(\"Perimeter : %.2f\" ,r5.perimeter());
System.out.println();
System.out.println(\"--------------------------------------\");
System.out.println(\"The third RECTANGLE\");
Rectangle r3 = new Rectangle ();
Scanner input = new Scanner (System.in);
System.out.println(\"length : \");
r3.setLength(input.nextDouble());
System.out.println(\"width : \");
r3.width = input.nextDouble();
r3.printOutput();
}
}
Solution
package week05;
public class Rectangle {
private double width;
private double length;
public Rectangle() {
// TODO Auto-generated constructor stub
}
/**
* @param width
* @param length
*/
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
/**
* @param width
* @param length
*/
public void intialize(double width, double length) {
this.width = width;
this.length = length;
}
/**
* @return the width
*/
public double getWidth() {
return width;
}
/**
* @param width
* the width to set
*/
public void setWidth(double width) {
this.width = width;
}
/**
* @return the length
*/
public double getLength() {
return length;
}
/**
* @param length
* the length to set
*/
public void setLength(double length) {
this.length = length;
}
public double calculateArea() {
return (length * width);
}
public double perimeter() {
return ((length + width) * 2);
}
public void printDimensions() {
System.out.printf(\"Width: %.2f\", width);
System.out.println();
System.out.printf(\"Length : %.2f\", length);
System.out.println();
System.out.printf(\"Area : %.2f\", this.calculateArea());
System.out.println();
System.out.printf(\"Perimeter : %.2f\", this.perimeter());
System.out.println();
}
}
package week05;
import java.util.Scanner;
public class RectangleDemo {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(3.0, 5.2);
r1.printDimensions();
Rectangle r2 = new Rectangle();
r2.intialize(2.6, 5.4);
System.out.println(\"r2--> width : \" + r2.getWidth() + \" length : \"
+ r2.getLength());
Rectangle r3 = new Rectangle();
Scanner scanner = new Scanner(System.in);
System.out.print(\"Enter the width:\");
double width = scanner.nextDouble();
System.out.print(\"Enter the length:\");
double length = scanner.nextDouble();
r3.setLength(length);
r3.setWidth(width);
System.out.printf(\"R1 Perimeter: %.2f\ R1 Area: %.2f\ \",
r1.perimeter(), r1.calculateArea());
System.out.printf(\"R2 Perimeter: %.2f\ R1 Area: %.2f\ \",
r2.perimeter(), r2.calculateArea());
System.out.printf(\"R3 Perimeter: %.2f\ R1 Area: %.2f\ \",
r3.perimeter(), r3.calculateArea());
}
}
OUTPUT:
Width: 3.00
Length : 5.20
Area : 15.60
Perimeter : 16.40
r2--> width : 2.6 length : 5.4
Enter the width:2
Enter the length:3
R1 Perimeter: 16.40
R1 Area: 15.60
R2 Perimeter: 16.00
R1 Area: 14.04
R3 Perimeter: 10.00
R1 Area: 6.00





