/*Seat
No:-
Slip no 1
Assignment
Name:-Write a java program to read the characters from a file, if a character
is alphabet then reverse its case, if not then display its category on the
Screen. (whether it is Digit or Space)
*/
import
java.io.*;
class
Slip1
{
public static void main(String
args[])throws Exception
{
BufferedReaderbr=new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter
file Name");
String f1=br.readLine();
FileReaderfr=new
FileReader(f1);
intch;
while((ch=fr.read())!=-1)
{
char
c=(char)ch;
if(Character.isUpperCase(c))
{
c=Character.toLowerCase(c);
System.out.print(c);
}
else
if(Character.isLowerCase(c))
{
c=Character.toUpperCase(c);
System.out.print(c);
}
else
if(Character.isDigit(c))
{
System.out.print(c+"Digit");
}
else
if(Character.isSpaceChar(c))
System.out.print(c+"Space");
else
System.out.print(c);
}
fr.close();
}
}
/*Output:
Enter
file Name
testslip1.txt
mYSpacenAMESpaceISSpaceaPARNA
rOLLSpaceNO:
Space2Digit0Digit1Digit1Digit*/
/*Seat
no:
Slip
no 2
Assignment
name:-. Write a java program to accept n names of cites from user and display
them in descending order. */
class
Slip2
{
public static void main(String
args[])
{
int n=args.length;
inti,j;
String str[]=new
String[n];
for(i=0;i<n;i++)
{
str[i]=args[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if((str[i].compareTo(str[j]))>0)
{
String
tmp=str[j];
str[j]=str[i];
str[i]=tmp;
}
}
}
System.out.println("City
Name After Sorting");
for(i=0;i<n;i++)
{
System.out.println(str[i]);
}
}
}
/*Output:
Pune
Bhosari Mumbai Chakan Nashik Thane Ahemadnagar
City
Name After Sorting
Ahemadnagar
Bhosari
Chakan
Mumbai
Nashik
Pune
Thane
*/
/*Seat no:
Slip no 5
Assignment name:-. 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. */
importjava.util.*;
class Slip5
{
staticintcnt;
int
roll;
String
nm;
Slip5()
{
}
Slip5(intrno,String
name)
{
roll=rno;
nm=name;
cnt++;
System.out.println("Objects
created="+cnt);
}
public
String toString()
{
return
"rno="+roll+" name of student="+nm;
}
public
static void main(String a[])
{
intn,i,rno;
String
name;
System.out.println("Enter
no of students");
Scanner
s=new Scanner(System.in);
n=s.nextInt();
Slip5
ob[]=new Slip5[n];
for(i=0;i<n;i++)
{
System.out.println("Enter
roll no");
rno=s.nextInt();
System.out.println("Enter
name");
name=s.next();
ob[i]=new
Slip5(rno,name);
}
System.out.println("Students
are: ");
for(i=0;i<n;i++)
{
System.out.println(ob[i]);
}
}
}
/*Output:
Enter no of students
3
Enter roll no
11
Enter name
Geeta
Objects created=1
Enter roll no
12
Enter name
Aparna
Objects created=2
Enter roll no
13
Enter name
Rohini
Objects created=3
Students are:
rno=11 name of student=Geeta
rno=12 name of student=Aparna
rno=13 name of student=Rohini
*/
/*Seat no:
Slip no 9
Assignment name:- Write a java program
to display the contents of a file in reverse order.
*/
import java.io.*;
class Slip9
{
public static void main(String a[])
throws Exception
{
BufferedReaderbr = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter file
name");
String fname=br.readLine();
FileInputStream fin = new
FileInputStream(fname);
BufferedInputStreambis = new
BufferedInputStream(fin);
int s = bis.available();
System.out.println(s);
for(inti=s-1;i>=0;i--)
{ bis.mark(i);
bis.skip(i);
System.out.print((char)bis.read());
bis.reset();
}
fin.close();
}
}
/*
Output
Enter file name
slip7.java
563
}
}
;)(7pilS wen
{
)][sgragnirtS(niamdiovcitatscilbup
}
;)eurt(elbisiVtes
;))(tuoyaLwolF wen(tuoyaLtes
;)004,004(eziStes
;)EULB.roloC(dnuorgkcaBtes
;)l(dda
;)DER.roloC(dnuorgeroFtes.l
;))42,DLOB.tnoF,"aigroeG"(tnoF
wen(tnoFtes.l
;)"avajolleH"(lebaL wen=l
{
)(7pilS
;llebaL
{
emarFsdnetxe 7pilS ssalc
;*.twa.avajtropmi
/*
.emarFeht no eulB ?rolocdnuorgkcab ,
,aigroeG -tnoFsgnitteshtiw ?avaJolleH? yalps
nemngissA
.ijanaTinihoRtuaR:emaN
*/
*/
/*Seat
no:
Slip no 11
Assignment name:-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.(Name should contain
only characters)
*/
import java.lang.Exception;
class MyException extends Exception
{
MyException(String
message)
{
super(message);
}
}
class Slipno11
{
public
static void main(String args[])
{
String myName;
System.out.println(validateFirstName("Aparna"));
System.out.println(validateMiddleName("123"));
System.out.println(validateLastName("Lohakare"));
}
public
static boolean validateFirstName(String firstname)
{
return
firstname.matches("[A-Z][a-z]*");
}
public
static boolean validateMiddleName(String middlename)
{
return
middlename.matches("[A-Z][a-z]*");
}
public
static boolean validateLastName(String lastname)
{
return
lastname.matches("[A-Z][a-z]*");
}
}
/*Output:
C:\Aparna>java Slipno11
true
false
true
*/
/*Seat no:
Slip no 15
Assignment name:-Define an abstract
class Shape with abstract method area() and volume().Write a java program to
calculate area and volume of Cone and Cylinder.*/
abstract class Shape
{
float
pi=3.14F;
abstract
float areaofcone(float radius,float side);
abstract
float volumeofcone(float radius,float height);
abstract
float areaofcylinder(float radius,float height);
abstract
float volumeofcylinder(float radius,float height);
}
class AreaVolume extends Shape
{
float
areaofcone(float r,float s)
{
return
pi*r*s;
}
float
volumeofcone(float r,float h)
{
return
(1/3)*pi*r*r*h;
}
float
areaofcylinder(float r,float h)
{
return
2*pi*r*h;
}
float
volumeofcylinder(float r,float h)
{
return
pi*r*r*h;
}
}
class Slip15
{
public
static void main(String args[])
{
Shape
obj=new AreaVolume();
float
x=obj.areaofcone(1.2F,3.2F);
System.out.println("Area
of Cone="+x);
float
y=obj.volumeofcone(1.2F,3.2F);
System.out.println("Volume
of Cone="+y);
float
z=obj.areaofcylinder(1.2F,3.2F);
System.out.println("area
of cylinder="+z);
float
p=obj.volumeofcylinder(1.2F,3.2F);
System.out.println("volume
of cylinder="+p);
}
}
/*Output:
Area of Cone=12.057601
Volume of Cone=0.0
area of cylinder=24.115202
volume of cylinder=14.469123
*/
/*Seat no:
Slip no18
Assignment name:-Write a java program
that displays the number of characters,lines &words from a file.
*/
import java.io.*;
class Slip18
{
public
static int words=0;
public
static int lines=0;
public
static int chars=0;
public
static void wc(Reader r)throws IOException
{
StreamTokenizer
tok=new StreamTokenizer(r);
tok.resetSyntax();
tok.wordChars(33,255);
tok.whitespaceChars(0,'
');
tok.eolIsSignificant(true);
while(tok.nextToken()!=tok.TT_EOF)
{
switch(tok.ttype)
{
case
StreamTokenizer.TT_EOL:
lines++;
chars++;
break;
case
StreamTokenizer.TT_WORD:
words++;
default:
chars+=tok.sval.length();
break;
}
}
}
public
static void main(String args[])
{
if(args.length==0)
{
try
{
wc(new
InputStreamReader(System.in));
System.out.println(lines+"
"+words+" "+chars);
}catch(IOException
e){ }
}
else
{
int
twords=0,tchars=0,tlines=0;
for(int
i=0;i<args.length;i++)
{
try
{
words=chars=lines=0;
wc(new
FileReader(args[i]));
twords+=words;
tchars+=chars;
tlines+=lines;
System.out.println(args[i]+":
"+lines+" "+words+" "+chars);
}catch(IOException
e){
System.out.println(args[i]+":
error.");
}
}
System.out.println("total: "+tlines+" "+twords+"
"+tchars);
}
}
}
//Testslip18.txt
My name is Aparna
Roll no is 1234
collage name is Siddhant collage of
management studies
/*Output:
Testslip18.txt: 3 16 76
total:
3 16 76
*/
/*Seat no:
Slip no 22
Assignment name:-Define an Interface
Shape with abstract method area().Write a java program to calculate an area of
circle and sphere.(Use final keyword).
*/
interface Shape
{
final
float pi=3.11416f;
public
float areaOfCircle(float radius);
public
float areaOfSphere(float radius);
}
class AreaOfCirSphere implements Shape
{
public
float areaOfCircle(float r)
{
return
pi*r*r;
}
public
float areaOfSphere(float r)
{
return
(4/3)*pi*r*r;
}
}
class Slip22{
public
static void main(String args[])
{
Shape
area;
AreaOfCirSphere
ob=new AreaOfCirSphere();
area=ob;
float
areaOfCir=area.areaOfCircle(5.3F);
System.out.println("Area
of Circle="+areaOfCir);
float
areaOfSphere=area.areaOfSphere(3.2F);
System.out.println("Area
of Sphere="+areaOfSphere);
}
}
/*Output:
Area of Circle=87.47676
Area of Sphere=31.888998
*/
/*Seat no:
Slip no 23
Assignment name:-Write a java program to
accept to details of n Players from user(Player code,name,runs,innings-played
and number of times not out).The program should contain following menus:
-Display average runs of a single
player.
-Display average runs of all
players.(Use array of object,Method overloading and static keyword)
*/
import java.util.*;
import java.io.*;
class player
{
String
name;
int
trun,tno,ip,pcode;
float
avg;
static
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void
getdata()
{
try
{
System.out.println("\nEnter
Player Code: ");
pcode=Integer.parseInt(br.readLine());
System.out.println("Enter
Player name: ");
name=br.readLine();
System.out.println("Enter
Total Runs: ");
trun=Integer.parseInt(br.readLine());
System.out.println("Enter
Times Not Out: ");
tno=Integer.parseInt(br.readLine());
System.out.println("Enter
Innings Played: ");
ip=Integer.parseInt(br.readLine());
}
catch(Exception
e)
{
System.out.println(e);
}
}
void
putdata()
{
System.out.println(pcode
+ "\t" + name + "\t" + trun + "\t" + tno +
"\t" + ip + "\t" + avg);
}
void
getavg()
{
avg=trun/(ip-tno);
}
static
void getavg(player p[],int n)
{
for(int
i=0;i<n;i++)
{
p[i].avg=p[i].trun/(p[i].ip-p[i].tno);
p[i].putdata();
}
}
}
public class Slip23
{
static
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public
static void main(String args[])
{
int
n=0,ch;
player
p[]=null;
try
{
do
{
System.out.println("\n\n1.Enter
details of players. ");
System.out.println("2.Display
average runs of a single player ");
System.out.println("3.Display
average runs of all player ");
System.out.println("4.Exit
");
System.out.println("Enter
your choice: ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case
1 :
System.out.print("Enter
No. of Players: ");
n=Integer.parseInt(br.readLine());
p=new
player[n];
for(int
i=0;i<n;i++)
{
p[i]=new
player();
p[i].getdata();
}
break;
case
2:
System.out.println("Enter
Player Code For Avg Calculation :");
int
m=Integer.parseInt(br.readLine());
boolean
flag=false;
for(int
i=0;i<n;i++)
{
if(p[i].pcode==m)
{
flag=true;
p[i].getavg();
p[i].putdata();
}
}
if(!flag)
System.out.println("Player
not found");
break;
case
3:
System.out.println("Average
of All the Players");
player.getavg(p,n);
break;
case
4:
break;
default:
System.out.println("Wrong
Input (Try again)");
}
}while(ch!=4);
}
catch(Exception
e)
{
System.out.println(e);
}
}
}
/*Output:
1.Enter details of players.
2.Display average runs of a single
player
3.Display average runs of all player
4.Exit
Enter your choice:
1
Enter No. of Players: 2
Enter Player Code:
101
Enter Player name:
Aparna
Enter Total Runs:
122
Enter Times Not Out:
2
Enter Innings Played:
1
Enter Player Code:
102
Enter Player name:
Geeta
Enter Total Runs:
100
Enter Times Not Out:
3
Enter Innings Played:
1
1.Enter details of players.
2.Display average runs of a single
player
3.Display average runs of all player
4.Exit
Enter your choice:
2
Enter Player Code For Avg Calculation :
102
102
Geeta 100 3
1 -50.0
1.Enter details of players.
2.Display average runs of a single
player
3.Display average runs of all player
4.Exit
Enter your choice:
3
Average of All the Players
101
Aparna 122 2
1 -122.0
102
Geeta 100 3
1 -50.0
1.Enter details of players.
2.Display average runs of a single
player
3.Display average runs of all player
4.Exit
Enter your choice:
4
*/
/*Seat no:
Slip no 25
Assignment name:-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 travelling allowance,house rent allowance etx.
*/
import java.util.*;
import java.io.*;
class Emp
{
int
e_no;
float
bsal,gsal,tax ,pf,nsal,ded,da;
String
e_name;
Emp(int
n ,String en,float bs)
{
e_no=n;
e_name=en;
bsal=bs;
}
float
getSalary()
{
da=bsal*0.10f;
gsal=bsal+da;
tax=bsal*0.05f;
pf=bsal*0.04f;
ded=tax+pf;
nsal=gsal-ded;
return(nsal);
}
void
display()
{
System.out.println("EmpNo:"
+e_no+"Name: "+e_name +"Net Salary:" +nsal);
}
}
class Manager extends Emp
{
float
hra,ta,mns;
Manager(int
n,String en,float bs,float hr,float t)
{
super(n,en,bs);
hra=hr;
ta=t;
}
float
getSalary()
{
float
r=super.getSalary();
mns=r+hra+ta;
return(mns);
}
}
class Slip25
{
public
static void main(String args[])
{
Manager
ma=new Manager(01,"Aparna Lohakare",40000.0f,9200.0f,8420.0f);
float
shb=ma.getSalary();
ma.display();
System.out.println("Salary
of manager is\t" +shb);
}
}
/*Output:
EmpNo:1Name: Aparna LohakareNet
Salary:40400.0
Salary of manager is 58020.0
*/
/*Seat no:
Slip no 26
Assignment name:-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.util.*;
import java.io.*;
class Slip26
{
public
static void main(String args[])
{
int
n=args.length;
int
pri[]=new int[n];
int
per[]=new int[n];
int
k=0,i,j;
int
sum=0;
//prime
no
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)
{
pri[k++]=Integer.parseInt(args[i]);
}
}
System.out.println("Prime
Array=");
for(j=0;j<k;j++)
{
System.out.print("
"+pri[j]);
}
//Perfect
no
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;
}
}
System.out.println("\nPerfect
Array=");
for(i=0;i<k;i++)
{
System.out.println("
"+per[i]);
}
}
}
/* Output:
C:\Aparna>javac Slip26.java
C:\Aparna>java Slip26 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 17 18 19 20
Prime Array=
2
3 5 7 11 13 17 19
Perfect Array=
6
*/
/*Seat no:
Slip no 28
Assignment name:-Write a java program to
read n Students names from user,store
them into the ArrayList 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
Enter number of Employee :
7
Enter name :
renuka
pratiksha
madhuri
aparna
geeta
renuka
rohini
Sorted name are : [aparna, geeta,
madhuri, pratiksha, renuka, rohini]
*/
/*Seat no:
Slip no:29
Assignment name:-Write a java program to
accept n employee names from user,store them into the linkedlist class and
Display them by using.
-Iterator Interface
-ListIterator Interface
*/
import java.util.*;
import java.io.*;
public class Slip29
{
public static void main(String args[])throws Exception
{
int n;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
LinkedList li = new LinkedList ();
System.out.println("\nEnter number of Employee : ");
n = Integer.parseInt(br.readLine());
System.out.println("\nEnter name : ");
for(int i = 1; i <= n; i++)
{
li.add(br.readLine());
}
System.out.println("\nLink List Content : ");
Iterator it = li.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
System.out.println("\nReverse order : ");
ListIterator lt = li.listIterator();
while(lt.hasNext())
{
lt.next();
}
while(lt.hasPrevious())
{
System.out.println(lt.previous());
}
}
}
/*output
Enter number of Employee
2
Enter name :
abc
pqr
Link List Content :
abc
pqr
Reverse order :
pqr
abc
*/
/*Seat no:
Slip no 19
Assignment name:- 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
{
NumberZero()
{}
}
class Number
{
static int no;
Number() throws IOException
{
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter
no");
no=Integer.parseInt(br.readLine());
try
{ if(no==0)
{
throw new NumberZero();
}
cal();
}//end of try
catch(NumberZero e)
{
System.out.println("no
is zero");
}
}
void cal()
{ int l=0,r=0;
l = no%10;
//System.out.println("no =
"+no);
if(no>9)
{
while(no>0)
{
r = no%10;
no=no/10;
}
System.out.println("Addition
of first and last digit = "+(l+r));
}
else
System.out.println("Addition
of first and last digit = "+l);
}
}
class Slip19
{
public static void main(String a[]) throws IOException
{ Number n= new Number();
}
}
/*Output
Enter no
1
Addition of first and last digit = 1
Enter no
45
Addition of first and last digit = 9
Enter no
456
Addition of first and last digit = 10
*/