You are on page 1of 5

Assignment 7

Define a class named Bookfair with the following description :

Instance variable / Data members:

String Bname – Stores the name of the book.

double price - store the price of the book.

Member Methods:

void input( ) – To input and store the name and the price of the book.

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

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.

Solution
import java.util.*;

class Bookfair

String Bname;

double price;

double discount;

double paid;

public void input()

{
Scanner object= new Scanner(System.in);

System.out.println("Name of the book");

Bname= object.next();

System.out.println("Price of the book");

price= object.nextDouble();

public void calculate()

if(price<=1000)

discount=(2*price)/100;

else if(price>1000&&price<=3000)

discount=(10*price)/100;

else

discount=(15*price)/100;

paid=price-discount;

public void display()

System.out.println("Name of the book= "+Bname);

System.out.println("Price of the book after discount= "+paid);

public static void main(String args[])

Bookfair obj= new Bookfair();

obj.input();
obj.calculate();

obj.display();

Assignment 8
Define a class ElectricBill With the following specifications:

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% 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.
Solution
import java.util.*;

class ElectricBill

String n;

int units;

double bill,surcharge;

public void accept()

Scanner object= new Scanner(System.in);

System.out.println("Name of the customer");

n= object.next();

System.out.println("Number of units consumed");

units= object.nextInt();

public void calculate()

if(units<=100)

bill=(2*units);

else if(units>100&&units<=300)

bill=(2*100)+(3*(units-100));

else

bill= (2*100)+(3*200)+(5*(units-300));

surcharge=(bill*2.5)/100;
bill=bill+surcharge;

public void print()

System.out.println("Name of the consumer= "+n);

System.out.println("No. of units consumed= "+units);

System.out.println("Bill amount= "+bill);

public static void main(String args[])

ElectricBill obj= new ElectricBill();

obj.accept();

obj.calculate();

obj.print();

You might also like