GeometricObject public abstract class GeometricObject { protected String colour;
ID: 3840673 • Letter: G
Question
GeometricObject
public abstract class GeometricObject
{
protected String colour;
protected boolean filledIn;
// Construct a geometric object
public GeometricObject(String colour, boolean filledIn)
{
this.colour = colour;
this.filledIn = filledIn;
}
// Return colour
public String getColour()
{
return colour;
}
// Set a new colour
public void setColour(String colour)
{
this.colour = colour;
}
// Abstract method findArea
public abstract double findArea();
// Abstract method findPerimeter()
public abstract double findPerimeter();
public String toString()
{
return " Colour: " + colour + " and filled " + filledIn;
}
}
Circle
public class Circle extends GeometricObject
{
private double radius;
// Constructor
public Circle(String colour, boolean filledIn, double radius)
{
super(colour, filledIn);
this.radius = radius;
}
// Get and set methods for radius
public double getRadius()
{
return radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
// Methods to calculate area and perimeter
public double findArea()
{
return Math.PI * radius * radius;
}
public double findPerimeter()
{
return 2 * Math.PI * radius;
}
// toString method
public String toString()
{
String toScreen;
toScreen = super.toString() + "Radius of a circle is " + radius;
return toScreen;
}
}
Rectangle
public class Rectangle extends GeometricObject
{
private double width;
private double length;
// Constructor
public Rectangle(String colour, boolean filledIn, double width, double length)
{
super(colour, filledIn);
this.width = width;
this.length = length;
}
// Getters and setters
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getLength()
{
return length;
}
public void setLength(double length)
{
this.length = length;
}
// Calculate area and perimeter
public double findArea()
{
return length * width;
}
public double findPerimeter()
{
return 2 * (length + width);
}
// Override parent toString method
public String toString()
{
String toScreen;
toScreen = super.toString() + "Width and length of a rectangle is " + width + " " + length + " respectively";
return toScreen;
}
}
EquilateralTriangle
public class EquilateralTriangle extends GeometricObject
{
private double side;
public EquilateralTriangle(String colour, boolean filledIn, double side) //Constructor
{
super(colour, filledIn);
this.side = side;
}
public double getSide()
{
return side;
}
public void setSide(double side)
{
this.side = side;
}
public double findArea()
{
return ( Math.sqrt(3) * side *side)/4;
}
public double findPerimeter()
{
return 3 * side;
}
public String toString()
{
return super.getColour() + " " + super.filledIn + " " + getSide();
}
} //End Class
Cylinder
public class Cylinder extends GeometricObject
{
private double radius;
private double height;
public Cylinder(String colour, boolean filledIn, double radius,double height) //Constructor
{
super(colour, filledIn);
this.radius = radius;
this.height = height;
}
public double getRadius()
{
return radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
public double getHeight()
{
return height;
}
public void setHeight(double height)
{
this.height = height;
}
public double findArea()
{
return 2* Math.PI * radius *(height+ radius);
}
public double findPerimeter()
{
return 2 * (2* Math.PI * radius+height);
}
public String toString()
{
return super.getColour() + " " + super.filledIn + " " + getRadius()+ " " + getHeight();
}
} //End Class
Panel
// Import packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Question5Panel extends JPanel implements ActionListener
{
// data declarations
private JLabel shapeLabel;
private JRadioButton circleButton;
private JRadioButton rectangleButton;
private JRadioButton equTButton;
private JRadioButton cylinderButton;
private ButtonGroup shape;
private JPanel selectShape;
private JLabel radius;
private JTextField radiusIn;
private JLabel rectangle;
private JLabel length;
private JTextField lengthIn;
private JLabel width;
private JTextField widthIn;
private JLabel side;
private JTextField sideIn;
private JLabel cylinder;
private JLabel height;
private JTextField heightIn;
private JLabel ra;
private JTextField raIn;
private JPanel westPanel;
private JPanel circlePanel;
private JPanel rectanglePanel;
private JPanel equTPanel;
private JPanel cylinderPanel;
private JPanel outputPanel;
private JTextArea output;
private Circle myCircle;
private Rectangle myRectangle;
private Rectangle myEquTriangle;
private Rectangle myCylinder;
// constructor to initiate data and set up GUI
public Question5Panel()
{
setLayout(new BorderLayout());
// setting up north area of GUI
shapeLabel = new JLabel("Please select a shape ");
shapeLabel.setFont(new Font("Arial", Font.BOLD, 16));
// organizing radio buttons and their behaviours
circleButton = new JRadioButton("Circle");
rectangleButton = new JRadioButton("Rectangle");
equTButton = new JRadioButton("EquilateralTriangle");
cylinderButton = new JRadioButton("Cylinder");
shape = new ButtonGroup();
shape.add(circleButton);
shape.add(rectangleButton);
shape.add(equTButton);
shape.add(cylinderButton);
// adding components to panel to be located north in the GUI
selectShape = new JPanel();
selectShape.add(shapeLabel);
selectShape.add(circleButton);
selectShape.add(rectangleButton);
selectShape.add(equTButton);
selectShape.add(cylinderButton);
add(selectShape, BorderLayout.NORTH);
// setting up west area of GUI
westPanel = new JPanel();
// setting up components for the circlePanel of the GUI
circlePanel = new JPanel();
radius = new JLabel("radius is: ");
radiusIn = new JTextField(10);
circlePanel.add(radius);
circlePanel.add(radiusIn);
TitledBorder circleBorder = BorderFactory.createTitledBorder("Circle");
circlePanel.setBorder(circleBorder);
add(westPanel, BorderLayout.WEST);
// setting up components for the rectanglePanel of GUI
length = new JLabel("length is: ");
lengthIn = new JTextField(10);
width = new JLabel("width is: ");
widthIn = new JTextField(10);
// adding components to the rectanglePanel of GUI
rectanglePanel = new JPanel();
rectanglePanel.add(length);
rectanglePanel.add(lengthIn);
rectanglePanel.add(width);
rectanglePanel.add(widthIn);
TitledBorder rectangleBorder = BorderFactory.createTitledBorder("Rectangle");
rectanglePanel.setBorder(rectangleBorder);
// setting up components for the equTPanel of the GUI
equTPanel= new JPanel();
side = new JLabel("side is: ");
sideIn = new JTextField(10);
equTPanel.add(side);
equTPanel.add(sideIn);
TitledBorder equtBorder = BorderFactory.createTitledBorder("EquilateralTriangle");
equTPanel.setBorder(equtBorder);
// setting up components for the cylinderPanel of GUI
height = new JLabel("height is: ");
heightIn = new JTextField(10);
ra = new JLabel("radius is: ");
raIn = new JTextField(10);
// adding components to the cylinderPanel of GUI
cylinderPanel = new JPanel();
cylinderPanel.add(height);
cylinderPanel.add(heightIn);
cylinderPanel.add(ra);
cylinderPanel.add(raIn);
TitledBorder cylinderBorder = BorderFactory.createTitledBorder("Cylinder");
cylinderPanel.setBorder(cylinderBorder);
add(westPanel, BorderLayout.WEST);
// adding the circlePanel, rectanglePanels, equTPanel, and cylinderPanel into the westPanel
westPanel.setLayout(new GridLayout(2, 1));
westPanel.setPreferredSize(new Dimension(400, 15));
westPanel.add(circlePanel);
westPanel.add(rectanglePanel);
westPanel.add(equTPanel);
westPanel.add(cylinderPanel);
// setting up center area of GUI
outputPanel = new JPanel();
output = new JTextArea(15, 25);
outputPanel.add(output);
add(outputPanel, BorderLayout.CENTER);
// add listeners to radio buttons
circleButton.addActionListener(this);
rectangleButton.addActionListener(this);
equTButton.addActionListener(this);
cylinderButton.addActionListener(this);
// add listeners to all textfields
radiusIn.addActionListener(this);
lengthIn.addActionListener(this);
widthIn.addActionListener(this);
sideIn.addActionListener(this);
heightIn.addActionListener(this);
raIn.addActionListener(this);
} // end constructor
//***************************************************************************
// Students to complete the functionality of the GUI through actionPerformed
//***************************************************************************
public void actionPerformed(ActionEvent ae)
{
System.out.println(ae.getActionCommand());
String data = "";
if (ae.getActionCommand().equals("Circle"))
{
data = getData(0);
output.setText(data);
}
else if (ae.getActionCommand().equals("Rectangle"))
{
data = getData(0, 0);
System.out.println(data);
output.setText(data);
}
else if (ae.getActionCommand().equals("EquilateralTriangle"))
{
data = getData(0);
output.setText(data);
}
else if (ae.getActionCommand().equals("Cylinder"))
{
data = getData(0, 0);
System.out.println(data);
output.setText(data);
}
if (circleButton.isSelected())
{
System.out.println("--");
try
{
int i=Integer.parseInt(radiusIn.getText());
data=getData(i);
System.out.println();
output.setText(data);
}
catch (Exception e)
{
}
}
if (rectangleButton.isSelected())
{
try
{
try
{
int i=Integer.parseInt(lengthIn.getText());
int j=Integer.parseInt(widthIn.getText());
System.out.println(i+","+j);
data=getData(i,j);
output.setText(data);
}
catch (Exception e)
{
}
}
catch (Exception e)
{
}
}
if (equTButton.isSelected())
{
System.out.println("--");
try
{
int a=Integer.parseInt(sideIn.getText());
data=getData1(a);
System.out.println();
output.setText(data);
}
catch (Exception e)
{
}
}
if (cylinderButton.isSelected())
{
try
{
try
{
int a=Integer.parseInt(heightIn.getText());
int b=Integer.parseInt(raIn.getText());
System.out.println(a+","+b);
data=getData1(a,b);
output.setText(data);
}
catch (Exception e)
{
}
}
catch (Exception e)
{
}
}
}
private String getData(int i, int j)
{
Rectangle rectangle=new Rectangle("black",false,i,j);
return "rectangle has length of " + i + " with a width of " + j + " " + "and a perimeter of " + rectangle.findPerimeter() + " and a area of " + rectangle.findArea();
}
private String getData(int i)
{
Circle circle = new Circle("black",false,i);
return "circle has radius of " + i + " " + "and a perimeter of " + circle.findPerimeter() + " and a area of " + circle.findArea();
}
private String getData1(int a, int b)
{
Cylinder cylinder=new Cylinder("black",false,a,b);
return "cylinder has height of " + a+ " with a radius of " + b+ " " + "and a perimeter of " + cylinder.findPerimeter() + " and a area of " + cylinder.findArea();
}
private String getData1(int a)
{
EquilateralTriangle eqt=new EquilateralTriangle("black",false,a);
return " Equilaternal triangle has side of " + a + " " + "and a perimeter of " + eqt.findPerimeter() + " and a area of " + eqt.findArea();
}
} // end class
Frame
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.*;
public class Question5Frame
{
public static void main(String [] args)
{
JFrame myFrame = new JFrame("Lab 3: Question 5") ;
// create an instance of Question5Panel and add to frame
Question5Panel myPanel = new Question5Panel( ) ;
myFrame.add (myPanel );
// set up functionality of frame
myFrame.setSize(500, 310 );
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ;;
}//end main
} // end class
Explanation / Answer
ONLY QUESTION5PANEL CLASS ELSE EVERYTHING IS SAME
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.border.*;
public class Question5Panel extends JPanel implements ActionListener
{
// data declarations
private JLabel shapeLabel;
private JButton readShapeData;
private JRadioButton circleButton;
private JRadioButton rectangleButton;
private JRadioButton equTButton;
private JRadioButton cylinderButton;
private ButtonGroup shape;
private JPanel selectShape;
private JLabel radius;
private JTextField radiusIn;
private JLabel rectangle;
private JLabel length;
private JTextField lengthIn;
private JLabel width;
private JTextField widthIn;
private JLabel side;
private JTextField sideIn;
private JLabel cylinder;
private JLabel height;
private JTextField heightIn;
private JLabel ra;
private JTextField raIn;
private JPanel westPanel;
private JPanel circlePanel;
private JPanel rectanglePanel;
private JPanel equTPanel;
private JPanel cylinderPanel;
private JPanel outputPanel;
private JTextArea output;
private Circle myCircle;
private Rectangle myRectangle;
private Rectangle myEquTriangle;
private Rectangle myCylinder;
// constructor to initiate data and set up GUI
public Question5Panel()
{
setLayout(new BorderLayout());
// setting up north area of GUI
readShapeData=new JButton("Read shape info");
shapeLabel = new JLabel("Please select a shape ");
shapeLabel.setFont(new Font("Arial", Font.BOLD, 16));
// organizing radio buttons and their behaviours
circleButton = new JRadioButton("Circle",true);
rectangleButton = new JRadioButton("Rectangle");
equTButton = new JRadioButton("EquilateralTriangle");
cylinderButton = new JRadioButton("Cylinder");
shape = new ButtonGroup();
shape.add(circleButton);
shape.add(rectangleButton);
shape.add(equTButton);
shape.add(cylinderButton);
// adding components to panel to be located north in the GUI
selectShape = new JPanel();
selectShape.add(shapeLabel);
selectShape.add(circleButton);
selectShape.add(rectangleButton);
selectShape.add(equTButton);
selectShape.add(cylinderButton);
add(selectShape, BorderLayout.NORTH);
// setting up west area of GUI
westPanel = new JPanel();
// setting up components for the circlePanel of the GUI
circlePanel = new JPanel();
radius = new JLabel("radius is: ");
radiusIn = new JTextField("0",10);
circlePanel.add(radius);
circlePanel.add(radiusIn);
TitledBorder circleBorder = BorderFactory.createTitledBorder("Circle");
circlePanel.setBorder(circleBorder);
add(westPanel, BorderLayout.WEST);
// setting up components for the rectanglePanel of GUI
length = new JLabel("length is: ");
lengthIn = new JTextField("0",10);
width = new JLabel("width is: ");
widthIn = new JTextField("0",10);
// adding components to the rectanglePanel of GUI
rectanglePanel = new JPanel();
rectanglePanel.add(length);
rectanglePanel.add(lengthIn);
rectanglePanel.add(width);
rectanglePanel.add(widthIn);
TitledBorder rectangleBorder = BorderFactory.createTitledBorder("Rectangle");
rectanglePanel.setBorder(rectangleBorder);
// setting up components for the equTPanel of the GUI
equTPanel= new JPanel();
side = new JLabel("side is: ");
sideIn = new JTextField("0",10);
equTPanel.add(side);
equTPanel.add(sideIn);
TitledBorder equtBorder = BorderFactory.createTitledBorder("EquilateralTriangle");
equTPanel.setBorder(equtBorder);
// setting up components for the cylinderPanel of GUI
height = new JLabel("height is: ");
heightIn = new JTextField("0",10);
ra = new JLabel("radius is: ");
raIn = new JTextField("0",10);
// adding components to the cylinderPanel of GUI
cylinderPanel = new JPanel();
cylinderPanel.add(height);
cylinderPanel.add(heightIn);
cylinderPanel.add(ra);
cylinderPanel.add(raIn);
TitledBorder cylinderBorder = BorderFactory.createTitledBorder("Cylinder");
cylinderPanel.setBorder(cylinderBorder);
add(westPanel, BorderLayout.WEST);
// adding the circlePanel, rectanglePanels, equTPanel, and cylinderPanel into the westPanel
westPanel.setLayout(new GridLayout(2, 1));
westPanel.setPreferredSize(new Dimension(400, 15));
westPanel.add(circlePanel);
westPanel.add(rectanglePanel);
westPanel.add(equTPanel);
westPanel.add(cylinderPanel);
// setting up center area of GUI
outputPanel = new JPanel();
output = new JTextArea(15, 25);
outputPanel.add(output);
add(outputPanel, BorderLayout.CENTER);
add(readShapeData, BorderLayout.SOUTH);
// add listeners to radio buttons
readShapeData.addActionListener(this);
circleButton.addActionListener(this);
rectangleButton.addActionListener(this);
equTButton.addActionListener(this);
cylinderButton.addActionListener(this);
// add listeners to all textfields
radiusIn.addActionListener(this);
lengthIn.addActionListener(this);
widthIn.addActionListener(this);
sideIn.addActionListener(this);
heightIn.addActionListener(this);
raIn.addActionListener(this);
radiusIn.setEditable(true);
lengthIn.setEnabled(false);
widthIn.setEnabled(false);
sideIn.setEnabled(false);
heightIn.setEnabled(false);
raIn.setEnabled(false);
} // end constructor
//***************************************************************************
// Students to complete the functionality of the GUI through actionPerformed
//***************************************************************************
public void actionPerformed(ActionEvent ae)
{
System.out.println(ae.getActionCommand());
String data = "";
if (ae.getActionCommand().equals("Circle"))
{
data = getData(0);
output.setText(data);
radiusIn.setEditable(true);
lengthIn.setEnabled(false);
widthIn.setEnabled(false);
sideIn.setEnabled(false);
heightIn.setEnabled(false);
raIn.setEnabled(false);
}
else if (ae.getActionCommand().equals("Rectangle"))
{
data = getData(0, 0);
System.out.println(data);
output.setText(data);
radiusIn.setEditable(false);
lengthIn.setEnabled(true);
widthIn.setEnabled(true);
sideIn.setEnabled(false);
heightIn.setEnabled(false);
raIn.setEnabled(false);
}
else if (ae.getActionCommand().equals("EquilateralTriangle"))
{
data = getData(0);
output.setText(data);
radiusIn.setEditable(false);
lengthIn.setEnabled(false);
widthIn.setEnabled(false);
sideIn.setEnabled(true);
heightIn.setEnabled(false);
raIn.setEnabled(false);
}
else if (ae.getActionCommand().equals("Cylinder"))
{
data = getData(0, 0);
System.out.println(data);
output.setText(data);
radiusIn.setEditable(false);
lengthIn.setEnabled(false);
widthIn.setEnabled(false);
sideIn.setEnabled(false);
heightIn.setEnabled(true);
raIn.setEnabled(true);
}else{
try {
BufferedReader br=new BufferedReader(new FileReader("GeometricOjects.txt"));
StringTokenizer st;
String str;
while((str=br.readLine())!=null){
st = new StringTokenizer(str,",");
while (st.hasMoreTokens()) {
if(st.nextToken().equals("Circle")){
data = getData(Integer.parseInt(st.nextToken()));
output.setText(data);
JOptionPane.showMessageDialog(this,data);
}else if(st.nextToken().equals("Rectangle")){
data = getData(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
output.setText(data);
JOptionPane.showMessageDialog(this,data);
}else if(st.nextToken().equals("Triangle")){
data = getData1(Integer.parseInt(st.nextToken()));
output.setText(data);
JOptionPane.showMessageDialog(this,data);
}else{
data = getData1(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
output.setText(data);
JOptionPane.showMessageDialog(this,data);
}
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Question5Panel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Question5Panel.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (circleButton.isSelected())
{
System.out.println("--");
try
{
int i=Integer.parseInt(radiusIn.getText());
data=getData(i);
System.out.println();
output.setText(data);
}
catch (Exception e)
{
}
}
if (rectangleButton.isSelected())
{
try
{
int i=Integer.parseInt(lengthIn.getText());
int j=Integer.parseInt(widthIn.getText());
System.out.println(i+","+j);
data=getData(i,j);
output.setText(data);
}
catch (Exception e)
{
}
}
if (equTButton.isSelected())
{
System.out.println("--");
try
{
int a=Integer.parseInt(sideIn.getText());
data=getData1(a);
System.out.println();
output.setText(data);
}
catch (Exception e)
{
}
}
if (cylinderButton.isSelected())
{
try
{
int a=Integer.parseInt(heightIn.getText());
int b=Integer.parseInt(raIn.getText());
System.out.println(a+","+b);
data=getData1(a,b);
output.setText(data);
}
catch (Exception e)
{
}
}
}
private String getData(int i, int j)
{
Rectangle rectangle=new Rectangle("black",false,i,j);
return "rectangle has length of " + i + " with a width of " + j + " " + "and a perimeter of " + rectangle.findPerimeter() + " and a area of " + rectangle.findArea();
}
private String getData(int i)
{
Circle circle = new Circle("black",false,i);
return "circle has radius of " + i + " " + "and a perimeter of " + circle.findPerimeter() + " and a area of " + circle.findArea();
}
private String getData1(int a, int b)
{
Cylinder cylinder=new Cylinder("black",false,a,b);
return "cylinder has height of " + a+ " with a radius of " + b+ " " + "and a perimeter of " + cylinder.findPerimeter() + " and a area of " + cylinder.findArea();
}
private String getData1(int a)
{
EquilateralTriangle eqt=new EquilateralTriangle("black",false,a);
return " Equilaternal triangle has side of " + a + " " + "and a perimeter of " + eqt.findPerimeter() + " and a area of " + eqt.findArea();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.