KnowledgeBoat Logo
|

Computer Applications

DLF, a real estate company, applies floor rise charges on its flats as a percentage of the flat's cost, depending on the floor on which the flat is located. Define a class with the following specifications to compute the total cost of a flat.

Class name: RealEstate

Member variables:

NameDescription
floorFloor number where the flat is located
ratePer square foot rate of the flat (in rupees)
areaArea of flat in square feet
frcFloor rise charges of the flat (in rupees)
costCost of the flat excluding floor rise charges
netCostTotal cost of the flat including floor rise charges

Member methods:

void accept () — This method takes user input for the floor number, flat area, and per square foot rate using the methods of the Scanner class only.

void calculate () — This method calculates total cost of the flat based on the following conditions:

Cost of flat = Flat area x Per square foot rate

Floor rise charge percentage (FRC %) is determined as per the table below:

Floor No.FRC Percentage
Upto 40.5
5 - 92.0
10 - 142.5
15 and above3.0

$\text{Floor rise charge} = \dfrac{\text{FRC Percentage}}{100} \times \text{Cost of flat}$

Total cost = (Cost of flat) + (Floor rise charge)

void display () — To display the details in the given format.

FLR FRC COST NETCOST
xx  xx   xx    xx

Write a main method to create an object of the RealEstate class, call the accept(), calculate(), and display() methods, and display the computed values.

import java.util.Scanner;

class _____(1)_____ {
    
    private int floor;
    _______(2)_________

    public void accept() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter floor number: ");
        _______(3)_________
        System.out.print("Enter flat area (in square feet): ");
        area = in.nextDouble();
        System.out.print("Enter per square foot rate (in rupees): ");
        rate = in.nextDouble();
    }

    public void calculate() {
        cost = area * rate;
        
        double frcPercentage;
        if ____(4)____ {
            frcPercentage = 0.5;
        } _______(5)_________ {
            frcPercentage = 2.0;
        } _______(6)_________ {
            frcPercentage = 2.5;
        } else {
            _______(7)_________
        }
        
        _______(8)_________
        netCost = cost + frc;
    }

    public void display() {
        System.out.println("FLR\tFRC\tCOST\tNETCOST");
        _______(9)_________
    }

    public static void main(String args[]) {
        _______(10)_________
        obj.accept();
        obj.calculate();
        obj.display();
    }
}

Java Classes

1 Like

Answer

  1. RealEstate
  2. private double rate, area, frc, cost, netCost;
  3. floor = in.nextInt();
  4. (floor <= 4)
  5. else if (floor <= 9)
  6. else if (floor <= 14)
  7. frcPercentage = 3.0;
  8. frc = (frcPercentage / 100) * cost;
  9. System.out.println(floor + "\t" + frc + "\t" + cost + "\t" + netCost);
  10. RealEstate obj = new RealEstate();

Explanation

import java.util.Scanner;

class RealEstate {

    private int floor;
    private double rate;
    private double area;
    private double frc;
    private double cost;
    private double netCost;

    public void accept() {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter floor number: ");
        floor = sc.nextInt();
        System.out.print("Enter flat area (in square feet): ");
        area = sc.nextDouble();
        System.out.print("Enter per square foot rate (in rupees): ");
        rate = sc.nextDouble();
    }

    public void calculate() {
        cost = area * rate;
        
        double frcPercentage;
        if (floor <= 4) {
            frcPercentage = 0.5;
        } else if (floor <= 9) {
            frcPercentage = 2.0;
        } else if (floor <= 14) {
            frcPercentage = 2.5;
        } else {
            frcPercentage = 3.0;
        }
        
        frc = (frcPercentage / 100) * cost;
        netCost = cost + frc;
    }

    public void display() {
        System.out.println("FLR\tFRC\tCOST\tNETCOST");
        System.out.println(floor + "\t" + frc + "\t" + cost + "\t" + netCost);
    }

    public static void main(String args[]) {
        RealEstate obj = new RealEstate();
        obj.accept();
        obj.calculate();
        obj.display();
    }
}

Variable Description Table

Data Members of RealEstate Class:

Variable NameData TypePurpose
floorintThe floor number where the flat is located, input by the user.
areadoubleThe area of the flat in square feet, input by the user.
ratedoubleThe rate per square foot in rupees, input by the user.
costdoubleThe cost of the flat calculated as the product of area and rate.
frcdoubleThe floor rise charge calculated as a percentage of the flat cost.
netCostdoubleThe total cost of the flat, including the floor rise charge, calculated as the sum of cost and frc.

Method: void accept()

Variable NameData TypePurpose
inScannerScanner object for taking input from the user through the keyboard.

Method: void calculate()

Variable NameData TypePurpose
frcPercentagedoubleThe percentage value of floor rise charge based on the floor number.

Method: void main(String args[])

Variable NameData TypePurpose
objRealEstateAn object of the RealEstate class used to call its methods.

Program Explanation

Let's go through the Java program step by step to understand how it works:

1. Class Definition:

  • The class RealEstate is defined with several private instance variables: floor (an integer representing the floor number), rate (a double representing the rate per square foot), area (a double representing the area of the flat), frc (a double for floor raise cost), cost (a double for the total cost), and netCost (a double for the cost including floor raise cost).

2. Method accept():

  • This method uses a Scanner object to accept input from the user through the keyboard.
  • The floor, area, and rate are read from the user input. They represent the floor number, the area of the flat in square feet, and the rate per square foot in rupees, respectively.

3. Method calculate():

The calculate() method in the RealEstate class is designed to compute the cost of a flat, including additional charges based on the floor where the flat is located.

Initialization of Cost:

  • The method begins by calculating the initial cost of the flat, excluding any floor rise charges.
  • This is achieved with the formula cost = area * rate;, where area is the flat's area in square feet and rate is the cost per square foot (both defined as instance variables in the class). This calculation provides the base price of the flat.

Determine Floor Rise Charge Percentage (frcPercentage):

  • The method includes a series of conditional statements (if-else if-else) to determine the floor rise charge percentage based on the floor number (floor).
  • The floor rise charges (FRC) percentage is determined using the following logic:
    • Floors 4 or below: The FRC percentage is set to 0.5%.
    • Floors 5 to 9: The FRC percentage is set to 2.0%.
    • Floors 10 to 14: The FRC percentage is set to 2.5%.
    • Floors 15 and above: The FRC percentage is set to 3.0%.

Calculate Floor Rise Charge (frc):

  • Using the determined frcPercentage, the actual floor rise charge in rupees is computed with the formula frc = (frcPercentage / 100) * cost;.
  • This formula converts the percentage into a decimal and multiplies it by the base cost of the flat to obtain the total floor rise charge.

Calculate Total Cost (netCost):

  • Finally, the total cost of the flat (netCost) is computed by adding the base cost and floor rise charges: netCost = cost + frc;.

The calculate() method, therefore, systematically applies a percentage-based additional charge to the base cost of the flat depending on the floor level, calculating the total cost of the flat.

4. Method display():

  • This method prints a table header and the calculated values for floor, frc, cost, and netCost in a tabular format.

5. main Method:

  • This is the entry point of the program.
  • An instance of the RealEstate class is created.
  • The accept(), calculate(), and display() methods are called in sequence to get the user input, perform the calculations, and display the results respectively.

Output

DLF, a real estate company, applies floor rise charges on its flats as a percentage of the flat's cost, depending on the floor on which the flat is located. Define a class with the following specifications to compute the total cost of a flat. Practice Test ICSE Computer Applications Class 10

Answered By

3 Likes