I have the following code. It works fine, but rather than having a single panel
ID: 3532055 • Letter: I
Question
I have the following code. It works fine, but rather than having a single panel in which the user inputs their 3 pieces of data, and the results is displayed on the same panel, I want four individual panels. Can someone please modify my code to do this?
Write a program called InheritanceTest.java to support an inheritance hierarchy for class Point-Square-Cube. Use Point as the superclass of the hierarchy. Specify the instance variables and methods for each class. The private variable of Point should be the x-y coordinates. The private data of Square should be the sideLength. The private data of Cube should be depth. Each class must provide applicable accessor, mutator, and toString() methods for manipulating private variables of each corresponding class. In addition, the Square class must provide the area() and perimeter() methods. The Cube must provide the area() and volume() methods.
Write a program that instantiates objects of your classes, ask the user to enter the value for x, y, and sideLength, test all instance methods and outputs of each object
Explanation / Answer
//just see this modified version of your code, if it fullfills your requirenment ,please rate
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class Point {
private int x;
private int y;
public Point(int x,int y) {
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public String toString() {
String output = "Point : X coordinate = " + x + ", Y coordinate = "+ y;
return output;
}
}
class Square extends Point {
protected double sideLength;
public Square(Point p,double sideLength){
super(p.getX(), p.getY());
this.sideLength=sideLength;
}
public double getSideLength() {
return sideLength;
}
public void setSideLength(int sideL){
this.sideLength=sideL;
}
public double area() {
double area = sideLength * sideLength;
return area;
}
public double perimeter(){
return sideLength*4;
}
public String toString() {
String output = super.toString()+", Square: sidelength = " + sideLength;
return output;
}
}
class Cube extends Square {
double depth;
public Cube(Square s) {
super(new Point(s.getX(), s.getY()),s.getSideLength());
this.depth=s.getSideLength();
}
public double getDepth() {
return depth;
}
public void setDepth(int depth){
this.depth=depth;
}
public double volume(){
return (depth*depth*depth);
}
public double area() {
double area = depth*depth;
return area;
}
public String toString() {
String output = super.toString()+", Cube: depth = " + depth;
return output;
}
}
public class InheritanceTest extends JFrame implements ActionListener
{
JButton submit;
JLabel xCo,yCo,sideL;
JTextField xT,yT,sideT;
JTextArea xA;
public InheritanceTest(){
super("Inheritance Test");
setVisible(true);
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
submit=new JButton("Submit");
xCo=new JLabel("X-coordinate");
yCo=new JLabel("Y-coordinate");
sideL=new JLabel("Side Length");
xT=new JTextField();
yT=new JTextField();
sideT=new JTextField();
add(xCo);
add(yCo);
add(sideL);
add(xT);
add(yT);
add(sideT);
add(submit);
setLayout(null);
xCo.setBounds(20,20,100,20);
yCo.setBounds(20,50,100,20);
sideL.setBounds(20,80,100,20);
xT.setBounds(100,20,100,20);
yT.setBounds(100,50,100,20);
sideT.setBounds(100,80,100,20);
submit.setBounds(100,110,100,20);
submit.addActionListener(this);
}
public static void main(String args[]) {
new InheritanceTest();
}
public void actionPerformed(ActionEvent e) {
int x,y;
double sideLength;
x=Integer.parseInt(xT.getText());
y=Integer.parseInt(yT.getText());
sideLength=Double.parseDouble(sideT.getText());
Point point=new Point(x, y);
Square sq = new Square(point, sideLength);
String result1;
String result2;
String result3;
String result4;
String result=("***************** Result ******************** ");
result1=result+("Area of Square: " + sq.area());
result2=result+(" Circumference of Square: " + sq.perimeter());
Cube cu = new Cube(sq);
result3=result+(" Area of Cube: " + cu.area());
result4=result+(" Volume of Cube: " + cu.volume());
Thread t1=new Thread(new a(result1,100));
Thread t2=new Thread(new a(result2,150));
Thread t3=new Thread(new a(result3,200));
Thread t4=new Thread(new a(result4,250));
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class a extends JFrame implements Runnable
{
int loc=0;
String str;
a(String str,int loc)
{
this.str=str;
this.loc=loc;
}
public void run()
{
loc=loc+100;
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(loc, loc, 400, 200);
setTitle("InheritanceTest.java");
JTextArea xA=new JTextArea();
xA.setText(str);
xA.setBounds(10, 10, 300,200);
add(xA);
setLayout(null);
}
}