0% found this document useful (0 votes)
58 views13 pages

Lab Assignment 1

This document contains the code for several Java classes implementing different object-oriented programming concepts: Circle, Rectangle, Account, Marks, Point, Book, and Student. For each class, the code defines attributes and behaviors through methods, and includes a runner class to demonstrate creating and using objects of that class.

Uploaded by

Jawad Nasir
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)
58 views13 pages

Lab Assignment 1

This document contains the code for several Java classes implementing different object-oriented programming concepts: Circle, Rectangle, Account, Marks, Point, Book, and Student. For each class, the code defines attributes and behaviors through methods, and includes a runner class to demonstrate creating and using objects of that class.

Uploaded by

Jawad Nasir
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/ 13

LAB ASSIGNMENT # 1

Object Oriented Programming

Submitted by: Syed Ahmad Hussain


Registration No.: SP18-BSE-108

Submitted to: Madam Saneeha Amir

COMPUTER LAB 7
COMSATS ISLAMABAD
Class Circle:
public class circle {
int radius;

public void setParameters(int r){


radius = r;

}
public void display(){
System.out.println(radius);
}
public void calculateCircumference(){
double circum;
circum = 2*3.14*radius;
System.out.println("Circumference of a circle" + circum);
}
public void calculateArea(){
double area;
area = 3.14*(radius*radius);
System.out.println(radius);
}
}

Runner:
public static void main(String[] args) {
circle c1 = new circle();
c1.setParameters(8);
c1.calculateArea();
c1.calculateCircumference();
c1.display();

circle c2 = new circle();


c2.setParameters(6);
c2.calculateArea();
c2.calculateCircumference();
c2.display();
}

Class Rectangle:
public class Rectangle {
int length,width;

public void setParameters(int l,int w){


length = l;
width = w;

}
public void display(){
System.out.println("Length is :" + length + " width is " + width);
}
public boolean checksquare(){
if (length==width)
return true;
else return false;
}
public void calculateArea(){
double area;
area = length*width;
System.out.println(area);
}

Runner:
Rectangle r1 = new Rectangle();
r1.setParameters(4, 6);
r1.calculateArea();
r1.checksquare();
r1.display();

Rectangle r2 = new Rectangle();


r2.setParameters(5, 7);
r2.calculateArea();
r2.checksquare();
r2.display();
Class Account:
public class account {
double balance;
int yearofOpening;
String CNIC ;

public void setParameters(double b,int y,String c){


balance = b;
yearofOpening = y;
CNIC = c;

}
public void display(){
System.out.println("balance is " + balance +
" year of opening is" + yearofOpening +
"CNIC is" + CNIC);
}
public void withdraw(int w){
balance = balance - w;
}
public void deposit(int d){
balance = balance + d;
}
public boolean validCNIC(){
if (CNIC.length() == 13)
return true;
else return false;
}
}

Runner:

account a1 = new account();


a1.setParameters(5000, 2015, "6110106855343");
a1.deposit(1000);
a1.withdraw(500);
a1.display();

account a2 = new account();


a2.setParameters(1000, 2017, "6110106855456");
a2.deposit(2000);
a2.withdraw(1000);
a2.display();

Class Marks:
public class marks {
int sub1;
int sub2;
int sub3;

public void setparameters(int s1,int s2,int s3){


sub1 = s1;
sub2 = s2;
sub3 = s3;
}
public void display(){
System.out.println("sub 1" + sub1 + "sub 2" + sub2 + "sub 3" + sub3);
}
public void sum(){
int sum;
sum = sub1+sub2+sub3;
}
public void percentage(){
int sum = sub1+sub2+sub3;
int percentage = (sum/300) * 100;
System.out.println("percentage is" + percentage+"%");
}
public void checkFailure(){
double percentage = ((sub1+sub2+sub3)/300) * 100;
if (percentage<50)
System.out.println("Fail");
else System.out.println("pass");
}
}

Runner:
marks m1 = new marks();
m1.setparameters(56, 68, 80);
m1.sum();
m1.percentage();
m1.checkFailure();
m1.display();
marks m2 = new marks();
m2.setparameters(56, 68, 80);
m2.sum();
m2.percentage();
m2.checkFailure();
m2.display();

Class Point:
public class point {
int x;
int y;

public void setparameters(int x1,int y1){


x = x1;
y = y1;
}
public void display(){
System.out.println("x =" + x + "y = " + y);
}
public void move(int dx,int dy){
x = x + dx;
y = y + dy;
}
public boolean checkorigin(){
if (x==0 & y==0)
return true;
else return false;
}
}

Runner:
point p1 = new point();
p1.setparameters(4, 5);
p1.move(2, 3);
p1.checkorigin();
p1.display();

point p2 = new point();


p2.setparameters(5, 6);
p2.move(4, 3);
p2.checkorigin();
p2.display();

Class book:
public class book {
String author;
String chapterNames[] = new String[5];

public void setparameters(String a,String[]cn){


author = a;
for(int i=0; i<5; i++)
chapterNames[i] = cn[i];
}
public void display(){
System.out.println("Author is " + author);
for(int i=0; i<5; i++)
System.out.println(chapterNames[i]);
}
public boolean checkIfAuthorNameStartsWithA(){
if (author.startsWith("A"))
return true;
else return false;
}
public boolean searchChapter(String sc){
for(int i=0; i<5; i++)
if (sc.equals(chapterNames[i]))
return true;
else return false;
return false;
}
}

Runner:
String[] chapters= {"cho1","cho2","cho3","cho4","cho5"};
book b1 = new book();
b1.setparameters("Charles Babbage", chapters);
b1.checkIfAuthorNameStartsWithA();
b1.searchChapter("cho1");
b1.display();
String[] chapters1= {"cho1","cho2","cho3","cho4","cho5"};
book b2 = new book();
b2.setparameters("Umaira Ahmed", chapters1);
b2.checkIfAuthorNameStartsWithA();
b2.searchChapter("cho2");
b2.display();

Class student:
public class student {
String name;
double GPA;
String subjects[] = new String[5];
String email;

public void setparameters(String n, double gpa, String[]s,String e){


name = n;
GPA = gpa;
email = e;
for(int i=0; i<5; i++)
subjects[i] = s[i];
}
public void dispay(){
System.out.println("name:" + name +
"GPA is " + GPA +
"email is" + email);
for(int i=0; i<5; i++)
System.out.println(subjects[i]);
}
public boolean Searchsubject(String subject){
for(int i=0; i<5; i++)
if (subject.equals(subjects[i]))
return true;
else return false;
return false;
}
public void checkprob(){
if (GPA < 2.0)
System.out.println("Prob");
else
System.out.println("Pass");
}
public boolean validateEmail(){
if (email.endsWith("@.com"))
return true;
else return false;
}
}

Runner:
String[] sub= {"maths","english","urdu","islamiat","physics"};
student s1 = new student();
s1.setparameters("Ahmad", 3.0, sub, "[email protected]");
s1.checkprob();
s1.validateEmail();
s1.Searchsubject("urdu");
s1.dispay();
String[] sub1= {"maths","english","urdu","islamiat","physics"};
student s2 = new student();
s2.setparameters("Saad", 2.7, sub1, "[email protected]");
s2.checkprob();
s2.validateEmail();
s2.Searchsubject("english");
s2.dispay();

You might also like