Please read the instructions for each part carefully. Comments out the codes and
ID: 3648293 • Letter: P
Question
Please read the instructions for each part carefully. Comments out the codes and their functionality as much as possible. Also compile the program before responding with the code.public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
so, the get method name is isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
/** Return a string representation of this object */
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
write a program, in 4 parts, involving inheritance.
Program:
Part 1: Modify GeometricObject
Modify the GeometricObject class from the text so that the user can select the color of the object, and whether or not it is filled. (The Date of the object should still be set to the time of that object's creation.)
Part 2: Ellipse Class
Design a class Ellipse that extends class GeometricObject. An ellipse is characterized by the length a of its half-major axis and the length b of its half-minor axis.(a ? b).
The area of an ellipse if given by ?ab.
The perimeter of an ellipse is, amazingly, impossible to calculate precisely without using an infinite sum. Two approximations to the perimeter (P1 and P2) are given below. Your estimate of the perimeter should be the average of these two values.
and
(Those are brackets [] after the p, not "floor" symbols.)
Be sure to include the following methods in the Ellipse class:
Accessor ("get") and mutator ("set") methods for major and minor axes
perimeter()
area()
Part 3: Circle class
Write a new Circle class that has the functionality of the textbook's class, but that extends the Ellipse class rather than directly extending the GeometricObject class. If you do this right, you should be able to use the Ellipse class's area() method to correctly calculate the area of a circle. The perimeter of a circle is given exactly by 2?r, where r is the radius of a circle. So you'll need to write a perimeter method.
Part 4: Driver class
Create a driver class that tests objects of classes Ellipse, and Circle. It should allow the user to select whether they want to construct a Circle, Ellipse, or GeometricObject. It should then ask the user the appropriate questions to create the object of the desired type.
Use JOptionPanes for input. You should all be familiar with the JOptionPane.showInputDialog(...) method that opens a text box. However, there are some other useful JOptionPane methods. I list them below, with code examples for you to experiment with:
Fun with JOptionPanes
The following code is an example of the use of dropdown menus. This particular code is a subset of the infamous "select your state" code that you see on all sorts of web sites. This one defaults to Minnesota.
Object[] stateList = { "Minnesota", "Iowa", "North Dakota", "South Dakota", "Wisconsin"};
String state = (String)JOptionPane.showInputDialog(null, "Please select your state:", "State", JOptionPane.PLAIN_MESSAGE, null, stateList, "Minnesota");
The following code puts up a box that allows you to select Yes or No.
Object[] options = {"Yes", "No"};
int answer = JOptionPane.showOptionDialog( null,
"Make a Decision",
"Decision", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, null );
Compile and run the program.
Explanation / Answer
public class EllipseApp extends JComponent implements ActionListener { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; Ellipse2D.Float ellipse = new Ellipse2D.Float(45, 30, 300, 300); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.draw(ellipse); g2.setColor(Color.BLUE); g2.fill(ellipse); } public static void main(String[] args) { EllipseApp ellipse = new EllipseApp(); JTextField text1 = new JTextField("Width", 5); JTextField text2 = new JTextField("Height", 5); JButton button = new JButton("OK"); JPanel panel = new JPanel(); panel.add(text1); panel.add(text2); panel.add(button); JFrame frame = new JFrame(); frame.setSize(400, 400); frame.setTitle("An Ellipse"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.setVisible(true); frame.add(ellipse); frame.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { } } public static void main(String[] args) { Shape shapes[] = new Shape[2]; shapes[0] = new Circle(3); shapes[1] = new Rectangle(1,3); } public class Circle implements Shape { int radius; public Circle(int i) { super(); this.radius = i; } public double getArea() { return Math.PI * this.radius * this.radius; } } public class Rectangle implements Shape { int x; int y; public Rectangle(int x, int y) { super(); this.y = y; this.x = x; } public double getArea() { return this.x * this.y; } } public interface Shape { double getArea(); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.