帮我修复代码。 [英] Help me to fix the code.

查看:87
本文介绍了帮我修复代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好编码员。我这里有几个代码。我试图解决如何编写Java书籍中的问题。我总结了下面的问题:



Hello coders. I have a few codes here. I tried to solve the question in the How to program Java book. I summarized the question below:

You are required to implement the following design as well as a main() method in another class to test your implementation:
Implement the hierarchy below where
- MyShape is an abstract class with an abstract Draw method,
- MyBoundedShape is an abstract class with an abstract GetArea method,
- MyLine, MyOval, MyRectangle are concrete classes
In the main() method,
- Ask the user to select 5 shapes and input their dimension
- Draw selected shapes
- Compute and show the area of selected shapes if they are a bounded shape



当我运行代码时,它没有显示区域或计算区域。帮我解决这个问题。谢谢顺便说一下。



我的尝试:



MyShape class


When I ran the code, it didn't show the area or the calculated area. Help me to fix this. Thanks btw.

What I have tried:

MyShape class

public abstract class MyShape 
{
	public abstract void Draw();
	

}



Myline类


Myline class

public class Myline extends MyShape 
{
private int length;
public Myline (int length)
{
	length=0;
}
public void setlength( int length )
 {
 length = 0; 
 } 


 public int getlength()
 {
 return length;
 } 
public void Draw()
{
	System.out.printf("Drawing a line with the length",getlength());
}
}



Myextendedshape类


Myextendedshape class

public abstract class MyextendedShape extends MyShape 
{
	protected double area;
	public abstract double getArea();

}



Myoval类


Myoval class

public class Myoval extends MyextendedShape
{
	private double Line1;
	private double Line2;
	public void Draw() {
        System.out.println("I am drawing  a Oval");
    }

     Myoval()
    {
        Line1= 0.0;
        Line2 = 0.0;        
    }

    Myoval(double Line1, double Line2){
        this.Line1 =Line1;
        this.Line2 = Line2;
    }



    public double getLine1() {
        return Line1;
    }

    public void setLine1(double Line1) {
        this.Line1 = Line1;
    }

    public double getLine2() {
        return Line2;
    }

    public void setLine2(double Line2) {
        this.Line2 = Line2;
    }

    @Override
    public double getArea() {
        return calculateArea();
    }

    
    private double calculateArea(){
        return area = 3.14*Line1 * Line2;
    }

    public String toString(){
        return "The Line number 1 of the oval is: " + Line1 + " and the Line number 2 is: " + Line2 + ", "
                + "and the area is: " + getArea();
    }

}



我的矩形类


My rectangle class

public class MyRectangle extends MyextendedShape {
    private double length, width;

    public void Draw() {
        System.out.println("I am drawing  a Rectangle");
    }

    MyRectangle()
    {
        length= 0.0;
        width = 0.0;        
    }

    MyRectangle(double length, double width){
        this.length =length;
        this.width = width;
    }



    public double getLenght() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    @Override
    public double getArea() {
        return calculateArea();
    }

   
    private double calculateArea(){
        return area = width * length;
    }

    public String toString(){
        return "The width of the rectangle is: " + width + " and the length is: " + length + ", "
                + "and the area is: " + getArea();
    }

}



测试


Test

public class test {
	 public static void main(String [] args) {
		 
		 MyShape s = new Myline(6);
		 s.Draw();
		 
		 s =  new Myoval(4.0, 5.0);
		 s.Draw();

	        s = new MyRectangle(4.0,6.0);
                s.Draw();

	    }
}

推荐答案

看看两件事:

1)错误信息。

Look at two things:
1) The error message.
Cannot instantiate the type MyRectangle





2)课程本身。


And
2) The class itself.

public abstract class MyRectangle extends MyextendedShape {

它被声明为 abstract 类,具体表示你不能创建这个类的实例,只能创建具体的派生类。



想想它作为汽车片刻:汽车是抽象概念 - 你不能拥有汽车,你拥有梅赛德斯或福特。更具体地说,你拥有一辆Mercedes。AClass。A180 CDI,或Ford。Fiesta。Zetec 1.1

A180 CDI和Zetec 1.1是抽象的梅赛德斯和福特类派生的具体课程,这两个课程都来自抽象的汽车课程。



你只能实例化具体类,而不是抽象类 - 所以你的应用程序不会编译,这意味着它不会产生可执行文件,因此你在磁盘上找到的EXE中没有包含任何更改,并且可以执行!

删除关键字 abstract 并查看它是否干净地编译 - 直到它完成,你无法运行它!

It's declared as an abstract class which specifically means "you cannot create an instance of this class, only of concrete derived classes".

Think of it as cars for a moment: Car is an abstract concept - you can't own a "Car", you own a "Mercedes" or a "Ford". More specifically, you own a "Mercedes"."AClass"."A180 CDI", or a "Ford"."Fiesta"."Zetec 1.1"
"A180 CDI" and "Zetec 1.1" are the concrete classes which derive from the abstract "Mercedes" and "Ford" classes, both of which are derived from the abstract "Car" class.

You can only instantiate concrete classes, not abstract ones - so your app will not compile and that means it doesn't produce an executable, so none of your changes are included in the EXE you find on disk and can execute!
Remove the keyword abstract and see if it compiles cleanly - until it does, you can't run it!


这篇关于帮我修复代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆