我正在做一项任务,现在有点丢失了。赋值需要我创建一个与给定驱动程序一起使用的RightTriangle类。以下是详细信息:
你的RightTriangle类需要两个构造函数:
您的RightTriangle类需要以下公共方法:
RightTriangleDriver.java
import java.util.*;
public class RightTriangleDriver
{
public static void main(String[] args)
{
/*
Menu:
1 - test default constructor and toString
2 - test parameterized constructor and toString
3 - test getArea
4 - test getPerimeter
*/
Scanner kb = new Scanner(System.in);
int option = kb.nextInt();
switch(option)
{
case 1: //1 - test default constructor and toString
try
{
System.out.println("Testing default constructor, then toString()");
RightTriangle r = new RightTriangle();
System.out.println("Got: " + r);
}
catch(Throwable ex)
{
System.out.println("\tgot: " + ex);
}
break;
case 2: //2 - test parameterized constructor and toString
try
{
System.out.println("Testing parameterized constructor, then toString()");
double side1 = kb.nextDouble();
double side2 = kb.nextDouble();
RightTriangle r = new RightTriangle(side1, side2);
System.out.println("Got: " + r);
}
catch(Throwable ex)
{
System.out.println("Got: " + ex);
}
break;
case 3: //3 - test getArea
try
{
System.out.println("Testing getArea()");
double side1 = kb.nextDouble();
double side2 = kb.nextDouble();
RightTriangle r = new RightTriangle(side1, side2);
System.out.println("Got: " + r.getArea());
}
catch(Throwable ex)
{
System.out.println("Got: " + ex);
}
break;
case 4: //4 - test getPerimeter
try
{
System.out.println("Testing getPerimeter()");
double side1 = kb.nextDouble();
double side2 = kb.nextDouble();
RightTriangle r = new RightTriangle(side1, side2);
System.out.println("Got: " + r.getPerimeter());
}
catch(Throwable ex)
{
System.out.println("Got: " + ex);
}
break;
default:
System.out.println("I don't understand your request");
}
}
}
这是我迄今为止所拥有的
public class RightTriangle
{
private double side1;
private double side2;
//constructors
public RightTriangle()
{
this(1.0, 1.0);
}
public RightTriangle(double s1, double s2)
{
if(s1 > 0 && s2 > 0)
side1 = s1;
side2 = s2;
else
throw new IllegalArgumentException("All triangle edges must have positive length");
}
//Methods
public String toString()
{
return "A right triangle with edges " + side1 + ", " + side2 + " , and hypotenuse " + Math.sqrt((side1 * side1) + (side2 * side2)) + ".";
}
//get Area
public double getArea()
{
return side1 * side2 / 2;
}
//get Perimeter
public double getPerimeter()
{
return side1 + side2 + Math.sqrt((side1 * side1) + (side2 * side2));
}
//get hypotenuse
public double getHypotenuse()
{
return Math.sqrt((side1 * side1) + (side2 * side2));
}
答案 0 :(得分:0)
我可能错过了一些东西,但似乎你只需要根据赋值中的规范覆盖toTtring for RightTriangle。
如果你愿意,我可以详细说明。如果我有这样的声誉,我会发布这个评论作为评论。
编辑:
RightTriangle r = new RightTriangle();
System.out.println("Got: " + r);
此代码来自RightTriangleDriver。当p
,RightTriangle对象被赋予System.out.println时,将打印其toString()
值。
因此与以下内容相同:
RightTriangle r = new RightTriangle();
System.out.println("Got: " + r.toString());
根据作业中的规格:
您的RightTriangle类需要以下公共方法:
...
一个toString方法(),它以下列格式返回一个String:“一个边缘为1.0,1.0和斜边的直角三角形1.4142135623730951。”
编辑2:toString的更详细描述
对于java中的每个对象都存在 toString
。默认情况下,您自己的类'toString
方法将返回类似“RightTriangle @ l13e8ed”的内容。
在此赋值的规范中,您的任务是使toString()返回对象的特定描述而不是默认值。我们通过覆盖 toString
方法来实现这一目标。
如果要覆盖方法,可以使用与超类中相同的声明(返回类型,名称和参数类型)创建一个新方法。有更高级的规则,但一般来说,您只需使用相同的声明创建一个方法。在这种情况下,在public String toString()
中创建RightTriangle
将覆盖默认情况下存在的toString():
public String toString(){
return "A right triangle with edges " + firstSide + ", " + secondSide +", and hypotenuse " + hypotenuse + ".";
}