Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

class as basis of object computation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

class as basis of object computation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Question 4

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 class CabService


{
String car_type;
double km;
double bill;

public CabService() {
car_type = "";
km = 0.0;
bill = 0.0;
}

public void accept() {


Scanner in = new Scanner(System.in);
System.out.print("Enter car type: ");
car_type = in.nextLine();
System.out.print("Enter kilometer: ");
km = in.nextDouble();
}

public void calculate() {


if (km <= 5) {
if (car_type.equals("AC"))
bill = 150;
else
bill = 120;
}
else {
if (car_type.equals("AC"))
bill = 150 + 10 * (km - 5);
else
bill = 120 + 8 * (km - 5);
}
}

public void display() {


System.out.println("Car Type: " + car_type);
System.out.println("Kilometer Travelled: " + km);
System.out.println("Total Bill: " + bill);
}

public static void main(String args[]) {


CabService obj = new CabService();
obj.accept();
obj.calculate();
obj.display();
}
}
2021

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;

public class Pay


{
String name;
double salary;
double da;
double hra;
double pf;
double grossSal;
double netSal;

public Pay(String n, double s) {


name = n;
salary = s;
da = 0;
hra = 0;
pf = 0;
grossSal = 0;
netSal = 0;
}

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();

Pay obj = new Pay(empName, empSal);


obj.calculate();
obj.display();
}
}
2021b

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 class ShowRoom


{
private String name;
private long mobno;
private double cost;
private double dis;
private double amount;

public ShowRoom()
{
name = "";
mobno = 0;
cost = 0.0;
dis = 0.0;
amount = 0.0;
}

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter customer name: ");
name = in.nextLine();
System.out.print("Enter customer mobile no: ");
mobno = in.nextLong();
System.out.print("Enter cost: ");
cost = in.nextDouble();
}

public void calculate() {


int disPercent = 0;
if (cost <= 10000)
disPercent = 5;
else if (cost <= 20000)
disPercent = 10;
else if (cost <= 35000)
disPercent = 15;
else
disPercent = 20;

dis = cost * disPercent / 100.0;


amount = cost - dis;
}

public void display() {


System.out.println("Customer Name: " + name);
System.out.println("Mobile Number: " + mobno);
System.out.println("Amout after discount: " + amount);
}

public static void main(String args[]) {


ShowRoom obj = new ShowRoom();
obj.input();
obj.calculate();
obj.display();
}
}
2019

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;

public class RailwayTicket


{
private String name;
private String coach;
private long mobno;
private int amt;
private int totalamt;

private void accept() {


Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
name = in.nextLine();
System.out.print("Enter coach: ");
coach = in.nextLine();
System.out.print("Enter mobile no: ");
mobno = in.nextLong();
System.out.print("Enter amount: ");
amt = in.nextInt();
}

private void update() {


if(coach.equalsIgnoreCase("First_AC"))
totalamt = amt + 700;
else if(coach.equalsIgnoreCase("Second_AC"))
totalamt = amt + 500;
else if(coach.equalsIgnoreCase("Third_AC"))
totalamt = amt + 250;
else if(coach.equalsIgnoreCase("Sleeper"))
totalamt = amt;
}

private void display() {


System.out.println("Name: " + name);
System.out.println("Coach: " + coach);
System.out.println("Total Amount: " + totalamt);
System.out.println("Mobile number: " + mobno);
}

public static void main(String args[]) {


RailwayTicket obj = new RailwayTicket();
obj.accept();
obj.update();
obj.display();
}
}
2018

Define a class ElectricBill with the following specifications:


class : ElectricBill
Instance variables / data member:
String n — to store the name of the customer
int units — to store the number of units consumed
double bill — to store the amount to be paid
Member methods:
void accept( ) — to accept the name of the customer and number of units consumed
void calculate( ) — to calculate the bill as per the following tariff:
Number of
Rate per unit
units
First 100 units Rs.2.00
Number of
Rate per unit
units
Next 200 units Rs.3.00
Above 300 units Rs.5.00
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
void print( ) — To print the details as follows:
Name of the customer: ………………………
Number of units consumed: ………………………
Bill amount: ………………………
Write a main method to create an object of the class and call the above member methods.
Answer
import java.util.Scanner;

public class ElectricBill


{
private String n;
private int units;
private double bill;

public void accept() {


Scanner in = new Scanner(System.in);
System.out.print("Enter customer name: ");
n = in.nextLine();
System.out.print("Enter units consumed: ");
units = in.nextInt();
}

public void calculate() {


if (units <= 100)
bill = units * 2;
else if (units <= 300)
bill = 200 + (units - 100) * 3;
else {
double amt = 200 + 600 + (units - 300) * 5;
double surcharge = (amt * 2.5) / 100.0;
bill = amt + surcharge;
}
}

public void print() {


System.out.println("Name of the customer\t\t: " + n);
System.out.println("Number of units consumed\t: " +
units);
System.out.println("Bill amount\t\t\t: " + bill);
}

public static void main(String args[]) {


ElectricBill obj = new ElectricBill();
obj.accept();
obj.calculate();
obj.print();
}
}
2017

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 class BookFair


{
private String bname;
private double price;

public BookFair() {
bname = "";
price = 0.0;
}

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter name of the book: ");
bname = in.nextLine();
System.out.print("Enter price of the book: ");
price = in.nextDouble();
}

public void calculate() {


double disc;
if (price <= 1000)
disc = price * 0.02;
else if (price <= 3000)
disc = price * 0.1;
else
disc = price * 0.15;

price -= disc;
}

public void display() {


System.out.println("Book Name: " + bname);
System.out.println("Price after discount: " + price);
}

public static void main(String args[]) {


BookFair obj = new BookFair();
obj.input();
obj.calculate();
obj.display();
}
}
2016

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;

public class ParkingLot


{
private int vno;
private int hours;
private double bill;

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter vehicle number: ");
vno = in.nextInt();
System.out.print("Enter hours: ");
hours = in.nextInt();
}

public void calculate() {


if (hours <= 1)
bill = 3;
else
bill = 3 + (hours - 1) * 1.5;
}

public void display() {


System.out.println("Vehicle number: " + vno);
System.out.println("Hours: " + hours);
System.out.println("Bill: " + bill);
}

public static void main(String args[]) {


ParkingLot obj = new ParkingLot();
obj.input();
obj.calculate();
obj.display();
}
}
2015

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 class movieMagic


{
private int year;
private String title;
private float rating;

public movieMagic() {
year = 0;
title = "";
rating = 0.0f;
}

public void accept() {


Scanner in = new Scanner(System.in);
System.out.print("Enter Title of Movie: ");
title = in.nextLine();
System.out.print("Enter Year of Movie: ");
year = in.nextInt();
System.out.print("Enter Rating of Movie: ");
rating = in.nextFloat();
}

public void display() {


String message = "Invalid Rating";
if (rating <= 2.0f)
message = "Flop";
else if (rating <= 3.4f)
message = "Semi-Hit";
else if (rating <= 4.4f)
message = "Hit";
else if (rating <= 5.0f)
message = "Super-Hit";

System.out.println(title);
System.out.println(message);
}

public static void main(String args[]) {


movieMagic obj = new movieMagic();
obj.accept();
obj.display();
}
}
2014

Define a class named FruitJuice with the following description:


Data Members Purpose
int product_code stores the product code number
String flavour stores the flavour of the juice (e.g., orange, apple, etc.)
String pack_type stores the type of packaging (e.g., tera-pack, PET bottle, etc.)
int pack_size stores package size (e.g., 200 mL, 400 mL, etc.)
int product_price stores the price of the product
Member
Purpose
Methods
constructor to initialize integer data members to 0 and string data members
FruitJuice()
to ""
to input and store the product code, flavour, pack type, pack size and
void input()
product price
void discount() to reduce the product price by 10
void display() to display the product code, flavour, pack type, pack size and product price
Answer
import java.util.Scanner;
public class FruitJuice
{
private int product_code;
private String flavour;
private String pack_type;
private int pack_size;
private int product_price;

public FruitJuice() {
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter Flavour: ");
flavour = in.nextLine();
System.out.print("Enter Pack Type: ");
pack_type = in.nextLine();
System.out.print("Enter Product Code: ");
product_code = in.nextInt();
System.out.print("Enter Pack Size: ");
pack_size = in.nextInt();
System.out.print("Enter Product Price: ");
product_price = in.nextInt();
}

public void discount() {


product_price -= 10;
}

public void display() {


System.out.println("Product Code: " + product_code);
System.out.println("Flavour: " + flavour);
System.out.println("Pack Type: " + pack_type);
System.out.println("Pack Size: " + pack_size);
System.out.println("Product Price: " + product_price);
}

public static void main(String args[]) {


FruitJuice obj = new FruitJuice();
obj.input();
obj.discount();
obj.display();
}
}

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;

public class Library


{
private int accNum;
private String title;
private String author;

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);
}

public static void main(String args[]) {


Library obj = new Library();
obj.input();
obj.display();
obj.compute();
}
}
2012

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;

public class Mobike


{
private int bno;
private int phno;
private int days;
private int charge;
private String name;

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter Customer Name: ");
name = in.nextLine();
System.out.print("Enter Customer Phone Number: ");
phno = in.nextInt();
System.out.print("Enter Bike Number: ");
bno = in.nextInt();
System.out.print("Enter Number of Days: ");
days = in.nextInt();
}

public void compute() {


if (days <= 5)
charge = days * 500;
else if (days <= 10)
charge = (5 * 500) + ((days - 5) * 400);
else
charge = (5 * 500) + (5 * 400) + ((days - 10) *
200);
}

public void display() {


System.out.println("Bike No.\tPhone No.\tName\tNo. of
days \tCharge");
System.out.println(bno + "\t" + phno + "\t" + name + "\
t" + days
+ "\t" + charge);
}
public static void main(String args[]) {
Mobike obj = new Mobike();
obj.input();
obj.compute();
obj.display();
}
}
Output
2011

Define a class called with the following specifications:

Class name: Eshop

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

1000 – 25000 5.0%

25001 – 57000 7.5 %

57001 – 100000 10.0%

More than 100000 15.0 %

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;

public void accept() {


Scanner in = new Scanner(System.in);
System.out.print("Enter item name: ");
name = in.nextLine();
System.out.print("Enter price of item: ");
price = in.nextDouble();
}

public void calculate() {


double d = 0.0;

if (price < 1000)


d = 0.0;
else if (price <= 25000)
d = 5.0;
else if (price <= 57000)
d = 7.5;
else if (price <= 100000)
d = 10.0;
else
d = 15.0;

disc = price * d / 100.0;


amount = price - disc;

public void display() {


System.out.println("Item Name: " + name);
System.out.println("Net Amount: " + amount);
}

public static void main(String args[]) {


Eshop obj = new Eshop();
obj.accept();
obj.calculate();
obj.display();
}
}
2024

Design a class with the following specifications:

Class name: Student

Member variables:
name — name of student
age — age of student
mks — marks obtained
stream — stream allocated

(Declare the variables using appropriate data types)

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

>= 300 Science and Computer

Commerce and
>= 200 and < 300
Computer

>= 75 and < 200 Arts and Animation

< 75 Try Again

void print() – Display student name, age, mks and stream allocated.

Call all the above methods in main method using an object.

import java.util.Scanner;

public class Student


{
private String name;
private int age;
private double mks;
private String stream;

public void accept()


{
Scanner in = new Scanner(System.in);
System.out.print("Enter student name: ");
name = in.nextLine();
System.out.print("Enter age: ");
age = in.nextInt();
System.out.print("Enter marks: ");
mks = in.nextDouble();
}

public void allocation()


{
if (mks < 75)
stream = "Try again";
else if (mks < 200)
stream = "Arts and Animation";
else if (mks < 300)
stream = "Commerce and Computer";
else
stream = "Science and Computer";
}

public void print()


{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + mks);
System.out.println("Stream allocated: " + stream);
}

public static void main(String args[]) {


Student obj = new Student();
obj.accept();
obj.allocation();
obj.print();
}
}

2023

You might also like