0% found this document useful (0 votes)
128 views44 pages

Assigment 2 JAVA

The document contains 9 code snippets that demonstrate using classes and objects in Java. The snippets cover creating classes to calculate area and average, passing parameters to constructors, setting object properties, and printing object information. Classes are defined for rectangles, triangles, students, employees and more, with methods to calculate values or set/get properties. Objects are instantiated and their methods/properties accessed to output results.

Uploaded by

Shubham Bende
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views44 pages

Assigment 2 JAVA

The document contains 9 code snippets that demonstrate using classes and objects in Java. The snippets cover creating classes to calculate area and average, passing parameters to constructors, setting object properties, and printing object information. Classes are defined for rectangles, triangles, students, employees and more, with methods to calculate values or set/get properties. Objects are instantiated and their methods/properties accessed to output results.

Uploaded by

Shubham Bende
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 44

1.

Write a program to print the area of a rectangle by creating a class


named Area having two methods. First method named as setDim takes
length and breadth of rectangle as parameters and the second method
named as getArea returns the area of the rectangle. Length and breadth of
rectangle are entered through keyboard.

import java.util.Scanner;

class Area{
private float length,breadth;
public void setDim(float length,float breadth) {
this.breadth = breadth;
this.length = length;
}
public float getArea() {
return (this.breadth*this.length);
}
}

public class TestArea {

public static void main(String[] args) {


float l,b;
Area area=new Area();

Scanner sc = new Scanner(System.in);

System.out.print("Enter a Length : ");


l = sc.nextFloat();

System.out.print("Enter a Breadth : ");


b = sc.nextFloat();

area.setDim(l, b);
System.out.println("\nArea is : "+area.getArea());

sc.close();
}
}
2.Create a class named Student with String variable name and integer
variable roll_no. Assign the value of roll_no as ‘2’ and that of name as
“John” by creating an object of the class Student.

class Student{
private int roll_no;
private String name;
Student(int roll,String name){
this.roll_no = roll;
this.name = name;
}
@Override
public String toString() {
return "Roll No : "+this.roll_no+"\nName : "+this.name;
}
}

public class TestStudent {

public static void main(String[] args) {


// TODO Auto-generated method stub
Student s = new Student(2,"John");
System.out.println(s);
}
}
3.Assign and print the roll number, phone number and address of two
students having names
“Sam” and “John” respectively by creating two objects of class “Student”.

import java.util.Scanner;

class Student{
private int roll_no;
private String name,address,phone_number;
Student(String name){
this.name = name;
}

public int getRoll_no() {


return roll_no;
}

public void setRoll_no(int roll_no) {


this.roll_no = roll_no;
}

public void setAddress(String address) {


this.address = address;
}

public void setPhone_number(String phone_number) {


this.phone_number = phone_number;
}

@Override
public String toString() {
return "Roll No : "+this.roll_no+"\nName : "+this.name+"\nAddres :
"+this.address+"\nPhone Number : "+this.phone_number;
}
}

public class TestStudent2{

public static void main(String[] args) {


String name,phone,address;
int roll;

Scanner sc = new Scanner(System.in);


System.out.println("Enter 1st Student details : ");
System.out.print("Enter a Roll : ");
roll = sc.nextInt();
System.out.print("Enter a Name : ");
name = sc.next();
System.out.print("Enter a Address : ");
sc.nextLine();
address = sc.nextLine();
System.out.print("Enter a Phone Number : ");
phone = sc.next();

Student s1 = new Student(name);


s1.setAddress(address);
s1.setPhone_number(phone);
s1.setRoll_no(roll);

System.out.println("\nEnter 2nd Student details : ");


System.out.print("Enter a Roll : ");
roll = sc.nextInt();
System.out.print("Enter a Name : ");
name = sc.next();
System.out.print("Enter a Address : ");
sc.nextLine();
address = sc.nextLine();
System.out.print("Enter a Phone Number : ");
phone = sc.next();

Student s2 = new Student(name);


s2.setAddress(address);
s2.setPhone_number(phone);
s2.setRoll_no(roll);

System.out.println("\n"+s1);
System.out.println("\n"+s2);

sc.close();
}
}
4.Write a program to print the area and perimeter of a triangle having
sides of 3, 4 and 5 units by creating a class named Triangle without any
parameter in its constructor.

public class Triangle {


private float side1=3,side2=4,side3=5;

Triangle(){
System.out.println("Area : "+(this.side1*this.side2)/2);
System.out.println("Perimeter : "+(this.side1+this.side2+
(Math.sqrt(Math.pow(this.side1, 2)+Math.pow(this.side2, 2)))));
}
public static void main(String[] args) {
new Triangle();
}
}
5.Write a program to print the area and perimeter of a triangle having
sides of 3, 4 and 5 units by creating a class named Triangle with
constructor having the three sides as its parameters.

public class Triangle {


private float side1=3,side2=4,side3=5;

Triangle(float side1,float side2,float side3){


this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
System.out.println("Area : "+(this.side1*this.side2)/2);
System.out.println("Perimeter : "+(this.side1+this.side2+
(Math.sqrt(Math.pow(this.side1, 2)+Math.pow(this.side2, 2)))));
}
public static void main(String[] args) {
new Triangle(3,4,5);
}
}
6.Write a program to print the area of two rectangles having sides (4,5) and
(5,8) respectively by creating a class named Rectangle with a method
named Area which returns the area and length and breadth passed as
parameters to its constructor.

public class Rectangle {


private float length, breadth;

Rectangle(float l,float b) {
this.length = l;
this.breadth = b;
}
public float Area() {
return(this.length * this.breadth);
}

public static void main(String[] args) {


System.out.println("Rectangle 1\nLength : 4 and Breadth : 5");
System.out.println("\nArea is : "+new Rectangle(4, 5).Area());

System.out.println("\nRectangle 2\nLength : 5 and Breadth : 8");


System.out.println("\nArea is : "+new Rectangle(5,8).Area());
}
}
7.Write a program to print the area of a rectangle by creating a class
named Area taking the values of its length and breadth as parameters of its
constructor and having a method named returnArea which returns the
area of the rectangle. Length and breadth of rectangle are entered through
keyboard.

import java.util.Scanner;

class Area2{
private float legnth,breadth;
Area2(float l,float b) {
this.legnth = l;
this.breadth = b;
}
public float returnArea() {
return(this.breadth*this.legnth);
}
}
public class TestArea2 {

public static void main(String[] args) {


float l,b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Length : ");
l = sc.nextFloat();

System.out.print("Enter a breadth : ");


b = sc.nextFloat();

Area2 area = new Area2(l,b);

System.out.println("\nArea of Rectangle : "+area.returnArea());

sc.close();
}
}
8.Print the average of three numbers entered by user by creating a class
named Average having a method to calculate and print the average.

import java.util.Scanner;

class Average{
private int a,b,c;
Average(int a,int b,int c){
this.a = a;
this.b = b;
this.c = c;
}
public float getAverage() {
return ((this.a+this.b+this.c)/3.0f);
}
}
public class TestAverage {

public static void main(String[] args) {


int a,b,c;
Scanner sc = new Scanner(System.in);

System.out.println("Enter 3 Number : ");


a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();

Average avg = new Average(a,b,c);


System.out.println("\nAverge is : "+avg.getAverage());

sc.close();
}

}
9.Write a program that would print the information (name, year of joining,
salary, address) of three employees by creating a class named Employee .
The output should be as follows:
Name Year of joining Address
Robert 1994 64C- WallsStreat
Sam 2000 68D- WallsStreat
John 1999 26B- WallsStreat

import java.util.Scanner;

class Employee{
String name;
int year_of_joining;
float salary;
String address;

public Employee() {
super();
}

public Employee(String name, int year_of_joining, float salary, String address) {


super();
this.name = name;
this.year_of_joining = year_of_joining;
this.salary = salary;
this.address = address;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getYear_of_joining() {


return year_of_joining;
}

public void setYear_of_joining(int year_of_joining) {


this.year_of_joining = year_of_joining;
}

public float getSalary() {


return salary;
}

public void setSalary(float salary) {


this.salary = salary;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}
}
public class TestEmployee {
public static void main(String[] args) {
int year;
String name,address;
float salary;

Scanner sc = new Scanner(System.in);


Employee emp[] = new Employee[3];

System.out.println("Enter a 3 details of Employee : ");


for(int i=0;i<3; i++) {
System.out.println("Employee "+(i+1));
System.out.print("Enter a Name : ");
name = sc.next();
System.out.print("Enter a Year of Joinning : ");
year = sc.nextInt();
System.out.print("Enter a Salary : ");
salary = sc.nextFloat();
System.out.print("Enter a Address : ");
sc.nextLine();
address = sc.nextLine();
System.out.println();
emp[i] = new Employee(name,year,salary,address);

System.out.println("Name\tYear of Joinning Salary\tAddress");


for(int i=0; i<3; i++) {
System.out.println(emp[i].getName()+"\t\t"+emp[i].getYear_of_joining()+"\
t"+emp[i].getSalary()+"\t"+emp[i].getAddress());
}
sc.close();
}
}
10. Write a program by creating an Employee class having the following
methods and print the final salary.
1 - getInfo() which takes the salary, number of hours of work per day of
employee as parameter
2 - AddSal() which adds $10 to salary of the employee if it is less than $500.
3 - AddWork() which adds $5 to salary of employee if the number of hours
of work per day is more than 6 hours.

import java.util.Scanner;

class EmployeeDetail {
private String name;
private float salary, hours;

public EmployeeDetail() {
name = " ";
salary = 0;
hours = 0;
}

public void getInfo(String n, float sal, float hr) {


name = n;
salary = sal;
hours = hr;
}

public float AddSal() {


if(salary<500) {
salary = salary + 10;
}
return salary;
}

public float AddWork() {


if(hours > 6) {
salary = salary + 5;
}
return salary;
}
@Override
public String toString() {
return "\nName : "+this.name+"\nSalary : "+this.salary+"\nHours : "+this.hours;
}
}

public class TestEmployee1


{
public static void main (String[] args)
{
EmployeeDetail emp = new EmployeeDetail();
Scanner sc = new Scanner(System.in);
System.out.println("Enter name");
String name = sc.nextLine();
System.out.println("Enter salary");
float salary = sc.nextFloat();
System.out.println("Enter no. of hours of work");
float hours = sc.nextFloat();

emp.getInfo(name, salary, hours);


salary = emp.AddSal();
salary = emp.AddWork();

System.out.println(emp);

sc.close();
}
}
11. Write a java program to search a name from the list of names accepted
from command line.

import java.util.Scanner;

public class Q11 {

public static void main(String[] args) {


String name;
Scanner sc = new Scanner(System.in);

System.out.println("Enter a Name for Search : ");


name = sc.next();
sc.close();

for(int i=0; i<args.length; i++) {


if(name.compareTo(args[i])==0) {
System.out.println("\n"+name+" is FOUND !!!");
return;
}
}
System.out.println("\n"+name+" is Not FOUND !!!");

}
}
12. Write a program in java to check whether the entered character
through command line argument and check character is alphabet, digit or
space character. If it is alphabet then print whether it is capital or small
alphabet. Also change the alphabet into the reverse case.

public class Q12 {

public static void main(String[] args) {


if(args.length != 1 || args[0].length()!=1) {
System.out.println("Enter only one Character on Command Line");
}else {
char ch = args[0].charAt(0);
if(Character.isDigit(ch)) System.out.println(ch+" is Digit");
else if(Character.isSpaceChar(ch)) System.out.println(ch+" is Space");
else if(Character.isUpperCase(ch))
System.out.println(ch+" is Upper Case Alphabet \
n"+Character.toLowerCase(ch)+" is Lower Case ");
else System.out.println(ch+" is Lower Case Alphabet \
n"+Character.toUpperCase(ch)+" is Upper Case ");
}
}
}
13. Write a program in Java to accept name of cities from the user and sort
them in ascending order (Use Command Line Arguments)

public class Q13 {

public static void main(String[] args) {

for(int i=0; i<args.length; i++)


for(int j=0; j<args.length-i-1;j++)
if(args[j].compareTo(args[j+1])==-1) {
String temp = args[j];
args[j] = args[j+1];
args[j+1] = temp;
}
System.out.println("\nAfter Sorting : ");
for(int i=0; i<args.length; i++)
System.out.println(args[i]);
}
}
14. Define class named square as below:
Data Members: side Methods: area(), perimeter(). Create an object of class
square test it.

import java.util.Scanner;

class Square{
private float side;
Square(){}

Square(float side){
this.side = side;
}

float area() {
return (this.side * this.side);
}
float perimeter() {
return (this.side * 4);
}

public class Q14 {


public static void main(String[] args) {
float side;
Square s;
Scanner sc = new Scanner(System.in);

System.out.print("Enter a side of Square : ");


side = sc.nextFloat();

s = new Square(side);

System.out.println("\nArea of Square : "+s.area());


System.out.println("Perimeter of Square : "+s.perimeter());

sc.close();
}
}
15. Define a class called Employee with the name and salary. Create a 5
employee objects as a sort and print them as per their salary. That is
printing them as per their highest salary.

import java.util.Scanner;

class Employee{
private String name;
private float salary;
Employee(){}
public Employee(String name, float salary) {
super();
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}

}
public class Q15 {

public static void main(String[] args) {


Employee emp[] = new Employee[5];
String name;
float salary;

Scanner sc = new Scanner(System.in);

System.out.println("Enter a Details of 5 Employee");


for(int i = 0; i<5; i++) {
System.out.println("\nEmployee : "+(i+1));
System.out.print("Name : ");
name = sc.next();
System.out.print("Salary : ");
salary = sc.nextFloat();

emp[i] = new Employee(name,salary);


}

for(int i=0; i<5; i++)


for(int j=0; j<5-i-1; j++)
if(emp[j+1].getSalary() > emp[j].getSalary()) {
Employee temp = emp[j];
emp[j] = emp[j+1];
emp[j+1] = temp;
}

System.out.println("\nAfter sorting (Salary wise)");


System.out.println("Name\tSalary");
for(int i=0; i<5; i++)
System.out.println(emp[i].getName()+"\t"+emp[i].getSalary());

sc.close();

}
16. Design a class Employee. Include the following member Data member :
No,
Name, Basic salary,
a. DA(12% of basic) ,
b. HRA(25% of basic)
c. Professional Tax(5% of basic)
Methods : 1)Accept details of employee
2) Calculate the Net Salary
3) Display the Employee details

import java.util.Scanner;

class Employee{
private String name;
private int no;
private float basic_salary;

Employee(String name, int no, float basic_salary) {


this.name = name;
this.no = no;
this.basic_salary = basic_salary;
this.basic_salary = this.basic_salary+(this.basic_salary*(12+25-5)/100);
}

@Override
public String toString() {
return "No : "+this.no+"\nName : "+this.name+"\nTotal Salary : "+this.basic_salary;
}

}
public class Q16 {

public static void main(String[] args) {


int no;
String name;
float salary;

Scanner sc = new Scanner(System.in);

System.out.print("Enter a No : ");
no = sc.nextInt();
System.out.print("Enter a Name : ");
name = sc.next();
System.out.print("Enter a Salary : ");
salary = sc.nextFloat();
Employee emp = new Employee(name,no,salary);

System.out.println("\n"+emp);
sc.close();
}

}
17. Define a class Student with attribute PRN_No, name , College_code.
Define another class Stud_Result with attribute with attribute PRN_No,
Sub_Code, marks_Obtained. Create object array for both classes and write
a method which will return topper whose total marks are maximum.

import java.util.Scanner;

class Student{
private int PRN_NO,College_Code;
private String name;
public int getPRN_NO() {
return PRN_NO;
}
public void setPRN_NO(int pRN_NO) {
PRN_NO = pRN_NO;
}
public int getCollege_Code() {
return College_Code;
}
public void setCollege_Code(int college_Code) {
College_Code = college_Code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Stud_Result{
private int PRN_No, Sub_Code;
private float marks_Obtained;
public int getPRN_No() {
return PRN_No;
}
public void setPRN_No(int pRN_No) {
PRN_No = pRN_No;
}
public int getSub_Code() {
return Sub_Code;
}
public void setSub_Code(int sub_Code) {
Sub_Code = sub_Code;
}
public float getMarks_Obtained() {
return marks_Obtained;
}
public void setMarks_Obtained(float marks_Obtained) {
this.marks_Obtained = marks_Obtained;
}
public static int getTopper(Stud_Result sr[],int n) {
float max = sr[0].getMarks_Obtained();
int prn = sr[0].getPRN_No();
for(int i=1; i<n; i++)
if(max < sr[i].getMarks_Obtained()) {
max = sr[i].getMarks_Obtained();
prn = sr[i].getPRN_No();
}

return prn;
}
}

public class Q17 {

public static void main(String[] args) {


int prn_no,college_no,sub_no;
int n;
float marks;
String name;

Scanner sc = new Scanner(System.in);

System.out.print("How many Student details you wants to enter : ");


n = sc.nextInt();

Student s[] = new Student[n];


Stud_Result sr[] = new Stud_Result[n];

for(int i=0; i<n; i++) {


s[i] = new Student();
sr[i] = new Stud_Result();

System.out.println("\nStudent : "+(i+1));
System.out.print("Enter College code : ");
college_no = sc.nextInt();
s[i].setCollege_Code(college_no);
System.out.print("Enter PRN No : ");
prn_no = sc.nextInt();
s[i].setPRN_NO(prn_no);
sr[i].setPRN_No(prn_no);
System.out.print("Enter Name : ");
name = sc.next();
s[i].setName(name);
System.out.print("Enter Subject Code : ");
sub_no = sc.nextInt();
sr[i].setSub_Code(sub_no);
System.out.print("Enter Marks : ");
marks = sc.nextFloat();
sr[i].setMarks_Obtained(marks);
}

prn_no = Stud_Result.getTopper(sr, n);

System.out.println("\nDetails of Topper\n");
for(int i=0; i<n; i++) {
if(prn_no==s[i].getPRN_NO()) {
System.out.println("College Code : "+s[i].getCollege_Code());
System.out.println("PRN No : "+s[i].getPRN_NO());
System.out.println("Name : "+s[i].getName());
}
}

for(int i=0; i<n; i++) {


if(prn_no==sr[i].getPRN_No()) {
System.out.println("Subject Code : "+sr[i].getSub_Code());
System.out.println("Marks : "+sr[i].getMarks_Obtained());
}
}

sc.close();
}
}
18. Define class staff with data members name,b-date, designation and
salary. Use constructor and method display()-which will display the details
of staff having lowest salary.

import java.util.Scanner;

class Staff{
private String name,b_date, designation;
private float salary;

public Staff(String name, String b_date, String designation, float salary) {


super();
this.name = name;
this.b_date = b_date;
this.designation = designation;
this.salary = salary;
}

static void display(Staff obj[],int n) {


float min = obj[0].salary;
int loc = 0;

for(int i=1; i<n; i++)


if(min > obj[i].salary) {
min = obj[i].salary;
loc = i;
}

System.out.println("Name : "+obj[loc].name);
System.out.println("Birth Date : "+obj[loc].b_date);
System.out.println("Designation : "+obj[loc].designation);
System.out.println("Salary : "+obj[loc].salary);
}
}

public class Q18 {

public static void main(String[] args) {


String name,b_date, designation;
float salary;
int n;

Scanner sc = new Scanner(System.in);

System.out.println("How many details of Staff you wants to enter : ");


n = sc.nextInt();

Staff staff[] = new Staff[n];

for(int i=0; i<n; i++) {


System.out.println("\nStaff : "+(i+1));
System.out.print("Enter a Name : ");
name = sc.next();
System.out.print("Enter a Birth Date : ");
b_date = sc.next();
System.out.print("Enter a Designation : ");
designation = sc.next();
System.out.print("Enter a Salary : ");
salary = sc.nextFloat();

staff[i] = new Staff(name,b_date,designation,salary);


}

System.out.println("\nA Staff having lowest salary is ");


Staff.display(staff, n);

sc.close();

}
19. Define a class to represent a bank account. Include the following
members:
Data Members –
Name of the depositor
Account number
Type of account
Balance amount in the account
Member Functions –
To assign initial values
To deposit an amount
To withdraw an amount after checking the balance
To display name and balance
Write a main program to test the class for handling N customers.

import java.util.Scanner;

class BankAccount {
private String name, typeOfAccount;
private int accountNo;
private float balanceAmount, totalAmount;

public BankAccount(String name, String typeOfAccount, int accountNo, float


balanceAmount) {
super();
this.name = name;
this.typeOfAccount = typeOfAccount;
this.accountNo = accountNo;
this.balanceAmount = balanceAmount;
this.totalAmount = this.balanceAmount;
}

public boolean withdrow(float amount) {


if (this.balanceAmount >= (this.totalAmount - amount)) {
this.totalAmount -= amount;
return true;
} else
return false;
}

public void deposit(float amount) {


this.totalAmount += amount;
}

public int getAccountNo() {


return accountNo;
}

public float checkBalance() {


return this.totalAmount;
}

public class Q19 {

public static void main(String[] args) {


String name, typeOfAccount;
int accountNo;
float balanceAmount,amount=0.0f;
int n, loc;

Scanner sc = new Scanner(System.in);

System.out.println("How many account details you want to enter : ");


n = sc.nextInt();

BankAccount bk[] = new BankAccount[n];

for (int i = 0; i < n; i++) {


System.out.println("\nAccount No : " + (i + 1));
System.out.print("Enter a Name : ");
name = sc.next();
System.out.print("Enter a Account No : ");
accountNo = sc.nextInt();
System.out.print("Enter a Account Type : ");
typeOfAccount = sc.next();
if (typeOfAccount.toLowerCase().compareTo("saving") == 0)
balanceAmount = 5000;
else
balanceAmount = 10000;

bk[i] = new BankAccount(name, typeOfAccount, accountNo,


balanceAmount);
}

while (true) {
System.out.print("\n1. Deposit\n2. Withdrow\n3. Check Balance\n4. EXIT\
nEnter a Choice : ");
int ch = sc.nextInt();

if (ch == 4) {
System.out.println("\nThank You !!!");
break;
}

System.out.print("\nEnter a Account No : ");


accountNo = sc.nextInt();

loc = -1;
for (int i = 0; i < n; i++)
if (accountNo == bk[i].getAccountNo()) {
loc = i;
break;
}
if (loc == -1) {
System.out.println("\nAccount No is NOT FOUND !!!\n");
continue;
}

if (ch != 3) {
System.out.print("Enter a Amount : ");
amount = sc.nextFloat();
}
switch (ch) {
case 1:
bk[loc].deposit(amount);
break;
case 2:
bk[loc].withdrow(amount);
break;
case 3:
System.out.println("Total Amount : " + bk[loc].checkBalance());
break;
default:
System.out.println("\nInvalid Choice !!");
}
}
}
}
20. Write a method add which add two number and two strings. Use
method overloading.

import java.util.Scanner;

class Overload{
public static int add(int a,int b) {
return (a+b);
}
public static String add(String a,String b) {
return (a+b);
}
}

public class Q20 {

public static void main(String[] args) {


int a,b;
String s1,s2;

Scanner sc = new Scanner(System.in);


System.out.println("Enter a 2 Numbers : ");
a = sc.nextInt();
b = sc.nextInt();

System.out.println("\nEnter a 2 Strings : ");


s1 = sc.next();
s2 = sc.next();

System.out.println("\nAddition of to number : "+Overload.add(a, b));


System.out.println("Addition of to Strings : "+Overload.add(s1, s2));

sc.close();
}
}
21. Write a program to demonstrate this keyword.

import java.util.Scanner;

class Area2{
private float legnth,breadth;
Area2(float l,float b) {
this.legnth = l;
this.breadth = b;
}
public float returnArea() {
return(this.breadth*this.legnth);
}
}
public class TestArea2 {

public static void main(String[] args) {


float l,b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Length : ");
l = sc.nextFloat();

System.out.print("Enter a breadth : ");


b = sc.nextFloat();

Area2 area = new Area2(l,b);

System.out.println("\nArea of Rectangle : "+area.returnArea());

sc.close();
}
}
22. Consider that payroll s/w needs to be developed for computerization of
operations of an ABC organization. The organization has employees.
a. Construct a class Employee with following members using private access
specifies.
Emp_id integer
Emp_name String
Basic_Sal double
Hra double
medical double
pf double
pt double
net_sal double
gross_sal double
Use following expressions for
calculations Hra = 50%of basic sal
Pf = 12% of basic sal
Pt = 200
b. Write default constructor parameterized constructor to initialize objects.
c. Input values from user.
d. Write a method to display the net_sal gross_sal using fol.
Formula. Gross_sal = basic_sal + hra + medical
Net_sal = gross_sal – ( pt + pf)
e. Use static variable static method for the Employee class to display total
number

of employees using static method total employees. Modify the employee


class to implement auto generation of employee id.

import java.util.Scanner;

class Employee{
private Integer Emp_id;
private String Emp_name;
private double Basic_Sal,Hra,medical,pf,pt,net_sal,gross_sal;
private static int count=0;
public Employee(String emp_name, double basic_Sal, double medical) {
super();
this.Emp_id = ++count;
Emp_name = emp_name;
Basic_Sal = basic_Sal;
this.medical = medical;
Hra = (Basic_Sal * 50)/100;
pf = (Basic_Sal * 12) /100;
pt = 200;
gross_sal = basic_Sal + Hra + this.medical;
net_sal = gross_sal - (pf+pt);
}

public static int getCount() {


return count;
}

public static void display(Employee emp[]) {


System.out.println("ID\tName\tBasic Salary\tMedical\tHRA\tPF\tPT\tGross Salary\
tNet Salary");
for(int i=0; i<count; i++) {
System.out.println(emp[i].Emp_id+"\t"+emp[i].Emp_name+"\
t"+emp[i].Basic_Sal+"\t\t"+emp[i].medical+"\t"+emp[i].Hra+"\t"+emp[i].pf+"\t"+emp[i].pt+"\
t"+emp[i].gross_sal+"\t\t"+emp[i].net_sal);
}
}
}
public class Q22 {
public static void main(String[] args) {
String name;
double basic_salary,medical;

Scanner sc = new Scanner(System.in);

Employee emp[] = new Employee[100];

while(true) {
System.out.print("\n1. Add Employee\n2. Get Count\n3. Display Details\n4.
EXIT\nEnter a Choice : ");
int ch = sc.nextInt();

switch(ch) {
case 1: System.out.print("Enter Name : ");
name = sc.next();
System.out.print("Enter a Basic Salary : ");
basic_salary = sc.nextDouble();
System.out.print("Enter a Medical : ");
medical = sc.nextDouble();
emp[Employee.getCount()] = new Employee(name,
basic_salary, medical);
break;
case 2: System.out.println("Count : "+Employee.getCount());
break;
case 3: if(Employee.getCount()!=0)
Employee.display(emp);
else System.out.println("\nNo Data Found !!!\n");
break;
case 4: System.out.println("\nTHANK YOU !!!\n");
sc.close();
return;
default : System.out.println("\nINVALID CHOICE !!!\n");
}
}
}
}
23. Write a program to demonstrate Static nested classes.

import java.util.Scanner;

class Teacher{
static class HoD{
private int id,experiences;
private String name,subject;
public HoD(int id, String name, String subject, int experiences) {
super();
this.id = id;
this.name = name;
this.subject = subject;
this.experiences = experiences;
}
@Override
public String toString() {
return "\nID : "+this.id+"\nName : "+this.name+"\nSubject : "+this.subject+"\
nExperiences : "+this.experiences;
}
}
}

public class Q23 {

public static void main(String[] args) {


int id,experiences;
String name,subject;

Scanner sc = new Scanner(System.in);

System.out.println("ID : ");
id = sc.nextInt();
System.out.println("Name : ");
name = sc.next();
System.out.println("Subject : ");
subject = sc.next();
System.out.println("Experiences : ");
experiences = sc.nextInt();

Teacher.HoD hod = new Teacher.HoD(id, name, subject, experiences);

System.out.println(hod);

sc.close();
}
}
24. Write a program to demonstrate inner classes in java.

import java.util.Scanner;

class Teacher{
class HoD{
private int id,experiences;
private String name,subject;
public HoD(int id, String name, String subject, int experiences) {
super();
this.id = id;
this.name = name;
this.subject = subject;
this.experiences = experiences;
}
@Override
public String toString() {
return "\nID : "+this.id+"\nName : "+this.name+"\nSubject : "+this.subject+"\
nExperiences : "+this.experiences;
}
}
}

public class Q24 {

public static void main(String[] args) {


int id,experiences;
String name,subject;

Scanner sc = new Scanner(System.in);

System.out.println("ID : ");
id = sc.nextInt();
System.out.println("Name : ");
name = sc.next();
System.out.println("Subject : ");
subject = sc.next();
System.out.println("Experiences : ");
experiences = sc.nextInt();

Teacher.HoD hod = new Teacher().new HoD(id, name, subject, experiences);

System.out.println(hod);

sc.close();
}
}
25. Write a program to demonstrate ‘this’ keyword in java.

import java.util.Scanner;

class Area2{
private float legnth,breadth;
Area2(float l,float b) {
this.legnth = l;
this.breadth = b;
}
public float returnArea() {
return(this.breadth*this.legnth);
}
}
public class TestArea2 {

public static void main(String[] args) {


float l,b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Length : ");
l = sc.nextFloat();

System.out.print("Enter a breadth : ");


b = sc.nextFloat();

Area2 area = new Area2(l,b);

System.out.println("\nArea of Rectangle : "+area.returnArea());

sc.close();
}
}

You might also like