/*** Define a class 'Book' described as below:
Data members: Name, Author, Publication, Cost, Numberof Pages.
Member Methods:
1. Accept book details
2. Display the details
3. Increase the cost of book by 50/-
Write main method to call above functions for a book.*/
import java.util.Scanner;
public class Book
{
String name,author, publication;
double cost;
int num;
void accept() // to accept the details of the book
{
Scanner sc=new Scanner (System.in);
System.out.println("Please enter the name of book, author and publications");
name=sc.nextLine();
author=sc.nextLine();
publication=sc.nextLine();
System.out.println("Please enter the cost of the book, no. of pages");
cost=sc. nextDouble();
num=sc.nextInt();
}
void display() // to display the detalsof the book
{
System.out.println(" Name of the book: "+ name);
System.out.println(" Name of the author: "+ author);
System.out.println(" Name of the publications: "+ publication);
System.out.println(" Cost of the book: "+ cost);
System.out.println(" No. of pages: "+ num);
}
void increment()
{
cost+=50; //
}
public static void main(String args[])
{
Book obj=new Book();
obj.accept(); // calling accept method
obj.display(); //
System.out.println("Changed values");
obj.increment();
obj.display();
}
}
/**Define a class 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 vno and hours
void calculate() – To compute the parking charge at the rate of Rs.3
for the first hour or part thereof, and Rs.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*/
import java.util.Scanner;
public class ParkingLot
{
int vno;
int hours;
double bill;
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter vehicle number: ");
vno = sc.nextInt();
System.out.print("Enter hours: ");
hours = sc.nextInt();
}
public void calculate()
{
bill = 3 + (hours - 1) * 1.50;
}
public void display()
{
System.out.println("Vehicle number: " + vno);
System.out.println("Hours: " + hours);
System.out.println("Bill: Rs. " + bill);
}
public static void main(String[] args)
{
ParkingLot obj = new ParkingLot();
obj.input();
obj.calculate();
obj.display();
}
}
/** Define a class Bill that caluculates the telephone bill of a consumer with the following
* description:
* Instance variables/data members:
* int bno: bill number
* String name: name of the consumer
* int call: no. of calls consumed in a month
* double amt: bill amount to be paid by the person
* Methods/Member functions:
* Bill() : constructor to initialize data members with default initial value
* Bill(...): parameterised constructor to accept billno, name and no. of calls consumed.
* Calculate() : to calulate the monthly telephone bill for a consumer as per the following
condition
* units consumed rate
* First 100 calls 0.60/- call
* Next 100 calls 0.80/- call
* Next 100 calls 1.20/- call
* above 300 calls 1.50/- call
* Fixed monthly rental applicable to all consumer: Rs. 125/-
* Display() : to display the details
* create an object in the main() method and invoke the above functions to perform the
desired task.
*/
import java.util.Scanner;
public class Bill
{
int bno;
String name;
int call;
double amt;
Bill()
{
bno = 0;
name = "";
call = 0;
amt = 0.0;
}
Bill(int bn, String nam, int cal)
{
bno = bn;
name = nam;
call = cal;
}
public void calculate()
{
double charge;
if (call <= 100)
charge = call * 0.6;
else if (call <= 200)
charge = 60 + ((call - 100) * 0.8);
else if (call <= 300)
charge = 60 + 80 + ((call - 200) * 1.2);
else
charge = 60 + 80 + 120 + ((call - 300) * 1.5);
amt = charge + 125;
}
public void display()
{
System.out.println("Bill No: " + bno);
System.out.println("Name: " + name);
System.out.println("Calls: " + call);
System.out.println("Amount Payable: " + amt);
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String custName = in.nextLine();
System.out.print("Enter Bill Number: ");
int billNum = in.nextInt();
System.out.print("Enter Calls: ");
int numCalls = in.nextInt();
Bill obj = new Bill(billNum, custName, numCalls);
obj.calculate();
obj.display();
}
}
/*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 rs.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.*/
import java.util.*;
public class ShowRoom
{
String name;
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()
{
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();
}
}
/**Define a class Electric Bill 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% 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.*/
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 name: ");
n = sc.next();
System.out.print("Enter units: ");
units = sc.nextInt();
}
public void calculate()
{
if (units<= 100)
{
bill = units * 2;
}
else if (units>100 && units<=300)
{
bill = units * 3;
}
else
{
bill = units * 5;
double surcharge = bill * 2.5 / 100;
bill = bill + surcharge;
}
}
public void print() {
System.out.println("Name of the customer: " +n);
System.out.println("Number of units consumed: " +units);
System.out.println("Bill amount: " +bill);
}
public static void main(String[] args)
{
ElectricBill EB = new ElectricBill();
EB.accept();
EB.calculate();
EB.print();
}
}