Professional Documents
Culture Documents
Roll No : 66
Div : B
Experiment No : 6
Problem Statement 1
Create a new class Food in the Java project SwiftFood with the details given below.
class Food
{
String foodName;
String cuisine;
String foodType;
int quantityAvailable;
double unitPrice;
Food(String f,String c,String ft,int q,double u)
{
foodName=f;
cuisine=c;
foodType=ft;
quantityAvailable=q;
unitPrice=u;
}
void display()
{
System.out.println(foodName);
System.out.println(cuisine);
System.out.println(foodType);
System.out.println(quantityAvailable);
System.out.println(unitPrice);
}
public static void main(String[] ars)
{
Food fo=new Food("Dosa","ColdDrink","Vegetarian",67,34.98);
fo.display();
}
}
Problem Statement 2
Method Description
findAverage()
Calculate the average of three numbers
Return the average rounded off to two decimal digits
double avg=(double)(num1+num2+num3)/3;
double roundoff=Math.round(avg*100.0)/100.0;
System.out.println(roundoff);
return roundoff;
class Tester {
calculator.findAverage(12,8,15);
calculator.findAverage(10,20,30);
// Invoke the method findAverage of the Calculator class and display the average
Problem Statement 3
Create a new class Order in the Java project SwiftFood with the instance variables and methods
mentioned below.
}
public static void main(String[] args)
{
Order o=new Order(101,"Spnach Alfredo Pasta","Ordered");
o.calculateTotalPrice(700);
}
}
Problem statement 4
Problem statement 5
Calculator(int num)
this.num=num;
int sumOfDigits()
int z,count=0,sum=0,digits,i;
z=num;
while(z>0)
count++;
z=z/10;
z=num;
for(i=1;i<=count;i++)
digits=z%10;
sum=sum+digits;
z=z/10;
System.out.println(sum);
return sum;
}
}
class Tester {
calculator.sumOfDigits();
calculator1.sumOfDigits();
}
Problem statement 6
Method Description
calculateArea()
Calculate and return the area of the rectangle. The area should be
rounded off to two decimal digits.
calculatePerimeter()
Calculate and return the perimeter of the rectangle. The perimeter
should be rounded off to two decimal digits.
Test the functionalities using the provided Tester class.
class Rectangle
float length;
float width;
double area=(double)length*width;
double roundOff=Math.round(area*100.0)/100.0;
System.out.println(roundOff);
return roundOff;
}
double calculatePerimeter(float length,float width)
double peri=(double)2*(length+width);
double round=Math.round(peri*100.0)/100.0;
System.out.println(round);
return round;
class Tester {
rectangle.calculateArea(12f,5f);
rectangle.calculatePerimeter(12f,5f);
rectangle1.calculateArea(6f,3f);
rectangle1.calculatePerimeter(6f,3f);
}
Problem statement 7
Method Description
Order()
Initialize the instance variables appropriately with the values passed to the
constructor.
Set the value of status to 'Ordered'.
public class Order
{
int orderId;
String orderedFoods;
Order()
{
System.out.println("Status of Order 1: Ordered");
System.out.println("Status of Order 2: Ordered");
}
Order(int orderId,String orderedFoods)
{
this.orderId=orderId;
this.orderedFoods=orderedFoods;
System.out.println("Id of order 2: "+orderId);
System.out.println("Items ordered in order 2: "+orderedFoods);
}
public static void main(String[] args)
{
Order or=new Order();
Order or1=new Order(1001,"Garlic Shrimp");
}
}
Problem statement 8
return employeeId;
employeeId=Id;
}
return employeeName;
employeeName=N;
return salary;
salary=s;
return bonus;
bonus=d;
}
public int accessorjob()
return jobLevel;
jobLevel=j;
if (this.jobLevel >= 4) {
this.bonus = 100;
} else {
this.bonus = 50;
this.salary += this.bonus;
class Tester {
employee.mutatorempId("C101");
employee.mutatorempName("Steve");
employee.mutatorempSal(650);
employee.mutatorjob(4);
employee.calculateSalary();
System.out.println("Employee Details");
Problem statement 9
Make necessary changes to the Order and Food classes by making all the instance
variables private and adding getter and setter methods for the instance variables.
class Food
{
private String foodName;
private String cuisine;
private String foodType;
private int quantityAvailable;
private double unitPrice;
public void getfoodName(String N)
{
foodName=N;
}
public String setfoodName()
{
return foodName;
}
public void getcuisine(String C)
{
cuisine=C;
}
public String setcuisine()
{
return cuisine;
}
public void getfoodType(String F)
{
foodType=F;
}
public String setfoodType()
{
return foodType;
}
public void getquantityAvailable(int Q)
{
quantityAvailable=Q;
}
public int setquantityAvailable()
{
return quantityAvailable;
}
public void getunitPrice(int P)
{
unitPrice=P;
}
public double setunitPrice()
{
return unitPrice;
}
public static void main(String[] ars)
{
Food food=new Food();
food.getfoodName("Dosa");
food.getcuisine("Cold Drink");
food.getfoodType("Vegetarian");
food.getquantityAvailable(234);
food.getunitPrice(23);
System.out.println("Food Name : "+food.setfoodName());
System.out.println("Cuisine : "+food.setcuisine());
System.out.println("Food Type : "+food.setfoodType());
System.out.println("Quantity : "+food.setquantityAvailable());
System.out.println("Unit Price : "+food.setunitPrice());
}
}
}
}
Problem statement 10
Make the necessary changes in the code such that the member variables are
initialized with proper values and the output is generated as follows:
class Camera {
public Camera() {
this.brand = "Nikon";
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
return cost;
this.cost = cost;
this.memory = 16;
this.setBrand(brand);
this.setCost(cost);
return memory;
this.memory = memory;
}
class Tester {
Problem statement 10
The Point class is used for representing a point with two coordinates.
class Point
this.xCoordinate=xCoordinate;
this.yCoordinate=yCoordinate;
}
public double getxCoordinate()
return xCoordinate;
this.xCoordinate=xCoordinate;
return yCoordinate;
this.yCoordinate=yCoordinate;
double distance=Math.sqrt((getxCoordinate()-0)*(getxCoordinate()-0)+(getyCoordinate()-
0)*(getyCoordinate()-0));
return Math.round(distance*100.0)/100.0;
double distance=Math.sqrt((point.xCoordinate-xCoordinate)*(point.xCoordinate-xCoordinate)+
(point.yCoordinate-yCoordinate)*(point.yCoordinate-yCoordinate));
return Math.round(distance*100.0)/100.0;
class Tester {
}
Problem Statement 11
EPay Wallet is a wallet application using which users can pay various bills.
Users can make payments only if they have enough wallet balance.
There are two kinds of users – User and PremiumUser. PremiumUser gets
reward points for every payment.
Implement the classes based on the class diagram and description given
below
class User
this.id=id;
this.userName=userName;
this.emailId=emailId;
this.walletBalance=walletBalance;
}
public int getId()
return id;
this.id=id;
return userName;
this.userName=userName;
return emailId;
this.emailId=emailId;
return walletBalance;
this.walletBalance=walletBalance;
if(billAmount<=getWalletBalance())
setWalletBalance(getWalletBalance()-billAmount);
return true;
return false;
super(id,userName,emailId,walletBalance);
}
public int getRewardPoints()
return rewardPoints;
this.rewardPoints=rewardPoints;
@Override
if(billAmount<=getWalletBalance())
setRewardPoints(getRewardPoints()+(int)(billAmount*0.1));
return super.makePayment(billAmount);
class Tester {
processPayment(user, 70);
processPayment(premiumUser, 150);
processPayment(premiumUser, 80);
processPayment(premiumUser, 120);
if (user.makePayment(billAmount)) {
} else {
System.out.println();
Problem Statement 12
Implement the class Bill based on the class diagram and description given
below.
class Bill
{
//Implement your code here
private int counter=9000;
private String billId="B";
private String paymentMode;
public Bill(String paymentMode)
{
this.paymentMode=paymentMode;
}
public String getBillId()
{
return billId+counter;
}
public void setBillId(String billId)
{
this.billId=billId;
}
public String getPaymentMode()
{
return paymentMode;
}
public void setPaymentMode(String paymentMode)
{
this.paymentMode=paymentMode;
}
public int getCounter()
{
return counter++;
}
class Tester {
public static void main(String[] args) {
Problem statement 13
class Participant
{
//Implement your code here
private int counter=1000;
private String registrationId="D";
private String name;
private long contactNumber;
private String city;
public Participant(String name,long contactNumber,String city)
{
this.name=name;
this.contactNumber=contactNumber;
this.city=city;
}
public String getRegistrationId()
{
return registrationId+getCounter();
}
public int getCounter()
{
return counter++;
}
public void setCounter(int counter)
{
this.counter=counter;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public long getContactNumber()
{
return contactNumber;
}
public void setContactNumber(long contactNumber)
{
this.contactNumber=contactNumber;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city=city;
}
class Tester {
}
}
Problem statement 13
class Participant
this.registrationId="D"+ ++Participant.counter;
this.name=name;
this.contactNumber=contactNumber;
this.city=city;
return registrationId;
return counter;
Participant.counter=counter;
return name;
this.name=name;
return contactNumber;
{
this.contactNumber=contactNumber;
return city;
this.city=city;
class Tester {
//Create more objects and add them to the participants array for testing your code
Problem Statement 14
class Tester {
public static void main(String[] args) {
Booking booking1 = new Booking("jack@email.com", 100);
Booking booking2 = new Booking("jill@email.com", 350);
//Create more objects and add them to the bookings array for testing your
code
Booking[] bookings = { booking1, booking2 };
for (Booking booking : bookings) {
if (booking.isBooked()) {
System.out.println(booking.getSeatsRequired()+" seats
successfully booked for "+booking.getCustomerEmail());
}
else {
System.out.println("Sorry "+booking.getCustomerEmail()+",
required number of seats are not available!");
System.out.println("Seats available:
"+Booking.getSeatsAvailable());
}
}
}
}
Problem Statement 15
You have already created the Customer class in the SwiftFood project. The address
instance variable is of type String but address itself can be represented as a
combination of doorNo, street, city and zipCode.
So, create an Address class based on the class diagram given below and make
necessary changes to the Customer class.
class Address
{
private String doorNo;
private String street;
private String city;
private int zipCode;
Address(String doorNo,String street,String city,int zipCode)
{
this.doorNo=doorNo;
this.street=street;
this.city=city;
this.zipCode=zipCode;
}
public String getDoorNo()
{
return doorNo;
}
public void setDoorNo(String doorNo)
{
this.doorNo=doorNo;
}
public String getStreet()
{
return street;
}
public void setStreet(String street)
{
this.street=street;
}
public int getZipCode()
{
return zipCode;
}
public void setZipCode(int zipCode)
{
this.zipCode=zipCode;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city=city;
}
}
class Customer
{
private String customerId;
private String customerName;
private long contactNumber;
private Address address;
Customer() {}
Customer(String customerId,String customerName,long contactNumber,Address
address)
{
this.customerId=customerId;
this.customerName=customerName;
this.contactNumber=contactNumber;
this.address=address;
}
Customer(String customerId,String customerName,Address address)
{
this.customerId=customerId;
this.customerName=customerName;
this.address=address;
}
public String getCustomerId()
{
return customerId;
}
public void setCustomerId(String customerId)
{
this.customerId=customerId;
}
public String getCustomerName()
{
return customerName;
}
public void setCustomerName(String customerName)
{
this.customerName=customerName;
}
public long getContactNumber()
{
return contactNumber;
}
public void setContactNumber(long contactNumber)
{
this.contactNumber=contactNumber;
}
public Address getAddress()
{
return address;
}
public void setAddress(Address address)
{
this.address=address;
}
public void displayCustomerDetails()
{
System.out.println("Customer ID: "+getCustomerId());
System.out.println("Name: "+getCustomerName());
System.out.println("Contact no: "+getContactNumber());
System.out.println("Address: ");
System.out.println("\tDoor No: "+address.getDoorNo());
System.out.println("\tStreet: "+address.getStreet());
System.out.println("\tCity: "+address.getCity());
System.out.println();
}
public double payBill(double totalPrice)
{
return totalPrice;
}
public static void main(String[] args)
{
Address a1=new Address("A45","xy road","Hyderabad",500012);
Customer c1=new Customer("101","Parag",890000000,a1);
c1.displayCustomerDetails();
}
}
Problem statement 16
You need to develop an application for cab service providers by implementing the
classes based on the class diagram and description given below.
package associationEx1;
class CabServiceProvider
{
//Implement your code here
private String cabServiceName;
private int totalCab;
public double rewardPrice;
CabServiceProvider(String cabServiceName,int totalCab)
{
this.cabServiceName=cabServiceName;
this.totalCab=totalCab;
}
public String getCabServiceName()
{
return cabServiceName;
}
public void setCabServiceName(String cabServiceName)
{
this.cabServiceName=cabServiceName;
}
public int getTotalCab()
{
return totalCab;
}
public void setTotalCab(int totalCab)
{
this.totalCab=totalCab;
}
double calculateRewardPrice(Driver driver)
{
if(cabServiceName=="Halo")
{
if(driver.getAverageRating()>=4.5 && driver.getAverageRating()<=5)
{
rewardPrice=10*driver.getAverageRating();
}
else if(driver.getAverageRating()>=4 &&
driver.getAverageRating()<=4.5)
{
rewardPrice=5*driver.getAverageRating();
}
else
{
return rewardPrice=0;
}
}
else if(cabServiceName=="Aber")
{
if(driver.getAverageRating()>=4.5 && driver.getAverageRating()<=5)
{
rewardPrice=8*driver.getAverageRating();
}
else if(driver.getAverageRating()>=4 &&
driver.getAverageRating()<=4.5)
{
rewardPrice=3*driver.getAverageRating();
}
else
{
return rewardPrice=0;
}
}
else
{
return rewardPrice;
}
}
}
class Driver {
private String driverName;
private float averageRating;
public Driver(String driverName, float averageRating){
this.driverName=driverName;
this.averageRating=averageRating;
}
public String getDriverName(){
return this.driverName;
}
public void setDriverName(String driverName){
this.driverName=driverName;
}
public float getAverageRating(){
return this.averageRating;
}
public void setAverageRating(float averageRating){
this.averageRating=averageRating;
}
class Tester {
public static void main(String args[]){
CabServiceProvider cabServiceProvider1 = new CabServiceProvider("Halo",
50);