You are on page 1of 31

OOP (java)

 
Lab Task 4
 
 
SUBMITTED TO:   

Mam Sneeha Amir

SUBMITTEED BY: 

Shayan Zameer SP21-BCS-088

CLASS:   
BCS-3B
1. Create an Encapsulated class Rectangle class with length and width as data members.
Create two constructors and a function to calculate Area.

In the runner create two objects using default constructor and argument constructor.
Call the getter function to find the length of both objects. Display which object has
larger length.

SOLUTION CODE:

public class rectangle{


private int length;
private int width;

// default constructor

public rectangle(){

length = 20;
width = 40;

public rectangle(int a ,int b){

if(a>0){
length= a;
}else{
length = 0;
}
if (b>0) {
width = b;
}else{
width= 0;
}

//Setters
void setLength(int a){
if(a>0){
length= a;
}else{
length = 0;

}
}
void setWidth(int b){
if (b>0) {
width = b;
}else{
width= 0;
}

//getters

int getLength(){
return length;
}
int getWidth(){
return width;
}

// display method

void display(){
System.out.println("THE LENGTH OF RECTANGLE IS "+ length);
System.out.println("THE WIDTH OF RECTANGLE IS "+ width);
}

int calculateArea(){
int area= length * width;
return area;
}

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

// object1

rectangle obj1 = new rectangle();

//object 2
rectangle obj2= new rectangle(30,60);

int lenofone=obj1.getLength();
int lenoftwo=obj2.getLength();

if(lenofone>lenoftwo){
System.out.println("OBJECT 1 HAS GREATER LENGTH");
}else{
System.out.println("OBJECT 2 HAS GREATER LENGTH");
}

}
}

2. Create an Encapsulated class Point class with x and y as data members. Create two
constructors and a function to move the point.
In the runner create two points using default constructor and argument constructor.
Now set the y coordinate of second point equal to the x coordinate of first point. Call
display function to verify the result.

SOLUTION CODE:
class point{
private double x;
private double y;

// constructor
public point(){
x = 1.7;
y = 2.5;
}
// argumented

public point (double a, double b){


x= a;
y = b;
}

// setters

void setX(double a){


x = a;

}
void setY(double a){
y = a;

// getters

double getX(){
return x;
}
double getY(){
return y;

void to_move_point(double a, double b){


x = x+a;
y = y+b;

void display(){
System.out.println("THE VALUE OF X IS"+ x);
System.out.println("THE VALUE OF Y IS"+ y);
}

RUNNER FILE

public class pointrunner{


public static void main(String[] args) {
// object 1
point obj1= new point();

// object2
point obj2 = new point(1.4,3.5);

obj2.setY(obj1.getX());

obj2.display();
}
}
3. Create an Encapsulated class Student with following characteristics:
Data Members:
a. String Name
b. Int [] Result_array[5] // Result array contains the marks for 5 subjects
Methods:
a. Student ( ??? ) // argument Constructor
b. verifyArrayLength(?) //private method
c. Average ( ??? ) // it calculates and returns the average based on the marks
in the array.
Runner:
a. Create two objects of type Student and call the Average method.
b. Create a third student with name as object 1 and result array as object 2

SOLUTION CODE:

class student{
private String name;
private int [] result_array= new int[5];

// default constructor

student(){
name = "shayan";
}
// argumented constructor
student( String n, int[]array){
name = n;
if(array.length<result_array.length){
for(int i = 0;i<array.length;i++){
array[i]=result_array[i];
}
}else{
for(int i = 0;i<result_array.length;i++){
result_array[i]=array[i];
}
}
}

// setters

void setName(String n){


name= n;
}
void setResult_array(int[]array)
{

if(array.length<result_array.length){
for(int i = 0;i<array.length;i++){
array[i]=result_array[i];
}
}else{
for(int i = 0;i<result_array.length;i++){
result_array[i]=array[i];
}

}
}

// getters
String getName(){
return name;
}
int[] getResult_Array(){
return result_array;
}

private int verify_Length_Of_Array(){

int length= result_array.length;


return length;
}

void display(){
System.out.println("THE STUDENT NAME IS "+ name);
System.out.println("\nMARKS IN RESULT ARRAY IS AS FOLLOWS\n");
System.out.println();

for(int i = 0;i<result_array.length;i++){
System.out.println(result_array[i]);
}
}

int calculate_Average(){
int sum = 0;
int avg;
for(int i = 0;i<result_array.length;i++){
sum= sum+result_array[i];
}
avg= sum/result_array.length;

return avg;

}
}

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

int [] array = {1,2,3,4,5};


int [] array1 = {6,7,8,9,10,11};

student obj1 = new student("shayan", array);


student obj2 = new student("usman", array1);

System.out.println("THE AVERAGE OF STUDENT1 = "+


obj1.calculate_Average());
System.out.println("THE AVERAGE OF STUDENT2 = "+
obj2.calculate_Average());

student obj3 = new


student(obj1.getName(),obj2.getResult_Array());

obj3.display();

}
}
4. Create an Encapsulated class Account class with balance as data member. Create two
constructors and methods to withdraw and deposit balance.
In the runner create two accounts. The second account should be created with the
same balance as first account.

SOLUTION CODE:
class account{
private int balance;

//default constructor
public account(){
balance = 40000;
}
// arumented constructor

public account(int a){


if(a>0){
balance = a;
}else{
balance = 0;
}
}
// setters
void setbalance(int a){
if(a>0){
balance = a;
}else{
balance = 0;
}
}
// getter
int getbalance(){
return balance;
}

// methods

int withdraw(int amount){


if(amount>0&&amount<=balance){
balance = balance-amount;
}else{
System.out.println("enter amount must be greater than
0");
}
return balance;

int deposit(int amount){


if(amount>0){
balance = balance+amount;
}else{
System.out.println("enter amount must be greater than
0");
}
return balance;

void display(){
System.out.println("THE BALANCE IS" + balance);
}
}

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

account obj1 = new account();


account obj2 = new account(obj1.getbalance());

obj2.display();

}
}
5. Create an Encapsulated class Marks with three data members to store three marks.
Create two constructors , display and set and get methods for all data members. Create
a private method CalculateTotalMarks(). Also create a public method
calculatePercentage().

In the runner create two objects. Compare the percentage of both objects. Compare
the marks of subject1 for both objects.

SOLUTION CODE:

class marks{
private int mark1;
private int mark2;
private int mark3;

// default constructor

marks(){
mark1= 20;
mark2= 30;
mark3= 40;
}
// argumented contsructor
marks(int a , int b ,int c){
if(a>0){
mark1=a;
}else{
mark1=0 ;
}
if(b>0){
mark2=b;
}else{
mark2=0 ;
}

if(c>0){
mark3=c;
}else{
mark3=0 ;
}
}

// setters

void setMark1(int a){


if(a>0){
mark1=a;
}else{
mark1=0 ;
}

}
void setMark2(int a){
if(a>0){
mark2=a;
}else{
mark2=0 ;
}

}
void setMark3(int a){
if(a>0){
mark3=a;
}else{
mark3=0 ;
}

// getters

int getMark1(){
return mark1;
}
int getMark2(){
return mark2;
}
int getMark3(){
return mark3;
}

private int total_marks(){


int total = 0;
total = mark1+mark2+mark3;
return total;
}

public double percentage(){


double per;
double a;
a=(mark1+mark2+mark3)/300;
per= a*100;

return per;
}

RUNNER FILE

public class marksrunner{


public static void main(String[] args) {

marks obj1 = new marks();


marks obj2 = new marks(20,50,70);

double per1= obj1.percentage();


double per2= obj2.percentage();

if(per1>per2){
System.out.println("STUDENT 1 HAS HIGHEST
PERCENTAGE");
}else{
System.out.println("STUDENT 2 HAS HIGHEST
PERCENTAGE");

}
}
}

6. Create an encapsulated Student Class with name, Gpa, subjects[5] and email as data
members. Create
a. Default constructor
b. four- argument constructor
c. verifyArrayLength(?) //private method
d. Display()
e. searchSubject (?)
f. checkProbStatus (?) //Boolean return
g. validateEmail(?) //Boolean return

In the runner create two Student Objects. Compare the GPA of both students and
display the student with higher GPA.
Check if two students are studying same subjects.

SOLUTION CODE:

class Question6student{
private String name;
private double gpa;
int size= 5;
private String[] subjects= new String[size];
private String email;

// default constructor

Question6student(){
// values set to all data members are null
}

// four argument constructor


public Question6student(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];

}
}

// setters
void setName(String n){
name = n;
}
void setGpa(double g){
if(g>0.0 && g<4.0){
gpa=g;
}else{
gpa= 0.0;
}
}
void setEmail(String e){
email = e;
}

void setSubjects(String [] array){

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];

}
}

}
// getters
String getName(){
return name;
}
String getEmail(){
return email;
}
double getGpa(){
return gpa;
}

String [] getSubjects(){
return subjects;
}

// 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
class Q6studentrunner{
public static void main(String[] args) {
String[] array1= {"oop","dsa","stats","linear","pakstudies"};
String[] array2= {"physics","dsa","stats","linear","pakstudies"};

Question6student obj1= new


Question6student("Shayn",3.3,array1,"adgjas@.com");
Question6student obj2= new
Question6student("usman",2.3,array2,"hfkys@.com");

double gpa1= obj1.getGpa();


double gpa2= obj2.getGpa();

if(gpa1>gpa2){
System.out.println("Student1 has greater gpa");
}else{
System.out.println("Student2 has greater gpa");
}

int count = 0;

for(int i = 0;i<array1.length;i++){
for(int j = 0;j<array2.length;j++){
if(array1[i]==array2[j]){
count++;
}
}
}
System.out.println("BOTH STUDENTS HAVE "+ count+" same
subjects to study");

}
}

7. Create an encapsulated 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:
 verifyArrayLength(?) //private method

 display
o This methods displays all the data members of the class
 addADepartment
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 two University Objects.
Compare the number of departments in both universities and print the
uniName with more departments.
Compare Location of both objects and print if both have same location.

SOLUTION CODE:

class university{
private String uniName;
private String location;
private String rectorName;
private 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;
rectorName= recn;
location= "islamabad";

// constructor for 4 argument

public university(String un, String recn,String loc,String[]array){


uniName = un;

rectorName= recn;
location= loc;

if(array.length<departments.length){
for(int i = 0;i<array.length;i++){
array[i]=departments[i];
}
}else{
for(int i = 0;i<departments.length;i++){
departments[i]=array[i];
}

// setter

void setName(String n){


uniName = n;
}

void setLocation(String n){


location = n;
}
void setRectorName(String n){
rectorName = n;
}

void setDepartments(String[]array){
if(array.length<departments.length){
for(int i = 0;i<array.length;i++){
array[i]=departments[i];
}
}else{
for(int i = 0;i<departments.length;i++){
departments[i]=array[i];
}

// getters

String getuniName(){
return uniName;
}

String getRectorName(){
return rectorName;
}
String getLocation(){
return location;
}

String[] getDepartments(){
return departments;
}

// 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 unirunner{
public static void main(String[] args) {

String [] array = new String[20];

array[0]= "maths";
array[1]= "bba";
array[2]= "physics";
array[3]= "EE";

String [] array1 = new String[20];

array1[0]= "BAF";
array1[1]= "bba";
array1[2]= "SE";
array1[3]= "AI";
university obj1 = new university("air", "usman", "wah",array);
university obj2 = new
university("comsats","shayan","islamabad",array1);
String location1 = obj1.getLocation();
String location2 = obj2.getLocation();

if(location1== location2){
System.out.println("both uni have same location");
} else{
System.out.println("both uni have different locations");
}

System.out.println(obj1.getuniName());
System.out.println(obj2.getuniName());
}
}

You might also like