我应该得到用户输入的三角形的颜色以及三角形的宽度和高度,但是,我甚至无法显示输出。任何提示或指示都会受到高度赞赏,因为我现在只是在学校学习抽象课程。谢谢。
三角类:
import java.util.Scanner;
public class Triangle extends GeometricObject {
private double base;
private double height;
private int color;
public Triangle() {
}
public static void main(String[] args) {
System.out.println("Enter a width for triangle:");
double base = Integer.parseInt(input.nextInt());
System.out.println("Enter height for triangle:");
double height = Integer.parseInt(input.nextInt());
}
public Triangle(double base, double height) {
this.base = base;
this.height = height;
this.color = color;
}
public double getBase() {
return base;
}
public void setWidth(double width) {
this.base = base;
}
public double getHeight() {
return height;
}
public int getColor(){
return color;
}
public void setHeight(double height) {
this.height = height;
}
public void setColor(int color){
this.color = color;
}
@Override
public double getArea() {
return 0.5* base * height;
}
@Override
public double getPerimeter() {
return 2 * (base + height);
}
}
这是GeometricClass:
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** 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;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
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();
}
答案 0 :(得分:1)
你的代码有很多缺陷:
1)Scanner input = new Scanner(System.in);
缺失。你必须得到编译错误。
2)Integer.parseInt
需要String
参数。
3)为什么要将int
分配给double
。而是使用Double.parseDouble
。
4)您的构造函数Triangle(double base, double height)
有两个参数,但您仍然在编写this.color = color;
。为什么呢?
5)int color;
为什么颜色为int
?应该是String
。
6)父类中存在color
。为什么要在子类上添加另一个呢?
必须遵守这些要点。
我做了一些基本的改动自己尝试其余的。这是修改后的代码:
public class Triangle extends GeometricObject {
private double base;
private double height;
private int color;
public Triangle() {
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a width for triangle:");
double base = Double.parseDouble("" + input.nextInt());
System.out.println("Enter height for triangle:");
double height = Double.parseDouble("" + input.nextInt());
GeometricObject triangle = new Triangle(base, height);
System.out.println("Area of triangle " + triangle.getArea());
System.out.println("Perimeter of triangle " + triangle.getPerimeter());
}
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
public double getBase() {
return base;
}
public void setWidth(double width) {
this.base = base;
}
public double getHeight() {
return height;
}
public int getColor() {
return color;
}
public void setHeight(double height) {
this.height = height;
}
public void setColor(int color) {
this.color = color;
}
@Override
public double getArea() {
return 0.5 * base * height;
}
@Override
public double getPerimeter() {
return 2 * (base + height);
}
}
您必须通过阅读上述代码来探索自己。就像为什么我使用下面的选项:
GeometricObject triangle = new Triangle(base, height);
答案 1 :(得分:0)
什么是input
?宣布在哪里?
您应该将Scanner对象指向System.in。通过这种方式,您可以要求用户输入。
Scanner sc = new Scanner(System.in);
while (sc.hasNext() ) {
System.out.println('Enter color');
int color = Integer.parseInt(sc.nextLine());
}