Solutions Assignment-1 Data Members and Functions
Solutions Assignment-1 Data Members and Functions
import java.util.*;
class Student
{
String name;
int age;
double mks;
String stream;
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();
}
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";
}
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()
{
Student obj = new Student();
obj.accept();
obj.allocation();
obj.print();
}
}
3. A private cab service company provides service within the city at the following rates:
AC CAR NON AC CAR
UPTO 5 KM Rs. 150/- Rs. 120/-
BEYOND 5 KM Rs. 10/- per km Rs. 08 per km
import java.util.*;
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.println("Enter Car type AC or NON Ac ");
car_type = sc.nextLine();
System.out.println("Enter km ");
km = sc.nextDouble();
}
void calculate()
{
if(car_type.equalsIgnoreCase("AC CAR"))
{
if(km <= 5)
bill = 150;
else
bill = 150 + (km-5) * 10;
}
else
{
if(km <= 5)
bill = 120;
else
bill = 120 + (km-5) * 8;
}
}
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()
{
CabService obj = new CabService();
obj.accept();
obj.calculate();
obj.display();
}
}
import java.util.*;
class ShowRoom
{
String name;
long mobno;
double cost,dis,amount;
ShowRoom() //default constructor
{
name="";
mobno=0;
cost=0.0;
dis=0.0;
amount=0.0;
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
name=sc.nextLine();
System.out.print("Enter your Mobile Number: ");
mobno=sc.nextLong();
System.out.print("Enter cost of item purchased: ");
cost=sc.nextDouble();
}
void calculate()
{
if(cost<=10000)
dis = cost * 5/100;
else
if(cost>10000 && cost<=20000)
dis = cost * 10/100;
else
if(cost>20000 && cost<=35000)
dis = cost * 15/100;
else
dis = cost * 20/100;
amount = cost - dis;
}
void display()
{
System.out.println("Name of the customer: "+name);
System.out.println("Mobile number : "+mobno);
System.out.println("Amount to be paid after discount: "+amount);
}
public static void main()
{
ShowRoom obj = new ShowRoom();
obj.input();
obj.calculate();
obj.display();
}
}
First_ AC 700
Second_AC 500
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.
import java.util.*;
class RailwayTicket
{
String name,coach;
long mobno;
int amt,totalamt;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Ente name: ");
name=sc.nextLine();
System.out.print("Enter coach(in uppercase): ");
coach=sc.nextLine();
System.out.print("Enter mobile number: ");
mobno=sc.nextLong();
System.out.print("Enter amount: ");
amt=sc.nextInt();
}
void update()
{
if(coach.equals("FIRST AC"))
totalamt=amt+700;
else
if(coach.equals("SECOND AC"))
totalamt=amt+500;
else
if(coach.equals("THIRD AC"))
totalamt=amt+250;
else
if(coach.equals("SLEEPER"))
totalamt=amt+0;
}
void display()
{
System.out.println("NAME: " + name);
System.out.println("COACH: " + coach);
System.out.println("MOBILE NUMBER: " + mobno);
System.out.println("AMOUNT: " + amt);
System.out.println("TOTAL AMOUNT: " + totalamt);
}
public static void main()
{
RailwayTicket obj=new RailwayTicket();
obj.accept();
obj.update();
obj.display();
}
}
6. Define a class ElectricBill with the following specifications : (2017)
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 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.*;
class ElectricBill
{
String n;
int units;
double bill;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter name of the customer : ");
n = sc.nextLine();
System.out.print("Enter number of units : ");
units = sc.nextInt();
}
void calculate()
{
if(units <= 100)
bill = units * 2;
else
if(units <= 300)
bill = (100 * 2) + (units - 100) *3;
else
bill = (100 * 2) + (200 * 3) + (units - 300) * 5;
if(units > 300) // calculating surcharge for units greater than 300
bill = bill + bill * 2.5/100;
}
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( )
{
ElectricBill obj = new ElectricBill();
obj.accept();
obj.calculate();
obj.print();
}
}
More than Rs. 1000 and less than or equal to Rs. 3000 10% of price
import java.util.*;
class BookFair
{
String Bname;
double price;
public BookFair() //default constructor
{
Bname="";
price=0.0;
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Bookname");
Bname=sc.nextLine();
System.out.print("Enter Price");
price=sc.nextDouble();
}
void calculate()
{
double discount=0.0;
if(price<=1000)
discount=price*2/100;
else
if(price<=3000)
discount=price*10/100;
else
discount=price*15/100;
price=price-discount;
}
void display()
{
System.out.print("Bookname : "+Bname);
System.out.print("Price after discount : "+price);
}
public static void main()
{
BookFair obj=new BookFair();
obj.input();
obj.calculate();
obj.display();
}
}
import java.util.*;
class ParkingLot
{
int vno, hours;
double bill;
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();
}
void calculate()
{
if(hours<=1)
bill=3;
else
bill=3+(hours-1)* 1.50;
}
void display()
{
System.out.print("Vehicle no. is : "+vno);
System.out.print("Hour is : "+hours);
System.out.print("Your bill is : "+bill);
}
public static void main()
{
ParkingLot obj=new ParkingLot();
obj.input();
obj.calculate();
obj.display();
}
}
import java.util.*;
class MovieMagic
{
int year;
String title;
float rating;
public MovieMagic()
{
year=0;
title="";
rating=0.0f;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Title Of The Movie");
title= sc.nextLine();
System.out.print("Enter the Year Of Release Of a Movie");
year= sc.nextInt();
System.out.print("Enter the Popularity Rating Of The Movie");
rating= sc.nextFloat();
}
void display()
{
System.out.println("Title of the movie is " + title);
10. Define a class named Fruit Juice with the following description: (2013)
Instance variables / data members :
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. tetra-pack, PET bottle, etc.)
in pack_size — stores package size (E.g. 200 ml, 400 ml, etc.)
in product_price — stores the price of the product
Member methods :
(i) FruitJuice() — Default constructor to initialize integer data members to 0 and String
data members to.
(ii) void input( ) — To input and store the product code, flavour, pack type, pack size and
product price.
(iii) void discount( ) — To reduce the product price by 10.
(iv) void display() — To display the product code, flavor, pack type, pack size and
product price.
import java.util.*;
class FruitJuice
{
int product_code,pack_size,product_price;
String flavour,pack_type;
public FruitJuice()
{
product_code=0;
pack_size=0;
product_price=0;
flavour="";
pack_type="";
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the product code number:");
product_code=sc.nextInt();
System.out.println("Enter the flavour of the juice:");
flavour=sc.nextLine();
System.out.println("Enter the type of packaging:");
pack_type=sc.nextLine();
System.out.println("Enter the package size:");
pack_size=sc.nextInt();
System.out.println("Enter the product price:");
product_price=sc.nextInt();
}
void discount()
{
product_price =product_price-10;
}
void display()
{
System.out.println("product code number:"+product_code);
System.out.println("flavour of the juice:"+flavour);
System.out.println("type of packaging:"+pack_type);
System.out.println("package size:"+pack_size);
System.out.println("product price:"+product_price);
}
public static void main( )
{
FruitJuice obj=new FruitJuice();
obj.input();
obj.discount();
obj.display();
}
}
11. Define a class called Library with the following description : (2012)
Instance variables/data members :
int acc_num — 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:
(i) void input ( ) — To input and store the accession number, title and author.
(ii) void compute ( ) — To accept the number of days late, calculate the display the fine
charged at the rate of Rs. 2 per day.
(iii) 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.
import java.util.*;
class Library
{
int acc_num;
String title, author;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the accession number of the book:");
acc_num = sc.nextInt();
System.out.print("Please enter the title of the book:");
title=sc.nextLine();
System.out.print("Please enter the author of the book:");
author=sc.nextLine();
}
void compute()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the number of days late the book is returned:");
int days=sc.nextInt();
double fine=2*days;
System.out.println("Fine is "+fine);
}
void display()
{
System.out.println("ACCESSION NUMBER \t TITLE \t AUTHOR");
System.out.println(acc_num +"\t"+title+"\t"+ author);
}
public static void main()
{
Library obj=new Library();
obj.input();
obj.compute();
obj.display();
}
}
import java.util.*;
class Mobike
{
int bno,phno,days,charge;
String name;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the bike's number:");
bno=sc.nextInt();
System.out.println("Please enter the phone number of the number:");
phno=sc.nextInt();
System.out.println("Please enter the name of the customer:");
name=sc.nextLine();
System.out.println("Please days:");
days = sc.nextInt();
}
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;
}
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()
{
Mobike obj=new Mobike();
obj.input();
obj.compute();
obj.display();
}
}
13. Define a class student described as below: (2010)
Data members / instance variables:
name, age, m1, m2, m3 (marks in 3 subjects), maximum, average Member methods
(i) A parameterized constructor to initialize the data members
(ii) To accept the details of a student
(iii) To compute the average and the maximum out of three marks
(iv) To display the name, age, marks in three subjects, maximum and average.
Write a main method to create an object of a class and call the above member
methods.
import java.util.*;
class Student
{
String name;
int age, m1, m2, m3 ,max;
double avg;
public Student(String n, int a, int x, int y, int z)
{
name = n;
age = a;
m1 = x;
m2 = y;
m3 = z;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter name : ");
name = sc.nextLine();
System.out.print("Enter age : ");
age = sc.nextInt();
System.out.print("Enter marks in 3 subjects : ");
m1 = sc.nextInt();
m2 = sc.nextInt();
m3 = sc.nextInt();
}
void calc()
{
max = (m1 > m2 && m1 > m3) ? m1 : (m2 > m1 && m2 > m3) ? m2 : m3;
avg = (m1+m2+m3)/3.0;
}
void display()
{
System.out.println("Name is " + name);
System.out.println("Age is " + age);
System.out.println("Marks of Subject 1 " + m1);
System.out.println("Marks of Subject 2 " + m2);
System.out.println("Marks of Subject 3 " + m3);
System.out.println("Maximum is " + max);
System.out.println("Average is " + avg);
}
public static void main( )
{
Student obj = new Student("Parshva",10,77,88,99);
obj.accept();
obj.calc();
obj.display();
}
}
From 1,50,001 to 2,50,000 Rs. 5,000 + 20% of the income exceeding Rs. 1,50,000
Above Rs. 2,50,000 Rs. 25,000 + 30% of the income exceeding Rs. 2,50,000.
Output:
PAN Number Name Tax-Income Tax
xxxxxx xxxx xxxxx xxxx
import java.util.*;
class Employee
{
int pan;
String name;
double taxincome; // TAXABLE INCOME
double tax;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter pan number, name and taxable income : ");
pan = sc.nextInt();
name = sc.nextLine();
taxincome = sc.nextDouble();
}
void calc()
{
if(taxincome <= 100000)
tax = 0;
else
if(taxincome <= 150000)
tax = (taxincome - 100000)*10/100;
else
if(taxincome <= 250000)
tax = 5000 + (taxincome - 150000) * 20/100;
else
tax = 25000 + (taxincome - 250000) * 30/100;
}
void display()
{
System.out.println("Pan number \t Name \t Tax Income \t Tax");
System.out.println(pan + "\t\t" + name + "\t" + taxincome + "\t" + tax);
}