0

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());

}

}

  • If it passes your tests, what leads you to believe that your equals method is wrong? – Greg Hewgill Oct 23 '11 at 06:09
  • @Greg Hewgill I thought there might have been a better way to put it or the way I was setting it up was wrong. The onlything is, I dont know if she wants me to compare the rooms width compared to length, or compare two rooms dimensions. Also, if there should be two seperate objects to compare or what? – Alex Adamcik Oct 23 '11 at 06:26
  • @Greg HewgillI know she kind of dose not make sense, I don't understand what exactly to compare in my equal statement, the RoomDimensions ans the RoomCarpet data(whitch is the same, or the length and width? – Alex Adamcik Oct 23 '11 at 06:49

2 Answers2

0

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

@Override
public boolean equals(Object obj)
{

}
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • What dose overiding the hashCode mean? That wasn't in the chapter and IDK what it means. Thanks – Alex Adamcik Oct 23 '11 at 06:18
  • 1
    It means [this](http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java), but it may not be overly-helpful to get hung up on it yet. Just search for "override hashcode and equals" and have fun. – Dave Newton Oct 23 '11 at 06:24
0

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285