You are on page 1of 34

OOP (java)

Lab Task 3
 
 
SUBMITTED TO:   

Mam Saneeha Amir

SUBMITTEED BY: 

Shayan Zameer SP21-BCS-088

CLASS:   
BCS-3B
Lab Methods and Constructors

1. Create a class Account class with balance, yearofOpening and CNIC as data member.
Create :
a. Default constructor
b. One-argument Constructor (balance)
c. Three- argument constructor
d. setValues()
e. display()
f. withDraw()
g. deposit()
h. calculateAgeOfAccount()

SOLUTION CODE:
class Account{

double balance;
String cnic;
int yearofopening;
int accountage;

//default constructor
public Account(){
balance = 30000;
cnic= "6110158441299";
yearofopening = 2014;
}
// 1 argument constructor
public Account(double bal){

if(balance >0){
balance= bal;
}else{
balance=0;
}
cnic= "6110158441299";
yearofopening = 2014;
}
//three argument constructor
public Account(double bal ,String cn, int yo ){
if(balance >0){
balance= bal;
}else{
balance=0;
}
cnic= cn;
if(yo>0 ){
yearofopening = yo;
}else{
yearofopening= 0;
}

public void setValues(){


balance = 40000;
cnic= "61101473653984";
yearofopening = 2011;

public void display(){


System.out.println("BALANCE IS "+ balance);
System.out.println("CNIC OF USER IS "+ cnic);
System.out.println("ACCOUNT OPENING YEAR IS " +
yearofopening);
System.out.println("ACCOUNT AGE IS"+ accountage);
}

double withDraw(double amount){

if(amount<=balance){
balance = balance - amount;
}else{
balance = 0;
}
return balance;

double deposit(double amount){

balance = balance + amount;


return balance;
}

int ageOfAccount(int currentyear){

if(currentyear>0){
accountage = currentyear - yearofopening;
}
else{
accountage = 0;
}
return accountage;
}

RUNNER FILE:
public class runneracc{
public static void main(String[] args) {

//default constructor calling


Account obj = new Account();

obj.display();

// 1 argument constructor calling


System.out.println("");
System.out.println("");

System.out.println("1 argument constructor calling");

Account obj1 = new Account(40000.9);

obj1.deposit(10000);

obj1.display();

// three argument constructor calling

System.out.println("");
System.out.println("");
System.out.println("3 argument constructor calling");

Account obj2 = new Account(20000,"4682546234",2);


obj2.withDraw(5000);
obj2.deposit(2000);
obj2.ageOfAccount(2021);

obj2.display();
}
}

Code

2. Create a quadratic Equation Class with a, b and c as data members. Create


a. Default constructor
b. Three- argument constructor
c. Display()
d. setValues ()
e. getDiscriminant() //returns double
f. checkIfDescriminantIsGretaerThan100() //Boolean return

SOLUTION CODE:
public class quadequation{

double a;

double b;

double c;
public quadequation(){

a= 2.0;

b=4.6;

c=3.4;

public quadequation(double x,double y, double z){

a=x;

b=y;

c=z;

public void setValues(){

a= 5.0;

b=2.6;

c=7.4;

public void display(){

System.out.println("THE VALUE OF A IS"+ a);

System.out.println("THE VALUE OF B IS"+ b);

System.out.println("THE VALUE OF C IS"+ c);


System.out.println(checkDisc());

public double getDiscriminant(){

double disc = b*b-4*(a*c);

return disc;

public boolean checkDisc(){

if(getDiscriminant()>100){

return true;

}else {

return false;

RUNNER FILE:
public class runnerq{

public static void main(String[] args) {


// default constructor calling

System.out.println("");

System.out.println("default constructor calling");

quadequation obj = new quadequation();

obj.display();

System.out.println("");

System.out.println(" 3 argument constructor calling");

quadequation obj1 = new quadequation(2.8,4.5,8.7);

System.out.println("DISCRIMINANT OF SYSTEM IS");

System.out.println(obj1.getDiscriminant());

obj1.display();

}
Code

3. Create a class Rectangle with length and width as data members. Create
a. Default constructor
b. two- argument constructor
c. Display()
d. setValues ()
e. calculateArea ()
f. checkSquare () //Boolean return

SOLUTION CODE:
class rectangle{

int length;

int width;

// default constructor

public rectangle(){

// values giving to length and width are null

// 2 argument constructor
public rectangle(int a, int b){

if(a>0){

length =a;

if(b>0){

width = b ;

void display(){

System.out.println("THE LENGTH OF RECTANGLE IS "+ length);

System.out.println("THE WIDTH OF RECTANGLE IS "+ width);

void setValues(int a , int b ){

length = a;

width = b;

int calculateArea( ){
int area;

area = length* width;

return area;

RUNNER FILE:
public class runnerrec{

public static void main(String[] args) {

// calling default costructor

rectangle obj = new rectangle();

obj.calculateArea();

obj.display();

// calling 2 argument constructor


System.out.println("\nTWO ARGUMENT CONSTRUCTOR
CALLING\n");

rectangle obj1 = new rectangle(6,7);

obj1.setValues(4,7);

obj1.display();

System.out.println("\n");

System.out.println("THE CALCULATION OF AREA


IS"+obj1.calculateArea());

obj1.display();

Code
4. Create a class Point class with x and y as data members. Create
a. Default constructor
b. two- argument constructor
c. Display()
d. setValues ()
e. move ()
f. checkOrigin () //Boolean return

SOLUTION CODE:
class point{

int x;

int y;

// default constructor

public point(){

// values set to x and y are null

public point(int a , int b){

x=a ;

y = b;

void display(){

System.out.println("THE VALUE OF POINT X IS"+ x);

System.out.println("THE VALUE OF POINT Y IS"+ y);


}

void setvalues(int a , int b ){

x = a;

y=b;

void move(int a, int b ){

x = x+ a;

y = y + b;

System.out.println("THE POSITION OF X AFTER MOVE IS "+ x);

System.out.println("THE POSITION OF Y AFTER MOVE IS "+ y);

boolean checkOrigin(){

if(x==0 && y == 0 ){

return true;

}else {

return false;

}
}

RUNNER FILE:
public class runnerp{

public static void main(String[] args) {

System.out.println("DEFAULT CONSTRUCTOR CALLING ");

point obj = new point();

obj.display();

obj.move(3, 4);

System.out.println(obj.checkOrigin());

System.out.println("\n TWO ARGUMENT CONSTRUCTOR


CALLING\n");

point obj1 = new point( 5, 6);

obj1.display();

System.out.println("\n \n");

obj1.move(4,7);

System.out.println("ORIGIN CHECKING\n"+obj1.checkOrigin());
}

Code

5. Create a Book class with author and chapterNames[5] as data members. Create

a. Default constructor
b. two- argument constructor
c. Display()
d. setValues ()
e. checkIfAuthorNameStartsWithA ()
f. searchChapter () //Boolean return

SOLUTION CODE:
class book{

String authorname;

String [] chapternames= new String[5];

int size = 5;

// default constructor

public book(){
}

// two arguments constructor

public book( String author, String [] array){

authorname= author;

chapternames=array;

void display(){

System.out.println("THE AUTHOR NAME IS "+ authorname);

System.out.println("CHAPTER NAMES OF BOOK ARE ");

for(int i = 0 ; i<size;i++){

System.out.println(chapternames[i]);

void setValues(String author,String[]array){

authorname= author;
if(array.length<chapternames.length){

for(int i = 0;i<array.length;i++){

chapternames[i]=array[i];

}else{

for(int i = 0;i<chapternames.length;i++){

chapternames[i]=array[i];

System.out.println("THE AUTHOR NAME IS "+ authorname);

System.out.println("CHAPTER NAMES OF BOOK ARE ");

for(int i = 0 ; i<size;i++){

System.out.println(chapternames[i]);

}
}

boolean checkauthorname(){

if(authorname.charAt(0)=='A'){

return true;

}else{

return false;

boolean searchChapter(String chapname){

boolean flag = false;

for(int i = 0;i<size;i++){

if (chapternames[i]== chapname) {

flag= true;

break;

}else{

flag= false;
}

return flag;

RUNNER FILE:
public class runnerb{

public static void main(String[] args) {

System.out.println("DEFAULT CONSTRUCTOR CALLING");

String []array = {"oop","dsa","pf","isl","phy"};

book obj = new book();

obj.display();

// two argument constructor calling

System.out.println("\nTWO ARGUMENT CONSTRUCTOR


CALLING\n");
book obj1= new book("shayan khan",array);

obj1.display();

obj1.setValues("Shani",array);

System.out.println("\nCHECK IF AUTHOR NAME STARTS WITH


A\n");

System.out.println(obj1.checkauthorname());

System.out.println("\nCHAPTER SEARCHING IS \n");

System.out.println(obj1.searchChapter("oop"));

Code

6. Create a Student Class with name, Gpa, subjects[5] and email as data members.
Create
a. Default constructor
b. four- argument constructor
c. Display()
d. setValues ()
e. searchSubject ()
f. checkProbStatus () //Boolean return
g. validateEmail() //Boolean return
SOLUTION CODE:
class student{

String name;

double gpa;

int size= 5;

String[] subjects= new String[size];

String email;

// default constructor

public student(){

// values set to all data members are null

// four argument constructor

public student(String n,double g, String [] array,String e){

name= n;

email = e;

if(g>0.0 && g<4.0){


gpa=g;

}else{

gpa= 0.0;

email = e;

if(array.length<subjects.length){

for(int i = 0;i<array.length;i++){

subjects[i]=array[i];

}else{

for (int i = 0;i<subjects.length ;i++ ) {

subjects[i]=array[i];

}
// display method

void display(){

System.out.println("NAME OF STUDENT IS "+ name);

System.out.println("GPA OF STUDENT IS " + gpa);

System.out.println("EMAIL OF STUDENT IS "+ email);

System.out.println("SUBJECTS OF STUDENT ARE ");

for(int i = 0; i<size;i++){

System.out.println(subjects[i]);

// set values method

void setValues(String n,double g, String [] array,String e){

name= n;

if(g>0.0 && g<4.0){

gpa=g;

}else{

gpa= 0.0;

}
if(array.length<subjects.length){

for(int i = 0;i<array.length;i++){

subjects[i]=array[i];

}else{

for (int i = 0;i<subjects.length ;i++ ) {

subjects[i]=array[i];

// search method

boolean searchSubject(String sub){

boolean flag = false;


for(int i = 0;i<size;i++){

if(subjects[i]==sub){

flag = true;

break;

}else{

flag= false;

return flag;

//CHECK Probabation status method

boolean checkProb(){

if(gpa>=2.0){

return true;

}else{

return false;

}
// check email validation

boolean validateEmail(){

if(email.contains("@")&& email.contains(".com")){

return true;

}else{

return false;

RUNNER FILE:
public class runnerstu{

public static void main(String[] args) {

String []array = {"oop","dsa","pf","isl","phy"};


// default constructor calling

System.out.println("DEFAULT CONSTRUCTOR CALLING");

student obj = new student();

obj.display();

System.out.println("FOUR ARGUMENT CONSTRUCTOR


CALLING");

student obj1 = new student("shayan",3.0,array,"shayan.com");

obj1.display();

System.out.println(obj1.checkProb());

System.out.println(obj1.searchSubject("oop"));

Code

7. Create a class “University” having following characteristics:

Data Members:
 String uniName;
 String location;
 String rectorName;
 String departments[20]; // it’s a string array
Constructors:
 No argument
 Two argument (UniName and RectorName)
 A Constructor setting values of all parameters.
Methods:
 display
o This methods displays all the data members of the class
 addADepartments
o This method should add a new department the Departments [] array.
 checkIfLocatedInCapital
o This method will return true if university is located in federal capital or
provincial capital.
 searchDepartment
o This method will return true if a certain department is found

Runner class:
Create a runner class such that all Constructors and methods are called at least once.

SOLUTION CODE:
class university{

String uniName;

String location;

String rectorName;

String[] departments = new String[20];


// default constructor

public university(){

// values set to data members are null

// two argument constructor

public university(String un, String recn){

uniName = un;

departments[0]="CS";

departments[1]="SE";

rectorName= recn;

location= "islamabad";

// display method

void display(){

System.out.println("NAME OF UNIVERSITY IS "+ uniName);

System.out.println("LOCATION OF UNIVERSITY IS " + location);

System.out.println("RECTOR NAME IS "+ rectorName);

System.out.println("DEPARTMENTS OF UNIVERSITY ARE ");

for(int i = 0; i<5;i++){

System.out.println(departments[i]);
}

// check if located in capital

boolean checkLocation(){

if(location=="islamabad"){

return true;

}else{

return false;

// search a department in university

boolean searchDepartment(String dep){

boolean flag = false;

for(int i = 0; i< 5; i++){

if(departments[i]==dep){

flag = true;

break;

}else{

flag = false;

}
}

return flag;

// Add a department

void addDepartment(String dep){

int count = 0;

int i;

for( i = 0; i<departments.length; i++){

if(departments[i]==null){

departments[i]= dep;

break;

count++;

for(int j = 0;j<=count;j++){

System.out.println(departments[j]);

}
}

RUNNER FILE:
public class runneruni{

public static void main(String[] args) {

university obj = new university("comsats","shayan");

obj.display();

obj.addDepartment("BAF");

You might also like