0% found this document useful (0 votes)
6 views15 pages

Fiza Oop 4

This document outlines the lab assignments for the Object Oriented Programming course at Bahria University, Karachi Campus, for Spring 2024. It includes tasks related to Java programming concepts such as constructors, class implementation, and object manipulation. The document also provides detailed solutions for various programming tasks, including creating a Computer class, Cylinder calculations, Rectangle initialization, and a Flight class implementation.

Uploaded by

fizajamshed4
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)
6 views15 pages

Fiza Oop 4

This document outlines the lab assignments for the Object Oriented Programming course at Bahria University, Karachi Campus, for Spring 2024. It includes tasks related to Java programming concepts such as constructors, class implementation, and object manipulation. The document also provides detailed solutions for various programming tasks, including creating a Computer class, Cylinder calculations, Rectangle initialization, and a Flight class implementation.

Uploaded by

fizajamshed4
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/ 15

Bahria University,

Karachi Campus

Course: CSC-210 - Object Oriented Programming


Term: Spring 2024, Class: BSE- 2(C)

Submitted By:

Fiza Khan 89219


(Name) (Reg. No.)

Submitted To:

Engr. Mahawish/Engr. Saniya Sarim

Signed Remarks: Score:


INDEX
SNO DATE LAB LAB OBJECTIVE SIGN
NO
1 16/2/2024 1 Introduction To Java Programming Language

2 23/2/2024 2 Implementation Of Class and Object In OOP

3 1/3/2024 3 Access Modifiers in Java

4 8/3/2024 4 Constructors in Java


Bahria University,
Karachi Campus

LAB EXPERIMENT NO.


4
LIST OF TASKS
TASK NO OBJECTIVE
1 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.

2 Use Constructor to set the radius and height of cylinder and calculate surface area
and Volume of cylinder.

3 Use constructor overloading to initialize a rectangle of length 4 and breadth 5 for


using custom parameters.

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;

public class Constructors {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
Computer c=new Computer();
System.out.println("____________COMPUTER SPECIFICATIONS____________");
c.GetValues();
System.out.println("-----------------------------------------------");
c.Display();
System.out.println("-----------------------------------------------");
String name,type,processor;
double ram,hdd;
System.out.println("Do you want to update your specifications? Y/N");
char ans=input.next().charAt(0);
if (ans=='Y'|| ans=='y') {
System.out.println("How many vaules do you want to update? (1-5)");
int rep1=input.nextInt ();
input.nextLine();
switch (rep1) {
case 1:
System.out.println("Enter the updated name:");
name = input.next();
c = new Computer(name, c.getType(), c.getProcessor(), c.getRam(),
c.getHdd());
System.out.println("Data updated!");
System.out.println("----------------------------------------------
-");
c.Display();

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 class Computer {


Scanner input=new Scanner(System.in);
private String name;
private String type;
private String processor;
private double ram;
private double hdd;

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

public String getType() {


return type;
}

public String getProcessor() {


return processor;
}

public double getRam() {


return ram;
}

public double getHdd() {


return hdd;
}

public void Display ( ) {


System.out.println("Name: "+name);
System.out.println("Type: "+type);
System.out.println("Processor: "+processor);
System.out.println("Ram: "+ram);
System.out.println("HDD: "+hdd);
}

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;

public class Task_2 {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
System.out.println("_________CYLINDER_________");
System.out.println("Enter the value of radius:");
double radius=input.nextDouble();
System.out.println("Enter the value of height:");
double height=input.nextDouble();
Cylinder c1=new Cylinder(radius,height);
System.out.println("--------------------------");
c1.Display();
System.out.println("--------------------------");
System.out.println("Surface Area: "+c1.SA());
System.out.println("Volume: "+c1.Volume());
}
}

Cylinder class:
package task_2;

public class Cylinder {


private double radius;
private double height;

public Cylinder(double radius,double height){


this.radius=radius;
this.height=height;
}
public void Display(){
System.out.println("Radius: "+radius);
System.out.println("Height: "+height);
}
public double SA(){
return (2*3.142*radius*height)+(2*3.142*Math.pow(radius, 2));
}
public double Volume(){
return 2*Math.pow(radius, 2)*height;
}
}

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;

public class Task_3 {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
Rectangle r1=new Rectangle();
System.out.println("___________OLD VALUES__________");
r1.Display();
System.out.println("===============================");
System.out.println("Enter the new value of length:");
double length=input.nextDouble();
System.out.println("Enter the new value of breadth:");
double breadth=input.nextDouble();
Rectangle r2=new Rectangle(length,breadth);
System.out.println("___________NEW VALUES__________");
r2.Display();
System.out.println("===============================");
}

FIZA KHAN 7
8/3/2024 Object Oriented Programming
[Constructors in Java]

Rectangle class:
package task_3;

public class Rectangle {


private double length;
private double breadth;

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

g. An equals method to compare 2 flights.


(NOTE: 2 Flights considered being equal if they have the same flight
number)
h. The following method:

FIZA KHAN 9
8/3/2024 Object Oriented Programming
[Constructors in Java]

private String shortAndCapital (String name) {


if (name.length() <= 3) {

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;

public class Test {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
Flight f1=new Flight(8924,"Karachi","Dubai",200);
Flight f2=new Flight(8177,120);
Flight f3=new Flight(7521);
System.out.println("_______FLIGHT 1_______");
System.out.println(f1);
System.out.println("======================");
System.out.println("_______FLIGHT 2_______");
System.out.println(f2);
System.out.println("======================");
System.out.println("_______FLIGHT 3_______");
System.out.println(f3);
System.out.println("======================");
System.out.println("Enter the number of seats for reservation: ");
int rseats=input.nextInt();
System.out.println("--------------------------------------------");
f1.reserve(rseats);
System.out.println("Enter the number of seats for cancellation: ");
int cseats=input.nextInt();
System.out.println("--------------------------------------------");
f2.cancel(cseats);
System.out.println("--------------------------------------------");
f1.equals(f2);
}

FIZA KHAN 10
8/3/2024 Object Oriented Programming
[Constructors in Java]

Flight class:
package task_4;

public class Flight {


private int fno;
private String src;
private String des;
private int aseats;

public Flight(int fno,String src,String des,int aseats){


this.fno=fno;
this.src=shortAndCapital(src);
this.des=shortAndCapital(des);
this.aseats=aseats;
}
private String shortAndCapital (String name) {
if (name.length() <= 3) {
return name.toUpperCase();
}
else {
return name.substring(0,3).toUpperCase();
}
}
public Flight(int fno,int aseats){
this.fno=fno;
this.src="";
this.des="";
this.aseats=aseats;
}
public Flight(int fno){
this.fno=fno;
this.src="";
this.des="";
this.aseats=0;
}
public void reserve(int numberOfSeats){
if(numberOfSeats<=aseats){
aseats-=numberOfSeats;
System.out.println(numberOfSeats+" seats have been reserved for flight
"+fno);
}
else{
System.out.println("No seats are available");
}
}
public void cancel(int numberOfSeats){
if(numberOfSeats>0){
aseats+=numberOfSeats;
System.out.println(numberOfSeats+" seats have been canceled for flight
"+fno);
}
else{
System.out.println("Invalid number of seats");
}
}
public String toString(){

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

You might also like