MoBike with the following description:Instance variables / data members:
int bno: To store the bike's number.int phno: To store the phone number of the customer.String name: To store the name of the customer.int days: To store the number of days the bike is taken on rent.int charge: To calculate and store the rental charge.Member methods:
void input(): To input and store the details of the customer.void compute(): To compute the rental charge. The rent for a mobike is charged on the following basis:
Bike No. Phone No. Name No. of days Charge
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class MoBike {
// Instance variables
int bno; // Bike number
int phno; // Phone number of the customer
String name; // Name of the customer
int days; // Number of days the bike is taken on rent
int charge; // Rental charge
// Method to input and store the details of the customer
void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name:");
name = br.readLine();
System.out.println("Enter Bike number:");
bno = Integer.parseInt(br.readLine());
System.out.println("Enter Phone number:");
phno = Integer.parseInt(br.readLine());
System.out.println("Enter number of days:");
days = Integer.parseInt(br.readLine());
}
// Method to compute the rental charge based on the number of days
void compute() {
if (days <= 5) {
charge = days * 500; // First 5 days, 500 per day
} else if (days <= 10) {
charge = 5 * 500 + (days - 5) * 400; // Next 5 days, 400 per day
} else {
charge = 5 * 500 + 5 * 400 + (days - 10) * 200; // Rest, 200 per day
}
}
// Method to display the details in the specified format
void display() {
System.out.println("Bike No. \t Phone No. \t Name \t No. of days \t Charge");
System.out.println(bno + "\t\t" + phno + "\t\t" + name + "\t" + days + "\t\t" + charge);
}
}
public class Main {
public static void main(String[] args) throws IOException {
MoBike bike = new MoBike();
bike.input(); // Input customer details
bike.compute(); // Compute rental charge
bike.display(); // Display customer and bike details
}
}
Here is the table format for the variables of the MoBike class:
| Variable | Type | Description |
|---|---|---|
bno |
int |
Stores the bike's number. |
phno |
int |
Stores the phone number of the customer. |
name |
String |
Stores the name of the customer. |
days |
int |
Stores the number of days the bike is taken on rent. |
charge |
int |
Stores the calculated rental charge. |
Explanation:
Instance Variables:
bno: Stores the bike number.phno: Stores the customer's phone number.name: Stores the customer's name.days: Stores the number of days the bike is rented.charge: Stores the calculated rental charge.Methods:
input(): Takes input from the user for the name, bike number, phone number, and days of rental.compute(): Calculates the rental charge based on the number of days:
display(): Displays the details in the specified format.First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.