调用班级的地毯计算器错误?

时间:2014-11-07 03:32:09

标签: java class object

作业如上所述:

问题所在 Westfield地毯公司要求您编写一个计算矩形房间地毯价格的应用程序。要计算价格,您需要将地板面积(宽度乘以长度)乘以每平方英尺地毯的价格。例如,12英尺长,10英尺宽的地板面积为120平方英尺。用地毯覆盖每平方英尺8美元的地板将花费960美元(12x10x8 = 960)

首先,你应该创建一个名为RoomDimension的类,它有两个Feield:一个用于房间的长度,一个用于宽度。 RoomDimension类应该有一个返回房间区域的方法(房间的面积是房间的长度乘以房间的宽度)。

接下来,您应该创建一个RoomDarpet类,该类具有RoomDimension对象作为字段。它还应该有每平方英尺地毯成本的字段。 RoomCarpet类应该有一个返回地毯总成本的方法。

编写完这些课程后,请在要求用户输入房间尺寸和所需地毯每平方英尺价格的应用程序中使用它们。应用程序应显示地毯的总成本。

由于MainProgram第31行出错,我下面的代码似乎无法运行

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class MainProgram {


    public static void main(String[] args) {

        final double CARPET_PRICE_PER_SQFT = 8.0;

        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        // Display intro.
        System.out.println("This program will display the "
                + "carpet cost of a room." + "\nPlease enter the room's "
                + "dimension in feet.");

        // Get the length of the room.
        System.out.print("Enter the length of room: ");
        double length = keyboard.nextDouble();

        // Get the width of the room.
        System.out.print("Enter the width of room: ");
        double width = keyboard.nextDouble();

        //close keyboard
        keyboard.close();

        ****// Create RoomDimension and RoomCarpet objects.
        CarpetCalculatorProgram calculatorProgram = new CarpetCalculatorProgram();
        RoomDimension dimensions = calculatorProgram.new RoomDimension(length,
                width);
        RoomCarpet roomCarpet = calculatorProgram.new RoomCarpet(dimensions,
                CARPET_PRICE_PER_SQFT);****

        // Print the object calling the toString
        System.out.println(roomCarpet);
    }
}

以下是代码的其他类:

房间维度

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class RoomDimension {

    private double length;
    private double width;

    public RoomDimension(double length, double width) {
        super();
        this.length = length;
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }

    public double getArea() {
        return length * width;
    }

    @Override
    public String toString() {
        return "RoomDimension [length=" + length + ", width=" + width + "]";
    }


}

Room Carpet

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class RoomCarpet {

      private RoomDimension roomDimensions;
        private double costOfCarpet;

        public RoomCarpet(RoomDimension roomDimensions, double costOfCarpet) {
            super();
            this.roomDimensions = roomDimensions;
            this.costOfCarpet = costOfCarpet;
        }

        public double getTotalCost() {
            return costOfCarpet * roomDimensions.getArea();
        }

        @Override
        public String toString() {
            return "RoomCarpet [roomDimensions=" + roomDimensions
                    + ", costOfCarpet=" + costOfCarpet + ", "
                            + "total cost=" + getTotalCost() + "]";
        }

}

2 个答案:

答案 0 :(得分:2)

将所有内容粘贴到IDE中时出现的错误是

  

CarpetCalculatorProgram无法解析为类型

假设没有课程你没有发布:

没有CarpetCalculatorProgram类,并且那里没有内部RoomDimension / RoomCarpet类。 RoomDimension实际上是一个独立的顶级课程。代码必须是

    // Create RoomDimension and RoomCarpet objects.
    RoomDimension dimensions = new RoomDimension(length,
            width);
    RoomCarpet roomCarpet = new RoomCarpet(dimensions,
            CARPET_PRICE_PER_SQFT);

而不是使用new EnclosingClass().new InnerClass()语法。 OR

    // Create RoomDimension and RoomCarpet objects.
    CarpetCalculatorProgram calculatorProgram = new CarpetCalculatorProgram();
    CarpetCalculatorProgram.RoomDimension dimensions = calculatorProgram.new RoomDimension(length,
            width);
    CarpetCalculatorProgram.RoomCarpet roomCarpet = calculatorProgram.new RoomCarpet(dimensions,
            CARPET_PRICE_PER_SQFT);

并将两个类移入CarpetCalculatorProgram类:

public class CarpetCalculatorProgram {
    public class RoomDimension {
        ...
    }

    public class RoomCarpet {
        ...
    }
}

答案 1 :(得分:-2)

我的代码是否正常工作,请查看它是否对您有帮助。

import java.util.Scanner;
class RoomDimension{
        private int length;
        private int width;

    public RoomDimension(int length, int width){
            this.length= length;
            this.width= width;
    }
    public int Area(){
        int area= this.length* this.width;
        return area;
    }
    public int getlength(){
        return this.length;
    }
    public int getwidth(){
        return width;
    }
    }

    class RoomCarpet{
        private RoomDimension RD;
        private int costperSq;

    public RoomCarpet(RoomDimension RD, int costperSq){
        this.RD= RD;
        this.costperSq= costperSq;
    }

    public int TotalCost(){
        return this.costperSq* RD.Area();
    }
}

[主要]

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

        System.out.println("enter the length the room");
        int L= sc.nextInt();

        System.out.println("enter the width the room");
        int W= sc.nextInt();

        RoomDimension RD= new RoomDimension(L, W);

        System.out.println("enter how much the carpet costs per sq");
        int cost= sc.nextInt();

        RoomCarpet RC= new RoomCarpet(RD, cost);

        System.out.println("total cost of the carpet will be= "+RC.TotalCost());
    }
}