私はいくつかの宿題に取り組んでいますが、Point、Square、および Cube クラスから toString メソッドを呼び出して印刷する方法がわかりません。私はそれが何かばかげているにちがいないことを知っています。今私は持っていますか??? toSting メソッドが配置される場所に配置されます。私が考えることができるすべての組み合わせ(「クラス」.toStringなど)を試しました。誰かが私がどこを台無しにしているのか教えてもらえますか? ありがとう!!
import javax.swing.JOptionPane;
public class InheritanceTest {
public static void main(String args[]){
// Declare variables
String xString = null;
String yString = null;
String sideString = null;
int x = 0;
int y = 0;
int sideLength = 0;
try{
xString = JOptionPane.showInputDialog("Enter x coordinate:");
x = Integer.parseInt(xString);
yString = JOptionPane.showInputDialog("Enter y coordinate:");
y = Integer.parseInt(yString);
sideString = JOptionPane.showInputDialog("Enter side of Square:");
sideLength = Integer.parseInt(sideString);
} // End try
catch(NumberFormatException e){
JOptionPane.showMessageDialog( null,"The value you entered is not a valid number. Please try again",
"Error", JOptionPane.ERROR_MESSAGE);
} // End catch
JOptionPane.showMessageDialog(null, "Point: \n" +????????, "Results", JOptionPane.INFORMATION_MESSAGE);
} // End main
}// End Class
class Point{
//Declare variables
private int x;
private int y;
// Point constructor
Point(int xCoordinate,int yCoordinate){
x = xCoordinate;
y = yCoordinate;
}// End Point Constructor
// Accessor to return x coordinate
public int getX(){
return x;
}// End getX method
// Accessor to return y coordinate
public int getY(){
return y;
}// End getY method
// Format and display coordinates
public String toString(){
return "Corner = [" + x + "," + y + "]\n";
}// End toString
}// End Point Class
abstract class Square extends Point{
//Declare variables
private double sideLength;
// Square constructor
Square(int x, int y, double s){
super(x, y);
sideLength = s;
} // End Square constructor
// Accessor to return side length
public double getSide(){
return sideLength;
} // End getSide
// Method to calculate area of square
public double area(){
return sideLength * sideLength;
} // End area method
// Method to calculate perimeter of square
public double perimeter(){
return 4 * sideLength;
} // End perimeter method
// Format and display the square
public String toString(){
return super.toString() + "Side length is: " + sideLength + "\n" +
"Area is: " + area() + "\n" + "Perimeter is: " + perimeter();
} // End toString
}// End Square Class
abstract class Cube extends Square{
// Declare variable
double depth;
// Cube constructor
public Cube(int x, int y, double s, double z){
super(x, y, s);
depth = z;
} // End Cube constructor
// Method to calculate area of cube
public double area(){
return 6 * super.area();
} // End Area
// Method to calculate volume of cube
public double volume(){
return super.area() * depth;
} // End volume
// Format and display the cube
public String toString(){
return "Depth is: " + depth + "\n" + "Area is: " + area() + "\n" + "Volume is: " + volume();
} // End toString
} // End Cube class