Object Oriented Programming
ASSIGNMENT 1
NAME: M Mubashir Hassan (FA23-BAI-
034)
CLASS: BSAI
SEMESTER: 4th
SUBJECT: OOP
SUBMITTED TO: Miss Bushra Naz
DATED: 05-03-2025
COMSATS UNIVERSITY
ISLAMABAD,ISLAMABAD CAMPUS
Question #1
Source Code
import java.util.Scanner;
class Worker {
private String name;
private int hoursWorked;
private double hourlyWage;
public Worker(String name, int hoursWorked, double hourlyWage) {
this.name = name;
this.hoursWorked = hoursWorked;
this.hourlyWage = hourlyWage;
}
public double calculatePay() {
double pay = hoursWorked * hourlyWage;
if (hoursWorked > 40) {
pay += 0.5 * hourlyWage * (hoursWorked - 40);
}
return pay;
}
public void displayWorkerDetails() {
System.out.println("Name: " + name + "\tPay is: " + calculatePay());
System.out.println("\t\t\t****\t\t\t");
}
}
public class WorkerTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the details of worker1");
System.out.print("Enter Name: ");
String w1Name = input.next();
System.out.print("Enter Hours worked: ");
int w1Hours = input.nextInt();
System.out.print("Enter Hourly wage: ");
double w1Wage = input.nextDouble();
System.out.println("Enter the details of worker2");
System.out.print("Enter Name: ");
String w2Name = input.next();
System.out.print("Enter Hours worked: ");
int w2Hours = input.nextInt();
System.out.print("Enter Hourly wage: ");
double w2Wage = input.nextDouble();
Worker worker1 = new Worker(w1Name, w1Hours, w1Wage);
Worker worker2 = new Worker(w2Name, w2Hours, w2Wage);
worker1.displayWorkerDetails();
worker2.displayWorkerDetails();
}
}
Output
Question # 2
Source Code
class QuadraticEquation {
private double a, b, c;
public QuadraticEquation() {
this.a = 1;
this.b = 1;
this.c = 1;
}
public QuadraticEquation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public void setValues(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double getDiscriminant() {
return (b * b) - (4 * a * c);
}
public boolean checkIfDiscriminantIsGreaterThan100() {
return getDiscriminant() > 100;
}
public void display() {
System.out.println("Quadratic Equation: " + a + "x^2 + " + b + "x + " + c);
System.out.println("Discriminant: " + getDiscriminant());
}
}
class Main {
public static void main(String[] args) {
QuadraticEquation equation1 = new QuadraticEquation(2, 5, 3);
equation1.display();
System.out.println("Is discriminant > 100? "
+ equation1.checkIfDiscriminantIsGreaterThan100());
QuadraticEquation equation2 = new QuadraticEquation(10, 5, 1);
equation2.display();
System.out.println("Is discriminant > 100? "
+ equation2.checkIfDiscriminantIsGreaterThan100());
}
}
Output
Question # 3
Source Code
class SavingsAccount {
private static double annualInterestRate;
private double savingsBalance;
public SavingsAccount(double savingsBalance) {
this.savingsBalance = savingsBalance;
}
public void calculateMonthlyInterest() {
double interest = (savingsBalance * annualInterestRate) / 12;
savingsBalance += interest;
}
public static void modifyInterestRate(double newRate) {
annualInterestRate = newRate;
}
public void displayBalance() {
System.out.println("Current Balance: " + savingsBalance);
}
}
public class SavingsAccountTest {
public static void main(String[] args) {
SavingsAccount saver1 = new SavingsAccount(2000.00);
SavingsAccount saver2 = new SavingsAccount(3000.00);
SavingsAccount.modifyInterestRate(0.03); // 3% Interest Rate
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
System.out.println("After 1st month:");
saver1.displayBalance();
saver2.displayBalance();
SavingsAccount.modifyInterestRate(0.04); // 4% Interest Rate
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
System.out.println("After 2nd month:");
saver1.displayBalance();
saver2.displayBalance();
}
}
Output