Oop Lab#2
Oop Lab#2
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);
}
Task# 2
class Book{
String aurthor;
String[] chapternames=new String[10];
Book(){
//aurthor="nouman";
//chapternames="a","g","u";
}
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;
}
}
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;
}
}
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;
//};
public void display() {
System.out.println(houseno + " " + street + " " + code+" " + city);
}
};
class Person{
String name;
Adress add;
// 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());
}
}
Task# 2
class Date {
private int day;
private int month;
private int year;
class Aurthor{
String name;
String getname(){
return name;
}
public void display() {
System.out.println("Aurthor Names :"+name);
}
};
class Book{
String name;
Date de;
Aurthor ae;
// 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();
}
}
Task# 3
class Point{
int xcord;
int ycord;
int getxcord(){
return xcord;
}
void display(){
System.out.println("Xcord : "+xcord+" Ycord : "+ycord);
}
class Line{
int point1;
int point2;
Point 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();
}
Task# 4
class Pizza {
private String size;
private int cheeseToppings;
private int pepperoniToppings;
private int hamToppings;
class PizzaOrder {
private Pizza[] pizzas = new Pizza[3];
private int numPizzas;
Lab 6
Task # 1
class Person{
String name;
String phone;
String adddres;
String email;
String status;
}
}
Task # 2
import java.util.Scanner;
class Publication {
private String title;
private double price;
@Override
public void display() {
super.display();
System.out.println("Page Count: " + pageCount);
}
}
@Override
public void display() {
super.display();
System.out.println("Playing Time (minutes): " + playingTimeMinutes);
}
}
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 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 Shape() {
this.color = "no color";
this.filled = false;
public Shape(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
public Circle() {
this.radius = 0.0;
}
public Rectangle() {
this.length = 0.0;
this.width = 0.0;
}
@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);