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:
| Name | Description |
|---|---|
| floor | Floor number where the flat is located |
| rate | Per square foot rate of the flat (in rupees) |
| area | Area of flat in square feet |
| frc | Floor rise charges of the flat (in rupees) |
| cost | Cost of the flat excluding floor rise charges |
| netCost | Total 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 4 | 0.5 |
| 5 - 9 | 2.0 |
| 10 - 14 | 2.5 |
| 15 and above | 3.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
3 Likes
Answer
RealEstateprivate double rate, area, frc, cost, netCost;floor = in.nextInt();(floor <= 4)else if (floor <= 9)else if (floor <= 14)frcPercentage = 3.0;frc = (frcPercentage / 100) * cost;System.out.println(floor + "\t" + frc + "\t" + cost + "\t" + netCost);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 Name | Data Type | Purpose |
|---|---|---|
| floor | int | The floor number where the flat is located, input by the user. |
| area | double | The area of the flat in square feet, input by the user. |
| rate | double | The rate per square foot in rupees, input by the user. |
| cost | double | The cost of the flat calculated as the product of area and rate. |
| frc | double | The floor rise charge calculated as a percentage of the flat cost. |
| netCost | double | The total cost of the flat, including the floor rise charge, calculated as the sum of cost and frc. |
Method: void accept()
| Variable Name | Data Type | Purpose |
|---|---|---|
| in | Scanner | Scanner object for taking input from the user through the keyboard. |
Method: void calculate()
| Variable Name | Data Type | Purpose |
|---|---|---|
| frcPercentage | double | The percentage value of floor rise charge based on the floor number. |
Method: void main(String args[])
| Variable Name | Data Type | Purpose |
|---|---|---|
| obj | RealEstate | An 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
RealEstateis 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), andnetCost(a double for the cost including floor raise cost).
2. Method accept():
- This method uses a
Scannerobject to accept input from the user through the keyboard. - The
floor,area, andrateare 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;, whereareais the flat's area in square feet andrateis 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%.
- Floors 4 or below: The FRC percentage is set to
Calculate Floor Rise Charge (frc):
- Using the determined
frcPercentage, the actual floor rise charge in rupees is computed with the formulafrc = (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, andnetCostin a tabular format.
5. main Method:
- This is the entry point of the program.
- An instance of the
RealEstateclass is created. - The
accept(),calculate(), anddisplay()methods are called in sequence to get the user input, perform the calculations, and display the results respectively.
Output

Answered By
1 Like