Define a class called MoBike
with the following description:
Java Programming Language (Article) (Program)
31
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:- First five days: ₹500 per day.
- Next five days: ₹400 per day.
- Rest of the days: ₹200 per day.
Bike No. Phone No. Name No. of days Charge
Program:
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 } }
Output:
Explanation:
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:- First 5 days are charged at ?500 per day.
- Next 5 days are charged at ?400 per day.
- Any additional days are charged at ?200 per day.
display()
: Displays the details in the specified format.
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.