Professional Documents
Culture Documents
1.) 1.)
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 Fact {
int i,r=1;
public void factorialFind(int n)
{
for(i=1;i<=n;i++)
{
r = r*i;
}
System.out.println("Factorial = "+r);
}
}
// First Package
package programspl;
//Second Package
package programs;
import programspl.TwoPL;
public class TwoP {
}
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 {
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 {
3.)
Solution
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 {
}
Package Level Modularisation
//First Package
package programspl;
}
------------------------------------------------------------------------------------------------------------
//Second Package
package programs;
import java.util.Scanner;
import programspl.ThreeOnePL;
public class ThreeOneP {
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 {
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 {
}
6.) 2.)
Solution in next Page-->
Solution
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
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 {
}
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 {
}
(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 {
}
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();
}
}