Question: Write the code in java from the following given code using JOPtionPane. Point.java public class Point { //Declaring instance variables private int x,y; //Zero Argumented

Write the code in java from the following given code using JOPtionPane.

Point.java

public class Point { //Declaring instance variables private int x,y;

//Zero Argumented constructor public Point() { super(); }

//Parameterized constructor public Point(int x, int y) { super(); this.x = x; this.y = y; }

//Getter and Setter methods public int getX() { return x; }

public void setX(int x) { this.x = x; }

public int getY() { return y; }

public void setY(int y) { this.y = y; }

/* toString() method which displays * the contents of an object inside it */ @Override public String toString() { return "Point ("+ x + "," + y +")"; }

}

___________________

Circle.java

import java.text.DecimalFormat;

public class Circle extends Point {

//Declaring instance variables private double radius; //Zero Argumented constructor public Circle() { this.radius=0.0; } //Parameterized constructor public Circle(int x, int y,double radius) { super(x, y); setRadius(radius); } //Getter and Setter methods public double getRadius() { return radius; } public void setRadius(double radius) { if(radius>=0) this.radius = radius; else { System.out.println("Invalid.Radius Must be Positive."); radius=0.0; } } public double calArea() { return Math.PI*getRadius()*getRadius(); } /* toString() method which displays * the contents of an object inside it */ @Override public String toString() { DecimalFormat df=new DecimalFormat("#.##"); System.out.println(super.toString()); return "Circle# Radius=" + radius + " Area of Circle ="+df.format(calArea()); }

}

________________________

Cylinder.java

import java.text.DecimalFormat;

public class Cylinder extends Circle { //Declaring instance variables private double height;

//Zero Argumented constructor public Cylinder() { super(); }

//Parameterized constructor public Cylinder(int a,int b,double r,double height) { super(a,b,r); this.height = height; }

//Getter and Setter methods public double getHeight() { return height; }

public void setHeight(double height) { this.height = height; }

public double calVolume() { return Math.PI*getRadius()*getRadius()*getHeight(); }

/* toString() method which displays * the contents of an object inside it */ @Override public String toString() { DecimalFormat df=new DecimalFormat("#.##"); System.out.println(super.toString()); return "Cylinder# Height=" + height + " Volume Of the Cylinder ="+df.format(calVolume()); }

}

__________________________

Application.java

import java.util.ArrayList; import java.util.Scanner;

public class Application {

public static void main(String[] args) { //Declaring variables int x,y; double radius,height; //Creating an ArrayList Object ArrayList arl=new ArrayList(); /* Scanner class object is used to * read the inputs entered by the user */ Scanner sc=new Scanner(System.in); //Getting the point coordinates entered by the user System.out.print("Enter the X coordinate of Point :"); x=sc.nextInt(); System.out.print("Enter the Y coordinate of Point :"); y=sc.nextInt(); //Getting the radius of the circle enterd by the user System.out.print("Enter radius of the Circle :"); radius=sc.nextDouble(); //Getting the height of the circle entered by the user System.out.print("Enter Height of the Cylinder :"); height=sc.nextDouble(); /* Creating a Point class object by passing * the x,y coodinates as arguments */ Point p=new Point(x, y); //Adding the Point class object to the ArrayList arl.add(p); /* Creating a Circle class object by passing * the x,y coodinates and radius as arguments */ Circle c=new Circle(x, y, radius); //Adding the Circle class object to the ArrayList arl.add(c); /* Creating a Cylinder class object by passing * the x,y coodinates, radius,height as arguments */ Cylinder cyl=new Cylinder(x, y, radius, height); //Adding the Cylinder class object to the ArrayList arl.add(cyl); //Displaying Each object Information System.out.println("_____Displaying each Object Information_____"); for(Object o:arl) { System.out.println(o.toString()); System.out.println("_________________"); }

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!