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

Lab04 in

The document describes an object-oriented programming lab assignment involving fractions. It includes the code for a Fraction class that represents fractions with numerator and denominator fields. Methods are included for arithmetic operations like addition and subtraction that handle things like common denominators. The main method demonstrates using the class by creating Fraction objects and performing operations on them.

Uploaded by

muhammadasimf23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab04 in

The document describes an object-oriented programming lab assignment involving fractions. It includes the code for a Fraction class that represents fractions with numerator and denominator fields. Methods are included for arithmetic operations like addition and subtraction that handle things like common denominators. The main method demonstrates using the class by creating Fraction objects and performing operations on them.

Uploaded by

muhammadasimf23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Object-Oriented Programming Lab

CS123

Lab No: 04(In)

Student Name Muhammad Asim Hanif


Registration # F23608048
Semester 2nd
Batch Fall 2023
Instructor Ms. Noushin Saba
Lab Engineer Asfand Yar
Department Software Engineering
Date 14-03-2024
Practice 01:

CODE
CLASS CODE
public class Fraction{
private int num;
private int dem;

public Fraction(){
num=0;
dem=0;
}

public Fraction(int num){


this.num=num;
dem=1;
}

public Fraction(int num,int dem){


if(dem==0){
System.out.println("Dem cannot be 0.");
System.exit(1);
}
setNumerator(num);
setDenominator(dem);
}

public void setNumerator(int n){


this.num=n;
}

public void setDenominator(int n){


this.dem=n;
}

public int getNumerator(){


return num;
}

public int getDenominator(){


return dem;
}

public String tostring(){


return getNumerator()+"/"+ getDenominator();
}

private static int lcm(int a, int b){


return (a+b)/gcd(a,b);
}
private static int gcd(int a, int b){
while(b!=0){
int temp=b;
b= a%b;
a=temp;
}
return a;
}
public static Fraction add(Fraction f1, Fraction f2){
int commdeno=lcm(f1.dem, f2.dem);
int newnum=(commdeno/f1.dem)*f1.num+(commdeno/f2.dem)*f2.num;
Fraction result=new Fraction(newnum,commdeno);
return result;
}

public static Fraction less(Fraction f1, Fraction f2){


int commdeno=lcm(f1.dem, f2.dem);
int newnum=(commdeno/f1.dem)*f1.num-(commdeno/f2.dem)*f2.num;
Fraction result=new Fraction(newnum,commdeno);
return result;
}

public static void main(){


Fraction f=new Fraction(5,4);
System.out.println("Numerator" +f.getNumerator());
System.out.println("Denominator" +f.getDenominator());
Fraction f1= new Fraction(4,5);
Fraction f2= new Fraction(7,6);
Fraction res=Fraction.add(f1,f2);
System.out.println("Fraction: " +res.tostring());
System.out.println("Sum: " +res.add(f1,f2));
System.out.println("Less: " +res.less(f1,f2));
}
}

Screenshot

Explanation
1. Fraction Representation:
 The Fraction class represents fractions with numerator and
denominator fields.
 Constructors allow for creating fractions with different
initializations.
2. Arithmetic Operations:
 Static methods (add, less) handle addition and subtraction of
fractions.
 Helper functions (lcm, gcd) assist in calculating the necessary
values.
3. Error Handling:
 Error handling prevents division by zero when setting the
denominator.
 Invalid inputs trigger an error message and graceful program exit.
4. Demonstration:
 The main method showcases fraction creation and arithmetic
operations.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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

CODE
CLASS CODE
import java.lang.Math;
import java.util.Scanner;
public class AreaCalculator {
public int area(int side) {
int area = side * side;
return area;
}

public int area(int length, int width) {


return length * width;
}

public double area(double r) {


double a = 3.14 * (r * r);
return a;
}

public double area(int side1, int side2, int side3) {


double s = (side1 + side2 + side3) / 2;
double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
return area;
}

public double area(double base, double height) {


return base * height;
}

public double area(double base1, double base2, double height) {


double area = ((base1 * base2) / 2) * height;
return area;
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
AreaCalculator calc = new AreaCalculator();
System.out.println("Select the shape: ");
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 = input.nextInt();
switch (choice) {
case 1:
System.out.println("Area of Square: " + calc.area(5));
break;
case 2:
System.out.println("Area of Rectangle: " + calc.area(5, 6));
break;
case 3:
System.out.println("Area of Circle: " + calc.area(5));
break;
case 4:
System.out.println("Area of Triangle: " + calc.area(5, 6, 7));
break;
case 5:
System.out.println("Area of Parallelogram: " + calc.area(3.2, 5.1));
break;
case 6:
System.out.println("Area of Trapezoid: " + calc.area(5.6, 6.7, 7.8));
break;
default:
System.out.println("Invalid choice");
}
}
}

Screenshot

Explanation

1. The AreaCalculator class provides methods to calculate the areas of


various geometric shapes such as square, rectangle, circle, triangle,
parallelogram, and trapezoid.
2. It uses method overloading to handle different parameter combinations
for calculating area, including side lengths, radius, and base/height
dimensions.
3. The main method prompts the user to select a shape, reads the choice, and
then calculates and displays the area based on the chosen shape using the
appropriate method from the AreaCalculator class.
4. Input validation is implemented using a switch statement to handle
invalid choices and ensure the program executes smoothly.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Task 03:
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
CODE
CLASS CODE
public class Bank{
private int acc;
private int balance;
public Bank(){
this.acc = 123;
this.balance = 0;
}
public Bank(int accountNumber){
this.acc = accountNumber;
this.balance = 0;
}
public Bank(int accountNumber, int balance){
this.acc = accountNumber;
this.balance = balance;
}
public void depositAmount(int amount){
balance+=amount;
System.out.println("New Balance" +balance);
}
public void withdrawAmount(int amount){
if(balance>=amount){
balance-=amount;
System.out.println("Remaining Balance" +balance);
}
else{
System.out.println("Insufficient Balance");
System.out.println("Current Balance" +balance);
}
}
public int getAccountNumber(){
return acc;
}
public int checkBalance(){
return balance;
}
public static void main(){
Bank b1 = new Bank(234);
Bank b2 = new Bank(234,100);
b1.depositAmount(400);
b1.withdrawAmount(200);
b2.depositAmount(400);
b2.withdrawAmount(200);
}
}

Screenshot

Explanation

1. The Bank class represents a simple banking system with methods to


deposit and withdraw money, check balance, and retrieve account
number.
2. It has multiple constructors to initialize account number and balance, with
default values set if not provided.
3. The depositAmount method increases the balance by the specified
amount, while withdrawAmount decreases it if sufficient funds are
available.
4. In the main method, two bank objects b1 and b2 are created with different
constructors, and deposit and withdrawal operations are performed on
them to demonstrate the functionality.

You might also like