Change the below code in Java using JOptionPane import java.util.ArrayList; impo
ID: 3730430 • Letter: C
Question
Change the below code in Java using JOptionPane
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<Object> arl=new ArrayList<Object>();
/* 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("_________________");
}
}
}
Explanation / Answer
import java.util.*;
import javax.swing.*;
public class Application {
public static void main(String[] args) {
JFrame frame = new JFrame();
//Declaring variables
int x,y;
double radius,height;
//Creating an ArrayList Object
ArrayList<Object> arl=new ArrayList<Object>();
/* 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 :");
String temp = JOptionPane.showInputDialog(frame,"Enter the X coordinate of Point");
// convert string to integer
x = Integer.parseInt(temp);
//System.out.print("Enter the Y coordinate of Point :");
temp = JOptionPane.showInputDialog(frame,"Enter the Y coordinate of Point");
y = Integer.parseInt(temp);
//Getting the radius of the circle enterd by the user
//System.out.print("Enter radius of the Circle :");
//radius=sc.nextDouble();
temp = JOptionPane.showInputDialog(frame,"Enter radius of the Circle");
radius = Double.parseDouble(temp);
//Getting the height of the circle entered by the user
//System.out.print("Enter Height of the Cylinder :");
temp = JOptionPane.showInputDialog(frame,"Enter Height of the Cylinder");
height = Double.parseDouble(temp);
/* 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("_________________");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.