COMSATS UNIVERSITY ISLAMABAD
OBJECT ORIENTED PROGRAMMING – CSC241
Submitted To: Maam Saneeha Amir
Submitted by: Maliaqa Khan
Date : 06-03-2022
LAB METHODS AND CONSTRUCTORS
Question1
package com.company;
public class Account {
int balance;
int yearOfOpening;
String cnic;
public Account() {
public Account(int b) {
if (b >= 0) {
balance = b;
} else {
balance = 0;
System.out.println("The value of balance is invalid so default
value is assgined");
}
public Account(int b, int y, String c)
{
if (b >= 0)
{
balance = b;
}
else
{
balance = 0;
System.out.println("The value of balance is invalid so default
value is assgined");
if ( y > 0)
{
yearOfOpening = y;
}
else
{
yearOfOpening = 0000;
System.out.println("The value of year entered is invalid so
default value is assgined");
}
if (c.length() == 14)
{
cnic = c;
}
else
{
cnic = "00000-0000000-0";
System.out.println("CNIC entered is not valid ");
}
}
public void setValues(int b, int y, String c)
{
if (b >= 0)
{
balance = b;
}
else
{
balance = 0;
System.out.println("Balance entered invlaid. \n Default value is
assigned ");
}
if ( y > 0)
{
yearOfOpening = y;
}
else
{
yearOfOpening = 0000;
System.out.println("Year entered invlaid. \n Default value is
assigned ");
}
if (c.length() == 14)
{
cnic = c;
}
else
{
cnic = "00000-0000000-0";
System.out.println("CNIC entered is not valid. \n Default value
assigned ");
}
}
public void display()
{
System.out.println("Account Balance = " + balance);
System.out.println("Year of oppening = " + yearOfOpening);
System.out.println("CNIC = " + cnic);
}
public void withDraw(int amount)
{
balance = balance - amount;
System.out.println("Amount Left: " + balance);
}
public void deposit(int amount)
{
balance = balance + amount;
System.out.println("Total Balance = " + balance);
}
public void calculateAgeOfAccount(int present_age)
{
int ageOfAccount = present_age - yearOfOpening;
System.out.println("Age of account = " + ageOfAccount);
}
}
Question 2
package com.company;
public class Quadratic_Equation
{
int a;
int b;
int c;
public Quadratic_Equation()
{
}
public Quadratic_Equation(int num1, int num2, int num3)
{
if(num1 != 0)
{
a = num1;
}
else
{
System.out.println("Equation is not Quadratic.\n Linear Algebraic
equation");
}
b = num2;
c = num3;
}
public void display()
{
System.out.println("a, b and c are known numbers.\n There values are:
");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
public void setValues(int num1, int num2, int num3)
{
if(a != 0)
{
a = num1;
}
else
{
System.out.println("Equation is not Quadratic.\n Linear Algebraic
equation");
}
b = num2;
c = num3;
}
public double getDiscriminant()
{
double discriminant = (b * b) - 4*a*c;
return discriminant;
}
public boolean checkIfDescriminantIsGretaerThan100()
{
if (getDiscriminant() > 100)
{
return true;
}
else
{
return false;
}
}
}
Question -03
package com.company;
public class Rectangle {
double length;
double width;
// default constructor
public Rectangle()
{
}
// Two argument constructor
public Rectangle(double l, double w)
{
if(l > 0)
{
length = l;
}
else
{
System.out.println("Dimensions cannot be negative");
}
if (w > 0)
{
width = w;
}
else
{
System.out.println("Dimensions cannot be negative");
}
}
public void display()
{
System.out.println("Length = " + length);
System.out.println("Width = " + width);
}
// set values by user
public void setValues(double l, double w)
{
if(l > 0)
{
length = l;
}
else
{
System.out.println("Dimensions cannot be negative");
}
if (w > 0)
{
width = w;
}
else
{
System.out.println("Dimensions cannot be negative");
}
}
// returns area
public double calculateArea()
{
double area = length * width;
return area;
}
public boolean checkSquare()
{
if(length == width)
{
return true;
}
else
{
return false;
}
}
}
Question -04
package com.company;
public class Point {
double x;
double y;
// default constructor
public Point()
{
// two-argument constructor
public Point(int p1, int p2)
{
x = p1;
y = p2;
}
// display method
public void display()
{
System.out.println("x = " + x);
System.out.println("y = " + y);
// set value
public void setValues(double p1, double p2)
{
x = p1;
y = p2;
}
// Move the point from one loaction to another
public void move(double dx, double dy)
{
x = x + dx;
y = y + dy;
System.out.println("After Displacement point = (" + x + "," + y + ")"
);
}
// check if point is on origin (0,0)
public boolean checkOrigin()
{
if(( x == 0.0) && (y == 0.0))
{
return true;
}
else
{
return false;
}
}
}
Question -05
package com.company;
public class Book {
String author;
String [] chapterNames = new String[5];
public Book()
{
}
public Book(String a, String [] chapN)
{
author = a;
chapterNames = chapN;
}
public void setValues(String a, String [] chapNames)
{
author = a;
chapterNames = chapNames;
}
public void display()
{
System.out.print("Author name : " + author);
System.out.println();
System.out.println("Chapter names : ");
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 chapter)
{
for(int i = 0; i <= 5; i++)
{
if(chapterNames[i].equals(chapter))
{
return true;
}
}
return false;
}
}
Question -06
package com.company;
public class Student {
String name;
Double Gpa;
String [] subjects = new String[5];
String email;
public Student()
{
name = "abc";
Gpa = 0.0;
email = "[email protected]";
}
public Student(String n, Double gpa, String[] s, String e)
{
name = n;
Gpa = gpa;
subjects = s;
email = e;
}
public void display()
{
System.out.println("Name: " + name);
System.out.println("Gpa: " + Gpa);
System.out.println("Email: " + email);
System.out.println("Subjects: ");
for(int i = 0; i < 5; i++)
{
System.out.println(subjects[i]);
}
System.out.println();
}
public void setValues(String n, Double gpa, String[] s, String e)
{
name = n;
Gpa = gpa;
subjects = s;
email = e;
}
public boolean searchSubject(String subToBeSearched)
{
for(int i = 0; i < 5; i++)
{
if(subjects[i] == subToBeSearched)
{
return true;
}
}
return false;
}
public boolean checkProbStatus()
{
if(Gpa < 2.0)
{
return true;
}
else
{
return false;
}
}
public boolean validateEmail()
{
if (email.contains("@") && email.contains(".com"))
{
return true;
}
else
{
return false;
}
}
}
Question -07
package com.company;
public class University {
// Data members
String uniName;
String location;
String rectorName;
int size = 20;
String [] departments = new String[size];
// Constructors
// No Argument constructor
public University()
{
// Two Argument Constructor
public University(String uniN, String recN)
{
uniName = uniN;
rectorName = recN;
}
// A constructor setting values of all parameters
public University(String name, String recName, String loc, String[] deps)
{
uniName = name;
rectorName = recName;
location = loc;
departments = deps;
// Methods
public void display()
{
System.out.println(" University : " + uniName);
System.out.println("Rector name : " + rectorName);
System.out.println("Location : " + location);
System.out.println("Departments: ");
for(int i = 0; i < departments.length; i++)
{
System.out.println(departments[i]);
}
}
public void addDepartments(String addDep)
{
if(departments[19] == null)
{
for(int i = 0; i < size; i ++)
{
if(departments[i] == null)
{
departments[i] = addDep;
break;
}
}
}
else
{
System.out.println("departments full ");
}
}
public boolean checkIfLocatedInCapital()
{
if ((location.equalsIgnoreCase("Islamabad")) ||
(location.equalsIgnoreCase("Quetta") )||
(location.equalsIgnoreCase("Lahore"))||
(location.equalsIgnoreCase("Peshawar")) ||
(location.equalsIgnoreCase("Karachi")) ||
(location.equalsIgnoreCase("Gilgit")))
{
return true;
}
else
{
return false;
}
}
public boolean searchDepartment(String dep)
{
for(int i = 0; i < departments.length; i++)
{
if (departments[i].equalsIgnoreCase(dep))
{
return true;
}
}
return false;
}
}
RUNNER CLASS FOR ALL QUESTIONS
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// ======================================= A C C O U N T
===========================================
// creating objects
Account a1 = new Account(10000);
Account a2 = new Account(20000, 2012, "61101-5678965-9");
Account a3 = new Account();
Scanner input = new Scanner(System.in);
System.out.println("Enter Account details for a3: ");
System.out.println("Balance :");
a3.balance = input.nextInt();
System.out.println("Year of oppening");
a3.yearOfOpening = input.nextInt();
System.out.println("CNIC : ");
a3.cnic = input.next();
a1.withDraw(9000);
a1.deposit(7200);
// ====================== Q U A D R A T I C E Q U A T I O N
=====================
Quadratic_Equation eq1 = new Quadratic_Equation(10, 20, 30);
Quadratic_Equation eq2 = new Quadratic_Equation();
eq1.display();
System.out.println(eq1.getDiscriminant());
System.out.println(eq1.checkIfDescriminantIsGretaerThan100());
eq2.setValues(3, 4, 5);
eq2.display();
System.out.println(eq2.getDiscriminant());
System.out.println(eq2.checkIfDescriminantIsGretaerThan100());
// =========================== R E C T A N G L E
==========================
// Default constructor object
Rectangle r1 = new Rectangle();
// Object with 2 argument constructor
Rectangle r2 = new Rectangle(40, 10);
r1.setValues(10,10);
System.out.println("Data of rectangle 1 : ");
r1.display();
System.out.println("Area of rectangle 1 = " + r1.calculateArea());
if(r1.checkSquare())
{
System.out.println("Rectangle 1 is a square" );
}
else
{
System.out.println("yes it is a rectangle");
}
System.out.println();
System.out.println("Data of rectangle 1 : ");
r2.display();
System.out.println("Area of rectangle 2 : " + r2.calculateArea());
// ============================ P O I N T ============================
// default constructor object
Point p1 = new Point();
// Two argument constructor object
Point p2 = new Point(2,3);
// display
p1.setValues(0,0);
System.out.println("POINT 1");
p1.display();
System.out.println("POINT 2");
p2.display();
// CHECK FOR ORIGIN
System.out.println("Origin = "+p1.checkOrigin());
System.out.println("Origin = "+p2.checkOrigin());
// MOVE
p1.move(3,4);
p2.move(2.5,1.5);
System.out.println();
// =========================== B O O K ===========================
String [] book2chps = {"Acceleration", "Velocity", "Time", "Gravity",
"Space"};
String [] book1chps = {"One", "Two", "Three", "Four", "Five"};
// Object with default constructor
Book b1 = new Book();
// Object with two argument constructor
Book b2 = new Book("Roald Dhal", book2chps);
// Object 1
b1.setValues("", book1chps);
b1.display();
System.out.println("Does author name starts with 'A' ? " +
b1.checkIfAuthorNameStartsWithA());
System.out.println("Search chapter One = " +
b1.searchChapter("One"));
System.out.println();
// Object 2
b2.display();
System.out.println("Does author name starts with 'A' ? " +
b2.checkIfAuthorNameStartsWithA());
System.out.println("Search chapter Velocity = " +
b2.searchChapter("Velocity"));
System.out.println();
// ===================== S T U D E N T ========================
String [] stu1subs = {"one", "two", "three", "four", "five"};
String [] stu2subs = {"DSA", "OOP", "MULICAL", "COMM SKILLS",
"Genetics"};
// Object with default constructor
Student s1 = new Student();
// Object with four- argument constructor
Student s2 = new Student();
// OBJECT 1
s1.subjects = stu1subs;
s1.display();
System.out.println("Student 1 is on probation ? " +
s1.checkProbStatus());
System.out.println("Physics is registered by student 1? " +
s1.searchSubject("Physics"));
// OBJECT 2
s2.setValues("Malaika", 3.0, stu2subs, "[email protected]");
s2.display();
System.out.println("Student 2 is on probation ? " +
s2.checkProbStatus());
System.out.println("DSA is registered by student 2? " +
s2.searchSubject("DSA"));
// =========================== U N I V E R S I T Y
======================
// object with default constructor
University u1 = new University();
// object with 2 argument construcor
University u2 = new University("COMSATS", "AISHA TALAT");
// object with all args
University u3 = new University("IQRA", "AYESHA MUSHTAQ", "ISLAMABAD",
new String[] {"CS", "SE", "BBA", "PSYCOLOGY", "MATH"});
System.out.println("Is " + u3.uniName + " located in fed or prov
capital ? " + u3.checkIfLocatedInCapital());
System.out.println("Is " + u3.uniName + " have department SE " +
u3.searchDepartment("SE"));
u3.addDepartments("PHYSICS");
u3.display();
}