Professional Documents
Culture Documents
Lab Task 4
SUBMITTED TO:
SUBMITTEED BY:
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:
// default constructor
public rectangle(){
length = 20;
width = 40;
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
//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
// setters
}
void setY(double a){
y = a;
// getters
double getX(){
return x;
}
double getY(){
return y;
void display(){
System.out.println("THE VALUE OF X IS"+ x);
System.out.println("THE VALUE OF Y IS"+ y);
}
RUNNER FILE
// 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
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;
}
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) {
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
// methods
void display(){
System.out.println("THE BALANCE IS" + balance);
}
}
RUNNER FILE
public class accountrunner{
public static void main(String[] args) {
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 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;
}
return per;
}
RUNNER FILE
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
}
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;
}
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]);
}
}
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;
boolean checkProb(){
if(gpa>=2.0){
return true;
}else{
return false;
}
}
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"};
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");
}
}
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";
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 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;
}
}
if(departments[i]==dep){
flag = true;
break;
}else{
flag = false;
}
}
return flag;
}
// Add a department
}
RUNNER FILE
public class unirunner{
public static void main(String[] args) {
array[0]= "maths";
array[1]= "bba";
array[2]= "physics";
array[3]= "EE";
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());
}
}