T.Y.B.B.A.(C.A.) Sem-V Practical Examination Lab Course- I (Java ) Solved Slips

 

Slip:-1

Q1. 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

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

FileReader fr=new FileReader("f1.txt");

int ch;

while((ch=fr.read())!=-1)

{

char c=(char)ch;

if(Character.isSpaceChar(c))

{

System.out.println("Entered Character Is Space");

}

if(Character.isDigit(c))

{

System.out.println("Entered Character is Digit");

} 2

if(Character.isLetter(c))

{

System.out.println("Entered Character is Alphabet"); if(Character.isLowerCase(c))

{

c=Character.toUpperCase(c);

System.out.println("After Changing the Case " + c);

}

else

{

c=Character.toLowerCase(c); System.out.println("After Changing the Case " + c);

}

}

}

fr.close();

}

}

f1.txt

v 2

Output:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac slip1.java C:\Program Files\Java\jdk1.6.0_20\bin>java slip3

Entered Character is Alphabet

After Changing the Case V

Entered Character Is Space

Entered Character is Digit 4

Slip:-2

Q2. Write a java program to accept n names of cites from user and display them in descending order.

import java.io.*;

class sl2

{

public static void main(String args[ ])throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("how many no of cities you want to enter?");

int n=Integer.parseInt(br.readLine( ));

String str[ ]=new String[n];

int i,j;

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

{

System.out.println("enter city names:-"); str[i]=br.readLine( ); }

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

{

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

{

if((str[i].compareTo(str[j]))<0)

{ 5

String tmp=str[i];

str[i]=str[j];

str[j]=tmp;

}

}

}

System.out.println("after sorting:-");

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

{

System.out.println(str[i]);

}

}

}

Output:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac sl2.java

C:\Program Files\Java\jdk1.6.0_20\bin>java sl2 6

how many no of cities you want to enter?

3

enter city names:-

pune

enter city names:-

mumbai

enter city names:-

chennai

after sorting:-

pune

mumbai

chennai 7

Slip:-3

Q3.Write a java program to accept the details of nemployees (EName ,Salary) from the user, store them into the Hashtable and displaysthe Employee Names having maximum Salary.

import java.io.*;

import java.util.*;

class sl3

{

public static void main(String args[ ])throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Hashtable <String,Integer>h=new Hashtable<String,Integer>( );

String name=" ";

int sal=0;

System.out.println("how many no.of employees you want to enter:-"); int n=Integer.parseInt(br.readLine( ));

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

{

System.out.println("enter name and salary:-");

name=br.readLine( );

sal=Integer.parseInt(br.readLine( ));

h.put(name,sal); 8

}

Enumeration v=h.elements( );

Enumeration k=h.keys( );

while(k.hasMoreElements( ))

{

System.out.println(k.nextElement( )+" "+v.nextElement( ));

}

v=h.elements( );

k=h.keys( );

String str=" ";

int max=0;

while(v.hasMoreElements( ))

{

name=(String)k.nextElement( );

sal=(Integer)v.nextElement( );

if(sal>max)

{

max=sal;

str=name;

}

}

System.out.println(str+"has maximum salary"+max);

}

} 9

Output:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac sl3.java

C:\Program Files\Java\jdk1.6.0_20\bin>java sl3 how many no.of employees you want to enter:-2

enter name and salary:-

Neha

34000

enter name and salary:-

Hema

90000

Neha 34000

Hema 90000

Hema has maximum salary9000010

Slip:-4

Q4. 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.

import java.awt.* ;

import java.awt.event.*;

class slip4 extends Frame

{

TextField statusBar;

public static void main(String [ ]args)

{

new slip4( ).show( );

}

slip4( )

{

addMouseListener(new MouseAdapter( )

{

public void mouseClicked(MouseEvent e)

{

statusBar.setText("clicked at (" + e.getX( ) + " , " + e.getY( ) + ")"); repaint( );

} 11

public void mouseEntered(MouseEvent me)

{

statusBar.setText("clicked at (" + me.getX( ) + " , " + me.getY( ) + ")"); repaint( );

}

});

addWindowListener(new WindowAdapter( )

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

setLayout(new FlowLayout( ));

setSize(275,300);

setTitle("mouse click position");

statusBar=new TextField(20);

add(statusBar);

setVisible(true);

} 12

}

Slip:-5

Q5. 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

{

int rollno;

static int n;

String name;

student()

{

}

student(int rollno,String name)

{

n=n+1;

this.rollno=rollno;

this.name=name;

}

public String toString()

{

return rollno+" "+name; 13

}

public static void main(String args[])

{

student s1=new student(101,"yogesh"); student s2=new student(102,"ubale"); System.out.println(s1); System.out.println(s2); System.out.println("object is created:-"+n);

}

}

Output:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac student.java

C:\Program Files\Java\jdk1.6.0_20\bin>java student

 

1 neha

 

 

2 megha object2

14

Slip:-7

Q7. Write a java program to display Hello Javawith settings Font-Georgia, Foreground color- Red, background color Blue on the Frame (Use Label)

import java.awt.*;

class slip7 extends Frame

{

Label l;

slip7( )

{

l=new Label("Hello Java");

l.setFont(new Font("Georgia",Font.BOLD,14)); l.setForeground(Color.RED);

add(l);

setBackground(Color.BLUE);

setSize(300,300);

setLayout(new FlowLayout( ));

setVisible(true);

}

public static void main(String a[ ])

{

new slip7( );

}

} 15

Output: 16

Slip:-8

Q8. Write a java program to design a following GUI (Use Swing).

import java.awt.*;

import javax.swing.*;

class slip8 extends JFrame

{

JLabel l1,l2,l3,l4,l5,l6;

JTextField t1,t2,t3;

JTextArea t;

JPanel p,p1,p2,p3;

ButtonGroup bg;

JRadioButton m,f; 17

JCheckBox c1,c2,c3;

JButton b1,b2;

slip8( )

{

p=new JPanel( );

p1=new JPanel( );

l1=new JLabel("First Name");

l2=new JLabel("Last Name");

l3=new JLabel("Address");

l4=new JLabel("Mobile Number");

t1=new JTextField(10);

t2=new JTextField(10);

t3=new JTextField(10);

t=new JTextArea(2,10);

p.add(l1);

p.add(t1);

p.add(l2);

p.add(t2);

p.add(l3);

p.add(t);

p.add(l4);

p.add(t3);

p.setLayout (new GridLayout(4,2));

l5=new JLabel("Gender"); 18

m=new JRadioButton("male");

f=new JRadioButton("female");

bg=new ButtonGroup( );

bg.add(m);

bg.add(f);

p1.add(l5);

p1.add(m);

p1.add(f);

p1.setLayout(new GridLayout(1,3));

p2=new JPanel( );

l6=new JLabel("your interest");

c1=new JCheckBox("computer");

c2=new JCheckBox("sports");

c3=new JCheckBox("music");

p2.add(l6);

p2.add(c1);

p2.add(c2);

p2.add(c3);

p2.setLayout(new GridLayout(1,4));

p3=new JPanel( );

b1=new JButton("submit");

b2=new JButton("clear");

p3.add(b1);

p3.add(b2); 19

add(p);

add(p1);

add(p2);

add(p3);

setSize(300,400);

setLayout (new FlowLayout(FlowLayout.LEFT)); setVisible(true);

setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);

}

public static void main(String a[ ])

{

new slip8( );

}

} 20

Slip:-9

Q9. Write a java program to display the contents of a file in reverse order.

import java.io.*;

class example

{

public static void main(String[] args)throws IOException

{

FileReader fr=new FileReader("input.txt");

FileWriter fw=new FileWriter("output.txt");

BufferedReader b=new BufferedReader(fr);

String data;

String reverse;

while((data=b.readLine())!=null)

{

String[]words=data.split(" ");

for(String a:words)

{ 21

StringBuilder builder=new StringBuilder(a);

System.out.println(builder.reverse().toString());

}

}

}

}

Output:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac example.java

C:\Program Files\Java\jdk1.6.0_20\bin>java example ih

woh

era

uoy 22

Slip:-10

Q10. Write an applet application in Java for designing Temple.

import java.awt.*;

import java.applet.*;

public class slip10 extends Applet

{

public void paint(Graphics g)

{

g.drawRect(200,200,250,150);

g.drawRect(330,9,20,15);

g.drawRect(311,305,20,44);

g.drawLine(451,199,326,63);

g.drawLine(198,200,325,65);

g.drawLine(326,62,331,10);

}

} 23

HTML code

<html>

<head>

<title> ex</title>

</head>

<body>

<applet code="slip10.class" height="500" width="500"></applet>

</body>

</html>

 

OutPut: 24

Slip:- 12

Q1) 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.*;

class slip12

{

public static void main(String args[ ])throws Exception

{

for(int i=0;i<3;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:

C:\Program Files\Java\jdk1.6.0_20\bin>javac slip12.java

C:\Program Files\Java\jdk1.6.0_20\bin>java slip12 abc.txt

file is deletedabc.txt 25

Slip:-13

Q13) 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 Slip13

{

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='*'; 26

fw.write(ch1);

}

else if(Character.isSpace(ch1))

{

fw.write(ch1);

}

}

fr.close();

fw.close();

}

}

OUTPUT:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac Slip13.java

C:\Program Files\Java\jdk1.6.0_20\bin>java Slip13

C:\Program Files\Java\jdk1.6.0_20\bin>java Slip13

enter file name to copy

f1

enter destination file

f2 27

Slip:-14

Q.14) Write a Java program which will create a frame if we try to close it, it should change it’s color and it remains visible on the screen(Use swing). [Marks 30]

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class Slip14 extends JFrame

{

JPanel p = new JPanel();

Slip14()

{ setVisible(true);

setSize(400,400);

setTitle("Swing Background");

add(p);

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{ p.setBackground(Color.RED);

JOptionPane.showMessageDialog(null,"Close window","Login",JOptionPane.INFORMATION_MESSAGE);

}

});

}

public static void main(String args[])

{

new Slip14();

}

}

OUTPUT:- 28 29

Slip:- 15

Q15). Define an abstract class Shape with abstract methods area() and volume(). Write a java program to calculate area and volume of Cone and Cylinder. [Marks 30]

import java.util.*;

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)); 30

}

public void vol()

{

System.out.println("volume of Cylinder = "+(3.14*r*r*h));

}

}

class Slip15

{ public static void main(String a[])

{ Scanner sc = new Scanner(System.in);

System.out.println("Enter radius, side and height for cone");

int r =sc.nextInt();

int s1 =sc.nextInt();

int h =sc.nextInt();

Shape s;

s=new Cone(r,s1,h);

s.area();

s.vol();

System.out.println("Enter radius, height for cylinder");

r =sc.nextInt();

h =sc.nextInt();

s=new Cylinder(r,h);

s.area();

s.vol();

}

}

OUTPUT:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac Slip15.java

C:\Program Files\Java\jdk1.6.0_20\bin>java Slip15

Enter radius, side and height for cone

5 8 6

Area of Cone = 125.60000000000001

volume of Cone = 157.0

Enter radius, height for cylinder

5 6 9

Area of Cylinder = 188.4

volume of Cylinder = 471.0 31

Slip :-16

Q.16)Write an application in Java using Awt to display 4 X 4 squares on the screen. One of the block will be active with black color. All other blocks should be filled with blue color. Provide command buttons as follows to move the active cell. The active cell should be changed only if it is within the boundary of the squares otherwise give the beep.

importjava.awt.*;

importjavax.swing.*;

importjava.applet.*;

importjava.awt.event.*;

public class Demo1 extends JFrame implements ActionListener

{

staticintptr=1;

JButtonB[];

Container pane=getContentPane();

Demo1()

{

int b=1;

JPaneljp=new JPanel();

JPanel j1=new JPanel();

pane.setLayout(new BorderLayout());

jp.setLayout(new GridLayout(4,4));

jp.setFont(new Font("SansSerif",Font.BOLD,10));

pane.setBackground(Color.gray);

jp.setBackground(Color.gray);

j1.setBackground(Color.gray);

B=new JButton[20];

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

{

B[i]=new JButton(""+i);

B[i].setBackground(Color.blue);

B[i].setForeground(Color.white); 32

jp.add(B[i]);

b++;

}

pane.add(jp,BorderLayout.CENTER);

pane.setLayout(new FlowLayout());

JButton B1=new JButton("UP");

B1.setBackground(Color.yellow);

B1.addActionListener(this);

JButton B2=new JButton("RIGHT");

B2.setBackground(Color.yellow);

B2.addActionListener(this);

JButton B3=new JButton("DOWN");

B3.setBackground(Color.yellow);

B3.addActionListener(this);

JButton B4=new JButton("LEFT");

B4.setBackground(Color.yellow);

B4.addActionListener(this);

j1.add(B1);

j1.add(B2);

j1.add(B3);

j1.add(B4);

pane.add(j1,BorderLayout.SOUTH);

}

// HANDLING EVENTS

public static void main(String a[])

{

Demo1 d = new Demo1();

d.setSize(300,200);

d.setTitle("Button Demo");

d.show();

d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEventae)

{

String str=(String)ae.getActionCommand(); 33

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

{

B[i].setBackground(Color.blue);

B[i].setForeground(Color.white);

B[ptr].setBackground(Color.black);

}

try

{

if(str.equals("LEFT"))

{

if(ptr>0)

{

B[ptr].setBackground(Color.black);

ptr--;

}

else

JOptionPane.showMessageDialog(pane,"Out Of Range!...");

//repaint();

}

if(str.equals("RIGHT"))

{

if(ptr>0)

{

B[ptr].setBackground(Color.black);

ptr++;

}

else

JOptionPane.showMessageDialog(pane,"Out Of Range!...");

// repaint();

}

if(str.equals("UP"))

{

if(ptr>0)

{

B[ptr].setBackground(Color.black);

ptr-=4; 34

}

else

JOptionPane.showMessageDialog(pane,"Out Of Range!...");

// repaint();

}

if(str.equals("DOWN"))

{

if(ptr>0)

{

B[ptr].setBackground(Color.black);

ptr+=4;

}

else

JOptionPane.showMessageDialog(pane,"Out Of Range!...");

// repaint();

}

}

catch(Exception e)

{

JOptionPane.showMessageDialog(pane,"Out Of Range!...");

}

}

} 35

OUTPUT:- 36

Slip :-17

Q.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. [Marks 30]

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class InvalidPasswordException extends Exception

{}

class Slip17 extends JFrame implements ActionListener

{

JLabel name, pass;

JTextField nameText;

JPasswordField passText;

JButton login, end;

static int cnt=0;

Slip17()

{

name = new JLabel("Name : ");

pass = new JLabel("Password : ");

nameText = new JTextField(20);

passText = new JPasswordField(20);

login = new JButton("Login");

end = new JButton("End");

login.addActionListener(this);

end.addActionListener(this);

setLayout(new GridLayout(3,2));

add(name);

add(nameText); 37

add(pass);

add(passText);

add(login);

add(end);

setTitle("Login Check");

setSize(300,300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==end)

{

System.exit(0);

}

if(e.getSource()==login)

{

try

{

String user = nameText.getText();

String pass = new String(passText.getPassword());

if(user.compareTo(pass)==0)

{ JOptionPane.showMessageDialog(null,"Login Successful","Login",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

else

{

throw new InvalidPasswordException();

}

}

catch(Exception e1)

{

cnt++; 38

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);

}

}

}

}

public static void main(String args[])

{

new Slip17();

}

}

OUTPUT: 39 40

Slip:-18

Q18) Write a java program that displays the number of characters, lines & words from a file.

import java.io.*;

class slipno18

{

public static void main(String args[])throws Exception

{

int ccnt=0,lcnt=1,wcnt=0,c;

FileInputStream fin=new FileInputStream("abc.txt");

while((c=fin.read())!=-1)

{

ccnt++;

if(c==32||c==13)

wcnt++;

if(c==1)

lcnt++;

}

System.out.println("Number of Character are"+ccnt);

System.out.println("Number of words are"+wcnt);

System.out.println("Number of line are"+lcnt);

}

}

OUTPUT:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac slipno18.java

C:\Program Files\Java\jdk1.6.0_20\bin>java slipno18

Number of Character are19

Number of words are2

Number of line are1 41

Slip:-19

Q.19) 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). [Marks 30]

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 number:");

no=Integer.parseInt(br.readLine( ));

try

{

if(no==0)

{

throw new NumberZero( );

}

cal( );

}

catch(NumberZero e)

{

System.out.println(" number is zero:");

}

}

void cal( )

{

int l=0,r=0;

l=no%10; 42

System.out.println(" number="+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:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac slip19.java

C:\Program Files\Java\jdk1.6.0_20\bin>java slip19

enter number:

15

number=15

addition of first and last digit=6 43

Slip :-20

Q.20)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) [Marks 30]

import games.*;

class test

{

public static void main(String args[])

{

indoor d[]=new indoor[3];

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

{

d[0]=new indoor(98,"PP");

d[1]=new indoor(87,"L");

}

System.out.println("FOR INDOOR GAMES");

System.out.println(" ");

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

{d[i].disp();}

outdoor dq[]=new outdoor[2];

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

{

dq[0]=new outdoor(98,"PP");

dq[1]=new outdoor(87,"L");

}

System.out.println("FOR OUTDOOR GAMES");

System.out.println(" ");

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

{dq[i].disp();}

}

}

package games;

public class indoor 44

{int code;

String name;

public indoor(int a,String b)

{code=a;

name=b;

}

public void disp()

{

System.out.println("CODE OF GAME== "+code);

System.out.println("NAME OF PLAYER=="+name);

}

}

package games;

public class outdoor

{int code;

String name;

public outdoor(int a,String b)

{code=a;

name=b;

}

public void disp()

{

System.out.println("CODE OF GAME"+code);

System.out.println("NAME OF PLAYER"+name);

}

} 45

Output:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac test11.java

C:\Program Files\Java\jdk1.6.0_20\bin>java test11

FOR INDOOR GAMES

CODE OF GAME== 98

NAME OF PLAYER==PP

CODE OF GAME== 87

NAME OF PLAYER== L

FOR OUTDOOR GAMES

CODE OF GAME 98

NAME OF PLAYER PP

CODE OF GAME 87

NAME OF PLAYER L 46

Slip:-22

Q. Define an Interface Shape with abstract method area(). Write a java program to calculate an area of Circle and Sphere.(use final keyword)

PROGRAMM:-

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 Slip22

{

public static void main(String a[]) throws IOException

{ 47

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: -

C:\Program Files\Java\jdk1.6.0_20\bin>javac Slip22.java

C:\Program Files\Java\jdk1.6.0_20\bin>java Slip22

Enter radius for circle :

5

Area of Circle = 78.5

Enter radius for sphere :

8

Area of Sphere = 803.84

C:\Program Files\Java\jdk1.6.0_20\bin> 48

Slip:- .23

Q. Write a java program to accept the details of n Cricket 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 objects, Method overloading and static keyword)

PROGRAMM:-

import java.io.*;

class Player

{

String Name;

int TotalRuns, TimesNotOut, InningsPlayed,pcode;

float Avg;

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

void getData()

{

try

{

System.out.println("Enter Player Code: ");

pcode=Integer.parseInt(br.readLine());

System.out.println("Enter Player Name: ");

Name = br.readLine();

System.out.println("Enter Total Runs: ");

TotalRuns = Integer.parseInt(br.readLine());

System.out.println("Enter Times Not Out: ");

TimesNotOut = Integer.parseInt(br.readLine());

System.out.println("Enter Innings Played: ");

InningsPlayed = Integer.parseInt(br.readLine());

}

catch(Exception e)

{

System.out.println(e);

}

}

void putData() 49

{

System.out.println(pcode + "\t"+

Name+"\t"+TotalRuns+"\t"+TimesNotOut+"\t"+InningsPlayed+"\t"+Avg);

}

void getAvg()

{

Avg= TotalRuns / (InningsPlayed - TimesNotOut);

}

static void getAvg(Player p[],int n)

{

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

{

p[i].Avg=p[i].TotalRuns / (p[i].InningsPlayed - p[i].TimesNotOut);

}

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

{

p[i].putData();

}

}

}

public class slipno23

{

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String args[])

{

try

{

System.out.println("Enter No.of Players: ");

int n = Integer.parseInt(br.readLine());

Player p[] = new Player[n];

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

{

p[i] = new Player();

p[i].getData();

}

System.out.println("Player Code For Avg Calculation");

int m=Integer.parseInt(br.readLine());

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

{

if(p[i].pcode==m)

{

p[i].getAvg();

p[i].putData();

}

}

System.out.println("Average Of All The Players");

Player.getAvg(p,n);

}

catch(Exception e)

{

System.out.println(e);

}

}

}

OUTPUT: -

C:\Program Files\Java\jdk1.6.0_20\bin>javac slipno23.java

C:\Program Files\Java\jdk1.6.0_20\bin>java slipno23

Enter No.of Players:

2

Enter Player Code:

8

Enter Player Name:

sachin tendulkar

Enter Total Runs:

760

Enter Times Not Out:

5

Enter Innings Played:

12

Enter Player Code:

9

Enter Player Name:

saurav Ganguly

Enter Total Runs:

709 51

Enter Times Not Out:

7

Enter Innings Played:

10

Player Code For Avg Calculation

8

8 sachin tendulkar 760 5 12 108.0

Average Of All The Players

8 sachin tendulkar 760 5 12 108.0

9 saurav Ganguly 709 7 10 236.0

C:\Program Files\Java\jdk1.6.0_20\bin> 52

Slip :-24

Q.24) Write a Java Program to accept the details of Employee(Eno, EName,Sal) from the user and display it on the next Frame. (Use AWT)

import java.awt.*;

import java.awt.event.*;

class Slip24 extends Frame implements ActionListener

{

Label l1,l2,l3,l;

TextField txt1,txt2,txt3;

Button submit,clear;

Panel p1;

Slip24()

{

l=new Label("EMPLOYEE INFORMTION");

l1=new Label("Name ");

l2=new Label("Address ");

l3=new Label("Salary ");

txt1=new TextField(20);

txt2=new TextField(20);

txt3=new TextField(20);

submit=new Button("submit");

submit.addActionListener(this);

clear=new Button("Clear");

clear.addActionListener(this);

p1=new Panel();

//p1.setLayout(new GridLayout(6,2));

p1.add(l1);

p1.add(txt1);

p1.add(l2);

p1.add(txt2); 53

p1.add(l3);

p1.add(txt3);

p1.add(submit);

p1.add(clear);

add(p1);

setVisible(true);

setSize(400,400);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==submit)

{

new Employee_Detail(txt1.getText(),txt2.getText(),txt3.getText());

}

if(ae.getSource()==clear)

{

txt1.setText("");

txt2.setText("");

txt3.setText("");

}

}

public static void main(String args[])

{

new Slip24();

}

} 54

Slip:-.25

Q.25) 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 allowance etc.

import java.io.*;

class employee

{

int id=2;

String name="PAYAL";

int basic_salary=23000;

int w=24000;

void getsalary()

{

System.out.println("salary to be wITHDRAWN="+w);

}

}

class manager extends employee

{

int sal;

int da=1200;

int hra=1200;

int ta=1200;

void getsalary()

{

sal=(basic_salary+da+hra+ta);

System.out.println("EMPLOYEE DETAILS");

System.out.println("EMPLOYEE CODE= "+id);

System.out.println("EMPLOYEE NAME= "+name);

System.out.println("EMPLOYEE GROSS SALARY= "+sal);

}

}

class slipno25

{

public static void main(String s[])

{ 55

employee e=new employee();

e.getsalary();

manager m=new manager();

m.getsalary();

}

}

OUTPUT:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac slipno25.java

C:\Program Files\Java\jdk1.6.0_20\bin>java slipno25

salary to be wITHDRAWN=24000

EMPLOYEE DETAILS

EMPLOYEE CODE= 2

EMPLOYEE NAME= PAYAL

EMPLOYEE GROSS SALARY= 26600 56

Slip:-26

Q. 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.

PROGRAMM:-

import java.io.*;

class slipno26

{

public static void main(String s[])

{

int sum[]=new int[20];

int i;

System.out.println("ELEMENTS OF ARRAY ARE:");

for(i=0;i<s.length;i++)

{

sum[i]=Integer.parseInt(s[i]);

System.out.println(sum[i]);

}

int prime[]=new int[20];

System.out.println("ELEMENTS OF ARRAY OF PRIME NUMBER ARE:");

for(i=0;i<s.length;i++)

{

if(sum[i]%2==0)

{

prime[i]=sum[i];

System.out.println(prime[i]);}

}

int p[]=new int[20];

System.out.println("ELEMENTS OF ARRAY OF PERFECT NUMBER ARE:");

for(i=0;i<s.length;i++)

{

int b=1,s1=0;

while(b<sum[i])

{

if(sum[i]%b==0)

{

s1=s1+b;

} 57

b++;

}

if(s1==sum[i])

{

p[i]=sum[i];

System.out.println(p[i]);

}

}

}

}

Output:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac sl26.java

C:\Program Files\Java\jdk1.6.0_20\bin>java sl26

ELEMENTS OF ARRAY ARE:

ELEMENTS OF ARRAY OF PRIME NUMBER ARE:

ELEMENTS OF ARRAY OF PERFECT NUMBER ARE:

C:\Program Files\Java\jdk1.6.0_20\bin>java sl26 3 4 5 6 7

ELEMENTS OF ARRAY ARE:

3

4

5

6

7

ELEMENTS OF ARRAY OF PRIME NUMBER ARE:

4

6

ELEMENTS OF ARRAY OF PERFECT NUMBER ARE:

6 58

Slip:-28

Q.28) 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.

PROGRAMM:-

import java.util.*;

import java.io.*;

import java.lang.*;

class slipno28

{

public static void main(String si[])throws IOException

{

BufferedReader b=new BufferedReader(new InputStreamReader(System.in));

System.out.print("ENTER NUMBER OF RECORDS YOU WANT RO ENTER");

int n=Integer.parseInt(b.readLine());

String e[]=new String[n];

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

{

System.out.print("ENTER NAME OF STUDENT=");

e[i]=b.readLine();

}

ArrayList<String> a1=new ArrayList<String>();

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

{

a1.add(e[i]);

System.out.println(a1);

}

System.out.println("STUDENT NAMES BEFORE SORTING ARE AS FOLLOWS");

System.out.println(" ");

System.out.println(a1);

System.out.println(" ");

SortedSet<String> d=new TreeSet<String>();

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

{

d.add(e[i]);

Iterator iw=d.iterator(); 59

System.out.println("STUDENT NAMES AFTER SORTING ARE AS FOLLOWS");

while(iw.hasNext())

{

Object element=iw.next();

System.out.println(element.toString());

}

}

}

}

OUTPUT:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac slipno28.java

C:\Program Files\Java\jdk1.6.0_20\bin>java slipno28

ENTER NUMBER OF RECORDS YOU WANT RO ENTER2

ENTER NAME OF STUDENT=sachin

ENTER NAME OF STUDENT=rahul

[sachin]

[sachin, rahul]

STUDENT NAMES BEFORE SORTING ARE AS FOLLOWS

[sachin, rahul]

STUDENT NAMES AFTER SORTING ARE AS FOLLOWS

sachin

STUDENT NAMES AFTER SORTING ARE AS FOLLOWS

rahul

sachin 60

Slip:-29

Q. 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

PROGRAMM:-

import java.util.*;

import java.io.*;

class slipno29

{

public static void main(String s[])throws IOException

{

BufferedReader b=new BufferedReader(new inputStreamReader(System.in));

System.out.print("ENTER NUMBER OF RECORDS YOU WANT TO ENTER");

int n=Integer.parseInt(b.readLine());

String e[]=new String[n];

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

{

System.out.print("ENTER NAME OF EMPLOYEE=");

e[i]=b.readLine();

}

LinkedList<String> a1=new LinkedList<String>();

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

{

a1.add(e[i]);

System.out.println(a1);

}

System.out.println("EMPLOYEE NAMES ARE AS FOLLOWS");

System.out.println(" ");

System.out.println(a1);

System.out.println(" ");

System.out.println(" ");

System.out.println("USE OF ITERATOR");

System.out.println(" ");

System.out.println(" ");

Iterator<String> itr=a1.iterator();

while(itr.hasNext()) 61

{

System.out.println(itr.next());}

System.out.println(" ");

System.out.println(" ");

System.out.println("USE OF LIST ITERATOR");

System.out.println(" ");

System.out.println(" ");

System.out.println("TRAVERSE IN FORWARD DIRECTION");

System.out.println(" ");

System.out.println(" ");

}

ListIterator<String> it=a1.listIterator();

while(it.hasNext())

{

System.out.println(it.next());

}

System.out.println(" ");

System.out.println(" ");

System.out.println("TRAVERSE IN PREVIOUS DIRECTION");

while(it.hasPrevious())

{

System.out.println(it.previous());

}

}

}

OUTPUT:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac slipno29.java

C:\Program Files\Java\jdk1.6.0_20\bin>java slipno29

ENTER NUMBER OF RECORDS YOU WANT RO ENTER2

ENTER NAME OF EMPLOYEE=rahul

ENTER NAME OF EMPLOYEE=saurav

[rahul]

[rahul, saurav]

EMPLOYEE NAMES ARE AS FOLLOWS

[rahul, saurav] 62

USE OF ITERATOR

rahul

saurav

USE OF LIST ITERATOR

TRAVERSE IN FORWARD DIRECTION

rahul

saurav

Slip:-30

Q. 30)Write an applet application in Java for smile face.

PROGRAMM:-

*JAVA FILE

import java.awt.*;

import java.applet.*;

public class my extends Applet

{

public void paint(Graphics g)

{

g.setColor(Color.black);

g.drawOval(20,40,250,250);

g.fillOval(50,100,50,70); 63

g.fillOval(185,100,50,70);

g.drawLine(138,160,138,210);

g.drawLine(138,210,155,210);

g.drawArc(95,156,100,100,180,180);

}

}

*HTML FILE:-

<html>

<title>APPLET TEST</title>

</head>

<body>

<applet code="my.class" height="400" width="400">

</applet>

</body>

</html>

OUTPUT:-

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Slip 6. Create a calculator with functionality in an Applet.

Calculator.java

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class Calculator extends Applet implements ActionListener

{

String str = " ";

int v1, v2, result;

TextField t1;

Button b[] = new Button[10];

Button add, sub, mul, div, clear, mod, equals;

char choice;

public void init()

{

Color k = new Color(120, 89, 90);

setBackground(k);

t1 = new TextField(10);

GridLayout g1 = new GridLayout(4, 5);

 

 

setLayout(g1);

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

{

b[i] = new Button("" + i);

}

add = new Button("add");

sub = new Button("sub");

mul = new Button("mul");

div = new Button("div");

mod = new Button("mod");

clear = new Button("clear");

equals = new Button("equals");

t1.addActionListener(this);

add(t1);

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

{

add(b[i]);

}

add(add);

add(sub);

add(mul);

add(div);

add(mod);

add(clear);

add(equals);

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

{

b[i].addActionListener(this);

}

add.addActionListener(this);

sub.addActionListener(this);

mul.addActionListener(this);

div.addActionListener(this);

mod.addActionListener(this);

clear.addActionListener(this);

equals.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

String str = ae.getActionCommand();

char ch = str.charAt(0);

if (Character.isDigit(ch))

t1.setText(t1.getText() + str);

else

{

if (str.equals("add"))

{

v1 = Integer.parseInt(t1.getText());

choice = '+';

t1.setText("");

}

else if (str.equals("sub"))

{

v1 = Integer.parseInt(t1.getText());

choice = '-';

t1.setText("");

}

if (str.equals("mul"))

{

v1 = Integer.parseInt(t1.getText());

choice = '*';

t1.setText("");

}

 

if (str.equals("div"))

{

v1 = Integer.parseInt(t1.getText());

choice = '/';

t1.setText("");

}

if (str.equals("mod"))

{

v1 = Integer.parseInt(t1.getText());

choice = '%';

t1.setText("");

}

if (str.equals("clear"))

{

t1.setText("");

}

if (str.equals("equals"))

{

v2 = Integer.parseInt(t1.getText());

switch (choice)

{

case '+': result = v1 + v2;

break;

case '-': result = v1 - v2;

break;

case '*': result = v1 * v2;

break;

case '/': result = v1 / v2;

break;

case '%': result = v1 % v2;

break;

}

t1.setText("" + result);

}

}

}

}

 

 

 

 

Calculator.html

<html>

<head>

<title>smile face</title>

</head>

<body>

<applet code="Calculator.class" width=400 height=400></applet>

</body>

</html>

 

 

OutPut:

 

 

C:\Program Files\Java\jdk1.6.0_20\bin>javac Calculator.java

 

C:\Program Files\Java\jdk1.6.0_20\bin>

 

 

 

 

 

 

 

Post a Comment

0 Comments