1.Define a class Student with attributes rollno and name. Define default and parameterized constructor. Override the toString() method. Keep the count of Objects created. Create objects using parameterized constructor and Display the object count after each object is created.
import java.io.*;
class Student
{
static int cnt;
int roll;
String nm;
Student()
{
nm="";
}
Student(int rno,String name)
{
roll=rno;
nm=name;
cnt++;
System.out.println("Total student count="+cnt);
}
public String toString()
{
return "rno = "+roll+" nm = "+nm;
}
public static void main(String a[]) throws Exception
{
int n,i,rno;
String name;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter no of students");
n=Integer.parseInt(br.readLine());
Student ob[]=new Student[n];
for(i=0;i<n;i++)
{
System.out.println("enter roll no");
rno=Integer.parseInt(br.readLine());
System.out.println("enter name");
name=br.readLine();
ob[i] = new Student(rno,name);
}
System.out.println("Students are : ");
for(i=0;i<n;i++)
{
System.out.println(ob[i]);
}
}
}
Output :
D:\Njava>javac Student.java
D:\Njava>java Student
....................................................................................................................................................
2.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.
import java.io.*;
class Employee
{
int id;
String name;
float sal;
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
void accept()throws IOException
{
System.out.println("Enter employee id and name");
id=Integer.parseInt(br.readLine());
name=br.readLine();
}
float getSalary()throws IOException
{
System.out.println("Enter employee salary");
sal=Float.parseFloat(br.readLine());
return sal;
}
}
class Manager extends Employee
{
float hra,ta,s;
float getSalary()throws IOException
{
super.accept();
s=super.getSalary();
ta=50.40F;
hra=110.20F;
System.out.println("Salary after adding travelling allowances and houserent allowances="+(s+hra+ta));
return (s+hra+ta);
}
void disp()throws IOException
{
float a=getSalary();
}
}
public class EmployeeManager
{
public static void main(String args[])throws IOException
{
Manager m=new Manager();
m.disp();
}
}
........................................................................................................................
Output :
D:\Njava>javac EmployeeManager.java
D:\Njava>javac EmployeeManager.java
D:\Njava>java EmployeeManager
........................................................................................................................
........................................................................................................................
14.Write an applet application in Java for designing Temple.
........................................................................................................................
3.Define an abstract class Shape with abstract methods area() and volume(). Write a java program to calculate area and volume of Cone and Cylinder.
import java.io.*;
abstract class Shape
{
abstract public void area();
abstract public void vol();
}
class Cone extends Shape
{
int r,s,h;
Cone(int r,int s,int h)
{
this.r=r;
this.s=s;
this.h=h;
}
public void area()
{
System.out.println("Area of Cone = "+(3.14*r*s));
}
public void vol()
{
System.out.println("volume of Cone = "+(3.14*r*r*h)/3);
}
}
class Cylinder extends Shape
{
int r,h;
Cylinder(int r,int h)
{
this.r=r;
this.h=h;
}
public void area()
{
System.out.println("Area of Cylinder = "+(2*3.14*r*h));
}
public void vol()
{
System.out.println("volume of Cylinder ="+(3.14*r*r*h));
}
}
class AbstractExample
{
public static void main(String args[])
{
Shape s1=new Cone(5,7,6);
s1.area();
s1.vol();
Shape s2=new Cylinder(1,9);
s2.area();
s2.vol();
}
}
........................................................................................................................
Output :
D:\Njava>javac AbstractExample.java
D:\Njava>java AbstractExample
........................................................................................................................
4.Define an Interface Shape with abstract method area(). Write a java program to calculate an area of Circle and Sphere.(use final keyword.)
import java.io.*;
interface Shape
{
float pi=3.14f;
void area();
}
class Circle implements Shape
{
int r;
Circle(int r)
{
this.r = r;
}
public void area()
{
System.out.println("Area of Circle = "+(pi*r*r));
}
}
class Sphere implements Shape
{
int r;
Sphere(int r)
{
this.r = r;
}
public void area()
{
System.out.println("Area of Sphere = "+(4*pi*r*r));
}
}
class Interface_Slip
{
public static void main(String a[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter radius for circle : ");
int r =Integer.parseInt(br.readLine());
Shape s;
s=new Circle(r);
s.area();
System.out.println("Enter radius for sphere : ");
r =Integer.parseInt(br.readLine());
s=new Sphere(r);
s.area();
}
}
........................................................................................................................
Output :
D:\Nikhiljava>javac Interface_Slip.java
D:\Nikhiljava>java Interface_Slip
Enter radius for circle :
5
Area of Circle = 78.5
Enter radius for sphere :
3
Area of Sphere = 113.04
........................................................................................................................
5.Write a java program to accept n employee names from user, store them into the LinkedList class and display them by using.
a. Iterator Interface
b. ListIterator Interface
import java.util.*;
import java.io.*;
public class LinkedListExample
{
public static void main(String args[])throws IOException
{
try
{
LinkedList<String> al =new LinkedList<String>();
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
System.out.println("Enter Employee u want to add in linkedlist");
int n=Integer.parseInt(br.readLine());
System.out.println("enter name of emp");
for(int i=0;i<n;i++)
{
String name=br.readLine();
al.add(name);
System.out.println("name of emp using iterator Interface");
Iterator<String>itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
System.out.println("name of emp using ListIterator");
ListIterator<String>itr1=al.listIterator();
while(itr1.hasNext())
{
System.out.println(itr1.next());
}
}
}catch(Exception e ){
System.out.println(e);
}
}
}
........................................................................................................................
Output:
D:\Nikhiljava>
D:\Nikhiljava>javac LinkedListExample.java
D:\Nikhiljava>java LinkedListExample
Enter Employee u want to add in linkedlist
2
enter name of emp
Sheamus
name of emp using iterator Interface
Sheamus
name of emp using ListIterator
Sheamus
Cesaro
name of emp using iterator Interface
Sheamus
Cesaro
name of emp using ListIterator
Sheamus
Cesaro
........................................................................................................................
6.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.
6.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.
import java.io.*;
class InvalidNameException extends Exception
{}
class EmpName
{
String nm;
EmpName(String nm)
{
this.nm=nm;
}
void display()
{
System.out.println("Name = "+nm);
}
public static void main(String a[]) throws Exception
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter name : ");
String name=br.readLine();
try
{
int len=name.length();
int flag=0;
for(int i=0;i<len;i++)
{
char ch=name.charAt(i);
if(Character.isLowerCase(ch)||Character.isUpperCase(ch))
{
flag=1;
}
else
{
flag=-1;
break;
}
}
if(flag==-1)
{
throw new InvalidNameException();
}
else
{
EmpName s=new EmpName(name);
s.display();
}
}
catch(InvalidNameException e)
{
System.out.println("Name is invalid");
}
}
}
........................................................................................................................
Output :
D:\Njava>javac EmpName.java
D:\Njava>java EmpName
Enter name :
5
Name is invalid
........................................................................................................................
7.Write a java program to accept a number from the user, if number is zero then throw user defined Exception “Number is 0” otherwise calculate the sum of first and last digit of a given number (Use static keyword).
import java.io.*;
class NumberZero extends Exception
{}
class NumberException
{
static void add_digit(int no)
{
int l=0,f=0;
l = no%10;
if(no>9)
{
while(no>0)
{
f = no%10;
no=no/10;
}
}
System.out.println("Addotion of first and last digit ="+(f+l));
}
public static void main(String a[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter no");
int no=Integer.parseInt(br.readLine());
try
{
if(no==0)
{
throw new NumberZero();
}
else
add_digit(no);
}
catch(NumberZero e)
{
System.out.println("no is zero");
}
}
}
........................................................................................................................
........................................................................................................................
Output :
D:\Njava>javac NumberException.java
D:\Njava>java NumberException
Enter no
0
no is zero
D:\shubhamjava>java NumberException
Enter no
96547
Addotion of first and last digit =16
........................................................................................................................
8.Write a package for Games in Java, which have two classes Indoor and Outdoor.
Use a function display () to generate the list of players for the specific games. (Use Parameterized constructor, finalize() method and Array Of Objects)
import java.io.*;
public class Indoor
{
String gname,pname;
Indoor(String pn,String gn)
{
gname=gn;
pname=pn;
}
void display()
{
System.out.println("Enter game name="+gname);
System.out.println("Enter Player name="+pname);
}
}
________________________________________________________________
import java.io.*;
public class Outdoor
{
String gname,pname;
Outdoor(String pn,String gn)
{
gname=gn;
pname=pn;
}
void display()
{
System.out.println("Enter game name="+gname);
System.out.println("Enter Player name="+pname);
}
}
____________________________________________________________
import java.io.*;
class GamePlayer
{
protected void finalize()
{
System.out.println("Finalized mthod is invoked");
}
public static void main(String a[]) throws IOException
{
String nm,gm;
int i;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter no indoor player");
int n= Integer.parseInt(br.readLine());
Indoor in[]=new Indoor[n];
for(i=0;i<n;i++)
{
System.out.println("enter palyer name");
nm=br.readLine();
System.out.println("enter gamename");
gm=br.readLine();
in[i]=new Indoor(nm,gm);
}
System.out.println("enter no outdoor player");
int n1= Integer.parseInt(br.readLine());
Outdoor o[]=new Outdoor[n1];
for(i=0;i<n1;i++)
{
System.out.println("enter palyer name");
nm=br.readLine();
System.out.println("enter gamename");
gm=br.readLine();
o[i]=new Outdoor(nm,gm);
}
System.out.println("enter game name for indoor : ");
gm=br.readLine();
System.out.println("Player name of specified indoor game :");
for(i=0;i<n;i++)
{
in[i].display();
}
System.out.println("enter game name for outdoor : ");
gm=br.readLine();
System.out.println("Player name of specified outdoor game :");
for(i=0;i<n1;i++)
{
o[i].display();
}
GamePlayer ob = new GamePlayer();
ob.finalize();
}
}
........................................................................................................................
Output :
D:\Nikhiljava>java GamePlayer
enter no indoor player
2
enter palyer name
Magnus Carlson
enter gamename
Chess
enter palyer name
Sameer
enter gamename
Carom
enter no outdoor player
2
enter palyer name
AB devilliers
enter gamename
Cricket
enter palyer name
Neymar jr
enter gamename
Football
enter game name for indoor :
Chess
Player name of specified indoor game :
Enter game name=Chess
Enter Player name=Magnus Carlson
Enter game name= Carom
Enter Player name=Sameer
enter game name for outdoor :
Cricket
Player name of specified outdoor game :
Enter game name=Cricket
Enter Player name=AB devilliers
Enter game name=Football
Enter Player name=Neymar jr
Finalized mthod is invoked
........................................................................................................................
9.Write a java program that displays the number of characters, lines & words from a file.
import java.io.*;
class FileDisp
{
public static void main(String a[]) throws Exception
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter source file name ");
String f1=br.readLine();
FileReader fr = new FileReader(f1);
int ch ;
int wc= 0,line=0, cc=0;
while((ch=fr.read())!= -1)
{
char c = (char)ch;
cc++;
if(c=='\n')
{
line++;
wc++;
}
else if(c==' ' || c=='\n')
wc++;
}
System.out.println("Line count = "+line+"\n Character count "+cc+"\n word count = "+wc);
fr.close();
}
}
........................................................................................................................
Output:
D:\Nikhiljava>javac FileDisp.java
D:\Nikhiljava>java FileDisp
Enter source file name
A.docx
Line count = 43
Character count = 12497
word count = 84
........................................................................................................................
10.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.
import java.io.*;
class PerfectPrime
{
public static void main(String a[])
{
int no[]=new int[a.length];
int i,k=0,l=0;
int pm[]=new int[a.length];
int pr[]=new int[a.length];
for(i=0;i<a.length;i++)
{
no[i]=Integer.parseInt(a[i]);
}
System.out.println("Given array is: ");
for(i=0;i<a.length;i++)
{
System.out.println(no[i]);
}
for(i=0;i<a.length;i++)
{
int j=2;
int flag=0;
while(j<=no[i]/2)
{
if(no[i]%j==0)
{
flag=1;
break;
}
j++;
}
if(flag==0)
{
pm[k]=no[i];
k++;
}
else
{
int sum=0;
j=1;
while(j<=no[i]/2)
{
if(no[i]%j==0)
sum=sum+j;
j++;
}
if(no[i]==sum)
{
pr[l]=no[i];
l++;
}
}
}
System.out.println("prime numbers are");
for(i=0;i<k;i++)
{
System.out.println(pm[i]+" ");
}
System.out.println("perfect numbers are");
for(i=0;i<l;i++)
{
System.out.println(pr[i]+" ");
}
}
}
........................................................................................................................
Output :
D:\Njava>javac PerfectPrime.java
D:\Njava>java PerfectPrime 1 2 3 4 5 6 7 8 9 10
Given array is:
1
2
3
4
5
6
7
8
9
10
Prime numbers are :
1
2
3
5
7
Perfect numbers are :
6
........................................................................................................................
11.Write a java program to read n Students names from user, store them into the
Array List collection. The program should not allow duplicate names. Display the
names in Ascending order.
import java.util.*;
import java.io.*;
public class Slip28
{
public static void main(String args[])throws Exception
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
ArrayList a = new ArrayList();
System.out.println("\nEnter number of Employee : ");
int n = Integer.parseInt(br.readLine());
System.out.println("\nEnter name : ");
for(int i = 1; i <= n; i++)
{
a.add(br.readLine());
}
TreeSet tr = new TreeSet(a);
System.out.println("Sorted name are : "+tr);
}
}
........................................................................................................................
Output :
D:\Nikhiljava>javac Slip28.java
D:\Nikhiljava>java Slip28
Enter number of Employee :
3
Enter name :
Akshay
Amir
Ajay
Sorted name are : [Ajay, Akshay, Amir]
........................................................................................................................
12.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.
import java.io.*;
public class Text
{
public static void main(String args[])throws Exception
{
for(int i=0;i<args.length;i++)
{
File file=new File(args[i]);
if(file.isFile())
{
String name = file.getName();
if(name.endsWith(".txt"))
{
file.delete();
System.out.println("file is deleted " +file);
}
else
System.out.println(name + ""+file.length()+" bytes");
}
else
{
System.out.println(args[i]+ "is not a file");
}
}
}
}
........................................................................................................................
Output:
D:\Njava>Javac Text.java
D:\Njava>Java Text AA.txt
file is deleted AA.txt
........................................................................................................................
13.Write a java program to copy the contents of one file into the another file, while copying change the case of alphabets and replace all the digits by ‘*’ in target file.
import java.util.*;
import java.io.*;
class Target
{
public static void main(String a[]) throws Exception
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter file name to copy");
String f1=br.readLine();
System.out.println("enter destination file");
String f2=br.readLine();
FileReader fr=new FileReader(f1);
FileWriter fw=new FileWriter(f2);
int ch;
while((ch=fr.read() ) != -1)
{
char ch1=(char)ch;
if(Character.isUpperCase(ch1))
{
ch1=Character.toLowerCase(ch1);
fw.write(ch1);
}
else if(Character.isLowerCase(ch1))
{
ch1=Character.toUpperCase(ch1);
fw.write(ch1);
}
else if(Character.isDigit(ch1))
{
ch1='*';
fw.write(ch1);
}
else if(Character.isSpace(ch1))
{
fw.write(ch1);
}
}
fr.close();
fw.close();
}
}
........................................................................................................................
Output:
D:\Nikhiljava>Java Target
enter file name to copy
XYZ.docx
enter destination file
A.docx
import java.applet.Applet;
import java.awt.Graphics;
public class Slip10 extends Applet
{
public void paint(Graphics g)
{
g.drawRect(100,150,90,120);
g.drawRect(130,230,20,40);
g.drawLine(150,100,100,150);
g.drawLine(150,100,190,150);
g.drawLine(150,50,150,100);
g.drawRect(150,50,20,20);
}
}
/*<html>
<body>
<applet code="Slip10.class" width="300" height="300">
</applet>
</body>
</html>
*/
........................................................................................................................
Output:
D:\Nikhiljava>javac Slip10.java
D:\ Nikhiljava>appletviewer Slip10.java
........................................................................................................................
15.Write a java program to accept the details of ‘n’ employees (EName ,Salary) from the user, store them into the Hash table and displays the Employee Names having maximum Salary.
import java.util.*;
import java.io.*;
public class Slip3
{
public static void main(String args[])throws Exception
{
int n,sal=0;
String name="";
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
Hashtable h = new Hashtable();
System.out.println("\nEnter number of Employee : ");
n = Integer.parseInt(br.readLine());
for(int i = 1; i <= n; i++)
{
System.out.println("\nEnter name ");
name = br.readLine();
System.out.println("\nEnter Salary : ");
sal=Integer.parseInt(br.readLine());
h.put(name,sal);
}
Enumeration v = h.elements(); //sal
Enumeration k = h.keys(); //name
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;
}
}
System.out.println(str +" has maximum salary is "+max);
}
}
........................................................................................................................
Output:
D:\Nikhiljava>javac Slip3.java
D:\Nikhiljava>java Slip3
Enter number of Employee :
2
Enter name
Nikhil
Enter Salary :
25000
Blogger Comment
Facebook Comment