Assigment 2 JAVA
Assigment 2 JAVA
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);
}
}
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;
}
}
import java.util.Scanner;
class Student{
private int roll_no;
private String name,address,phone_number;
Student(String name){
this.name = name;
}
@Override
public String toString() {
return "Roll No : "+this.roll_no+"\nName : "+this.name+"\nAddres :
"+this.address+"\nPhone Number : "+this.phone_number;
}
}
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.
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.
Rectangle(float l,float b) {
this.length = l;
this.breadth = b;
}
public float Area() {
return(this.length * this.breadth);
}
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 {
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 {
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();
}
import java.util.Scanner;
class EmployeeDetail {
private String name;
private float salary, hours;
public EmployeeDetail() {
name = " ";
salary = 0;
hours = 0;
}
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;
}
}
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.
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);
}
s = new Square(side);
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 {
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;
@Override
public String toString() {
return "No : "+this.no+"\nName : "+this.name+"\nTotal Salary : "+this.basic_salary;
}
}
public class Q16 {
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;
}
}
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);
}
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());
}
}
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;
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);
}
}
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;
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;
}
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);
}
}
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 {
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
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);
}
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;
}
}
}
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();
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;
}
}
}
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();
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 {
sc.close();
}
}