需要有人检查我的程序,我认为我的 equals 方法是错误的

发布于 2024-12-11 10:10:07 字数 4621 浏览 0 评论 0原文

我想上帝已经完成了这项任务,想知道是否有人可以检查一下,这样我就可以确保没有错误,似乎我在这些程序上努力工作,但总是做错事。我在网上学习这门课程,所以我很难与老师沟通。我认为我的 equals 方法可能是错误的,但是,它们在运行程序时似乎没有错误,并且程序已 100% 完成。请您花时间查看一下,非常感谢您的宝贵时间。

任务: 关于第一堂课 创建一个名为 RoomDimension 的类,该类具有两个字段:一个用于房间的长度,另一个用于宽度。 RoomDimension 类应该有两个构造函数:一个没有参数(默认),另一个有两个参数。该类应该具有所有适当的 get 和 set 方法,一个返回房间面积的方法,一个允许我们打印房间的长度、宽度和面积的 toString 方法,以及一个用于比较房间尺寸的 equals 方法。

关于第二堂课 创建另一个名为 RoomCarpet 的类,该类具有两个字段:一个是 RoomDimension 对象,另一个是保存每平方英尺地毯成本的字段。该类应该有两个构造函数:一个不带参数,另一个具有两个字段参数(RoomDimension 和 double)。该类应该为每个字段提供一个 get 和 set 方法,一个返回房间铺地毯总成本的方法,一个 toString 方法,该方法将打印所有房间信息(长度、宽度、面积)和每平方地毯的成本英尺和房间地毯的总费用。 (美元金额应显示两位小数。),以及比较房间尺寸和地毯成本的 equals 方法。

关于申请程序 编写一个包含一个 RoomDimension 对象和一个 RoomCarpet 对象的应用程序。该程序应允许用户输入房间的长度和宽度以及每平方英尺地毯的成本。该程序应该实例化这两个对象并使用简单的 System.out.println 语句来打印有关 RoomCarpet 对象的所有信息。

我的代码:

 import java.text.DecimalFormat;


  public class RoomCarpet { 

    private RoomDimension rmSize;
    private double pricePerSqFt;


    //default constructor
    public RoomCarpet()
    {
        this.rmSize = new RoomDimension();
        this.pricePerSqFt = 0.00;
    }
    //parameters constructor
    public RoomCarpet(RoomDimension rmSize, double pricePerSqFt)
    {
        this.rmSize = new RoomDimension(rmSize.getRmLength(),rmSize.getRmWidth());
        this.pricePerSqFt = pricePerSqFt;
    }
    //accessor methods
    public RoomDimension getRmSize()
    {
        return new RoomDimension(rmSize.getRmLength(),rmSize.getRmWidth());
    }
    public double getPricePerSqFt()
    {
        return this.pricePerSqFt;
    }
    // mutator methods  
    public void setRmSize(RoomDimension rmSize)
    {
        this.rmSize = new RoomDimension(rmSize.getRmLength(), rmSize.getRmWidth());
    }
    public void setPricePerSqFt(double pricePerSqFt)
    {

        this.pricePerSqFt = pricePerSqFt;
    }
    // Or price for the room to be carpeted
    public double rmTotalCost() 
    {
        return rmSize.getAreaRoom() * pricePerSqFt;     
    }
    //toString method
    public String toString()
     {
        DecimalFormat dollar = new DecimalFormat("$#,##0.00");
        String str = this.rmSize.toString() + " Price per sq. ft : " +dollar.format(pricePerSqFt) + " Price to carpet Room: " + dollar.format(rmTotalCost()) +   '\n';
        return str;
    }

    public boolean equals(RoomCarpet object2)
    {
        boolean  status;
        if ((this.equals(object2)==true)&&(this.pricePerSqFt==object2.pricePerSqFt))
        status = true;
        else 
        status = false;
        return status;
    }


 }


 public class RoomDimension {
    private int rmLength;
    private int rmWidth;

    //Default constructor
    public RoomDimension()
    {
        rmLength=0;
        rmLength=0;
    }
    // constructor with parameters
    public RoomDimension(int rmLength, int rmWidth)
    {
        this.rmLength=rmLength;
        this.rmWidth=rmWidth;
    }

    // accessor methods
    public int getRmLength()
    {
        return this.rmLength;
    }
    public int getRmWidth()
    {
        return this.rmWidth;
    }
    //mutator methods
    public void setRmLength(int rmLength)
    {
        this.rmLength=rmLength;
     }
    public void setRmWidth(int rmWidth)
    {
        this.rmWidth =rmWidth;
    }
    //area of the room
    public int getAreaRoom()
    {
        return this.rmLength * this.rmWidth;
    }

    //toString Method
      public String toString()
    {
        String str = "Room Length: " + this.rmLength + " Room Width: " + this.rmWidth + " Area of Room: " + this.getAreaRoom();
        return str;
    }

    public boolean equals(RoomDimension object2)
{
        boolean status;
        if (this.getAreaRoom() == object2.getAreaRoom())
            status = true;
        else
            status = false;
        return status;
    }



}  

import java.util.Scanner;


public class CarpetPrice {

    public static void main(String[] args)
    {
        RoomDimension rmSize;
        RoomCarpet  rmCarpet;

        Scanner keyboard = new Scanner(System.in);

        rmSize=new RoomDimension();
    System.out.println(" Please enter the length of the room: ");
    int rmLength= keyboard.nextInt();
    rmSize.setRmLength(rmLength);

    System.out.println("Please enter the rooms width: ");
    int rmWidth = keyboard.nextInt();
    rmSize.setRmWidth(rmWidth);


    System.out.println("Please enter the price per sq foot: ");
    double pricePerSqFt = keyboard.nextDouble();
    rmCarpet = new RoomCarpet(rmSize, pricePerSqFt);

    System.out.println("\n"+rmCarpet.toString());

}

}

I am done with this assignment think god, and was wondering if someone could please check it so I can make sure there are no errors, it seems like I work hard on these programs but always doing something wrong. I am doing this course online so I have a hard time communicating with the instructor. I think my equals to methods might be wrong but, they seem to have no error when running the program and the program is 100% done. Please take the time to look over it, and thank you so much for your time.

Assignment:
About the first class
Create a class named RoomDimension that has two fields: one for the length of the room and another for the width. The RoomDimension class should have two constructors: one with no parameters (a default) and one with two parameters. The class should have all of the appropriate get and set methods, a method that returns the area of the room, a toString method that will allow us to print the length, width and area of the room and an equals method to compare room dimensions.

About the second class
Create another class named RoomCarpet that has two fields: one is a RoomDimension object and the other is a field that holds the cost of carpet per square foot. The class should have two constructors: one with no parameters and one with the two field parameters (RoomDimension and double). The class should have a get and set method for each field, a method that returns the total cost of carpeting the room, a toString method that will print all of the room information (length, width, area) and cost of the carpet per square foot and the total cost to carpet the room. (Dollar amounts should be displayed with two decimal places.), and an equals method that compares room dimensions and carpet cost.

About the application program
Write an application program that contains one RoomDimension object and one RoomCarpet object. The program should allow the user to enter the length and width of the room and the cost of the carpet per square foot. The program should instantiate both objects and use a simple System.out.println statement to print all of the information about the RoomCarpet object.

MY code:

 import java.text.DecimalFormat;


  public class RoomCarpet { 

    private RoomDimension rmSize;
    private double pricePerSqFt;


    //default constructor
    public RoomCarpet()
    {
        this.rmSize = new RoomDimension();
        this.pricePerSqFt = 0.00;
    }
    //parameters constructor
    public RoomCarpet(RoomDimension rmSize, double pricePerSqFt)
    {
        this.rmSize = new RoomDimension(rmSize.getRmLength(),rmSize.getRmWidth());
        this.pricePerSqFt = pricePerSqFt;
    }
    //accessor methods
    public RoomDimension getRmSize()
    {
        return new RoomDimension(rmSize.getRmLength(),rmSize.getRmWidth());
    }
    public double getPricePerSqFt()
    {
        return this.pricePerSqFt;
    }
    // mutator methods  
    public void setRmSize(RoomDimension rmSize)
    {
        this.rmSize = new RoomDimension(rmSize.getRmLength(), rmSize.getRmWidth());
    }
    public void setPricePerSqFt(double pricePerSqFt)
    {

        this.pricePerSqFt = pricePerSqFt;
    }
    // Or price for the room to be carpeted
    public double rmTotalCost() 
    {
        return rmSize.getAreaRoom() * pricePerSqFt;     
    }
    //toString method
    public String toString()
     {
        DecimalFormat dollar = new DecimalFormat("$#,##0.00");
        String str = this.rmSize.toString() + " Price per sq. ft : " +dollar.format(pricePerSqFt) + " Price to carpet Room: " + dollar.format(rmTotalCost()) +   '\n';
        return str;
    }

    public boolean equals(RoomCarpet object2)
    {
        boolean  status;
        if ((this.equals(object2)==true)&&(this.pricePerSqFt==object2.pricePerSqFt))
        status = true;
        else 
        status = false;
        return status;
    }


 }


 public class RoomDimension {
    private int rmLength;
    private int rmWidth;

    //Default constructor
    public RoomDimension()
    {
        rmLength=0;
        rmLength=0;
    }
    // constructor with parameters
    public RoomDimension(int rmLength, int rmWidth)
    {
        this.rmLength=rmLength;
        this.rmWidth=rmWidth;
    }

    // accessor methods
    public int getRmLength()
    {
        return this.rmLength;
    }
    public int getRmWidth()
    {
        return this.rmWidth;
    }
    //mutator methods
    public void setRmLength(int rmLength)
    {
        this.rmLength=rmLength;
     }
    public void setRmWidth(int rmWidth)
    {
        this.rmWidth =rmWidth;
    }
    //area of the room
    public int getAreaRoom()
    {
        return this.rmLength * this.rmWidth;
    }

    //toString Method
      public String toString()
    {
        String str = "Room Length: " + this.rmLength + " Room Width: " + this.rmWidth + " Area of Room: " + this.getAreaRoom();
        return str;
    }

    public boolean equals(RoomDimension object2)
{
        boolean status;
        if (this.getAreaRoom() == object2.getAreaRoom())
            status = true;
        else
            status = false;
        return status;
    }



}  

import java.util.Scanner;


public class CarpetPrice {

    public static void main(String[] args)
    {
        RoomDimension rmSize;
        RoomCarpet  rmCarpet;

        Scanner keyboard = new Scanner(System.in);

        rmSize=new RoomDimension();
    System.out.println(" Please enter the length of the room: ");
    int rmLength= keyboard.nextInt();
    rmSize.setRmLength(rmLength);

    System.out.println("Please enter the rooms width: ");
    int rmWidth = keyboard.nextInt();
    rmSize.setRmWidth(rmWidth);


    System.out.println("Please enter the price per sq foot: ");
    double pricePerSqFt = keyboard.nextDouble();
    rmCarpet = new RoomCarpet(rmSize, pricePerSqFt);

    System.out.println("\n"+rmCarpet.toString());

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔戏命师 2024-12-18 10:10:07

equals 方法必须有一个 Object 参数,并且您还必须重写 hashCode 方法。

@Override
public boolean equals(Object obj)
{

}

The equals method must have an Object argument and you have to override the hashCode method too.

@Override
public boolean equals(Object obj)
{

}
玩套路吗 2024-12-18 10:10:07

我想说,对于 RoomDimension,仅当长度和宽度都匹配时,两个对象才相等。特别是如果您铺设地毯,即使总面积相同,4x5 的房间与 1x20 的走廊也会有很大不同。我猜,对于 RoomCarpet 对象,只有两个尺寸相等且价格相同时才相等。

另外,我会编写一些测试,因为您可能会对调用 RoomCarpet 的 .equals() 方法(如上所述)时发生的情况感到惊讶。

最后,请注意缩进,因为这对于代码的任何读者都很重要。

I would say that for RoomDimension, two objects are equal only if both length and width match. Especially if you're laying carpet, a 4x5 room would be much different from a 1x20 hallway, even though the total area is the same. For the RoomCarpet object, again equal only if both the dimensions are equal and the price is the same, I guess.

Also, I would write some tests because you may be surprised at what happens when you call RoomCarpet's .equals() method (as written above).

Finally, pay attention to your indenting because that's important for any reader of your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文