0% found this document useful (0 votes)
7 views24 pages

Solutions Assignment-1 Data Members and Functions

The document outlines the specifications for several Java classes, including Courier, Student, CabService, ShowRoom, RailwayTicket, ElectricBill, BookFair, and ParkingLot. Each class has defined member variables and methods for accepting input, calculating bills or allocations, and displaying results. The document provides a comprehensive guide for implementing these classes in Java programming.

Uploaded by

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

Solutions Assignment-1 Data Members and Functions

The document outlines the specifications for several Java classes, including Courier, Student, CabService, ShowRoom, RailwayTicket, ElectricBill, BookFair, and ParkingLot. Each class has defined member variables and methods for accepting input, calculating bills or allocations, and displaying results. The document provides a comprehensive guide for implementing these classes in Java programming.

Uploaded by

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

1. DTDC a courier company charges for the courier based on the weight of the parcel.

Define a class with the following specifications: (2024)


class name : Courier
Member variables :
name - name of the customer
weight - weight of the parcel in kilograms address - address of the recipient
bill - amount to be paid
type - 'D'- domestic, 'I' - international
Member methods:
void accept ()-to accept the details using the methods of the Scanner class only.
void calculate () - to calculate the bill as per the following criteria:
Weight in Kgs Rate per Kg
First 5 Kgs Rs.800
Next 5 Kgs Rs. 700
Above 10 Kgs Rs.500
An additional amount of Rs. 1500 is charged if the type of the courier is I (International)
void print () - To print the details
void main () -to create an object of the class and invoke the methods
import java.util.*;
class Courier
{
String name;
int weight;
double bill;
char type;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter name ");
name = sc.nextLine();
System.out.println("Enter type (I for International and D for Domestic )");
type = sc.next().charAt(0);
System.out.println("Enter weight ");
weight = sc.nextInt();
}
void calculate()
{
if(weight <= 5)
bill = weight * 800;
else
if(weight <= 10)
bill = (5 * 800) + (weight - 5) * 700;
else
bill = (5 * 800) + (5 * 700) + (weight - 10) * 500;
if(type == 'I' || type == 'i')
bill = bill + 1500;
}
void print()
{
System.out.println("Name = "+ name);
System.out.println("Weight = "+ weight);
System.out.println("Type = "+ type);
System.out.println("Bill = "+ bill);
}
public static void main()
{
Courier obj = new Courier();
obj.accept();
obj.calculate();
obj.print();
}
}

2. Design a class with the following specifications: (2023)


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

>= 200 and < 300 Commerce and 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.*;
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

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 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.

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

4. Design a class name ShowRoom with the following description : (2019)


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.

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

5. Design a class Railway Ticket with following description : (2018)


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
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.

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

7. Define a class named BookFair with the following description: (2016)


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

8. Define a class named ParkingLot with the following description : (2015)


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 — Tb 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 Rs.3 for the first hours
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.*;
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();
}
}

9. Define a class named movieMagic with the following description:


Instance variables/data members: (2014)
int year — to store the year of release of a movie.
String title — to-store the title of the movie
float rating — to store the popularity rating of the movie
(minimum rating=0.0 and maximum rating=5.0)
Member methods:
(i) movieMagic() Default constructor to initialize numeric data members to 0 and String data
member to “ ”.
(ii) void accept() To input and store year, title and rating.
(iii) void display() To display the title of a movie and a message based on the rating as per the
table.

Rating Message to be displayed

0.0 to 2.0 Flop

2.1 to 3.4 Semi-hit

3.5 to 4.5 Hit

4.6 to 5.0 Super Hit


Write a main method to create an object of the class and call the above member methods.

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

if(rating >= 0.0 && rating <= 2.0)


System.out.println("Flop");
else
if(rating >= 2.1 && rating <= 3.4)
System.out.println("Semi-Hit");
else
if(rating >= 3.5 && rating <= 4.5)
System.out.println("Hit");
else
System.out.println("Super-Hit");
}
public static void main()
{
MovieMagic obj= new MovieMagic();
obj.accept();
obj.display();
}
}

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

12. Define a class called mobike with the following description:


Instance variables /data members: (2011)
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 — 4 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. Phone No. Name No. of days Charge
………….. …………….. ………. ………………. …………

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

14. Define a class employee having the following description : (2008)


Data members/instance variables:
int pan — to store personal account number
String name — to store name
double tax income — to store annual taxable income
double tax — to store tax that is calculated
Member functions:
input() — Store the pan number, name, taxable income
calc() — Calculate tax for an employee
display() — Output details of an employee
Write a program to compute the tax according to the given conditions and display the
output as per given format.
Total annual Taxable Income Tax Rate

Upto Rs. 1,00;000 No tax

From 1,00,001 to 1,50,000 10% of the income exceeding Rs. 1,00,000

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

public static void main( )


{
Employee obj = new Employee();
obj.input();
obj.calc();
obj.display();
}
}
15. Define a class salary described as below :
Data Members: Name, Address, Phone, Subject Specialization, Monthly Salary, Income
Tax.
Member methods:
(i) To accept the details of a teacher including the monthly salary.
(ii) To display the details of the teacher.
(iii) To compute the annual Income Tax as 5% of the annual salary above Rs. 1,75,000/-.
Write a main method to create object of a class and call the above member method.
import java.util.*;
class Salary
{
String name , addr, subsp;
double msal, tax;
long phone;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter name, address , subsp, phone no. and monthly salary : ");
name = sc.nextLine();
addr = sc.nextLine();
subsp = sc.nextLine();
phone = sc.nextLong();
msal = sc.nextDouble();
}
void display()
{
System.out.println("Name is " + name); // display all the details
System.out.println("Address " + addr);
System.out.println("Tax is " + tax);
}
void calculate()
{
double asal = msal * 12;
if(asal > 175000)
tax = (asal-175000) * 5/100;
else
tax = 0;
}
public static void main( )
{
Salary obj = new Salary();
obj.input();
obj.calculate();
obj.display();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy