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

Oop Lab#2

The document discusses object oriented programming concepts through examples of classes and objects. It contains the definitions of multiple classes like Distance, Fraction, Date etc. and discusses how to create objects of these classes and use their methods to perform operations on the objects.

Uploaded by

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

Oop Lab#2

The document discusses object oriented programming concepts through examples of classes and objects. It contains the definitions of multiple classes like Distance, Fraction, Date etc. and discusses how to create objects of these classes and use their methods to perform operations on the objects.

Uploaded by

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

OOP

LAB Assignment # 2

11/3/2023

Muhammad Nouman
FS22-BDS-028
Lab 4
Task # 1
class Distance {
int feet;
int inches;

Distance() {
feet = 0;
inches = 0;
}

Distance(int a, int b) {
feet = a;
inches = b;
}

void setfeet(int x) {
feet = x;
}

void setinches(int y) {
inches = y;
}

int getfeet() {
return feet;
}

int getinches() {
return inches;
}

void display() {
System.out.println("feet = " + feet + " inches = " + inches);
}

// Corrected add method


public Distance add(Distance a) {
//Distance d1 = new Distance(this.feet + a.feet, this.inches + a.inches);
// return d1;
return feet+ a.feet, this.inches + a.inches;
}
}

public class Task1 {


public static void main(String[] args) {
Distance d1 = new Distance(12, 13);
Distance d2 = new Distance(12, 13);

// Distance d3 = new Distance();


// d3=d1.add(d2); // Call the add method on d1 with d2 as the argument
Distance d3=d1.add(d2);
d3.display();
}
}

Task# 2
class Book{
String aurthor;
String[] chapternames=new String[10];

Book(){
//aurthor="nouman";
//chapternames="a","g","u";
}

Book(String a, String [] b){


aurthor=a;
chapternames=b;
}

boolean compareAurthor(Book a){


boolean b= aurthor==a.aurthor;
return b;
}

Book compareChapterNmaes(Book b){

int count=0;
for(int i=0;i<=chapternames.length; i++){
//if(chapternames!=null){
count++;

//}
}

int count2=0;
for(int j=0; j<=chapternames.length; j++){
//if(b.chapternames!=null){
count2++;
//}
}

if (count>count2){
return this;
}
//returns the current Book object (this) if it has
//more non-null chapter names, and it returns the other
// Book object (b) if b has more non-null chapter names.
else{
return b;
}
}

public void aurthorShow(){


System.out.println("aurthor : "+aurthor);
}

public class Task2{


public static void main(String[] args) {
String [] book1chapters={"a","b","c", "d","e"};
String [] book2chapters={"a","x","c", "v","w","k"};
Book b1=new Book("nouman",book1chapters);
Book b2=new Book("Talha",book2chapters);
System.out.println(b1.compareAurthor(b2));
Book b3=(b1.compareChapterNmaes(b2));
b3.aurthorShow();
}
}

Task# 3
class Fraction {
private int a;
private int b;

Fraction() {
a = 0;
b = 1;
}

Fraction(int a, int b) {
this.a = a;
this.b = b;

void setA(int a) {
this.a = a;
}

void setB(int b) {
if (b != 0) {
this.b = b;
} else {

this.b = 1;
}
}

int getA() {
return a;
}

int getB() {
return b;
}

void display() {
System.out.println(a + "/" + b);
}

boolean equals(Fraction f) {
///////////shortcut///////////
return this.a == f.a && this.b == f.b;
// boolean x= this.a==f.a && this.b==f.b;
//return x;

}
}

public class Task3 {


public static void main(String[] args) {
Fraction f1 = new Fraction(1, 2);
Fraction f2 = new Fraction(3, 2);
Fraction f3 = new Fraction(1, 2);
f1.display();
f2.display();
f3.display();

if (f1.equals(f3)) {
System.out.println("f1 and f3 are equal");
} else {
System.out.println("f1 and f3 are not equal");

Lab 5
Task # 1
class Adress {
private int houseno;
private int street;
private String city;
private int code;

// Adress(int house, int s,String c, int c1) {


// houseno= house;
// street = s;
// city= c;
// code =c1;
// }

void sethouseno(int a){


houseno=a;
}
void setstreet(int b){
street=b;
}
void setcity(String c){
city=c;
}
void setcode(int d){
code=d;
}

public int gethouseno() {


return houseno;
}
public int getstreet() {
return street;
}
public String getcity() {
return city;
}
public int getcode() {
return code;
}

//};
public void display() {
System.out.println(houseno + " " + street + " " + code+" " + city);
}
};

class Person{
String name;
Adress add;

Person(String n,Adress a){


name =n;
add=a;

// void setname(String n){


// name=n;
// }

// String getname(){
// return name;
// }

void display(){
System.out.println("Person name : "+name);
System.out.println("Addreass details are :");
System.out.println("street : "+add.getstreet());
System.out.println("house no : "+add.gethouseno());
System.out.println("city : "+add.getcity());
System.out.println("code : "+add.getcode());

}
}

public class Task1{


public static void main(String[]args){
Adress a=new Adress();
a.setstreet(5);
a.setcity("SDK");
a.sethouseno(45);
a.setcode(208);
Person p=new Person("nouman",a);
p.display();
}
}

Task# 2
class Date {
private int day;
private int month;
private int year;

Date(int theMonth, int theDay, int theYear) {


day = theDay;
month = theMonth;
year = theYear;
}

void setday(int a){


day=a;
}
void setmonth(int b){
month=b;
}
void setyear(int c){
year=c;
}
public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}

public void display() {


System.out.println(day + " " + month + " " + year);
}
}

class Aurthor{
String name;

void setname(String n){


name=n;
}

String getname(){
return name;
}
public void display() {
System.out.println("Aurthor Names :"+name);
}
};

class Book{
String name;
Date de;
Aurthor ae;

Book(String n,Date d,Aurthor a){


name =n;
de=d;
ae=a;

// void setname(String n){


// name=n;
// }

// String getname(){
// return name;
// }

void display(){
System.out.println("Book name : "+name);
System.out.println("Aurthor details are :");
ae.display();
//System.out.println("Name : "+ae.getnam
System.out.println("Published date :");
de.display();

}
}

public class Task2{


public static void main(String[]args){
Date b = new Date(1, 12, 1990);
Aurthor a=new Aurthor();
a.setname("Talha Khalid");
//a.getname();
Book p=new Book("Statistics",b,a);
p.display();
}
}

Task# 3
class Point{

int xcord;
int ycord;

Point(int a,int b){


xcord=a;
ycord=b;

void setxcord(int a){


xcord=a;
}

int getxcord(){
return xcord;
}

void setycord(int b){


ycord=b;
}
int getycord(){
return ycord;
}

void display(){
System.out.println("Xcord : "+xcord+" Ycord : "+ycord);
}

class Line{
int point1;
int point2;
Point p;

Line(int a,int b,Point p){


point1=a;
point2=b;
this.p=p;

}
//sqrt((x2-x1)2 + (y2-y1)2)
double calculateLength(){
double result=Math.sqrt((p.xcord*point2-p.xcord*point1)*2 +
(p.ycord*point2-p.ycord*point1)*2);
return result;
}

void display(){
System.out.println("point1 : "+ point1 +" point2 : "+point2);
//System.out.println("xcord and ycord are :");
p.display();
}

public class Task3{


public static void main (String[]args){
Point p=new Point(2,8);
Line l1=new Line(2,90,p);
Line l2=new Line(5,60,p);
l1.calculateLength();
l1.display();
l2.calculateLength();
l2.display();
}
}

Task# 4
class Pizza {
private String size;
private int cheeseToppings;
private int pepperoniToppings;
private int hamToppings;

public Pizza(String size, int cheeseToppings, int pepperoniToppings, int hamToppings) {


this.size = size;
this.cheeseToppings = cheeseToppings;
this.pepperoniToppings = pepperoniToppings;
this.hamToppings = hamToppings;
}

public double calcCost() {


double cost = 0;
switch (size) {
case "small":
cost = 10;
break;
case "medium":
cost = 12;
break;
case "large":
cost = 14;
break;
}
cost += (cheeseToppings + pepperoniToppings + hamToppings) * 2;
return cost;
}

public String getDescription() {


return "Pizza size: " + size + ", Cheese toppings: " + cheeseToppings + ", Pepperoni toppings: "
+ pepperoniToppings + ", Ham toppings: " + hamToppings;
}
}

class PizzaOrder {
private Pizza[] pizzas = new Pizza[3];
private int numPizzas;

public void addPizza(Pizza pizza) {


if (numPizzas < 3) {
pizzas[numPizzas] = pizza;
numPizzas++;
} else {
System.out.println("You can't add more than 3 pizzas to the order.");
}
}

public double calcTotal() {


double totalCost = 0;
for (int i = 0; i < numPizzas; i++) {
totalCost += pizzas[i].calcCost();
}
return totalCost;
}
}

public class Task4 {


public static void main(String[] args) {
Pizza pizza1 = new Pizza("large", 1, 1, 2);
System.out.println("Pizza 1: " + pizza1.getDescription());
System.out.println("Cost of Pizza 1: $" + pizza1.calcCost());
Pizza pizza2 = new Pizza("medium", 2, 0, 1);
System.out.println("Pizza 2: " + pizza2.getDescription());
System.out.println("Cost of Pizza 2: $" + pizza2.calcCost());
PizzaOrder order = new PizzaOrder();
order.addPizza(pizza1);
order.addPizza(pizza2);
System.out.println("Total cost of the order: $" + order.calcTotal())

Lab 6
Task # 1
class Person{
String name;
String phone;
String adddres;
String email;

Person(String a, String b, String c,String d){


name=a;
phone=b;
adddres=c;
email=d;
}

class Student extends Person{

String status;

Student(String name,String phone,String adddres,String email,String status){


super(name,phone,adddres,email);
status=status;
}
void display(){
System.out.println("Name : "+name+" phone : "+phone+" Address "+ adddres+"
Email "+email
+" status " +status);
}
}

class Employee extends Person{


String office;
int salary;
String datehired;

Employee(String name, String phone, String adddres,String email , String office,


int salary, String datehired){
super(name,phone,adddres,email);
this.office=office;
this.salary=salary;
this.datehired=datehired;
}
void display(){
System.out.println("Name : "+name+" phone : "+phone+" Address "+ adddres+"
Email "+email
+" Salary " +salary+" datehired "+datehired);
}
}

class Faculty extends Person{


int officehours;
String rank;
Faculty(String name, String phone, String adddres,String email ,int officehours, String rank){
super(name,phone,adddres,email);
this.officehours=officehours;
this.rank=rank;
}
void display(){
System.out.println("Name : "+name+" phone : "+phone+" Address "+ adddres+"
Email "+email
+" officehours " +officehours+" rank "+rank);
}
}

class Staff extends Person{


String Title;

Staff(String name, String phone, String adddres,String email ,String Title){


super(name,phone,adddres,email);
this.Title=Title;
}
void display(){
System.out.println("Name : "+name+" phone : "+phone+" Address "+ adddres+"
Email "+email
+ " Title "+Title);
}
}

public class Task1{


public static void main(String[]args){
Student s=new Student("Nouman","5555-9","208/p","[email protected]","Good");
s.display();
Employee e=new Employee("Nouman","5555-9","208/p","[email protected]","ab-2
room 107",150000,"12/8/24");
e.display();
Faculty f=new Faculty("Nouman","5555-9","208/p","[email protected]",7,"CEO");
f.display();
Staff s1=new Staff("Nouman","5555-9","208/p","[email protected]","secondary");
s1.display();

}
}

Task # 2
import java.util.Scanner;

class Publication {
private String title;
private double price;

public void setTitle(String title) {


this.title = title;
}

public void setPrice(double price) {


this.price = price;
}

public String getTitle() {


return title;
}

public double getPrice() {


return price;
}

public void display() {


System.out.println("Title: " + title);
System.out.println("Price: $" + price);
}
}

class Book extends Publication {


private int pageCount;

public void setPageCount(int pageCount) {


this.pageCount = pageCount;
}

public int getPageCount() {


return pageCount;
}

@Override
public void display() {
super.display();
System.out.println("Page Count: " + pageCount);
}
}

class Tape extends Publication {


private int playingTimeMinutes;

public void setPlayingTimeMinutes(int playingTimeMinutes) {


this.playingTimeMinutes = playingTimeMinutes;
}

public int getPlayingTimeMinutes() {


return playingTimeMinutes;
}

@Override
public void display() {
super.display();
System.out.println("Playing Time (minutes): " + playingTimeMinutes);
}
}

public class Task2 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Testing Book class


System.out.println("Enter details for a book:");
Book book = new Book();
System.out.print("Enter title: ");
book.setTitle(scanner.nextLine());
System.out.print("Enter price: $");
book.setPrice(scanner.nextDouble());
System.out.print("Enter page count: ");
book.setPageCount(scanner.nextInt());

// Testing Tape class


System.out.println("\nEnter details for a tape:");
Tape tape = new Tape();
scanner.nextLine(); // Consume newline
System.out.print("Enter title: ");
tape.setTitle(scanner.nextLine());
System.out.print("Enter price: $");
tape.setPrice(scanner.nextDouble());
System.out.print("Enter playing time in minutes: ");
tape.setPlayingTimeMinutes(scanner.nextInt());

// Displaying book and tape details


System.out.println("\nBook details:");
book.display();

System.out.println("\nTape details:");
tape.display();

scanner.close();
}
}

Task # 3
class Computer {
private int wordSize;
private int memorySize;
private int storageSize;
private int speed;

public Computer() {
// Default constructor
}

public Computer(int wordSize, int memorySize, int storageSize, int speed) {


this.wordSize = wordSize;
this.memorySize = memorySize;
this.storageSize = storageSize;
this.speed = speed;
}

public void display() {


System.out.println("Word Size: " + wordSize + " bits");
System.out.println("Memory Size: " + memorySize + " megabytes");
System.out.println("Storage Size: " + storageSize + " megabytes");
System.out.println("Speed: " + speed + " megahertz");
}
}

class Laptop extends Computer {


private int length;
private int width;
private int height;
private int weight;

public Laptop() {
// Default constructor
}

public Laptop(int wordSize, int memorySize, int storageSize, int speed, int length, int width, int height,
int weight) {
super(wordSize, memorySize, storageSize, speed);
this.length = length;
this.width = width;
this.height = height;
this.weight = weight;
}

@Override
public void display() {
super.display();
System.out.println("Length: " + length + " cm");
System.out.println("Width: " + width + " cm");
System.out.println("Height: " + height + " cm");
System.out.println("Weight: " + weight + " kg");
}
}

public class Task3 {


public static void main(String[] args) {
// Creating a Computer object
Computer basicComputer = new Computer(64, 4096, 512, 3000);
System.out.println("Basic Computer Details:");
basicComputer.display();

// Creating a Laptop object


Laptop myLaptop = new Laptop(64, 8192, 1024, 3200, 35, 25, 2, 2);
System.out.println("\nMy Laptop Details:");
myLaptop.display();
}
}

Lab Question Shapes


class Shape {
private String color;
private boolean filled;

public Shape() {
this.color = "no color";
this.filled = false;
public Shape(String color, boolean filled) {
this.color = color;
this.filled = filled;
}

public String getColor() {


return this.color;
}

public void setColor(String color) {


this.color = color;
}

public boolean isFilled() {


return this.filled;
}

public void setFilled(boolean filled) {


this.filled = filled;
}

public String toString() {


return "Color: " + this.color + " Filled: " + this.filled;
}
}

class Circle extends Shape {


private double radius;

public Circle() {
this.radius = 0.0;
}

public Circle(double radius) {


this.radius = radius;
}

public Circle(double radius, String color, boolean filled) {


super(color, filled);
this.radius = radius;
}

public double getRadius() {


return this.radius;
}

public void setRadius(double radius) {


this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}

public double getPerimeter() {


return 2 * Math.PI * radius;
}

public String toString() {


return super.toString() + " Radius: " + this.radius;
}
}

class Rectangle extends Shape {


private double length, width;

public Rectangle() {
this.length = 0.0;
this.width = 0.0;
}

public Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

public Rectangle(double length, double width, String color, boolean filled) {


super(color, filled);
this.length = length;
this.width = width;
}

public double getArea() {


return (length * width);
}

public double getPerimeter() {


return 2 * (length + width);
}

public String toString() {


return super.toString() + " Length: " + this.length + " Width: " + this.width;
}
}

class Square extends Rectangle {


private double side;
public Square() {
this.side = 0.0;
}

public Square(double side) {


super(side, side);
this.side = side;
}

public Square(double side, String color, boolean filled) {


super(side, side, color, filled);
this.side = side;
}

public double getSide() {


return this.side;
}

public void setSide(double side) {


this.side = side;
}

@Override
public String toString() {
return super.toString() + " Side: " + this.side;
}
public class LabQ1 {
public static void main(String[] args) {
Shape shape = new Shape("Red", true);
Circle circle = new Circle(5.0, "Blue", false);
Rectangle rectangle = new Rectangle(4.0, 6.0, "Green", true);
Square square = new Square(3.0, "Yellow", false);

System.out.println("Shape: " + shape.toString());


System.out.println("Circle: " + circle.toString());
System.out.println("Rectangle: " + rectangle.toString());
System.out.println("Square: " + square.toString());
}

You might also like