Assignment No 1 (Java Lab)
Assignment No 1 (Java Lab)
Assignment No 1 (Java Lab)
1. Write a Java program to create a class Person which contains data members as
P_Name, P_City, P_Contact_Number. Write methods to accept and display two
Persons information.
Answer :
Program :
import java.io.*;
class Person{
String P_Name;
String P_City;
String P_Contact_Number;
void accept()throws IOException{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter Person Name :- ");
P_Name = br.readLine();
System.out.println("Enter Person City :- ");
P_City = br.readLine();
System.out.println("Enter Person Contact Number :- ");
P_Contact_Number = br.readLine();
}
void display(){
System.out.println("Person Name = "+P_Name);
System.out.println("Person City = "+P_City);
System.out.println("Person Contact Number = "+P_Contact_Number);
}
}
class PersonInformation{
public static void main(String args[])throws IOException{
Person p1 = new Person();
Person p2 = new Person();
p1.accept();
p1.display();
p2.accept();
p2.display();
}
}
Output:
2. Define a class ‘Month’ having data members: name, total_days and
total_holidays. Define appropriate methods to initialize and display the
values of these data members. Input values for two objects and determine
which month is having maximum working days.
Answer :
Program :
class Month{
String name;
int total_days;
int total_holidays;
void accept(String n, int td, int th){
name = n;
total_days = td;
total_holidays = th;
}
void display(){
System.out.println("Name = "+name);
System.out.println("Total Days = "+total_days);
System.out.println("Total Holidays = "+total_holidays);
}
int MaxWork(){
return total_days-total_holidays;
}
}
class MaxWorkDay{
public static void main(String args[])throws IOException{
Month m1 = new Month();
Month m2 = new Month();
int day1;
int day2;
m1.accept("May", 31, 100);
m2.accept("Jun", 30, 6);
m1.display();
m2.display();
day1 = m1.MaxWork();
day2 = m2.MaxWork();
if(day1>day2){
System.out.println(m1.name+" have maximum working days.");
}
else{
System.out.println(m2.name+" have maximum working days.");
}
}
}
Output :
3. Define a class Shape having overloaded member function area() to calculate and
display area of Square and Rectangle
Answer :
Program :
class Shape{
double a;
a = s*s;
double a;
a = l*w;
class AreaOf{
ob.area(42);
ob.area(84, 54);
}
Output :
4. Define a class ‘Salary’ which will contain data members basic, TA, DA, HRA.
Write a program using constructors which will initialize these values for object.
Calculate total salary of the employee using the method.
Answer :
Program :
class Salary{
double basic;
double TA;
double DA;
double HRA;
Salary(){
basic = 30000;
TA = 3000;
DA = 2000;
HRA = 4000;
double TotalSalary(){
return (basic+TA+DA+HRA);
class SalaryCalculation{
double total;
total = s.TotalSalary();
}
Output :
5. Define a class Book having instance variables: name, totalPages, coverPrice and
author_name. Initialize theses values using constructor for four objects and
calculate average pages and average cover price.
Answer :
Program :
class Book{
String name;
String auther_name;
int totalPages;
int coverPrice;
name = n;
auther_name = an;
totalPages = tp;
coverPrice = cp;
void Display(){
class BookDetails{
Book b1 = new Book("Rich Dad Poor Dad", "Robert Kiyosaki", 887, 497);
int avcp;
b1.Display();
b2.Display();
b3.Display();
b4.Display();
avtp = (b1.totalPages+b2.totalPages+b3.totalPages+b4.totalPages)/4;
avcp = (b1.coverPrice+b2.coverPrice+b3.coverPrice+b4.coverPrice)/4;
Output :
6. Write a java Program to accept ‘n’ no’s through the command line and store all
the prime no’s and perfect no’s into the different arrays and display both the
arrays.
Answer :
Program :
import java.io.*;
class CommandLineArray{
int n=args.length;
int k=0,i,j;
int sum=0;
for(i=0;i<n;i++){
for(j=2;j<Integer.parseInt(args[i]);j++){
if(Integer.parseInt(args[i])%j==0)
break;
if(Integer.parseInt(args[i])==j)
pr[k++]=Integer.parseInt(args[i]);
for(j=0;j<k;j++){
System.out.print(" "+pr[j]);
k=0;
for(i=0;i<n;i++){
sum=0;
for(j=1;j<Integer.parseInt(args[i]);j++){
if(Integer.parseInt(args[i])%j==0){
sum=sum+j;
if(sum==Integer.parseInt(args[i])){
per[k++]=sum;
for(i=0;i<k;i++){
System.out.print(" "+per[i]);
Output :
7. Define an Employee class with suitable attributes having getSalary() method,
which returns salary withdrawn by a particular employee. Write a class Manager
which extends a class Employee, override the getSalary() method, which will
return salary of manager by adding traveling allowance, house rent allowance
etc.
Answer :
Program :
class Employee{
String name;
int sal ;
int id;
name = n;
sal = s;
id = i;
int getSalary(){
return sal;
hra = h;
ta = t;
int getSalary(){
return (super.getSalary()+hra+ta);
}
}
class EmpSal{
Output :
8. Define a class ‘Student’ having data members name and roll_no. Inherit class
‘Teacher’ from it to have data members name and subject. Derive one more
class from ‘Teacher’ called ‘Info’. All three classes contain the Overridden
method display to display respective information of the object. Input the data in
object using constructors.
Answer :
Program :
class Student{
String sname;
int roll_no;
sname = sn;
roll_no = rn;
void display(){
String tname;
String subject;
super(sn, rn);
tname = tn;
subject = s;
void display(){
void display(){
class Stud_Teach_Info{
i.display();
Output :
9. Create a class Shape which consists one final method area () and volume
().Create three subclasses Rect, Circle and Triangle and calculate area and
volume of it .Implement following inheritance. Use constructors to initialize the
objects.
Answer :
Program :
class Shape{
System.out.println("This is final");
void volume(){
System.out.println("This is volume");
double lenght;
double width;
double height;
lenght = l;
width = w;
height = h;
void rarea(){
double a;
a = lenght*width;
void volume(){
double v;
v = lenght*width*height;
double radius;
Circle(double r){
radius = r;
void carea(){
double a;
a = 3.14*radius*radius;
void volume(){
double v;
v = 4/3*3.14*radius*radius;
double base;
double heightt;
double a;
base = b;
heightt = ht;
}
void tarea(){
a = base*heightt/2;
void volume(){
double v;
v = 0.5*base*heightt*a;
class Rect_Cir_Tri{
r.rarea();
r.volume();
c.carea();
c.volume();
t.tarea();
t.volume();
}
Output :
10. Define an Interface Shape with abstract method area(). Write a java program
to calculate an area of Circle and Sphere.(use final keyword).
Answer :
Program :
import java.io.*;
interface Shape{
void area();
double a;
double r = 54;
a = PI*r*r;
double a;
double r = 85;
a = 4*PI*r*r;
class Cir_Sph_Interface{
s.area();
Output :
11. Write a package for Games in Java, which have two classes Indoor and
Outdoor. Use a method display () to generate the list of players for the specific
games. (Use Parameterized constructor, finalize() method and Array Of Objects)
Answer :
Program :
Indoor.java
package Games;
public indoor(){
player = p;
System.out.println(player);
System.out.println("Terminating Indoor....");
outdoor.java
package Games;
public indoor(){
}
public indoor(String p){
player = p;
System.out.println(player);
System.out.println("Terminating Indoor....");
Players.java
import Games.*;
class Players{
System.out.println("Indoor Players...");
in[i].display();
System.out.println("Undoor Players...");
in[i].display();
Output :
12. Write a java program to accept the details of ‘n’ employees (EName ,Salary)
from the user, store them into the Hashtable and displays the Employee Names
having maximum Salary.
Answer :
Program :
import java.util.*;
import java.io.*;
int n,sal=0;
String name;
n = Integer.parseInt(br.readLine());
name = br.readLine();
sal=Integer.parseInt(br.readLine());
h.put(name,sal);
Enumeration v = h.elements();
Enumeration k = h.keys();
while(k.hasMoreElements()){
System.out.println(k.nextElement()+" "+v.nextElement());
int max = 0;
String str="";
k = h.keys();
v = h.elements();
while(v.hasMoreElements()){
sal=(Integer)v.nextElement();
name = (String)k.nextElement();
if(sal>max){
max = sal;
str = name;
Output :
}
13. Write a java program to accept Employee name from the user and check
whether it is valid or not. If it is not valid then throw user defined Exception
“Name is Invalid” otherwise display it.
Answer :
Program :
import java.util.*;
import java.io.*;
class InvalidNameExc{
try{
int ch=(int)name.charAt(i);
else{
catch(InvalidNameException e){
Output :
14. Write a java program to accept n names of cites from user and display them
in descending order.
Answer :
Program :
import java.util.*;
class City{
String a[];
int n;
City(){
n=s.nextInt();
a=new String[n];
for(int i=0;i<n;i++){
a[i]=s.next();
void display(){
String temp="";
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(a[i].compareTo(a[j])>0){
temp=a[i];
a[i]=a[j];
a[j]=temp;
for(int i=0;i<n;i++){
System.out.println(a[i]+" ");
class TestCity{
obj.display();
}
Output :
15. Write a java program to accept list of file names through command line and
delete the files having extension “.txt”. Display the details of remaining files
such as FileName and size.
Answer :
Program :
import java.io.*;
class FileDeleteWithCommindLine{
if(f.isFile()){
f.delete();
else{
else{
Output :
16. Design a screen in Java to handle the Mouse Events such as MOUSE_MOVED
and MOUSE_CLICK and display the position of the Mouse_Click in a TextField.
Answer :
Program :
import java.awt.*;
import java.awt.event.*;
TextField statusBar;
new MouseEvents().show();
MouseEvents(){
addMouseListener(new MouseAdapter(){
repaint();
repaint();
);
addWindowListener(new WindowAdapter(){
System.exit(0);
});
setLayout(new FlowLayout());
setSize(275,300);
setVisible(true);
Output :
17. Write a Java program to design a screen using Awt that will take a user name
and password. If the user name and password are not same, raise an Exception
with appropriate message. User can have 3 login chances only. Use clear button
to clear the TextFields.
Answer :
Program :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
JTextField nameText;
JPasswordField passText;
UsernamePassword(){
login.addActionListener(this);
end.addActionListener(this);
setLayout(new GridLayout(3,2));
add(name);
add(nameText);
add(pass);
add(passText);
add(login);
add(end);
setTitle("Login Check");
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
System.exit(0);
if(e.getSource()==login){
try{
if(user.compareTo(pass)==0){
JOptionPane.showMessageDialog(null,"Login
Successful","Login",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
else{
catch(Exception e1){
cnt++;
JOptionPane.showMessageDialog(null,"Login
Failed","Login",JOptionPane.ERROR_MESSAGE);
nameText.setText("");
passText.setText("");
nameText.requestFocus();
if(cnt == 3){
JOptionPane.showMessageDialog(null,"3 Attempts
Over","Login",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
new UsernamePassword();
Output :
18. Write a Java program which will create a frame if we try to close it, it should
change it’scolor and it remains visible on the screen(Use swing).
Answer :
Program :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
CloseFormUseSwing(){
setVisible(true);
setSize(400,400);
setTitle("Swing Background");
add(p);
addWindowListener(new WindowAdapter(){
p.setBackground(Color.RED);
JOptionPane.showMessageDialog(null,"Close
window","Login",JOptionPane.INFORMATION_MESSAGE);
});
new CloseFormUseSwing();
Output :
19. Write a java program to accept the details employee (Eno, Ename ,Salary)
and add it into the JTable by clicking on the Button.(Max : 5 Records)
Answer :
Program :