Create a new project with two separate classes called TestCylinder and TestRecta
ID: 3693951 • Letter: C
Question
Create a new project with two separate classes called TestCylinder and TestRectangle.
The TestRectangle.java file contains the TestRectangle class which includes the main method, and another class called SimpleRectangle with instance variables for length and width and two constructors (one with arguments, one with no arguments), and instance methods that return the area, perimeter, and diagonal for rectangles. The TestCylinder.java file contains the TestCylinder class which includes the main method, and another class called SimpleCylinder with instance variables for radius and height, and two constructors (one with arguments, one with no arguments), and instance methods that get the surface area and get the volume for cylinders. Use length = 10 and width = 20 for the rectangle, and radius = 10 and height = 30 for the cylinder. You can right-click on each’s source code and select “Run file” to test each file. Zip the folder, calling it “Your Name PreLab09.zip” and send it to me as an attachment in a Blackboard message. Use the sample below to model your solution.
Rectangle: Area = Length*Width; Perimeter = (2*Length) + (2*Width); Diagonal =
Cylinder: Surface Area = 2*(*r2) + ( * 2*r * height); Volume = ( * r2 * height)
run:
The area of the rectangle of length 10.0 and width 20.0 is 200.0
The perimeter of the rectangle of length 10.0 and width 20.0 is 60.0
The diagonal of the rectangle of length 10.0 and width 20.0 is 22.360679774997898
run:
The surface area of the cylinder of radius 10.0 and height 30.0 is 2513.2741228718346
The volume of the cylinder of radius 10.0 and height 30.0 is 9424.77796076938
Explanation / Answer
TestRectangle.java
public class TestRectangle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleRectangle s = new SimpleRectangle(10, 20);
System.out.println("The area of the rectangle of length 10.0 and width 20.0 is "+s.area());
System.out.println("The perimeter of the rectangle of length 10.0 and width 20.0 is "+s.perimeter());
System.out.println("The diagonal of the rectangle of length 10.0 and width 20.0 is "+s.diagonal());
}
}
SimpleRectangle.java
public class SimpleRectangle {
private double width;
private double length ;
public SimpleRectangle (double width, double length ){
this.width = width;
this.length = length ;
}
public SimpleRectangle(){
}
public double area(){
return width * length;
}
public double perimeter(){
return 2 * width + 2 * length;
}
public double diagonal(){
return Math.sqrt(width * width + length * length);
}
}
Output:
The area of the rectangle of length 10.0 and width 20.0 is 200.0
The perimeter of the rectangle of length 10.0 and width 20.0 is 60.0
The diagonal of the rectangle of length 10.0 and width 20.0 is 22.360679774997898
----------------------------------------------------------------------------------------------
TestCylinder.java
public class TestCylinder {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleCylinder s = new SimpleCylinder(10, 30);
System.out.println("The surface area of the cylinder of radius 10.0 and height 30.0 is "+s.area());
System.out.println("The volume of the cylinder of radius 10.0 and height 30.0 is "+s.volume());
}
}
SimpleCylinder.java
public class SimpleCylinder {
private double radius ;
private double height ;
private final double PI = 3.14;
public SimpleCylinder (double radius , double height ){
this.radius = radius ;
this.height = height ;
}
public SimpleCylinder(){
}
public double area(){
return (2 * PI * radius * radius) + (PI * 2 * radius * height);
}
public double volume(){
return PI * radius * radius * height;
}
}
Output:
The surface area of the cylinder of radius 10.0 and height 30.0 is 2512.0
The volume of the cylinder of radius 10.0 and height 30.0 is 9420.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.