You are on page 1of 39

OOPS LAB QUESTIONS(ANSWERS)

1.)​ 1.)

Solution in next Page ->


Solution
import​ java.io.*;
import​ java.math.*;
import​ java.security.*;
import​ java.text.*;
import​ java.util.*;
import​ java.util.concurrent.*;
import​ java.util.regex.*;

public​ ​class​ Solution {

​private​ ​static​ ​final​ Scanner scanner = ​new​ Scanner(System.in);

​public​ ​static​ ​void​ main(String[] args) {


​int​ N = scanner.nextInt();
​for​(​int​ i=​1​;i<=​10​;i++)
System.out.println(​""​+N+​" x "​+i+​" = "​+N*i);

scanner.close();
}
}

2.)Develop code with a method in MyFirstClass a) factorial () that computes the factorial of
a given number. Modularize the design to class and package level. Note: 1. Draw the class
diagram and then implement. 2. Hard code the input
Solution

Class Level Modularisation


package programs;

class Fact {
int i,r=1;
public void factorialFind(int n)
{
for(i=1;i<=n;i++)
{
r = r*i;
}
System.out.println("Factorial = "+r);
}
}

public class OneTwo {

public static void main(String[] args) {


Fact d = new Fact();
d.factorialFind(5);

Package Level Modularisation

// First Package
package programspl;

public class TwoPL {


int i,r=1;
public void factorialFind(int n)
{
for(i=1;i<=n;i++)
{
r = r*i;
}
System.out.println("Factorial = "+r);
}
}

//Second Package
package programs;
import programspl.TwoPL;
public class TwoP {

public static void main(String[] args) {


TwoPL d = new TwoPL();
d.factorialFind(5);
}

}
2.)

Solution
package programs;

import java.util.Scanner;

class Student
{
long id;
String name,gender,department;
public void setValues(long i,String n,String g,String d)
{
id = i;
name = n;
gender = g;
department = d;
}
public String toString()
{
return "ID: "+id+"\nName: "+name+"\nGender: "+gender+"\nDepartment:
"+department;
}
}
public class TwoOne {

public static void main(String[] args) {


long id;
String name,gender,department;
Student a = new Student();
Student b = new Student();
Scanner s = new Scanner(System.in);
id = s.nextLong();
s.nextLine();
name = s.nextLine();
gender = s.nextLine();
department = s.nextLine();
a.setValues(id, name, gender, department);
id = s.nextLong();
s.nextLine();
name = s.nextLine();
gender = s.nextLine();
department = s.nextLine();
b.setValues(id, name, gender, department);
System.out.println(a);
System.out.println(b);
s.close();
}
}
Solution in next Page-->

Solution

package programs;

import java.util.Scanner;

class QuadraticEquation
{
int a,b,c;
double x1,x2,d;
public void setValues(int a,int b,int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public double getDiscriminant()
{
d = Math.pow(b, 2)-(4*a*c);
return d;
}
public double getRoot1()
{
x1 = ((-b)+Math.sqrt(d))/(2*a);
return x1;
}
public double getRoot2()
{
x2 = ((-b)-Math.sqrt(d))/(2*a);
return x2;
}
public int getA()
{
return a;
}
public int getB()
{
return b;
}
public int getC()
{
return c;
}
}
public class TwoTwo {

public static void main(String[] args) {


int a,b,c;
Scanner s = new Scanner(System.in);
System.out.println("Enter the values of a,b,c in ax^2+bx+c=0 to find the
equation's roots :");
a = s.nextInt();
b = s.nextInt();
c = s.nextInt();
QuadraticEquation k = new QuadraticEquation();
k.setValues(a, b, c);
System.out.println("In ax^2+bx+c=0 the values of a,b,c are:\na =
"+k.getA()+"\tb = "+k.getB()+"\tc = "+k.getC()+"\nDiscriminant =
"+k.getDiscriminant()+"\nRoots are x1 = "+k.getRoot1()+"\tx2 = "+k.getRoot2());
s.close();
}

3.)

Solution

Class Level Modularisation


package programs;

import java.util.Scanner;
class Factorial
{
int p=1,i;
public void findFactorail(int n)
{
for(i=1;i<=n;i++)
{
p = p*i;
}
System.out.println("The Factorial of "+n+" is "+p);
}
}
public class ThreeOne {

public static void main(String[] args) {


int n;
Scanner s = new Scanner(System.in);
System.out.println("Enter the Number to which we need to find the
factorial : ");
n = s.nextInt();
Factorial k = new Factorial();
k.findFactorail(n);
s.close();
}

}
Package Level Modularisation
//First Package
package programspl;

public class ThreeOnePL {


int p=1,i;
public void findFact(int n)
{
for(i=1;i<=n;i++)
{
p = p*i;
}
System.out.println("The Factorial of "+n+" is "+p);
}

}
------------------------------------------------------------------------------------------------------------
//Second Package
package programs;
import java.util.Scanner;
import programspl.ThreeOnePL;
public class ThreeOneP {

public static void main(String[] args) {


int n;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number to find it's factorial :");
n = s.nextInt();
ThreeOnePL k = new ThreeOnePL();
k.findFact(n);
s.close();
}
}
4.)

Solution

package programs;
import java.util.Scanner;
class Account
{
private long id;
private double balance;
public Account()
{
id = 0;
balance = 0;
}
public Account(long i,double b)
{
id = i;
balance = b;
}
public void setID(long i)
{
id = i;
}
public void setBalance(double b)
{
balance = b;
}
public long getID()
{
return id;
}
public double getBalance()
{
return balance;
}
public boolean check()
{
if(id>=0&&balance>=0)
return true;
else
return false;
}
public void withdraw(double w)
{
balance = balance-w;
}
public void deposit(double d)
{
balance = balance+d;
}
public void display()
{
System.out.println("Details:\nID:"+id+"\nBalance:"+balance);
}
}
public class FourOne {

public static void main(String[] args) {


int n,c=-1,choice;
long i;
double b,deposit,withdraw;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of accounts which can be created at
maximum : ");
n = s.nextInt();
Account[] d = new Account[n];
while(true)
{
System.out.println("MENU:\nOption 1:Create a default
Account\nOption 2:Create an account with specified id and balance\nOption
3:Withdraw\nOption 4:Deposit\nOption 5:Check if ID and Balance of an account and
check if they're positive\nOption 6:Get ID for a specified account\nOption 7:Get the
balance for a specific account\nOption 8:Set the ID for a specific account\nOption 9:Set
the balance for a specific account\nOption 10:Display the details for a certain
account\nEnter You Choice:");
choice = s.nextInt();
switch(choice)
{
case 1 : c++;
d[c] = new Account();
System.out.println("A Default Account has been created.");
break;
case 2 : c++;
System.out.println("Enter the id and balance with which the account
should be created : ");
i = s.nextLong();
b = s.nextDouble();
d[c] = new Account(i,b);
System.out.println("An Account has been created with ID:"+i+"
Balance:"+b);
break;
case 3 : System.out.println("Enter the serial number for the account
: ");
c = s.nextInt();
System.out.println("Enter the amount to be withdrawn :");
withdraw = s.nextDouble();
d[c].withdraw(withdraw);
System.out.println("The Amount "+withdraw+" has been withdrawed
from the account.");
break;
case 4 : System.out.println("Enter the serial number for the account
: ");
c = s.nextInt();
System.out.println("Enter the amount to be deposited :");
deposit = s.nextDouble();
d[c].deposit(deposit);
System.out.println("The Amount "+deposit+" has been deposited
from the account.");
break;
case 5 : System.out.println("Enter the serial number for the account
: ");
c = s.nextInt();
if(d[c].check())
System.out.println("YES");
else
System.out.println("NO");
break;
case 6 : System.out.println("Enter the serial number for the account
: ");
c = s.nextInt();
System.out.println("ID :"+d[c].getID());
break;
case 7 : System.out.println("Enter the serial number for the account
: ");
c = s.nextInt();
System.out.println("Balance : "+d[c].getBalance());
break;
case 8 : System.out.println("Enter the serial number for the account
: ");
c = s.nextInt();
i = s.nextLong();
d[c].setID(i);
System.out.println("The id:"+i+" for the account has been set.");
break;
case 9 : System.out.println("Enter the serial number for the account
: ");
c = s.nextInt();
b = s.nextDouble();
d[c].setBalance(b);
System.out.println("The balance:"+b+" has been set.");
break;
case 10 : System.out.println("Enter the serial number for the
account : ");
c = s.nextInt();
d[c].display();
break;
default :System.out.println("The process has been stopped.");
s.close();
System.exit(0);
}
}

Solution
package programs;
import java.util.Scanner;
class MyPoint
{
double x,y,distance1,distance2;
public MyPoint()
{
x = 0;
y = 0;
}
public MyPoint(double X,double Y)
{
x = X;
y = Y;
}
public double distance(double X,double Y,MyPoint a)
{
distance1 = Math.sqrt(Math.pow(a.x-X, 2)+Math.pow(a.y-y, 2));
return distance1;
}
public double distance(MyPoint a,MyPoint b)
{
distance2 = Math.sqrt(Math.pow(a.x-b.x, 2)+Math.pow(a.y-b.y, 2));
return distance2;
}
}
public class FourTwo {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
double x,y;
System.out.println("Enter the x co-ordinate of the point : ");
x = s.nextDouble();
System.out.println("Enter the y co-ordinate of the point : ");
y = s.nextDouble();
MyPoint a = new MyPoint();
MyPoint b = new MyPoint(x,y);
System.out.println("The Distance between (0,0) and the input point is
"+b.distance(a,b));
s.close();
}

}
6.) 2.)
Solution in next Page-->
Solution

import​ java.io.*;
import​ java.util.*;
import​ java.text.*;
import​ java.math.*;
import​ java.util.regex.*;

public​ ​class​ Solution {

​public​ ​static​ ​void​ main(String[] args) {


Scanner in = ​new​ Scanner(System.in);
String S = in.next();
​int​ start = in.nextInt();
​int​ end = in.nextInt();
System.out.println(S.substring(start,end));
}
}
7.)

Solution
package programs;
import java.util.Scanner;
class Shape
{
private String fillColor;
private String borderColor;
private boolean fill;
private double borderWidth;
public void setValues(String fc,String bc,boolean f,double bw)
{
fillColor = fc;
borderColor = bc;
fill = f;
borderWidth = bw;
}
public String getFC()
{
return fillColor;
}
public String getBC()
{
return borderColor;
}
public boolean getF()
{
return fill;
}
public double getBW()
{
return borderWidth;
}
}
class Rectangle extends Shape
{
double length,width;
public void setValues(double l,double w)
{
length = l;
width = w;
}
public double getL()
{
return length;
}
public double getW()
{
return width;
}
}
class Circle extends Shape
{
double radius;
public void setRadius(double r)
{
radius = r;
}
public double getRadius()
{
return radius;
}
}
public class SevenOne {

public static void main(String[] args) {


Rectangle rectangle = new Rectangle();
Circle circle = new Circle();
String fc,bc;
boolean f;
double bw,l,w,r;
Scanner s = new Scanner(System.in);
System.out.println("Enter the Fill Color, Border Color, Fill and
Border Width of rectangle");
fc = s.nextLine();
bc = s.nextLine();
f = s.nextBoolean();
bw = s.nextDouble();
rectangle.setValues(fc, bc, f, bw);
System.out.println("Enter the Length and width of rectangle");
l = s.nextDouble();
w = s.nextDouble();
rectangle.setValues(l, w);
System.out.println("Enter the Fill Color, Border Color, Fill and
Border Width of Circle");
fc = s.nextLine();
bc = s.nextLine();
s.nextLine();
f = s.nextBoolean();
bw = s.nextDouble();
circle.setValues(fc, bc, f, bw);
System.out.println("Enter the Radius of Circle");
r = s.nextDouble();
circle.setRadius(r);
System.out.println("Details of
Rectangle:\nFillColor:"+rectangle.getFC()+"\nBorderColor:"+rectangle.getBC()
+"\nFill:"+rectangle.getF()+"\nBorderWidth:"+rectangle.getBW()+"\nLength:"+r
ectangle.getL()+"\nWidth:"+rectangle.getW());
System.out.println("Details of
Circle:nFillColor:"+circle.getFC()+"\nBorderColor:"+circle.getBC()+"\nFill:"+circ
le.getF()+"\nBorderWidth:"+circle.getBW()+"\nRadius:"+circle.getRadius());
s.close();
}

}
Solution

package programs;
import java.util.Scanner;
class Shape1
{
private String fillColor;
private String borderColor;
private boolean fill;
private double borderWidth;
public void setValues(String fc,String bc,boolean f,double bw)
{
fillColor = fc;
borderColor = bc;
fill = f;
borderWidth = bw;
}
public String getFC()
{
return fillColor;
}
public String getBC()
{
return borderColor;
}
public boolean getF()
{
return fill;
}
public double getBW()
{
return borderWidth;
}
}
class TwoDShape extends Shape1
{
public void display()
{
System.out.println("2D Shape");
}
}
class ThreeDShape extends Shape1
{
public void display()
{
System.out.println("3D Shape");
}
}
class Rectangle1 extends TwoDShape
{
double length,width;
public void setValues(double l,double w)
{
length = l;
width = w;
}
public double getL()
{
return length;
}
public double getW()
{
return width;
}
}
class Circle1 extends TwoDShape
{
double radius;
public void setRadius(double r)
{
radius = r;
}
public double getRadius()
{
return radius;
}
}
class Cuboid extends ThreeDShape
{
double length,breadth,height;
public void setValues(double l,double b,double h)
{
length = l;
breadth = b;
height = h;
}
public double getLength()
{
return length;
}
public double getBreadth()
{
return breadth;
}
public double getHeight()
{
return height;
}
}
public class SevenTwo {

public static void main(String[] args) {


Rectangle1 rectangle = new Rectangle1();
Circle1 circle = new Circle1();
Cuboid cuboid = new Cuboid();
String fc,bc;
boolean f;
double bw,l,w,r,b,h;
Scanner s = new Scanner(System.in);
System.out.println("Enter the Fill Color, Border Color, Fill and
Border Width of rectangle");
fc = s.nextLine();
bc = s.nextLine();
f = s.nextBoolean();
bw = s.nextDouble();
rectangle.setValues(fc, bc, f, bw);
System.out.println("Enter the Length and width of rectangle");
l = s.nextDouble();
w = s.nextDouble();
rectangle.setValues(l, w);
System.out.println("Enter the Fill Color, Border Color, Fill and
Border Width of Circle");
fc = s.nextLine();
bc = s.nextLine();
s.nextLine();
f = s.nextBoolean();
s.nextLine();
bw = s.nextDouble();
circle.setValues(fc, bc, f, bw);
System.out.println("Enter the Radius of Circle");
r = s.nextDouble();
circle.setRadius(r);
System.out.println("Enter the Fill Color, Border Color, Fill and
Border Width of Cuboid");
s.nextLine();
fc = s.nextLine();
bc = s.nextLine();
f = s.nextBoolean();
s.nextLine();
bw = s.nextDouble();
cuboid.setValues(fc, bc, f, bw);
System.out.println("Enter the length,breadth and height of the
cuboid:");
l = s.nextDouble();
b = s.nextDouble();
h = s.nextDouble();
cuboid.setValues(l, b, h);
System.out.println("Details of
Rectangle:\nFillColor:"+rectangle.getFC()+"\nBorderColor:"+rectangle.getBC()
+"\nFill:"+rectangle.getF()+"\nBorderWidth:"+rectangle.getBW()+"\nLength:"+r
ectangle.getL()+"\nWidth:"+rectangle.getW());
System.out.println("Details of
Circle:\nFillColor:"+circle.getFC()+"\nBorderColor:"+circle.getBC()+"\nFill:"+cir
cle.getF()+"\nBorderWidth:"+circle.getBW()+"\nRadius:"+circle.getRadius());
System.out.println("Details of
Cuboid:\nFillColor:"+cuboid.getFC()+"\nBorderColor:"+cuboid.getBC()+"\nFill:"
+cuboid.getF()+"\nBorderWidth:"+cuboid.getBC()+"\nLenth:"+cuboid.getLengt
h()+"\nBreadth:"+cuboid.getBreadth()+"\nHeight:"+cuboid.getHeight());
s.close();
}

}
(Next Question in next Page)
8.)
Solution
package programs;

import java.util.Scanner;

class GeometricObject
{
String color;
boolean fill;
}
class Triangle extends GeometricObject
{
double side1,side2,side3,p,sp,area;
public Triangle()
{
side1 = 1;
side2 = 1;
side3 = 1;
}
public Triangle(double s1,double s2,double s3)
{
side1 = s1;
side2 = s2;
side3 = s3;
}
public double getS1()
{
return side1;
}
public double getS2()
{
return side2;
}
public double getS3()
{
return side3;
}
public double getPerimeter()
{
p = side1+side2+side3;
return p;
}
public double getArea()
{
sp = p/2;
area = Math.sqrt(sp*(sp-side1)*(sp-side2)+(sp-side3));
return area;
}
public String toString()
{
return "Triangle: side1 = "+side1+" side2 = "+side2+" side3 =
" +side3;
}
}
public class EightOne {

public static void main(String[] args) {


double s1,s2,s3;
Scanner s = new Scanner(System.in);
System.out.println("Enter the sides of the triangle : ");
s1 = s.nextDouble();
s2 = s.nextDouble();
s3 = s.nextDouble();
Triangle t1 = new Triangle();
Triangle t2 = new Triangle(s1,s2,s3);
s.nextLine();
System.out.println("Enter the fill color and fill boolean for
default triangle:");
t1.color = s.nextLine();
t1.fill = s.nextBoolean();
System.out.println("Enter the fill color and fill boolean for input
triangle:");
s.nextLine();
t2.color = s.nextLine();
t2.fill = s.nextBoolean();
System.out.println("default triangle\nPerimeter :
"+t1.getPerimeter()+" Area : "+t1.getArea());
System.out.println(t1);
System.out.println("Color:"+t1.color+" Fill:"+t1.fill);
System.out.println("triangle with specified
dimensions\nPerimeter : "+t2.getPerimeter()+" Area : "+t2.getArea());
System.out.println(t2);
System.out.println("Color:"+t2.color+" Fill:"+t2.fill);
s.close();
}

}
Solution

package programs;

import java.util.Scanner;

class Person
{
String name,address,email;
long number;
public void setDetials(String nm,String e,String a,long n)
{
name = nm;
address = a;
email = e;
number = n;
}
}
class Student1 extends Person
{
double cgpa;
public void setCGPA(double c)
{
cgpa = c;
}
public String getCGPA()
{
return "CGPA:"+cgpa;
}
public String printDetail()
{
return "Person
Details:\nName:"+name+"\tAddress:"+address+"\tEmail:"+email+"\tNumb
er:"+number+"\tCGPA:"+cgpa;
}
}
class Employee1 extends Person
{
double salary;
String dateHired;
public void setValues(double s,String dh)
{
salary = s;
dateHired = dh;
}
public double getSalary()
{
return salary;
}
public String getdateHired()
{
return dateHired;
}
public String printDetail()
{
return "Person
Details:\nName:"+name+"\tAddress:"+address+"\tEmail:"+email+"\tNumb
er:"+number+"\tSalary:"+salary+"\tDateHired:"+dateHired;
}
}
class Faculty extends Employee1
{
String designation;
public void setDes(String d)
{
designation = d;
}
public String getDesignation()
{
return designation;
}
public String printDetail()
{
return "Person
Details:\nName:"+name+"\tAddress:"+address+"\tEmail:"+email+"\tNumb
er:"+number+"\tSalary:"+salary+"\tDateHired:"+dateHired+"\tDesignation
:"+designation;
}
}
class Staff extends Employee1
{
double hoursWorked;
public void setHW(double hw)
{
hoursWorked = hw;
}
public double getHoursWorked()
{
return hoursWorked;
}
public String printDetail()
{
return "Person
Details:\nName:"+name+"\tAddress:"+address+"\tEmail:"+email+"\tNumb
er:"+number+"\tSalary:"+salary+"\tDateHired:"+dateHired+"\tHoursWork
ed:"+hoursWorked;
}
}
public class EightTwo {
public static void main(String[] args) {
Student1 s1 = new Student1();
Faculty f1 = new Faculty();
Staff s2 = new Staff();
Scanner s = new Scanner(System.in);
String name,email,address,designation,dateHired;
double cgpa,hoursWorked,salary;
long number;
name = s.nextLine();
email = s.nextLine();
address = s.nextLine();
number = s.nextLong();
cgpa = s.nextDouble();
s1.setDetials(name, email, address, number);
s1.setCGPA(cgpa);
s.nextLine();
name = s.nextLine();
email = s.nextLine();
address = s.nextLine();
number = s.nextLong();
s.nextLine();
designation = s.nextLine();
dateHired = s.nextLine();
salary = s.nextDouble();
f1.setDetials(name, email, address, number);
f1.setDes(designation);
f1.setValues(salary, dateHired);
s.nextLine();
name = s.nextLine();
email = s.nextLine();
address = s.nextLine();
number = s.nextLong();
hoursWorked = s.nextDouble();
s2.setDetials(name, email, address, number);
s2.setHW(hoursWorked);
s2.setValues(salary, dateHired);
System.out.println(s1.printDetail());
System.out.println(f1.printDetail());
System.out.println(s2.printDetail());
s.close();
}
}

You might also like