Lab Methods and Constructors TASK OOP
Lab Methods and Constructors TASK OOP
Date : 06-03-2022
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 ");
}
}
}
Question 2
package com.company;
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;
}
}
}
Question -03
package com.company;
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");
}
}
if (w > 0)
{
width = w;
}
else
{
System.out.println("Dimensions cannot be negative");
}
}
// returns area
public double calculateArea()
{
double area = length * width;
return area;
}
}
Question -04
package com.company;
// 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;
}
String author;
String [] chapterNames = new String[5];
public Book()
{
}
public Book(String a, String [] chapN)
{
author = a;
chapterNames = chapN;
}
else
{
return false;
}
String name;
Double Gpa;
String [] subjects = new String[5];
String email;
public Student()
{
name = "abc";
Gpa = 0.0;
email = "xyz@gmail.com";
}
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();
}
// Data members
String uniName;
String location;
String rectorName;
int size = 20;
String [] departments = new String[size];
// Constructors
// No Argument constructor
public University()
{
// Methods
System.out.println("Departments: ");
for(int i = 0; i < departments.length; i++)
{
System.out.println(departments[i]);
}
}
return true;
}
}
return false;
}
}
RUNNER CLASS FOR ALL QUESTIONS
package com.company;
import java.util.Scanner;
// ======================================= 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();
a1.withDraw(9000);
a1.deposit(7200);
// ====================== Q U A D R A T I C E Q U A T I O N
=====================
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
==========================
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 ============================
// display
p1.setValues(0,0);
System.out.println("POINT 1");
p1.display();
System.out.println("POINT 2");
p2.display();
System.out.println("Origin = "+p2.checkOrigin());
// MOVE
p1.move(3,4);
p2.move(2.5,1.5);
System.out.println();
// =========================== B O O K ===========================
// 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 ========================
// 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, "maliaqakhan@gmail.com");
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
======================