(Triangle类)设计一个名为Triangle的类,它扩展了GeometricObject

时间:2013-10-27 23:14:09

标签: java inheritance

该课程包含:

  • 三个 double 数据字段名为 side1 side2 side3 ,默认值 1.0 表示三角形的三个边

  • 一个无参数构造函数,用于创建指定 side1 side2 side3 的三角形。

    < / LI>
  • 所有三个数据字段的访问者方法。

  • 名为 getArea()的方法,如果此三角形返回该区域。

  • 一个名为 getPerimeter()的方法,它返回此三角形的周长。

*一个名为 toString()的方法,它返回三角形的字符串描述。

有关计算三角形面积的公式,请参阅ProgrammingExercise 2.15(在java编程简介第9版中) toString()方法实现如下:

返回“Triangle:side1 =”+ side1 +“side2 =”+ side2 +“side3”+ side3;

绘制三角形 GeometricObject 类的UML图并实现这些类。编写一个测试程序,提示用户输入三角形的三个边,颜色和布尔值,以指示填充三角形的位置。程序应使用这些边创建三角形对象,并使用输入设置颜色填充属性。该程序应显示区域,周长,颜色和真或假,以指示它是否填充。


我遇到麻烦的练习部分是测试程序。

如果你可以通过指导我如何从输入中制作三角形来帮助我,我将非常感激。

我目前的代码是:

GeometricObject

public class GeometricObject {
    private String color = " white ";
    private boolean filled;
    private java.util.Date dateCreated;

    public GeometricObject() {
        dateCreated = new java.util.Date();
    }

    public GeometricObject(String color, boolean filled) {
        dateCreated = new java.util.Date();
        this.color = color;
        this.filled = filled;   
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    public java.util.Date getDateCreated() {
        return dateCreated;
    }

    public String toString() {
        return "Created on " + dateCreated + "\n color: " + color + " and filled ";                 
    }   
 }

三角计划

public class Triangle extends GeometricObject {
    private double side1 = 1.0;
    private double side2 = 1.0;
    private double side3 = 1.0;

    public Triangle() {
    }

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    public double setSide1() {
        return side1;
    }

    public double setSide2() {
        return side2;
    }

    public double setSide3() {
        return side3;
    }

    public void setSide1(double side1) {
        this.side1 = side1;
    }

    public void setSide2(double side2) {
        this.side2 = side2;
    }

    public void setSide3(double side3) {
        this.side3 = side2;
    }

    public double getArea() {
        return (side1 + side2 + side3) / 2;
    }

    public double getPerimeter() {
        return side1 + side2 + side3;
    }

    public String toString() {
    return " Triangle: Side 1 = " + side1 + " Side 2 = " + side2
           + " Side 3 = " + side3;
    }
}

测试程序。

import java.util.Scanner;

public class TestTriangle  {
    private double side1 = 1.0;
    private double side2 = 1.0;
    private double side3 = 1.0;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter three sides of the Triangle");
        double side1 = input.nextDouble();
        double side2 = input.nextDouble();
        double side3 = input.nextDouble();

        System.out.println("Enter the color of the Triangle");
        String color = input.next();

        System.out.println(" Is the Triangle filled? Reply with 'True' or 'False' ");

        String filled = input.next(); 
    }
    {
        new Triangle(side1, side2, side3);
        //How do i get the information into theTriangle?

        System.out.println("The Triangle Sides are \n side 1: " + side1 + "\n Side 2: " + side2 + "\n Side 3: " + side3);
        System.out.println("The Triangle's Area is " + (side1 + side2 + side3) / 2);
        System.out.println("The Triangle's Perimeter is "
                           + (side1 + side2 + side3));
        System.out.println("The Triangle's Color is " + //what goes here?);
        System.out.println("Is the Triangle filled? " + //what goes here?);

    }
}

2 个答案:

答案 0 :(得分:1)

您需要像这样创建一个新的Triangle对象,以便您有一个参考

Trangle triangle = new Triangle(side1, side2, side3);

  //     ^^^^^^ This is the most important thing you're missing.  You need a reference
  //            point for your object.  That's the only way you can access it's
  //            properties.    

您还需要设置filledcolor属性

triangle.setFilled(filled);
triangle.setColor(color);

然后,你可以像这样调用它的方法:

System.out.println("The Triangle Sides are \n side 1: " 
       + triangle.getSide1() + "\n Side 2: " + triangle.getSide2() 
       + "\n Side 3: " + triangle.getSide3());

System.out.println("The Triangle's Area is " + triangle.getArea());

System.out.println("The Triangle's Perimeter is " + triangle.getPerimeter();

System.out.println("The Triangle's Color is " + triangle.getColor());
System.out.println("Is the Triangle filled? " + triangle.isFilled());

您可以访问GeometricObject的{​​{1}},isFilled()setFilled()getColor(),因为setColor()TriangleGeometricObject),因此它继承了所有方法。

顺便说一句,这不是如何计算三角形的面积:

extends

查看此link以获取正确的公式

编辑:代码的另一个问题

public double getArea() {
    return (side1 + side2 + side3) / 2;   // This is so wrong
}

编辑:三角公式

public double setSide1() {
    return side1;
}

public double setSide2() {
    return side2;
}

public double setSide3() {
    return side3;
}

/**** Should Be ******/

public double getSide1() {
    return side1;
}

public double getSide2() {
    return side2;
}

public double getSide3() {
    return side3;
}

答案 1 :(得分:0)

GeometricObject

package geometricobject;
import java.util.Scanner;

/**
 *
 * @author Kirubel 
 */

abstract class GeometricObject
{

private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject()
{


}

/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled)
{
    dateCreated = new java.util.Date();
    this.color = color;
    this.filled = filled;
}

/** Return color */
public String getColor()
{
    return color;
}

/** Set a new color */
public void setColor(String color)
{
    this.color = color;
}

/** Return filled. Since filled is boolean ,
 * the get method is named isFilled */
public boolean isFilled()
{
    return filled;
}

/** Set a new filled */
public void setFilled(boolean filled)
{
    this.filled = filled;
}

/** Get dateCreated */
public java.util.Date getDateCreated()
{
    return dateCreated;
}

@Override
public String toString() {
    return "created on " + dateCreated + "\ncolor: " + color +
            " and filled: " + filled;
}

/** Abstract method getArea */
public abstract double getArea();

/** Abstract method getPerimeter */
public abstract double getPerimeter();

/**
 * @param args the command line arguments
 */

}

三角

class Triangle extends GeometricObject
{

public double side1;
public double side2;
public double side3;
public Triangle() //default constructor
{
    side1=1.0;
    side2=1.0;
    side3=1.0;
}

public Triangle(double side1,double side2,double side3) //peremetrised constructor
{
    this.side1=side1;
    this.side2=side2;
    this.side3=side3;
}
@Override
public double getPerimeter() //function for getting peremeter for triangle
{
    return(side1+side2+side3) ;

}



@Override
// function for getting area of triangle
public double getArea() 
{
    double area,s;
    s=side1+side2+side3;
    area = Math.sqrt(s * (s- side1) * (s - side2) * (s - side3));
    return(area);

}
@Override
public String toString()//to print the data
{
    String s;
    s="Triangle:side1 "+ side1 +" side2 " + side2 + " side3 "+side3;
    return(s);
}

private String getSide1() {
    // TODO Auto-generated method stub
    return null;
}

private String getPerimeter(double d) {
    // TODO Auto-generated method stub
    return null;
}

private String getPerimeter(String string) {
    // TODO Auto-generated method stub
    return null;
}

private String getSide3() {
    // TODO Auto-generated method stub
    return null;
}

private String getSide2() {
    // TODO Auto-generated method stub
    return null;
}


public static void main(String args[])
{

    Scanner input = new Scanner(System.in);

    System.out.println("Enter three sides of the Triangle");
    double side1 = input.nextDouble();
    double side2 = input.nextDouble();
    double side3 = input.nextDouble();

    System.out.println("Enter the color of the Triangle");
    String color = input.next();

    System.out.println(" Is the Triangle filled? Reply with 'True' or 'False' ");

    String filled = input.next(); 
}


{
    Triangle triangle = new Triangle(side1, side2, side3);
    triangle.setFilled(isFilled());
    triangle.setColor(getColor());

    // Display 
    System.out.println("The Triangle Sides are \n side 1: " 
            + triangle.getSide1() + "\n Side 2: " + triangle.getSide2() 
            + "\n Side 3: " + triangle.getSide3());

    System.out.println("The Triangle's Area is " + triangle.getArea());
    System.out.println("The Triangle's Perimeter is " + triangle.getPerimeter());
    System.out.println("The Triangle's Color is " + triangle.getColor());
    System.out.println("Is the Triangle filled? " + triangle.isFilled());
}
}