write the classes point square cube for this main import java.awt.Point; import
ID: 3552968 • Letter: W
Question
write the classes point square cube for this main
import java.awt.Point;
import javax.swing.JOptionPane;
public class InheritanceTest {
public static void main(String[] args)
{
String strNumber;
int x = 0, y = 0, sl = 0;
x = getNumber("Enter x coordinate: ");
y = getNumber("Enter y coordinate: ");
sl = getNumber("Enter side of Square: ");
//Instantiate the classes
Point myPoint = new Point(x, y);
Square mySquare = new Square(x, y, sl);
Cube myCube = new Cube(x, y, sl);
//Display the output
JOptionPane.showMessageDialog(null, "Point: " + myPoint.toString()+
" Square: " + myPoint.toString() + mySquare.toString() +
" Cube: " + myPoint.toString()+ myCube.toString(),
"Result", JOptionPane.INFORMATION_MESSAGE);
}
public static int getNumber(String str)
{
String numStr = "";
int num = 0;
boolean isValid = false;
//while input is not valid
while(!isValid)
{
numStr = JOptionPane.showInputDialog(str);
if (numStr == null)
{
JOptionPane.showMessageDialog(null, "You must enter a valid value. Please try again", "Error", JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
//If the number entered is not a number, an error will occur
num = Integer.parseInt(numStr);
isValid = true;
}
catch (NumberFormatException nfe)
{
//Execute this code if an error is detected
JOptionPane.showMessageDialog(null, "The value you entered is not a valid number. Please try again", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
return num;
}
}
Explanation / Answer
import java.awt.Point;
import javax.swing.JOptionPane;
public class InheritanceTest {
private class Point{
int x,y;
public Point(int x,int y){
this.x = x;
this.y = y;
}
}
private class Square{
int x,y,sl;
public Square(int x,int y,int sl){
this.x = x;
this.y = y;
this.sl = sl;
}
}
private class Cube{
int x,y,sl;
public Cube(int x,int y,int sl){
this.x = x;
this.y = y;
this.sl = sl;
}
}
public static void main(String[] args)
{
String strNumber;
int x = 0, y = 0, sl = 0;
x = getNumber("Enter x coordinate: ");
y = getNumber("Enter y coordinate: ");
sl = getNumber("Enter side of Square: ");
//Instantiate the classes
Point myPoint = new Point(x, y);
Square mySquare = new Square(x, y, sl);
Cube myCube = new Cube(x, y, sl);
//Display the output
JOptionPane.showMessageDialog(null, "Point: " + myPoint.toString()+
" Square: " + myPoint.toString() + mySquare.toString() +
" Cube: " + myPoint.toString()+ myCube.toString(),
"Result", JOptionPane.INFORMATION_MESSAGE);
}
public static int getNumber(String str)
{
String numStr = "";
int num = 0;
boolean isValid = false;
//while input is not valid
while(!isValid)
{
numStr = JOptionPane.showInputDialog(str);
if (numStr == null)
{
JOptionPane.showMessageDialog(null, "You must enter a valid value. Please try again", "Error", JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
//If the number entered is not a number, an error will occur
num = Integer.parseInt(numStr);
isValid = true;
}
catch (NumberFormatException nfe)
{
//Execute this code if an error is detected
JOptionPane.showMessageDialog(null, "The value you entered is not a valid number. Please try again", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
return num;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.