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

Asgn 1 Object Oriented Programming

This is the assignment of subject oop

Uploaded by

sparkleg83
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Asgn 1 Object Oriented Programming

This is the assignment of subject oop

Uploaded by

sparkleg83
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ABBOTTABAD UNIVERSITY OF SCIENCE & TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE


Assigment No 1:

Submitted to: Mam Kinza Khurshid

Class: BSSE 2nd (B)

Session: Spring 2024

Subject: Object Oriented Programming


Submitted by:
Hamza Yousaf (232476)
Q1. Java Program for Integer Operations:

class IntegerOperations {
private int num1;
private int num2;

public IntegerOperations(int num1, int num2) {


this.num1 = num1;
this.num2 = num2;
}

public void calculateSum() {


int sum = num1 + num2;
System.out.println("Sum: " + sum);
}

public void calculateDifference() {


int difference = num1 - num2;
System.out.println("Difference: " + difference);
}

public void calculateProduct() {


int product = num1 * num2;
System.out.println("Product: " + product);
}

public void calculateAverage() {


double average = (double)(num1 + num2) / 2;
System.out.println("Average: " + average);
}

public void calculateDistance() {


int distance = Math.abs(num1 - num2);
System.out.println("Distance: " + distance);
}

public void findMaximum() {


int max = Math.max(num1, num2);
System.out.println("Maximum: " + max);
}
}

public class Main {


public static void main(String[] args) {
int input1 = 43;
int input2 = 8;

IntegerOperations operations = new IntegerOperations(input1,


input2);

operations.calculateSum();
operations.calculateDifference();
operations.calculateProduct();
operations.calculateAverage();
operations.calculateDistance();
operations.findMaximum();
}
}

This program utilizes object-oriented principles by defining a class


IntegerOperations to encapsulate the operations on integers. Each
operation is implemented as a separate method within this class. The
Main class is used to test the functionality of the IntegerOperations
class by providing test data and invoking its methods.
Q2. Cube of Numbers in Java:

class CubeCalculator {
public void displayCubes(int n) {
for (int i = 1; i <= n; i++) {
int cube = calculateCube(i);
System.out.println("Cube of " + i + ": " + cube);
}
}

private int calculateCube(int num) {


return num * num * num;
}
}

public class Main {


public static void main(String[] args) {
int givenInteger = 5;
CubeCalculator calculator = new CubeCalculator();
calculator.displayCubes(givenInteger);
}
}
This program utilizes object-oriented principles by defining a class
CubeCalculator to encapsulate the logic for calculating cubes of
numbers. The displayCubes method iterates from 1 up to the given
integer n and calculates the cube of each number using the
calculateCube method. The Main class is used to test the functionality
of the CubeCalculator class by providing a test value for the given
integer.

You might also like