3 Carpet Calculator The Westfield Carpet Company has asked y

3. Carpet Calculator
The Westfield Carpet Company has asked you to write an application that calculates the
price of carpeting for rectangular rooms. To calculate the price, you multiply the area of the
floor (width times length) by the price per square foot of carpet. For example, the area of
floor that is 12 feet long and 10 feet wide is 120 square feet. To cover that floor with carpet
that costs $8 per square foot would cost $960. (12 3 10 3 8 5 960.)
First, you should create a class named RoomDimension that has two fields: one for the length
of the room and one for the width. The RoomDimension class should have a method that
returns the area of the room. (The area of the room is the room’s length multiplied by the
room’s width.)
Next you should create a RoomCarpet class that has a RoomDimension object as a field. It
should also have a field for the cost of the carpet per square foot. The RoomCarpet class
should have a method that returns the total cost of the carpet.
Figure 8-21 is a UML diagram that shows possible class designs and the relationships
among the classes. Once you have written these classes, use them in an application that
asks the user to enter the dimensions of a room and the price per square foot of the desired
carpeting. The application should display the total cost of the carpet.
4. LandTract Class
Make a LandTract class that has two fields: one for the tract’s length and one for the width.
The class should have a method that returns the tract’s area, as well as an equals method
and a toString method. Demonstrate the class in a program that asks the user to enter the
dimensions for two tracts of land. The program should display the area of each tract of land
and indicate whether the tracts are of equal size.

UML diagram for Programming Challenge 3 RoomCarpet size : RoomDimension carpetCost : double + RoomCarpet(dim RoomDimension, cost double): + getTotalCost): double + toString) : String RoomDimension length : double width double + RoomDimension(len : double, w double): + getArea) : double +toString) String

Solution

3)

RoomDimension.java

public class RoomDimension {
  
//Declaring instance variables  
private double length;
private double width;

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

//Setters and getters
public double getLength() {
   return length;
}
public void setLength(double length) {
   this.length = length;
}
public double getWidth() {
   return width;
}
public void setWidth(double width) {
   this.width = width;
}

//This method will calculate the area of the carpet and return it.
public double getArea()
{
return getLength()*getWidth();  
}

//toString() method is used to display the contents of an Object inside it.
@Override
public String toString() {
   return \"RoomDimension#\ Length=\" + length + \"\ Width=\" + width;
}

}

_____________________________

RoomCarpet.java

public class RoomCarpet {
//Declaring instance variables
private RoomDimension size;
private double carpetCost;

//Parameterized constructor
public RoomCarpet(RoomDimension size, double carpetCost) {
   super();
   this.size = size;
   this.carpetCost = carpetCost;
}

//Setters and getters
public RoomDimension getSize() {
   return size;
}
public void setSize(RoomDimension size) {
   this.size = size;
}
public double getCarpetCost() {
   return carpetCost;
}
public void setCarpetCost(double carpetCost) {
   this.carpetCost = carpetCost;
}

//This method will calculate and return the total cost of the carpet
public double getTotalCost()
{
   return size.getArea()*getCarpetCost();
}

//toString() method is used to display the contents of an Object inside it.
@Override
public String toString() {
   return \"\ RoomCarpet#\ \"+size + \"\ CarpetCost=\" + carpetCost+\"\ Area of The Carpet :$\"+getTotalCost();
}

}

___________________________________

TestClass.java

import java.util.Scanner;

public class TestClass {

   public static void main(String[] args) {
      
       //Declaring variables
       double length,width,carpetCost;
      
       //Scanner class object is sued to read the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Getting the length of the carpet entered by the user
       System.out.print(\"Enter the Carpet Length (in ft):\");
       length=sc.nextDouble();
      
       //Getting width of the carpet entered by the user
       System.out.print(\"Enter the Carpet Width (in ft):\");
       width=sc.nextDouble();
      
       //getting the cost of the carpet entered by the user
       System.out.print(\"Enter the Cost of the Carpet (per sqft):$\");
       carpetCost=sc.nextDouble();
      
       /* creating the object for the RoomDimesion class
       * by passing the length and width as arguments
       */
       RoomDimension size=new RoomDimension(length, width);
      
       /* creating the object for the RoomCarpet class by passing
       * the RoomDimension and carpet cost as arguments
       */
       RoomCarpet rc=new RoomCarpet(size, carpetCost);
      
       //Displaying the content of the RoomCarpet Class Object
       System.out.println(rc.toString());
   }

}

__________________________________

Output:

Enter the Carpet Length (in ft):12
Enter the Carpet Width (in ft):10
Enter the Cost of the Carpet (per sqft):$8

RoomCarpet#
RoomDimension#
Length=12.0
Width=10.0
CarpetCost=8.0
Area of The Carpet :$960.0

_______________________________

4)

LandTract.java

public class LandTract {
   //Declaring instance variables
private double length;
private double width;

//parameterized constructor
public LandTract(double length, double width) {
   super();
   this.length = length;
   this.width = width;
}

//Getters and setters
public double getLength() {
   return length;
}
public void setLength(double length) {
   this.length = length;
}
public double getWidth() {
   return width;
}
public void setWidth(double width) {
   this.width = width;
}

//This method will calculate the area and return it
public double getArea()
{
   return getLength()*getWidth();
}

/* This method will compare whether the
* two LandTract Objects are equal or not
*/
@Override
public boolean equals(Object obj) {
   if (this == obj)
       return true;
   if (obj == null)
       return false;
   if (getClass() != obj.getClass())
       return false;
   LandTract other = (LandTract) obj;
   if (Double.doubleToLongBits(length) != Double
           .doubleToLongBits(other.length))
       return false;
   if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width))
       return false;
   return true;
}

//toString() method is used to display the contents of an Object inside it.
@Override
public String toString() {
   return \"\ LandTract#\ Length=\" + length + \"\ Width=\" + width+\"\ Area =\"+getArea();
}

}

___________________________________

TestClass.java

import java.util.Scanner;

public class TestClass {

   public static void main(String[] args) {
      
       //Declaring variables
       double length,width,length2,width2;
       boolean bool;
      
       //Scanner class object is sued to read the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Getting the length of the carpet entered by the user
       System.out.print(\"Enter Length :\");
       length=sc.nextDouble();
      
       //Getting width of the carpet entered by the user
       System.out.print(\"Enter Width :\");
       width=sc.nextDouble();
      
       System.out.println(\"_____Creating the LandTract#1 Object_____\");
      
       //Creating the LandTract Object by passing the length and width as arguments
       LandTract lt1=new LandTract(length, width);
      
       //Displaying the contents of the LandTract Object
       System.out.println(lt1.toString());
      
       //Getting the length of the carpet entered by the user
       System.out.print(\"\ Enter Length :\");
       length2=sc.nextDouble();
      
       //Getting width of the carpet entered by the user
       System.out.print(\"Enter Width :\");
       width2=sc.nextDouble();
      
       System.out.println(\"_____Creating the LandTract#2 Object_____\");
       //Creating the LandTract Object by passing the length and width as arguments
       LandTract lt2=new LandTract(length2, width2);
      
       //Displaying the contents of the LandTract Object
       System.out.println(lt2.toString());  
      
       System.out.println(\"___Comparing the LandTract#1 Object and LandTract#2 Object____\");
       bool=lt1.equals(lt2);
       if(bool)
       System.out.println(\":: LandTract#1 and LandTract#2 are equal ::\");
       else
       System.out.println(\":: LandTract#1 and LandTract#2 are not equal ::\");  

   }

}

____________________________________

Output:

Enter Length :7
Enter Width :8
_____Creating the LandTract#1 Object_____

LandTract#
Length=7.0
Width=8.0
Area =56.0

Enter Length :7
Enter Width :8
_____Creating the LandTract#2 Object_____

LandTract#
Length=7.0
Width=8.0
Area =56.0
___Comparing the LandTract#1 Object and LandTract#2 Object____
:: LandTract#1 and LandTract#2 are equal ::

___________________Thank You

3. Carpet Calculator The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calcula
3. Carpet Calculator The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calcula
3. Carpet Calculator The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calcula
3. Carpet Calculator The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calcula
3. Carpet Calculator The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calcula
3. Carpet Calculator The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calcula

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site