HAHAH I DON\"T KNOW WHERE TO START , how to start.. You are given four .java fil
ID: 3634154 • Letter: H
Question
HAHAH I DON"T KNOW WHERE TO START , how to start..
You are given four .java files. Shape.java is a complete class. Rectangle and Circle classes are inherited from the Shape class. Read over the Shape.java code to understand what it does. Then follow the steps in the rest of the three .java files to finish the exercise. ShapeMain.java is the driver program that uses various shapes given.
SHAPE :
public class Shape {
/*
* Shape.java is your base class or super class. You will be creating a
* Rectangle and a Circle class based on this class. */
protected String name;
protected double area = 0.0;
protected double perimeter = 0.0;
public Shape(String name) {
this.name = name;
}
}
RECTANGLE
CIRCLE
SHAPEMAIN
Explanation / Answer
public class Rectangle extends Shape {
// STEP 1:
// Create two attributes, length and width, to store the data for a rectangle
private double length, width;
// STEP 2:
// Create a constructor for the Rectangle class. This will set the lenght and the width also, set the
// name of the object "Rectangle"
public Rectangle(double length, double width)
{
super("Rectangle");
this.length = length;
this.width = width;
}
// STEP 3:
// Develop the calcArea() method in Shape class to calculate the area for a
// rectangle
public void calcArea()
{
area = length*width;
}
// STEP 4:
// Develop the calcPerimeter() method in Shape class to calculate the
// perimeter for a rectangle
public void calcPerimeter()
{
perimeter = 2*(length+width);
}
public String toString() {
return "This is a rectangle called " + name + " The area is " + area + " and perimeter is " + perimeter;
}
}
public class Circle extends Shape {
// STEP 0:
// Create one attribute, radius, to store the data for a circle
private double radius;
// STEP 1:
// Create a constructor for the Circle class. Call the constructor of the
// Shape class from it and pass it the name value. Store the radius data
// locally.
public Circle(double radius)
{
super("Circle");
this.radius = radius;
}
// STEP 2:
// Develop the calcArea() to calculate the area for a
// circle
public void calcArea()
{
area = Math.PI*radius*radius;
}
// STEP 3:
// Develop the calcPerimeter() to calculate the
// perimeter for a circle
public void calcPerimeter()
{
perimeter = 2*radius*Math.PI;
}
public String toString() {
return "This is a circle called " + name + " The area is " + area + " and circumference is " + perimeter;
}
}
public class ShapeMain {
public static void main(String[] args) {
// STEP 1:
// Create an object of the Circle class called round
Circle round = new Circle(10.0);
// Calculate the area and circumference of the circle and print them
round.calcArea();
round.calcPerimeter();
System.out.println(round);
System.out.println();
// STEP 2:
// Create an object of the Rectangle class
Rectangle box = new Rectangle(6.0, 7.0);
// Calculate the area and perimeter of the rectangle and print them called box
box.calcArea();
box.calcPerimeter();
System.out.println(box);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.