Java (Triangle 类)设计一个名为 Triangle 的类,它扩展了 GeometricObject
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19624819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(The Triangle class) Design a class named Triangle that extends GeometricObject
提问by Khilmarsen
The class contains:
该类包含:
Three doubledata fields named side1, side2, and side3with default values 1.0to denote three sides of the triangle
A no-arg constructor that creates a triangle with specified side1, side2,and side3.
The accessor methods for all three data fields.
A method named getArea()that returns the area if this triangle.
A method named getPerimeter()that returns the perimeter of this triangle.
三个双命名的数据字段SIDE1,侧面2,和SIDE3用默认值1.0表示的三角形的三个边
甲无参数构造创建具有指定的三角形SIDE1,侧面2,和SIDE3。
所有三个数据字段的访问器方法。
一个名为getArea() 的方法,它返回这个三角形的面积。
一个名为getPerimeter() 的方法,它返回这个三角形的周长。
*A method named toString()that returns a string description for the triangle.
*一个名为toString() 的方法,它返回三角形的字符串描述。
For the formula to compute the area of a triangle, see ProgrammingExercise 2.15( in An introduction to java programming 9th edition) The toString()method is implemented as follows:
计算三角形面积的公式参见ProgrammingExercise 2.15(在Java编程简介第9版中)toString()方法实现如下:
return " Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 " + side3;
return " 三角形: side1 = " + side1 + " side2 = " + side2 + " side3 " + side3;
Draw the UML diagrams for the classes Triangleand GeometricObjectand implement the classes. Write a test program that prompts the user to enter three sides of the triangle, a color, and a boolean value to indicate wheter the triangle is filled. The program should create a triangleobject with these sides and set the colorand filledproperties using the input. The program should display the area, perimeter, color and true or false to indicate wheter it is filled or not.
为Triangle和GeometricObject类绘制 UML 图并实现这些类。编写一个测试程序,提示用户输入三角形的三个边、一种颜色和一个布尔值来指示三角形是否被填充。程序应该使用这些边创建一个三角形对象,并使用输入设置颜色和填充属性。程序应该显示面积、周长、颜色和真假以表明它是否被填充。
The part of the exercise that i am having trouble with is the test program.
我遇到问题的练习部分是测试程序。
If you could help me out by giving me pointers about how i can make the Triangle from the input i would be very grateful.
如果你能通过给我一些关于如何从输入中制作三角形的指示来帮助我,我将非常感激。
The code i have at the moment is this :
我目前的代码是这样的:
The 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 ";
}
}
The Triangle program
三角计划
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;
}
}
The Testprogram.
测试程序。
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?);
}
}
采纳答案by Paul Samsotha
You need to create a new Triangle
object like this, so that you have a reference
你需要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.
You also need to set it's filled
and color
properties
您还需要设置它的filled
和color
属性
triangle.setFilled(filled);
triangle.setColor(color);
Then, you can invoke its methods like this:
然后,您可以像这样调用它的方法:
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());
You're able to access the GeometricObject
's isFilled()
, setFilled()
, getColor()
, and setColor()
because a Triangle
is a GeometricObject
(extends
), so it inherits all its methods.
您可以访问GeometricObject
's isFilled()
, setFilled()
, getColor()
, 并且setColor()
因为 aTriangle
是GeometricObject
( extends
),所以它继承了它的所有方法。
By the way, this is not how to calculate the area of a triangle:
顺便说一下,这不是计算三角形面积的方法:
public double getArea() {
return (side1 + side2 + side3) / 2; // This is so wrong
}
Check out this linkfor correct formula
查看此链接以获取正确的公式
Edit: With another problem with code
编辑:另一个代码问题
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;
}
Edit: Triangle Formula
编辑:三角形公式
public double getArea() {
int p = getPerimeter() / 2
return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3));
}
回答by kiru
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
*/
}
Triangle
三角形
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());
}
}