Fiza Oop 4
Fiza Oop 4
Karachi Campus
Submitted By:
Submitted To:
2 Use Constructor to set the radius and height of cylinder and calculate surface area
and Volume of cylinder.
4 Design then implement a class to represent a Flight. A Flight has a flight number,
a source, a destination and a number of available seats. This should be
implemented using proper access modifier.
Submitted On:
14/3/2024
(Date: DD/MM/YY)
8/3/2024 Object Oriented Programming
[Constructors in Java]
LAB # 04
Task # 01:
Write a program using the concepts of a default constructor. Consider a
computer system whose name, type, processor specification, ram, hard disk
drives, mother board, optical drive etc, in a default constructor, desired
values are entered by the user in a get method (that takes information from
the user) and the displays the inputted information via display method. The
user shall be asked to change any of the provided information if he/she
agrees to change the information then new values shall be asked from the
user.
Solution:
Main class:
package constructors;
import java.util.Scanner;
FIZA KHAN 1
8/3/2024 Object Oriented Programming
[Constructors in Java]
break;
case 2:
System.out.println("Enter the updated name:");
name = input.next();
System.out.println("Enter the updated type of system:");
type = input.next();
c = new Computer(name, type, c.getProcessor(), c.getRam(),
c.getHdd());
System.out.println("Data updated!");
System.out.println("----------------------------------------------
-");
c.Display();
break;
case 3:
System.out.println("Enter the updated name:");
name= input.next();
System.out.println("Enter the updated type of system:");
type = input.next();
System.out.println("Enter the updated processor: ");
input.nextLine();
processor = input.nextLine();
c = new Computer(name, type, processor, c.getRam(), c.getHdd());
System.out.println("Data updated!");
System.out.println("----------------------------------------------
-");
c.Display();
break;
case 4:
System.out.println("Enter the updated name:");
name = input.next();
System.out.println("Enter the updated type of system:");
type = input.next();
System.out.println("Enter the updated processor: ");
input.nextLine();
processor = input.nextLine();
System.out.println("Enter the updated size of RAM:");
ram= input.nextDouble();
c = new Computer(name, type, processor, ram, c.getHdd());
System.out.println("Data updated!");
System.out.println("----------------------------------------------
-");
c.Display();
break;
case 5:
System.out.println("Enter the updated name:");
name = input.next();
System.out.println("Enter the updated type of system:");
type = input.next();
System.out.println("Enter the updated processor: ");
input.nextLine();
processor = input.nextLine();
System.out.println("Enter the updated size of RAM:");
ram = input.nextDouble();
System.out.println("Enter the updated size of hard disk drive:");
hdd = input.nextDouble();
c = new Computer(name, type, processor, ram, hdd);
System.out.println("Data updated!");
FIZA KHAN 2
8/3/2024 Object Oriented Programming
[Constructors in Java]
System.out.println("----------------------------------------------
-");
c.Display();
break;
default:
System.out.println("Invalid option selected!");
break;
}
}
else {
System.out.println("No data updated!");
System.out.println("-----------------------------------------------");
c.Display();
}
}
Computer class:
package constructors;
import java.util.Scanner;
public Computer(){
this.name="";
this.type="";
this.processor="";
this.ram=0;
this.hdd=0;
}
public Computer(String name){
this.name=name;
}
public Computer(String name,String type){
this.name=name;
this.type=type;
}
public Computer(String name,String type,String processor){
this.name=name;
this.type=type;
this.processor=processor;
}
public Computer(String name,String type,String processor,double ram){
this.name=name;
this.type=type;
FIZA KHAN 3
8/3/2024 Object Oriented Programming
[Constructors in Java]
this.processor=processor;
this.ram=ram;
}
public Computer(String name,String type,String processor,double ram,double hdd){
this.name=name;
this.type=type;
this.processor=processor;
this.ram=ram;
this.hdd=hdd;
}
public void GetValues(){
System.out.println("Enter the name of system:");
name=input.next();
System.out.println("Enter the type of system:");
type=input.next();
System.out.println("Enter the type of processor:");
input.nextLine();
processor=input.nextLine();
System.out.println("Enter the size of RAM:");
ram=input.nextDouble();
System.out.println("Enter the size of hard disk drive:");
hdd=input.nextDouble();
}
public String getName() {
return name;
}
FIZA KHAN 4
8/3/2024 Object Oriented Programming
[Constructors in Java]
Output:
FIZA KHAN 5
8/3/2024 Object Oriented Programming
[Constructors in Java]
Task # 02:
Use Constructor to set the radius and height of cylinder and calculate surface
area and Volume of cylinder.
Solution:
Main class:
package task_2;
import java.util.Scanner;
Cylinder class:
package task_2;
FIZA KHAN 6
8/3/2024 Object Oriented Programming
[Constructors in Java]
Output:
Task # 03:
Use Constructor to set the radius and height of cylinder and calculate surface
area and Volume of cylinder.
Solution:
Main class:
package task_3;
import java.util.Scanner;
FIZA KHAN 7
8/3/2024 Object Oriented Programming
[Constructors in Java]
Rectangle class:
package task_3;
Rectangle(){
length=4;
breadth=5;
}
public Rectangle(double length,double breadth){
this.length=length;
this.breadth=breadth;
}
public void Display(){
System.out.println("Length: "+length);
System.out.println("Breadth: "+breadth);
}
}
Output:
FIZA KHAN 8
8/3/2024 Object Oriented Programming
[Constructors in Java]
Task # 04:
Design then implement a class to represent a Flight. A Flight has a flight
number, a source, a destination and a number of available seats. This should be
implemented using proper access modifier. The class should have:
a. A constructor to initialize the 4 instance variables. You have to shorten
the name of the source and the destination to 3 characters only if it is
longer than 3 characters by a call to the method in the ‘h’ part.
b. An overloaded constructor to initialize the flight number and the number
of available seats instance variables only.
(NOTE: Initialize the source and the destination instance variables to
empty string, i.e." ")
c. An overloaded constructor to initialize the flight number instance variable
only.
(NOTE: Initialize the source and the destination instance variables to
empty string; and the number of available seats to zero)
d. A method public void reserve(int numberOfSeats) to reserve seats on the
flight. (NOTE: You have to check that there is enough number of seats to
reserve)
e. A method public void cancel(int numberOfSeats) to cancel one or more
reservations
f. A toString method to easily return the flight information as follows:
Flight No: 1234
From: KAR
To: LAH
Available Seats: 18
FIZA KHAN 9
8/3/2024 Object Oriented Programming
[Constructors in Java]
return name.toUpperCase();
} else {
return name.substring(0,3).toUpperCase();
}
Write
} a test class for the Flight class you wrote. You should try to use all the
methods you wrote.
Solution:
Test class:
package task_4;
import java.util.Scanner;
FIZA KHAN 10
8/3/2024 Object Oriented Programming
[Constructors in Java]
Flight class:
package task_4;
FIZA KHAN 11
8/3/2024 Object Oriented Programming
[Constructors in Java]
return "Flight Number: " +fno+ "\nSource: " +src+ "\nDestination: " +des+
"\nAvailable Seats: " +aseats;
}
public void equals(Flight flight) {
System.out.println("Checking if the two flights have the same flight number or
not=========>");
if (this.fno == flight.fno) {
System.out.println("The flights have the same flight number.");
}
else {
System.out.println("The flights have different flight number.");
}
}
}
Output:
FIZA KHAN 12