class as basis of object computation
class as basis of object computation
A private Cab service company provides service within the city at the following rates:
AC CAR NON AC CAR
Upto 5 KM ₹150/- ₹120/-
Beyond 5 KM ₹10/- PER KM ₹08/- PER KM
Design a class CabService with the following description:
Member variables /data members:
String car_type — To store the type of car (AC or NON AC)
double km — To store the kilometer travelled
double bill — To calculate and store the bill amount
Member methods:
CabService() — Default constructor to initialize data members. String data members to '' ''
and double data members to 0.0.
void accept() — To accept car_type and km (using Scanner class only).
void calculate() — To calculate the bill as per the rules given above.
void display() — To display the bill as per the following format:
CAR TYPE:
KILOMETER TRAVELLED:
TOTAL BILL:
Create an object of the class in the main method and invoke the member methods.
Answer
import java.util.Scanner;
public CabService() {
car_type = "";
km = 0.0;
bill = 0.0;
}
Question 4
Define a class named Pay with the following description:
Instance variables /Data members:
String name — To store the name of the employee
double salary — To store the salary of the employee
double da — To store Dearness Allowance
double hra — To store House Rent Allowance
double pf — To store Provident Fund
double grossSal — To store Gross Salary
double netSal — To store Net Salary
Member methods:
Pay(String n, double s) — Parameterized constructor to initialize the data members.
void calculate() — To calculate the following salary components:
1. Dearness Allowance = 15% of salary
2. House Rent Allowance = 10% of salary
3. Provident Fund = 12% of salary
4. Gross Salary = Salary + Dearness Allowance + House Rent Allowance
5. Net Salary = Gross Salary - Provident Fund
void display() — To display the employee name, salary and all salary components.
Write a main method to create object of the class and call the member methods.
Answer
import java.util.Scanner;
void calculate() {
da = salary * 15.0 / 100;
hra = salary * 10.0 / 100;
pf = salary * 12.0 / 100;
grossSal = salary + da + hra;
netSal = grossSal - pf;
}
void display() {
System.out.println("Employee Name: " + name);
System.out.println("Salary: " + salary);
System.out.println("Dearness Allowance: " + da);
System.out.println("House Rent Allowance: " + hra);
System.out.println("Provident Fund: " + pf);
System.out.println("Gross Salary: " + grossSal);
System.out.println("Net Salary: " + netSal);
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Employee Name: ");
String empName = in.nextLine();
System.out.print("Enter Salary: ");
double empSal = in.nextDouble();
Question 4
Design a class name ShowRoom with the following description:
Instance variables / Data members:
String name — To store the name of the customer
long mobno — To store the mobile number of the customer
double cost — To store the cost of the items purchased
double dis — To store the discount amount
double amount — To store the amount to be paid after discount
Member methods:
ShowRoom() — default constructor to initialize data members
void input() — To input customer name, mobile number, cost
void calculate() — To calculate discount on the cost of purchased items, based on following
criteria
Cost Discount (in percentage)
Less than or equal to ₹10000 5%
More than ₹10000 and less than or equal to ₹20000 10%
More than ₹20000 and less than or equal to ₹35000 15%
More than ₹35000 20%
void display() — To display customer name, mobile number, amount to be paid after discount.
Write a main method to create an object of the class and call the above member methods.
Answer
import java.util.Scanner;
public ShowRoom()
{
name = "";
mobno = 0;
cost = 0.0;
dis = 0.0;
amount = 0.0;
}
Question 4
Design a class RailwayTicket with following description:
Instance variables/data members:
String name — To store the name of the customer
String coach — To store the type of coach customer wants to travel
long mobno — To store customer’s mobile number
int amt — To store basic amount of ticket
int totalamt — To store the amount to be paid after updating the original amount
Member methods:
void accept() — To take input for name, coach, mobile number and amount.
void update() — To update the amount as per the coach selected (extra amount to be added in the
amount as follows)
Type of Coaches Amount
First_AC 700
Second_AC 500
Third_AC 250
sleeper None
void display() — To display all details of a customer such as name, coach, total amount and
mobile number.
Write a main method to create an object of the class and call the above member methods.
Answer
import java.util.Scanner;
Question 4
Define a class named BookFair with the following description:
Instance variables/Data members:
String Bname — stores the name of the book
double price — stores the price of the book
Member methods:
(i) BookFair() — Default constructor to initialize data members
(ii) void Input() — To input and store the name and the price of the book.
(iii) void calculate() — To calculate the price after discount. Discount is calculated based on the
following criteria.
Price Discount
Less than or equal to ₹1000 2% of price
More than ₹1000 and less than or equal to ₹3000 10% of price
More than ₹3000 15% of price
(iv) void display() — To display the name and price of the book after discount.
Write a main method to create an object of the class and call the above member methods.
Answer
import java.util.Scanner;
public BookFair() {
bname = "";
price = 0.0;
}
price -= disc;
}
Question 4
Define a class called ParkingLot with the following description:
Instance variables/data members:
int vno — To store the vehicle number.
int hours — To store the number of hours the vehicle is parked in the parking lot.
double bill — To store the bill amount.
Member Methods:
void input() — To input and store the vno and hours.
void calculate() — To compute the parking charge at the rate of ₹3 for the first hour or part
thereof, and ₹1.50 for each additional hour or part thereof.
void display() — To display the detail.
Write a main() method to create an object of the class and call the above methods.
Answer
import java.util.Scanner;
Question 4
Define a class named movieMagic with the following description:
Data Members Purpose
int year To store the year of release of a movie
String title To store the title of the movie
To store the popularity rating of the movie
float rating
(minimum rating=0.0 and maximum rating=5.0)
Member
Purpose
Methods
Default constructor to initialize numeric data members to 0 and String data
movieMagic()
member to "".
void accept() To input and store year, title and rating
To display the title of the movie and a message based on the rating as per
void display()
the table given below
Ratings Table
Rating Message to be displayed
0.0 to 2.0 Flop
2.1 to 3.4 Semi-Hit
3.5 to 4.4 Hit
4.5 to 5.0 Super-Hit
Write a main method to create an object of the class and call the above member methods.
Answer
import java.util.Scanner;
public movieMagic() {
year = 0;
title = "";
rating = 0.0f;
}
System.out.println(title);
System.out.println(message);
}
public FruitJuice() {
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}
2013
Question 4
Define a class called Library with the following description:
Instance Variables/Data Members:
int accNum — stores the accession number of the book.
String title — stores the title of the book.
String author — stores the name of the author.
Member methods:
1. void input() — To input and store the accession number, title and author.
2. void compute() — To accept the number of days late, calculate and display the fine
charged at the rate of Rs. 2 per day.
3. void display() — To display the details in the following format:
Accession Number Title Author
Write a main method to create an object of the class and call the above member methods.
Answer
import java.util.Scanner;
void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter book title: ");
title = in.nextLine();
System.out.print("Enter author: ");
author = in.nextLine();
System.out.print("Enter accession number: ");
accNum = in.nextInt();
}
void compute() {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of days late: ");
int days = in.nextInt();
int fine = days * 2;
System.out.println("Fine = Rs." + fine);
}
void display() {
System.out.println("Accession Number\tTitle\tAuthor");
System.out.println(accNum + "\t\t" + title + "\t" +
author);
}
Question 4
Define a class called 'Mobike' with the following specifications:
Data
Purpose
Members
int bno To store the bike 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
Purpose
Methods
void input() To input and store the details of the customer
void compute() To compute the rental charge
void display() To display the details in the given format
The rent for a mobike is charged on the following basis:
Days Charge
For first five days ₹500 per day
For next five days ₹400 per day
Days Charge
Rest of the days ₹200 per day
Output:
Bike No. Phone No. Name No. of days Charge
xxxxxxx xxxxxxxx xxxx xxx xxxxxx
Answer
import java.util.Scanner;
Member variables:
String name: name of the item purchased
double price: Price of the item purchased
Member methods:
void accept(): Accept the name and the price of the item using the methods of Scanner class.
void calculate(): To calculate the net amount to be paid by a customer, based on the following
criteria:
Discoun
Price
t
void display(): To display the name of the item and the net amount to be paid.
Write the main method to create an object and call the above methods.
import java.util.Scanner;
public class Eshop
{
private String name;
private double price;
private double disc;
private double amount;
Member variables:
name — name of student
age — age of student
mks — marks obtained
stream — stream allocated
Member methods:
void accept() — Accept name, age and marks using methods of Scanner class.
void allocation() — Allocate the stream as per following criteria:
mks stream
Commerce and
>= 200 and < 300
Computer
void print() – Display student name, age, mks and stream allocated.
import java.util.Scanner;
2023