Comsats University Islamabad Wah Campus Object Oriented Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Comsats University Islamabad

Wah Campus

Object Oriented Programming


Assignment # 03

Submitted to: Miss Samia Zafar

Submitted by:

Nofil Abbas FA21-BCS-046


Task 1
StaticNonStatic Class

package pkgstatic.non.pkgstatic; package pkgstatic.non.pkgstatic;

public class StaticNonStatic { public class Class {


private static int id;
public static void main(String[] args) { private String name;
Class c1=new Class(); static int create=0;
Class c2=new Class(); static int destroyed=0;
Class.setId(123);
c1.setName("ABC"); public Class(){
c2.setId(456); create++;
c2.setName("XYZ"); }
System.out.println(Class.getId());
System.out.println(c1.getName()); public static void setId(int r){
Class c3=new Class(); id=r;
c1=null;
c2=null; }
System.gc(); public void setName(String h){
Class.objcreated(); this.name=h;
Class.objdestroyed(); }
public static int getId(){
} return id;
}
} public String getName(){
return name;
}
public static void objcreated(){
System.out.println("Objects created: "+create);
}
public static void objdestroyed(){
System.out.println("Objects destroyed:
"+destroyed);
}
protected void finalize(){
destroyed++;
}
}
Task 2
Calculator Runner Calculator

package calculator.runner; package balance.runner;

import java.util.Scanner; public class Balance {


private double balance;

public class CalculatorRunner { public Balance(){

public static void main(String[] args) { }


Scanner sc=new Scanner(System.in); public Balance(double b){
balance=b;
System.out.println("\t\"CALCULATOR\""); }
System.out.println("Choose options"); public Balance(Balance b){
System.out.println("1: Sum\t\t2: Subtract\n3: balance=b.balance;
Multiply\t4: Divide\n5: Modulus\t6: Sin\n7: Cos\t\t8: }
Tan");
int c=sc.nextInt(); public void setBalance(double b){
switch(c){ balance=b;
case 1: }
System.out.println("Enter two values to be public void withdraw(double a){
added"); if(a<=balance){
System.out.println(Calculator.sum(sc.nextInt(), balance-=a;
sc.nextInt())); }
break; else{
case 2: System.out.println("insufficient Balance!");
System.out.println("Enter two values to be }
subtracted"); }
public void deposit(double a){
System.out.println(Calculator.subtract(sc.nextInt(), balance+=a;
sc.nextInt())); }
break; public void display(){
case 3: System.out.println("Balance: "+balance);
System.out.println("Enter two values to be }
multiplied"); }

System.out.println(Calculator.multiply(sc.nextInt(),
sc.nextInt()));
break;
case 4:
System.out.println("Enter two values to be
divided");

System.out.println(Calculator.divide(sc.nextInt(),
sc.nextInt()));
break;
case 5:
System.out.println("Enter two values for
modulus");

System.out.println(Calculator.modulus(sc.nextInt(),
sc.nextInt()));
break;
case 6:
System.out.println("Enter angle value");
System.out.println(Calculator.sin(sc.nextInt()));
break;
case 7:
System.out.println("Enter angle value");

System.out.println(Calculator.cos(sc.nextInt()));
break;
case 8:
System.out.println("Enter angle value");

System.out.println(Calculator.tan(sc.nextInt()));
break;

}
Task 3
Distance Runner Distance

package distance.runner; package distance.runner;

import java.util.Scanner;
public class Distance {
public class DistanceRunner { private double feets;
private double inches;

public static void main(String[] args) { public Distance(){


Scanner sc=new Scanner(System.in);
Distance d1=new Distance(); }
System.out.println("Enter feets and inches of d1"); public Distance(double f,double i){
d1.setFeets(sc.nextDouble()); feets=f;
d1.setInches(sc.nextDouble()); inches=i;
System.out.println("Enter feets and inches of d2"); }
double f = sc.nextDouble();
double i=sc.nextDouble(); public void setFeets(double f){
Distance d2=new Distance(f,i); feets=f;
}
Distance d3=Distance.add(d1, d2); public void setInches(double i){
d1.display(); inches=i;
d2.display(); }
d3.display(); public void display(){
System.out.println(Distance.equals(d1, d2));; System.out.println("Feets: "+feets+"\tInches:
} "+inches);
}
} public static Distance add(Distance d1,Distance d2){
Distance d3=new Distance();
d3.feets=d1.feets+d2.feets;
d3.inches=d1.inches+d2.inches;
return d3;
}
public static boolean equals(Distance d1,Distance
d2){
if(d1.feets==d2.feets && d1.inches==d2.inches){
return true;
}
else{
return false;
}
}
}

You might also like