通过不同类中的方法传递演示类中的用户变量

时间:2018-10-31 15:40:03

标签: java eclipse

我遇到麻烦的方法是“ getTotalCost”方法。

每种方法如下:

DemoRoomCarpet

package cnmt.sec01.homeworkassign05;

import javax.swing.JOptionPane;

public class DemoRoomCarpet {
    private static double length;
    private static double width;
    private static double cost;
    public static void main(String[] args) {
        length = Double.valueOf(JOptionPane.showInputDialog("What is the length of your carpet?"));
        width = Double.valueOf(JOptionPane.showInputDialog("What is the width of your carpet?"));
        cost = Double.valueOf(JOptionPane.showInputDialog("What is the cost per square foot of carpet?"));

        RoomDimension myRoomDim = 
                new RoomDimension(length,width);

        RoomCarpet myRoomCarpet =
                new RoomCarpet(cost);

        myRoomDim.setLength(length);
        myRoomDim.setWidth(width);
        myRoomCarpet.setCarpetCost(cost);

        myRoomCarpet.getTotalCost();

        myRoomCarpet.toString();
    }
}

RoomDimension

package cnmt.sec01.homeworkassign05;

public class RoomDimension {

    private double length,
                   width;


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

       public RoomDimension(RoomDimension object)
       {
           length = object.length;
           width = object.width;
       }


    /**
     * @return the length
     */
    public double getLength() {
        return length;
    }

    /**
     * @param length the length to set
     */
    public void setLength(double length) {
        this.length = length;
    }

    /**
     * @return the width
     */
    public double getWidth() {
        return width;
    }

    /**
     * @param width the width to set
     */
    public void setWidth(double width) {
        this.width = width;
    }

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

    public String toString() {
        String str = "Length is: " + length + "\nWidth is: " + width + "\nArea is: " + this.getArea();
        return str;
    }
}

和RoomCarpet

package cnmt.sec01.homeworkassign05;

public class RoomCarpet {

    private RoomDimension dim;
    private double carpetCost;

    public RoomCarpet(double carpetCost)
    {
        this.carpetCost = carpetCost;
    }

       public RoomCarpet(RoomCarpet object)
       {
           carpetCost = object.carpetCost;
       }

    /**
     * @return the carpetCost
     */
    public double getCarpetCost() {
        return carpetCost;
    }

    /**
     * @param carpetCost the carpetCost to set
     */
    public void setCarpetCost(double carpetCost) {
        this.carpetCost = carpetCost;
    }

    public double getTotalCost()
    {
        double total = dim.getArea() * carpetCost;
        return total;
    }

    public String toString()
    {
        String str = "Your total cost is " + this.getTotalCost() + " dollars";
        return str;
    }

}

我觉得这个问题是我缺少的小问题,但是我可能是错的。抱歉,没有评论,但是一旦遇到此错误,我将添加评论。传递getTotalCost方法会给出一个空指针异常,但是我相信我是通过它传递了用户输入的。

1 个答案:

答案 0 :(得分:0)

那是因为RoomDimension dim尚未初始化。必须使用构造函数或通过setter初始化该字段。

在RoomCarpet类中:

public RoomCarpet(double carpetCost, RoomDimension dim)
{
    this.carpetCost = carpetCost;
    this.dim = dim;
}

主要:

    RoomCarpet myRoomCarpet =
            new RoomCarpet(cost, myRoomDim);