Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java 1. Add a copy constructor to the Bank Account class. This constructor shoul

ID: 3862967 • Letter: J

Question

java

1. Add a copy constructor to the Bank Account class. This constructor should accept a bank account object as an argument. it should assign to the balance field the value in the argument's balance field. As a result ,the new object will be a copy of the argument object.Use 6% for state tax and 1% for county tax - sample data: 5000 in sales results in 300 in state tax and 50 in county tax.

2. 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 the 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*10*8=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 length multiply by the room's width).

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.

Once you have written this 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.

Explanation / Answer

BankAccount.java

public class BankAccount {
   //Declaring variables
   private double balance;

   //Parameterized constructor
   public BankAccount(double balance) {
       super();
       this.balance = balance;
   }

   //Copy constructor
   public BankAccount(BankAccount ba) {

       this.balance = ba.balance;
   }

   //getter method and setter method
   public double getBalance() {
       return balance;
   }

   public void setBalance(double balance) {
       this.balance = balance;
   }

}

___________________________

Test.java

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       double bal;
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Getting the balance entered by the user
       System.out.print("Enter the balance :");
       bal=sc.nextDouble();
      
       //Creating the BankAccount class Object by passing the balance as argument
       BankAccount account1=new BankAccount(bal);
      
       //Displaying the balance in the account 1
       System.out.println("Balance in account 1:"+account1.getBalance());

       //Creating the BankAccount class Object by passing the Account 1 as argument
       BankAccount account2=new BankAccount(account1);
      
       //Displaying the balance in the account 2
       System.out.println("Balance in account 2:"+account2.getBalance());

   }

}

___________________________

Output:

Enter the balance :5000
Balance in account 1:5000.0
Balance in account 2:5000.0

______________________________

2)

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;
   }

   // getters
   public double getLength() {
       return length;
   }

   public double getWidth() {
       return width;
   }

   // Calculate the area of the room
   public double areaOfRoom() {
       return getLength() * getWidth();

   }

}

____________________

RoomCarpet.java

public class RoomCarpet {
   // Declaring instance variable
   private double cost_of_carpet;

   // Creating RoomDimension class type variable
   RoomDimension roomdimension;

   // parameterized constructor
   public RoomCarpet(double cost_of_carpet, RoomDimension roomdimension) {
       super();
       this.cost_of_carpet = cost_of_carpet;
       this.roomdimension = roomdimension;
   }

   // Getter method
   public double getCost_of_carpet() {
       return cost_of_carpet;
   }

   // Method which calculate the total cost of carpet
   public double totalCostOfCarpet() {
       return getCost_of_carpet() * roomdimension.areaOfRoom();
   }

}

_____________________

DriverClass.java

import java.util.Scanner;

public class DriverClass {

   public static void main(String[] args) {
       //Declaring variables
       double length,width,carpetCostPerFt,total_carpet_cost;

       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);

       //Getting the length of the carpet
       System.out.print("Enter the Length of the Carpet (in feet):");
       length=sc.nextDouble();
      
       //Getting the width of the carpet  
       System.out.print("Enter the Width of the Carpet (in feet):");
       width=sc.nextDouble();
      
       //Getting the cost of the carpet per feet
       System.out.print("Enter cost of Carpet (per sq ft) :$");
       carpetCostPerFt=sc.nextDouble();
      
       /* Creating the Room Dimension class object
       * by passing the length and width as arguments
       */
       RoomDimension dimension=new RoomDimension(length, width);
      
       //Creating the Room Carpet class object
       RoomCarpet roomcarpet=new RoomCarpet(carpetCostPerFt, dimension);
      
       //Calling the method which calculates the total cost of the carpet
       total_carpet_cost=roomcarpet.totalCostOfCarpet();
      
       //Displaying the total cost of the carpet
       System.out.println("Total of cost of carpet is :$"+total_carpet_cost);
      
      
   }

}

_______________________

output:

Enter the Length of the Carpet (in feet):12
Enter the Width of the Carpet (in feet):10
Enter cost of Carpet (per sq ft) :$8
Total of cost of carpet is :$960.0

_____________Thank YOu