Method Programs
1. 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:
Price Discount
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.
import java.util.*;
public class Eshop
{
String name;
double price;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name ");
name=sc.nextLine();
System.out.println("Enter price");
price=sc.nextDouble();
}
void calculate()
{
if(price>=1000 && price <=25000)
price=price-(price*0.05);
else if(price >=25001 && price<=57000)
price=price-(price*0.075);
else if (price >=57001 && price<=100000)
price=price-(price*0.10);
else
price=price-(price*0.15);
}
void display()
{
1
System.out.println("name="+name);
System.out.println("price="+price);
}
public static void main(String args[])
{
Eshop obj = new Eshop();
obj.accept();
obj.calculate();
obj.display();
}
}
2. Define a class with the following specifications:
Class name : employee
Member Variables : eno – employee number
ename – name of the employee
age – age of the employee
basic – basic salary
Member methods:
void accept() - accept the details using scanner class
void calculate () – to calculate the net salary as per the given specifications
net = basic + hra+da-pf
hra = 16.5% of basic
da = 14.25% of basic
Pf = 12.0% of basic
If the age of the employee is above 55 he/she gets an additional allowance of Rs. 6000.
void print() – to print the details as per the following format
eno ename age basic net
void main() - to create an object of the class and invoke the methods
import java.util.*;
public class employee
{
int eno;
String ename;
int age;
double basic;
double net;
public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter name : ");
ename = sc.nextLine();
System.out.print("Enter employee no : ");
eno = sc.nextInt();
System.out.print("Enter age : ");
age = sc.nextInt();
2
System.out.print("Enter basic salary : ");
basic = sc.nextDouble();
}
public void calculate()
{
double hra =basic*(16.5/100.0);
double da = basic*(14.25/100.0);
double pf = basic*(12.0/100);
net = basic + hra + da - pf;
if(age > 55)
net=net+ 6000;
}
public void print()
{
System.out.println("eno"+"\t"+"ename"+"\t"+"age"+"\t"+"basic"+"\t"+"net");
System.out.println(eno + "\t" + ename + "\t" + age + "\t"+ basic + "\t" + net );
}
public static void main(String args[])
{
employee obj= new employee();
obj.accept();
obj.calculate();
obj.print();
}
}
3. Design a class Hotel with the following description:
Member variables:
String name – to store the name of the customer
long mno – to store the mobile number of the customer
double bill – to store the bill amount
double gst – to store GST amount
double st – to store the service tax
double tamt – to store the total amount to be paid by the customer
Member methods:
void accept() – to accept customer’s name, mobile number, bill.
void calculate() – to calculate GST, service tax and total amount to be paid by the customer.
gst = 18% on bill st = 12.5% on bill tamt = bill +gst +st
void display() – to display the customer’s name, mobile number and bill amount.
Write a main method to create an object and invoke the above member methods.
import java.util.*;
class Hotel {
String name;
long mno;
double bill;
double gst;
double st;
3
double tamt;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter customer name: ");
name = sc.nextLine();
System.out.print("Enter mobile number: ");
mno = sc.nextLong();
System.out.print("Enter bill amount: ");
bill = sc.nextDouble();
}
void calculate()
{
gst = 0.18 * bill;
st = 0.125 * bill;
tamt = bill + gst + st;
}
void display()
{
System.out.println("Customer Name: " + name);
System.out.println("Mobile Number: " + mno);
System.out.println("Total Bill: " + tamt);
}
public static void main(String[] args)
{
Hotel obj = new Hotel();
obj.accept();
obj.calculate();
obj.display();
}}
4. Define a class Electric Bill with the following specifications: [15]
class: ElectricBill
Instance Variable/ 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 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 units — Rate per unit
First 100 units — Rs.2.00
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.
4
import java.util.Scanner;
public class ElectricBill
{
String n;
int units;
double bill;
public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter customer name: ");
n = sc.nextLine();
System.out.print("Enter units consumed: ");
units = sc.nextInt();
}
public void calculate()
{
if (units <= 100)
bill = units * 2;
else if (units>100 && units<= 300)
bill = 100*2 + (units - 100) * 3;
else if(units >300)
{
bill = 100*2+ 200*3 + (units - 300) * 5;
bill = bill+ (bill * 2.5) / 100.0;
}
}
public void print()
{
System.out.println("Name of the customer: " + " \t”+"Number of units consumed: " + " \t”+"Bill amount: " );
System.out.println(n + " \t”+units" +”\t”+bill );
public static void main(String args[])
{
ElectricBill obj = new ElectricBill();
obj.accept();
obj.calculate();
obj.print();
}
}
5
Constructor
5. Define a class named BookFair with the following description: [15]
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 Rs 1000 2% of price
More than Rs 1000 and less than or equal to Rs 3000 10% of price
More than Rs 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.
import java.util.*;
class BookFair
{
String Bname;
double price;
// Default constructor
BookFair()
{
Bname = "";
price = 0.0;
}
// Input method
void Input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of the book: ");
Bname = sc.nextLine();
System.out.print("Enter the price of the book: ");
price = sc.nextDouble();
}
// Calculate method
void calculate()
{
if (price <= 1000)
price=price- (price*0.02);
else if (price <= 3000)
price=price- (price*0.10);
else
price = price-(price *0.15);
}
6
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();
}}
6. Define a class called mobike with the following description: [15]
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 detail of the customer.
void compute( ) – to compute the rental charge
The rent for a mobike is charged on the following basis.
First five days Rs 500 per day;
Next five days Rs 400 per day
Rest of the days Rs 200 per day
void display ( ) – to display the details in the following format:
Bike No. PhoneNo. No. of days Charge
import java.util.*;
public class Mobike
{
int bno;
int phno;
int days;
int charge;
String name;
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Customer Name: ");
name = sc.nextLine();
System.out.print("Enter Customer Phone Number: ");
phno = sc.nextInt();
System.out.print("Enter Bike Number: ");
bno = sc.nextInt();
System.out.print("Enter Number of Days: ");
days = sc.nextInt();
}
7
public void compute()
{
if (days <= 5)
charge = days * 500;
else if (days >5 && 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”+"\t"+“Phone No”+"\t"+”Name”+"\t"+ “No of days”+"\t"+”Charge");
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();
}
}
7. 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 customer’s mobile number.
double cost: to store the cost of the item purchased.
double dis: to store the discount amount.
double amount: to store the amount to be paid after discount.
Methods:
ShowRoom(): default constructor to initialize the data members
void input(): to input customer name, mobile number, cost
void calculate(): to calculate discount on the cost of purchased items, based on the
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, mobile number, amount to be paid after discount.
Write a main() method to create an object of the class and call the above methods
import java.util.Scanner;
public class ShowRoom
{
String name;
8
long mobno;
double cost;
double dis;
double amount;
public ShowRoom()
{
name = "";
mobno = 0;
cost = 0.0;
dis = 0.0;
amount = 0.0;
}
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter customer name: ");
name = sc.nextLine();
System.out.print("Enter customer mobile no: ");
mobno = sc.nextLong();
System.out.print("Enter cost: ");
cost = sc.nextDouble();
}
public void calculate()
{
if (cost <= 10000)
dis= 0.05;
else if (cost>10000 && cost<= 20000)
dis = 0.10;
else if (cost>20000 && cost<= 35000)
dis = 0.15;
else if (cost>35000)
dis = 0.20;
dis = cost * dis;
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();
}
}
9
8. 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 amountMember methods:
CabService() — Default constructor to initialize data members. String data membersto '' '' 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.
import java.util.Scanner;
class CabService
{
String car_type;
double km;
double bill;
CabService()
{
car_type = "";
km = 0.0;
bill = 0.0;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter car type (AC or NON AC): ");
car_type = sc.nextLine();
System.out.print("Enter kilometer travelled: ");
km = sc.nextDouble();
}
void calculate() {
10
if (car_type.equalsIgnoreCase("AC"))
{
if (km <= 5)
bill = 150.0;
else
bill = 150.0 + (km - 5) * 10.0;
}
else if (car_type.equalsIgnoreCase("NON AC"))
{
if (km <= 5)
bill = 120.0;
else
bill = 120.0 + (km - 5) * 8.0;
}
}
void display()
{
System.out.println("CAR TYPE: "+ "\t" +"KILOMETER TRAVELLED: "+ "\t" + "TOTAL BILL: " );
System.out.println(car_type+ "\t" + km + "\t" + bill)
}
public static void main(String args[])
{
CabService obj = new CabService();
obj.accept();
obj.calculate();
obj.display();
}
}
11