Make sure the program is asking for user input. (1)Objective: Implement java.uti
ID: 3860803 • Letter: M
Question
Make sure the program is asking for user input.
(1)Objective: Implement java.util.Comparator to determine the order/match of arbitrary objects. (1 points)
Write a java program to determine the order of two shape objects by comparing their area.
For example: determine the order of
(a)A 20x5 rectangular.
(b)A circle with radius 6.
Allow user to input length & width for the rectangular and radius for the circle to test your program. Use the Circle.java, Rectangular.java and Shape.java files found below.
//********************************************************************
// Circle.java
//
//********************************************************************
import java.text.*;
public class Circle extends Shape
{
private int radius, x, y;
//-----------------------------------------------------------------
// Constructor: Sets up this circle with the specified values.
//-----------------------------------------------------------------
public Circle (int size)
{
radius = size;
}
//-----------------------------------------------------------------
// Diameter mutator.
//-----------------------------------------------------------------
public void setRadius (int size)
{
radius = size;
}
//-----------------------------------------------------------------
// Diameter accessor.
//-----------------------------------------------------------------
public int getRadius ()
{
return radius;
}
//---------------------------------------------------------------------------
// Returns the calculated value of the area.
//---------------------------------------------------------------------------
public double computeArea()
{
return 3.1416*(radius)*(radius);
}
//---------------------------------------------------------------------------
// Returns the calculated value of the perimeter.
//---------------------------------------------------------------------------
public double computePerimeter()
{
return 3.1416 * 2 * radius;
}
}
//******************************************************************************
// Rectangle.java Author: Lewis/Loftus
//
// Solution to Programming Project 9.11
//******************************************************************************
import java.text.*;
public class Rectangle extends Shape
{
protected double width;
protected double length;
protected static DecimalFormat form = new DecimalFormat("0.##");
//---------------------------------------------------------------------------
// Sets up the rectangle by entering its width and length.
//---------------------------------------------------------------------------
public Rectangle(double wid, double len)
{
width = wid;
length = len;
}
//---------------------------------------------------------------------------
// Returns the double value of the width.
//---------------------------------------------------------------------------
public double getWidth()
{
return width;
}
//---------------------------------------------------------------------------
// Returns the double value of the length.
//---------------------------------------------------------------------------
public double getLength()
{
return length;
}
//---------------------------------------------------------------------------
// Returns the calculated value of the area.
//---------------------------------------------------------------------------
public double computeArea()
{
return length * width;
}
//---------------------------------------------------------------------------
// Returns the calculated value of the perimeter.
//---------------------------------------------------------------------------
public double computePerimeter()
{
return 2 * (length + width);
}
//---------------------------------------------------------------------------
// Returns pertinent information about the rectangle.
//---------------------------------------------------------------------------
public String toString()
{
return "Rectangle: width is " + form.format(width) +
", length is " + form.format(length) +
" perimeter is " + form.format(computePerimeter()) +
", area is " + form.format(computeArea());
}
}
//******************************************************************************
// Shape.java
//
//******************************************************************************
public abstract class Shape
{
abstract public double computeArea();
abstract public double computePerimeter();
}
Make sure the program asks for user input to get length & width for the rectangular and radius for the circle.
Explanation / Answer
class FindLargestShape { public static void main(String arg[]) { Rectangle r = new Rectangle(10, 4); Square s = new Square(7); Circle c = new Circle(3.5); System.out.println("Rectangle Area : " + r.getArea()); System.out.println("Square Area : " + s.getArea()); System.out.println("Circle Area : " + c.getArea()); System.out.println(); if ((r.getArea() > c.getArea()) && (r.getArea() > s.getArea())) { System.out.println("Rectangle has the largest area."); } else if( s.getArea() > c.getArea() ) { System.out.println("Square has the largest area."); } else { System.out.println("Circle has the largest area."); } } } class Rectangle { double length; double breadth; Rectangle(double length, double breadth) { this.length = length; this.breadth = breadth; } double getArea() { return length * breadth; } } class Square { double side; Square(double side) { this.side = side; } double getArea() { return side * side; } } class Circle { double radius; Circle(double radius) { this.radius = radius; } double getArea() { return (22.0/7.0) * radius * radius; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.