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

Lab 3 OOP

OOP codes

Uploaded by

BABLU MUSIX
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)
6 views

Lab 3 OOP

OOP codes

Uploaded by

BABLU MUSIX
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/ 6

ACTIVITY 1

public class Rectangle {


// Public member variables
public int length, width;

// Default constructor
public Rectangle() {
length = 5;
width = 2;
}

// Parameterized constructor
public Rectangle(int l, int w) {
length = l;
width = w;
}

// Method to calculate area


public int calculateArea() {
return (length * width);
}
}

public class Runner {


public static void main(String[] args) {
// First rectangle with default constructor
Rectangle rect1 = new Rectangle();
System.out.println("Area of rect1: " + rect1.calculateArea());

// Second rectangle with parameterized constructor


Rectangle rect2 = new Rectangle(10, 20);
System.out.println("Area of rect2: " + rect2.calculateArea());
}
}

ACTIVITY 2
public class Time {

int hours;
int minutes;
int seconds;

public Time() {
hours = 0;
minutes = 0;
seconds = 0;
}

public Time(int h, int m, int s) {


hours = h;
minutes = m;
seconds = s;
}

public void setHours(int h) {


if (h >= 0 && h < 24) {
hours = h;
} else {
System.out.println("Invalid hour input");
}
}

public void setMinutes(int m) {


if (m >= 0 && m < 60) {
minutes = m;
} else {
System.out.println("Invalid minute input");
}
}

public void setSeconds(int s) {


if (s >= 0 && s < 60) {
seconds = s;
} else {
System.out.println("Invalid second input");
}
}

public int getHours() {


return hours;
}

public int getMinutes() {


return minutes;
}

public int getSeconds() {


return seconds;
}

public void Display() {


System.out.println(hours + ":" + minutes + ":" + seconds);
}
}

public class Main {


public static void main(String[] args) {

Time t1 = new Time();

t1.setHours(0);
t1.setMinutes(0);
t1.setSeconds(0);

t1.Display();

Time t2 = new Time(11, 59, 59);


t2.Display();
}
}

ACTIVITY 3
public class Point {
private int x;
private int y;

public Point() {
x = 0;
y = 0;
}

public Point(int a, int b) {


x = a;
y = b;
}

public void setX(int a) {


x = a;
}

public void setY(int b) {


y = b;
}

public int getX() {


return x;
}

public int getY() {


return y;
}

public void display() {


System.out.println("x coordinate = " + x + " y coordinate = " + y);
}

public void movePoint(int a, int b) {


x = x + a;
y = y + b;
System.out.println("x coordinate after moving = " + x + " y coordinate after moving
= " + y);
}
}

public class Runner {


public static void main (String[] args) {

Point p1 = new Point();


p1.setX(10);
p1.setY(7);
p1.display();

Point p2 = new Point(10,11);


p2.movePoint(2,3);
p2.display();
}
}

ACTIVITY 4
public class Student {
private String name;
private int[] result_array = new int[5];

public Student(String name, int[] arr) {


this.name = name;
this.result_array = arr;
}
public double Average() {
double sum = 0;
for (int i = 0; i < result_array.length; i++) {
sum += result_array[i];
}
return sum / result_array.length;
}

public String getName() {


return name;
}
}

public class Runner {


public static void main(String[] args) {

int[] marks1 = {85, 90, 78, 92, 88};


int[] marks2 = {80, 85, 84, 86, 90};

Student student1 = new Student("Awais", marks1);


double avg1 = student1.Average();
System.out.println("Name = " + student1.getName() + " & Average: " + avg1);

Student student2 = new Student("Shahzaib", marks2);


double avg2 = student2.Average();
System.out.println("Name = " + student2.getName() + " & Average: " + avg2);

if (avg1 > avg2) {


System.out.println(student1.getName() + " has a higher average.");
} else if (avg2 > avg1) {
System.out.println(student2.getName() + " has a higher average.");
} else {
System.out.println("Both students have the same average.");
}
}
}

ACTIVITY 5
public class HotDogStand {
private int standID;
private int hotDogsSold;

public HotDogStand(int id) {


this.standID = id;
this.hotDogsSold = 0;
}

public void justSold() {


hotDogsSold++;
}

public int getHotDogsSold() {


return hotDogsSold;
}

public int getStandID() {


return standID;
}
}

public class Main {


public static void main(String[] args) {
HotDogStand stand1 = new HotDogStand(1001);
HotDogStand stand2 = new HotDogStand(1002);
HotDogStand stand3 = new HotDogStand(1003);

stand1.justSold();
stand2.justSold();
stand2.justSold();
stand3.justSold();
stand3.justSold();
stand3.justSold();

System.out.println("Hot Dog Stand ID: " + stand1.getStandID() + " sold " +


stand1.getHotDogsSold() + " hot dogs.");
System.out.println("Hot Dog Stand ID: " + stand2.getStandID() + " sold " +
stand2.getHotDogsSold() + " hot dogs.");
System.out.println("Hot Dog Stand ID: " + stand3.getStandID() + " sold " +
stand3.getHotDogsSold() + " hot dogs.");
}
}

You might also like