0% found this document useful (0 votes)
6 views

Lab # 4 Journal [OOP]

The document outlines several lab tasks related to object-oriented programming, including the design of a Student Management System, an Area Calculator, a Bank class, and an Online Shopping System. Each task involves creating classes with constructor and method overloading to manage various functionalities, such as displaying student details, calculating areas of geometric shapes, managing bank transactions, and calculating total bills for products. Java code implementations for each task are provided to demonstrate the required functionalities.

Uploaded by

apex.wali3
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 views

Lab # 4 Journal [OOP]

The document outlines several lab tasks related to object-oriented programming, including the design of a Student Management System, an Area Calculator, a Bank class, and an Online Shopping System. Each task involves creating classes with constructor and method overloading to manage various functionalities, such as displaying student details, calculating areas of geometric shapes, managing bank transactions, and calculating total bills for products. Java code implementations for each task are provided to demonstrate the required functionalities.

Uploaded by

apex.wali3
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/ 14

Student Name | Waleed Khalid | F24610019

Object Oriented Programming Lab


Lecturer Muntaha Noor

NUTECH UNIVERSITY ISLAMABAD


LAB#4 - Journal

March 17, 2025


LAB TASK # 1: Design a Student Management
System where:
1. Constructor Overloading is used to create student
objects with different sets of details:
Only name and ID
Name, ID, and age
Name, ID, age, and GPA
2.Method Overloading is used to display student details
in different ways:
Without parameters (shows all details)
With only ID (displays only the student ID)
With ID and Name (displays only ID and Name)
Write a Java program to implement this system…
Program:
public class StudentManagementSystem {
static class Student {
private String name; //Instance Varable
private String id;
private int age;
private double gpa;

public Student(String name, String id) { // Constructor for name and


ID
this.name = name;
this.id = id;
this.age = 0;
this.gpa = 0.0;
}

public Student(String name, String id, int age) { // Constructor for


name, ID, and age
this.name = name;
this.id = id;
this.age = age;
}

public Student(String name, String id, int age, double gpa) { //


Constructor for name, ID, age, and GPA
this.name = name;
this.id = id;
this.age = age;
this.gpa = gpa;
}

public void display() { // Method to display all details


System.out.println("Name: \n" + name + ", ID: " + id +
", Age: " + age +
", GPA: " + gpa);
}

public void display(String id) { // Method to display only ID


System.out.println("ID: " + id);
}

public void display(String id, String name) { // Method to display ID


and Name
System.out.println("ID: " + id + ", Name: " +
name); //println to print the details
}
}

public static void main(String[] args) { //Main Funtion


Student student1 = new Student("Waleed Khalid", "20"); //object
with Constructor
Student student2 = new Student("Aimen", "08", 19);
Student student3 = new Student("Nafees ", "01", 22, 3.5);
student1.display(); //method to display all Details
student2.display(student2.id); //method to display student id
student3.display(student3.id, student3.name);//Method to display student
id and name
}
}

Output:

LAB TASK # 2:
1. Create a class AreaCalculator
2. To compute the area of various geometric shapes including
squares, rectangles, circles, triangles, parallelograms, and
trapezoids
3. The class should overload calculateArea methods with
different parameters corresponding to the dimensions of each
shape
4. The program should prompt the user to choose a shape for
which the area needs to be calculated
5. It should request the necessary inputs (such as side lengths,
radius, base, and height) from the user
6. Based on the user's choice, the appropriate method from the
class should be invoked to compute the area

Program:
import java.util.Scanner;

public class AreaCalculator {

public double calculateArea(double side) { // Method to calculate area of a


square
return side * side;
}

public double calculateArea(double length, double width) { // Method to


calculate area of a rectangle
return length * width;
}

public double calculateAreaCircle(double radius) { // Method to calculate


area of a circle
return Math.PI * radius * radius;
}

public double calculateAreaTriangle(double base, double height) { //


Method to calculate area of a triangle
return 0.5 * base * height;
}

public double calculateAreaParallelogram(double base, double height)


{ // Method to calculate area of a parallelogram
return base * height;
}
public double calculateAreaTrapezoid(double base1, double base2, double
height) { // Method to calculate area of a trapezoid
return 0.5 * (base1 + base2) * height;
}

public static void main(String[] args) { // Main Funtion


Scanner scanner = new Scanner(System.in); //Scanner Object to get input
from user
AreaCalculator calculator = new AreaCalculator(); //Object for the
Calculator and with Default Parameters which will Set all data input to 0

System.out.println("Choose a shape to calculate the


area:"); //Menu for Choices to find area for multiple shapes
System.out.println("1. Square");
System.out.println("2. Rectangle");
System.out.println("3. Circle");
System.out.println("4. Triangle");
System.out.println("5. Parallelogram");
System.out.println("6. Trapezoid");
int choice = scanner.nextInt(); //Getting Input From User

double area = 0;

switch (choice) { //Used Switch to get the choice of user and call the
different method to calculate the area of the shape
case 1:
System.out.print("Enter the side length of the square:
"); //case 1 to get the side of the square
double side = scanner.nextDouble();
area = calculator.calculateArea(side);
break;
case 2:
System.out.print("Enter the length and width of the rectangle:
"); //case 2 to get the length and width of the rectangle
double length = scanner.nextDouble();
double width = scanner.nextDouble();
area = calculator.calculateArea(length, width);
break;
case 3:
System.out.print("Enter the radius of the circle:
"); //case 3 to get the radius of the circle
double radius = scanner.nextDouble();
area = calculator.calculateAreaCircle(radius);
break;
case 4:
System.out.print("Enter the base and height of the triangle:
"); //case 4 to get the base and height of the triangle
double baseTriangle = scanner.nextDouble();
double heightTriangle = scanner.nextDouble();
area = calculator.calculateAreaTriangle(baseTriangle,
heightTriangle);
break;
case 5:
System.out.print("Enter the base and height of the parallelogram:
"); //case 5 to get the base and height of the parallelogram
double baseParallelogram = scanner.nextDouble();
double heightParallelogram = scanner.nextDouble();
area = calculator.calculateAreaParallelogram(baseParallelogram,
heightParallelogram);
break;
case 6:
System.out.print("Enter the lengths of the two bases and the
height of the trapezoid: "); //case 6 to get the base and height of the
trapezoid
double base1 = scanner.nextDouble();
double base2 = scanner.nextDouble();
double heightTrapezoid = scanner.nextDouble();
area = calculator.calculateAreaTrapezoid(base1, base2,
heightTrapezoid);
break;
default:
System.out.println("Invalid choice."); //default case to print
if the user enters any other number
return;
}

System.out.println("The area is: " + area); //println to


print the area of the shape

}
}

Output:
Post LAB TASK # 1:
Create a class Bank with:
Attributes accountNumber and balance
Three overloaded constructors
No argument constructor
Parameterized that takes an accountNumber as a parameter
and initializes balance to 0.0
Parameterized that takes both an accountNumber and balance
as parameters
Methods
To deposit amount deposit(amount)
To withdraw amount withdraw(amount), withdraw method
should check whether
balance is greater than withdrawn amount, if not then it should
display
"Insufficient funds. Cannot withdraw”
To check balance checkBalance()
Get method to get accountNumber
Main method to demonstrates how to create bank objects using
different combinations of constructors and perform operations
such as depositing, withdrawing, checking the balance.

Program:
import java.util.Scanner; // Importing Scanner for user input

public class Bank {

private String accountNumber;


private double balance;

public Bank() { // No-argument constructor


this.accountNumber = "";
this.balance = 0.0;
}

public Bank(String accountNumber) { // Constructor with accountNumber


this.accountNumber = accountNumber;
this.balance = 0.0;
}

public Bank(String accountNumber, double balance) { // Constructor with


accountNumber and balance
this.accountNumber = accountNumber;
this.balance = balance;
}

public void deposit(double amount) { // Method to deposit amount


balance += amount;
}

public void withdraw(double amount) { // Method to withdraw amount


if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient funds. Cannot withdraw.");
}
}

public double checkBalance() { // Method to check balance


return balance;
}

public String getAccountNumber() { // Getter for accountNumber


return accountNumber;
}

public static void main(String[] args) { // Main method to demonstrate


functionality

Bank bank1 = new Bank(); // Creating bank objects using


different constructors
Bank bank2 = new Bank("12345");
Bank bank3 = new Bank("67890", 1000.0);

// Performing operations

bank1.deposit(500);
System.out.println("Account Number: " + bank1.getAccountNumber() + ",
Balance: " + bank1.checkBalance()); //calling methods

bank2.deposit(300); //calling deposit method and passing argument


bank2.withdraw(100);
System.out.println("Account Number: " + bank2.getAccountNumber() + ",
Balance: " + bank2.checkBalance());
bank3.withdraw(1500); // Should show insufficient funds (for testing)
bank3.withdraw(500);
System.out.println("Account Number: " + bank3.getAccountNumber() + ",
Balance: " + bank3.checkBalance());
}
}

Output:

Post LAB TASK # 2: Develop an Online Shopping


System where:
1. Constructor Overloading is used to create product
objects with different details:
Only Product Name and Price
Product Name, Price, and Category
Product Name, Price, Category, and Stock Quantity
2. Method Overloading is used to calculate the total bill
in different scenarios:
For a given quantity of a product
For a given quantity with a discount percentage
For a given quantity with discount and additional delivery
charges
Write a Java program to implement this system, ensuring
proper object-oriented design and real-world
applicability.
Program:
public class OnlineShoppingSystem {
// Product class definition
static class Product {
private String productName;
private double price;
private String category;
private int stockQuantity;

public Product(String productName, double price) { // Constructor for


Product Name and Price
this.productName = productName;
this.price = price;
this.category = "General"; // Default category
this.stockQuantity = 0; //
Default stock quantity
}

public Product(String productName, double price, String category) { //


Constructor for Product Name, Price, and Category
this.productName = productName;
this.price = price;
this.category = category;
this.stockQuantity = 0; // Default stock quantity
}
public Product(String productName, double price, String category, int
stockQuantity) { // Constructor for Product Name, Price, Category, and
Stock Quantity
this.productName = productName;
this.price = price;
this.category = category;
this.stockQuantity = stockQuantity;
}

public double calculateTotalBill(int quantity) { //


Method to calculate total bill for given quantity
return price * quantity;
}

public double calculateTotalBill(int quantity, double discountPercentage)


{ // Method to calculate total bill with discountv
double total = calculateTotalBill(quantity);
return total - (total * discountPercentage / 100);
}

public double calculateTotalBill(int quantity, double discountPercentage,


double deliveryCharges) { // Method to calculate total bill with
discount and delivery charges
double total = calculateTotalBill(quantity, discountPercentage);
return total + deliveryCharges;
}
}

public static void main(String[] args) { // Main method to demonstrate


functionality
Product product1 = new Product("Laptop", 1200.00); //
Creating product objects using different constructors
Product product2 = new Product("Smartphone", 800.00, "Electronics");
Product product3 = new Product("Headphones", 150.00, "Accessories", 50);

System.out.println("Total bill for 2 Laptops: $" +


product1.calculateTotalBill(2)); // Demonstrating total bill
calculations
System.out.println("Total bill for 3 Smartphones with 10% discount: $" +
product2.calculateTotalBill(3, 10));
System.out.println("Total bill for 5 Headphones with 5% discount and $10
delivery: $" + product3.calculateTotalBill(5, 5, 10));
}
}
Output:

You might also like