Professional Documents
Culture Documents
Ty BCS Core Java PDF
Ty BCS Core Java PDF
Core Java
Slip 1_1: Create abstract class shape. Drive three classes sphere. Cone and cylinder from it.
Calculate Area and volume of all ( use method overriding)
import java.io.*;
abstract class Shape
{
double r,h;
Shape(double r, double h)
{
this.r = r;
this.h = h;
}
abstract double calcArea();
abstract double calcVolume();
}
class Sphere extends Shape
{
Sphere(double r)
{
super(r,0);
}
double calcArea()
{
return 4*3.14*r*r;
}
double calcVolume()
{
return 4*3.14*Math.pow(r,3)/3;
}
}
class Cone extends Shape
{
Cone(double r, double h)
{
super(r,h);
}
double calcArea()
{
return 3.14*r*(r+Math.sqrt(r*r+h*h));
}
double calcVolume()
{
Slip 2_1: Write a menu driven program to perform the following operations on a set of integers
as shown in the following fig. the load operation should generate 10 random integer(2 digit) and
display the number on screen. The save operation should save the number to a file”number.txt”.
The short menu provides various operations and the result is displayed on the screen.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
JLabel l;
JTextField t;
JPanel p;
int n;
int arr[]=new int [20];
Slip2_1()
{
p=new JPanel();
mb=new JMenuBar();
m1=new JMenu("Operation");
l=new JLabel("Numbers");
t=new JTextField(20);
String str[]={"Load","Save","Exit","Ascending","Descending"};
m=new JMenuItem[str.length];
for(int i=0;i<str.length;i++)
{
m[i]=new JMenuItem(str[i]);
m[i].addActionListener(this);
}
p.add(l);
p.add(t);
mb.add(m1);
mb.add(m2);
m1.add(m[0]);
m1.add(m[1]);
m1.addSeparator();
m1.add(m[2]);
m2.add(m[3]);
m2.add(m[4]);
setLayout(new BorderLayout());
add(mb,BorderLayout.NORTH);
add(p,BorderLayout.CENTER);
setSize(300,150);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void sortasc()
{
for(int i=0;i<n;i++)
for(int j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
void sortdesc()
{
for(int i=0;i<n;i++)
for(int j=0;j<n-1;j++)
{
if(arr[j]<arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
StringBuffer s5=new StringBuffer();
for(int i=0;i<n;i++)
{
s5.append(new Integer(arr[i]).toString());
s5.append(" ");
}
t.setText(new String(s5));
}
Slip 3_1: Define a class MyDate( Day,Month,year) with method to accept and display a MyDate
object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateExeption” if the
date is invalid.
a. 12 15 2015
b. 31 6 1990
c. 29 2 2015
void display()
{
System.out.println("Date is valid : "+day+"/"+mon+"/"+yr);
}
}
class Slip3_1
{
public static void main(String arg[]) throws Exception
{
System.out.println("Enter Date : dd mm yyyy ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int day=Integer.parseInt(br.readLine());
int mon=Integer.parseInt(br.readLine());
int yr=Integer.parseInt(br.readLine());
int flag=0;
try
{
if(mon<=0 || mon>12)
}
}
Slip 4_1: Write a menu driven program to perform the following operations on a set of integers
as shown in the following figure. A load operation should generate 10 random integers (2 digit)
and display the no on screen. The save operation should save the no to a file “number.txt”. The
short menu provides various operations and the result is displayed on the screen.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
JLabel l;
JTextField t;
JPanel p;
int n;
int arr[]=new int [20];
Slip4_1()
{
p=new JPanel();
mb=new JMenuBar();
m1=new JMenu("Operation");
m2=new JMenu("Compute");
l=new JLabel("Numbers");
t=new JTextField(20);
String str[]={"Load","Save","Exit","Sum","Average"};
m=new JMenuItem[str.length];
for(int i=0;i<str.length;i++)
mb.add(m1);
mb.add(m2);
m1.add(m[0]);
m1.add(m[1]);
m1.addSeparator();
m1.add(m[2]);
m2.add(m[3]);
m2.add(m[4]);
setLayout(new BorderLayout());
add(mb,BorderLayout.NORTH);
add(p,BorderLayout.CENTER);
setSize(300,150);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/*void sortasc()
{
for(int i=0;i<n;i++)
for(int j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
StringBuffer s5=new StringBuffer();
for(int i=0;i<n;i++)
{
s5.append(new Integer(arr[i]).toString());
s5.append(" ");
}
t.setText(new String(s5));
void sortdesc()
{
for(int i=0;i<n;i++)
for(int j=0;j<n-1;j++)
{
if(arr[j]<arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
StringBuffer s5=new StringBuffer();
for(int i=0;i<n;i++)
{
s5.append(new Integer(arr[i]).toString());
s5.append(" ");
}
t.setText(new String(s5));
}*/
public int givesum()
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
}
return sum;
}
Slip 5_1: Define a class saving account (acno, name, balance) .define appropriate and operation
withdraw(), deposit(), and viewbalance(). The minimum balance must be 500. Create an object
and perform operation. Raise user defined “InsufficientFundException” when balance is not
sufficient for withdraw operation.
import java.io.*;
class SavingAccount
{
int ano;
String name;
float bal;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
SavingAccount(int a,String nm,float b)
{
ano=a;
name=nm;
bal=b;
}
class Slip5_1
{
public static void main(String ar[]) throws Exception
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Account no:");
int a =Integer.parseInt(br.readLine());
System.out.println("Enter Name:");
String nm=br.readLine();
System.out.println("Enter Balance:");
float b =Float.parseFloat(br.readLine());
SavingAccount sa = new SavingAccount(a,nm,b);
do
{
System.out.println(" \n 1.Deposit \n 2.Withdraw \n 3.Check Balance \n
4.Exit ");
System.out.println("Enter your choice :");
int ch=Integer.parseInt(br.readLine());
switch(ch)
}
}while(true);
}
}
Slip 6_1: Write a program to accept a decimal number in the Textfield. After clicking calculate
button, program should display the binary, octal, hexadecimal equivalent for the entered decimal
number.
import java.awt.*;
import java.awt.event.*;
public Slip6_1()
{
labelD= new Label("Decimal No:");
labelB = new Label("Binary No : ");
labelO = new Label("Octal No :");
labelH = new Label("Hexadecimal No :");
p = new Panel();
p.setLayout(new GrideLayout(5,2));
p.add(labelD);
setLayout(new FlowLayout());
add(p);
setTitle("Binary Calculator");
setSize(300,300);
setVisible(true);
cal.addActionListener(this);
clear.addActionListener(this);
}
if(btn == clear)
{
txtNo.setText("");
txtB.setText("");
txtO.setText("");
txtH.setText("");
}
}
Prime.java
package Series;
public class Prime
{
public void prime_range(int no)
{
for(int i=1;i<=no;i++)
{
int flag=0;
for(int j=2;j<=i/2;j++)
{
if(i%j==0)
{ flag=1;
break;
}
}
if(flag==0)
System.out.println(i+" ");
}
}
Squar.java
package Series;
public class Square
{
public void square_range(int no)
{
for(int i=1;i<=no;i++)
{
System.out.println(i+" = "+(i*i));
}
}
}
import Series.Prime;
import Series.Square;
class Slip7_1
{
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());
Prime p = new Prime();
System.out.println("Prime no upto given nos are : ");
p.prime_range(no);
}
}
Slip 8_1: Write a program to create the following GUI and apply the change to text in the
TextField.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
p1.setLayout(new GridLayout(4,2));
p1.add(font);
p1.add(style);
p1.add(fontcb);
p1.add(bold);
p1.add(size);
p1.add(italic);
p1.add(sizecb);
p2.setLayout(new FlowLayout());
p2.add(t);
bold.addItemListener(this);
italic.addItemListener(this);
fontcb.addItemListener(this);
sizecb.addItemListener(this);
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent ie)
{
String f = (String)fontcb.getSelectedItem();
System.out.println("font = "+f);
t.setFont(new Font(f,Font.BOLD,10));
String no =(String)sizecb.getSelectedItem();
int num=Integer.parseInt(no);
}
public static void main(String args[])
{
Font1 f1 = new Font1();
}
}
Slip9_1 & Slip19_1: Create an applet which display a message in the center of the screen. The
message indicates the events taking place on the applet window. Handle events like mouse click,
mouse moves, mouse dragged, mouse pressed. The message should update each time an event
occurs. The message should give details of the event such as which mouse button was pressed (
hint: use repaint(), mouselistener, MouseMotionListener)
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
/*
<applet code="MouseApplet.java" width=1500 height=800>
</applet>
*/
public class MouseApplet extends Applet
{
JPanel p;
JTextField t;
String msg;
public void init()
{
t=new JTextField(20);
add(t);
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
msg="Mouse Clicked : X = "+me.getX() + " Y = "+me.getY();
Slip 10_1: Write a program to implement simple arithmetic calculator. Perform appropriate
validations.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
p1=new JPanel();
p1.setLayout(new GridLayout(5,4));
for(int i=0;i<10;i++)
{
b[i]=new JButton(""+i);
}
equals=new JButton("=");
add=new JButton("+");
sub=new JButton("-");
for(int i=0;i<10;i++)
{
p1.add(b[i]);
}
p1.add(equals);
p1.add(add);
p1.add(sub);
p1.add(mul);
p1.add(div);
p1.add(clear);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
clear.addActionListener(this);
equals.addActionListener(this);
add(p,BorderLayout.NORTH);
add(p1);
setVisible(true);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
if(str.equals("="))
{
v2=Integer.parseInt(t.getText());
if(choice=='+')
result=v1+v2;
else if(choice=='-')
result=v1-v2;
else if(choice=='*')
result=v1*v2;
else if(choice=='/')
result=v1/v2;
t.setText(""+result);
}
if(str.equals("C"))
{
t.setText("");
}
}
public static void main(String a[])
{
Slip10_1 ob = new Slip10_1();
}
}
import java.io.*;
class Slip11_1
{
public static void main(String a[])
{
String fname=a[0];
File f = new File(fname);
int num=0;
if(f.isDirectory())
{
System.out.println("Given file "+fname+"is directory :");
System.out.println("List of files are : ");
String s[] = f.list();
for(int i=0; i<s.length; i++)
{
File f1 = new File(fname, s[i]);
if(f1.isFile())
{
num++;
System.out.println(s[i]); //file name in directory
}
else System.out.println("\n"+s[i]+" is a sub directory");
}
System.out.println("\nNumber of files are: "+num);
}
else
{
if(f.exists())
{
System.out.println("\n"+fname+" is a File");
System.out.println("Details of "+fname+" are : ");
System.out.println("Path of file is "+f.getPath());
System.out.println("Absolute Path of file is
"+f.getAbsolutePath());
System.out.println("Size of file is "+f.length());
}
else System.out.println(fname+" file is not present ");
Slip 12_1: Create the following GUI screen using appropriate layout manager. Accept the name,
class, hobbies from the user and display the selected options in a text box.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Slip12_1()
{
b1=new ButtonGroup();
p1=new JPanel();
p2=new JPanel();
b=new JButton("Clear");
b.addActionListener(this);
r1=new JRadioButton("FY");
r2=new JRadioButton("SY");
r3=new JRadioButton("TY");
b1.add(r1);
b1.add(r2);
b1.add(r3);
r1.addActionListener(this);
r2.addActionListener(this);
r3.addActionListener(this);
c1=new JCheckBox("Music");
c2=new JCheckBox("Dance");
c3=new JCheckBox("Sports");
p1.setLayout(new GridLayout(5,2));
p1.add(l1);p1.add(t1);
p1.add(l2);p1.add(l3);
p1.add(r1);p1.add(c1);
p1.add(r2); p1.add(c2);
p1.add(r3);p1.add(c3);
p2.setLayout(new FlowLayout());
p2.add(b);
p2.add(t2);
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.EAST);
setSize(400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
if(e.getSource()==r1)
{
cnt++;
if(cnt==1)
{
String s =t1.getText();
s1.append("Name = ");
s1.append(s);
}
s1.append(" Class = FY");
}
else if(e.getSource()==r2)
else if(e.getSource()==c1)
{
s1.append(" Hobbies = Music");
}
else if(e.getSource()==c2)
{
s1.append(" Hobbies = Dance");
}
else if(e.getSource()==c3)
{
s1.append(" Hobbies = Sports");
}
t2.setText(new String(s1));
// t2.setText(s2);
if(e.getSource()==b)
{
t2.setText(" ");
t1.setText(" ");
}
}
}
Slip 13_1: Write a program to accept a string as command line argument and check whether it is
a file or directory. Also perform operation as follows:
a. If it is a directory, delete all text file in that directory. Confirm delete operation from user
before deleting text file. Also, display a count showing the number of files deleted, if any, from
the directory.
b. If it is a file display various details of that file.
import java.io.*;
class Slip13_1
{
public static void main(String a[]) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String fname=a[0];
if(f1.isFile())
{
System.out.println(s[i]+" is a file");
if(s[i].endsWith("txt"))
{
System.out.println("\nDo you want to delete the file
with extension "+s[i]+" ?\n1:YES 2:NO");
int ch = Integer.parseInt(br.readLine());
}
}
}
Slip 14_1: Write a menu driven program to perform the following operations on a text file
“phone.txt” which contain name and phone number pairs. The menu should have options:
a. Search name and display phone number
b. Add new name-phone number pair.
import java.io.*;
import java.util.*;
class phone
{
String name,phno;
phone (String nm,String ph)
{
name = nm;
phno=ph;
}
class Slip14_1
{
public static void main (String args[]) throws IOException
{
String s, space = " ";
int i;
do
{
System.out.println("Menu :\n"+"1:Search for a phone no by
name\n"+"2:Add a new Record \n"+"3:Exit\n"+"Enter your Choice :" );
ch=Integer.parseInt(b.readLine());
switch (ch)
{
case 1:
System.out.println ("Enter name to be searched");
phone.search (p,n);
break;
case 2:
rf = new RandomAccessFile (f, "rw");
System.out.println ("Enter Records:\n <name><phone no>
:");
s = rf1.readLine ();
StringTokenizer t = new StringTokenizer (s, space);
String sn = new String (t.nextToken ());
String sp = new String (t.nextToken ());
phone p1 = new phone(sn,sp);
System.out.println(" Records are ");
for(i=0;i<n;i++)
System.out.println(p[i].toString());
System.out.println(p1.toString());
}
}while(ch!=3);
}
}
Slip 15_1: Write a program to read item information( id, name, price, qty) from the file
‘item.dat’. write a menu driven program to perform the following operations using random
access file:
a. Search for a specific item by name
b. Find costliest item
c. Display all items and total cost.
import java.io.*;
import java.util.*;
class item
{
String name,id;
int qty;
double price,total;
}
System.out.println("No Records Found");
}
for(int i=0;i<n;i++)
{
if(arr[i].price > max){
c=i;
}
}
System.out.println(arr[c].toString());
}
}
class Slip15_1
{
public static void main(String[] args)throws IOException
{
String s,space=" ";
int ch,i;
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
for( i=0;i<n;i++)
{
s=b.readLine()+"\n";
rf.writeBytes(s);
}
rf.close();
do {
System.out.println("Menu :\n"+"1:Search for a item by name\n"+"2:Find
costliest item\n"+"3:Display all items and total cost\n4:Exit\n"+"Choice :" );
ch=Integer.parseInt(b.readLine());
switch (ch) {
case 1: System.out.println("Enter item name to be searched:");
item.search(it,n);
break;
case 2: System.out.println("Costliest Item :");
item.search_cost(it,n);
break;
case 3: for(i=0;i<n;i++)
System.out.println(it[i].toString());
break;
case 4: break;
default:System.out.println("Invalid Option");
}
}while(ch!=4);
}
}
import java.io.*;
class Inning_Invalid extends Exception
{}
class CricketPlayer
{
CricketPlayer(){}
CricketPlayer(String n,int no,int n_out,int r)
{
name=n;
no_of_in=no;
not_out=n_out;
runs=r;
}
void cal_avg()
{
try
{
if(no_of_in==0)
throw new Inning_Invalid();
bat_avg=runs/no_of_in;
}
catch(Inning_Invalid ob)
{
System.out.println("no of inning can not be zero");
runs=0;
bat_avg=0;
}
}
void display()
{
System.out.println(name+"\t"+no_of_in+"\t"+not_out+"\t"+runs+"\t"+bat_avg);
}
float getavg()
{
return bat_avg;
}
}
}
class Slip16_1
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no. of Player:");
int n=Integer.parseInt(br.readLine());
CricketPlayer cp[]=new CricketPlayer[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter Name:");
String name=br.readLine();
System.out.print("Enter no of innings:");
int in=Integer.parseInt(br.readLine());
CricketPlayer.sortPlayer(cp,n);
}
}
Slip 17_1: Define an abstract class “staff” with members name and address. Define two
subclasses off this class- ‘Full Timestaff’ (department, salary) and part time staff
import java.io.*;
abstract class Staff
{
String name,address;
}
class PartTime extends Staff
{
int noofhrs;
double rate;
class Slip17_1
{
case 2:
System.out.println("Enter how many staff members:");
n=Integer.parseInt(br.readLine());
PartTime obj[]=new PartTime[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter name:");
name=br.readLine();
System.out.println("Enter Address:");
address=br.readLine();
System.out.println("Enter no of hrs:");
noofhrs=Integer.parseInt(br.readLine());
System.out.println("Enter rate:");
rate=Double.parseDouble(br.readLine());
obj[i]=new PartTime(name,address,noofhrs,rate);
}
System.out.println("Part Time details:");
System.out.println("========================");
for(int i=0;i<n;i++)
obj[i].display();
break;
case 3:break;
default:
System.out.println(" invalid input ");
break;
}
}while(ch!=3);
}
}
Slip 18_1 : Define a class MyNumber having one private int no member. Write a default
constructor to initialize it to 0 and another constructor to initialize it to a value (Use this). Write
methods isNegative, isPositive, isZero, isOdd, isEven.
Create an object in main.Use command line arguments to pass a value to the object and perform
the above tests.
class MyNumber
{
void isNegative()
{
if(no<0)
System.out.println("Given number is negative.\n ");
}
void isPositive()
{
if(no>0)
System.out.println("Given number is positive..\n");
void isZero()
{
if(no==0)
System.out.println("Given number is negative..\n ");
void isOdd()
{
if((no%2)==1)
System.out.println("Given number is Odd..\n ");
void isEven()
{
if((no%2)==0)
System.out.println("Given number is Even.\n ");
}
}
class Slip18_1
{
public static void main(String args[])
{
Slip 20_1: Write a program to create package “TY” which has a class TYmarks( computer total,
maths total, electronics total). Create another package “TY” which has a class TYmarks (Theory,
Practical). Create “n” object of student class having roll number, name, SY Marks and TY
Marks. Add the Marks of SY and TY computer subject and calculate grade (‘A’ for>=70,’B’
for>=60,’C’ for >=50, “Pass Class” for >=40 else “Fail”) and display the result of the student in
proper format.
package SY;
package TY;
/* slip 20_1 */
import SY.SYMarks;
import TY.TYMarks;
import java.io.*;
class Slip20_1
{
int rollno;
int ComputerTotal, MathsTotal, ElecTotal, Theory, Practicals,total;
String name;
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public Slip20_1()
{}
while((ComputerTotal<0 || ComputerTotal>100))
{
System.out.println("Invalid marks.....");
while((MathsTotal<0 || MathsTotal>100))
{
System.out.println("Invalid marks.....");
while((ElecTotal<0 || ElecTotal>100))
{
System.out.println("Invalid marks.....");
while((Theory<0 || Theory>100))
{
System.out.println("Invalid marks.....");
System.out.println("Please Reenter the marks: ");
Theory = Integer.parseInt(br.readLine());
}
while((Practicals<0 || Practicals>100))
{
System.out.println("Invalid marks.....");
}
public void CalculateGrade()
{
double percentage;
//st.getdata();
}
}
Slip 21_1: Define a student class ( roll number, name, percentage). Define a default and
parameterized constructor. keep a count of object created. Create object using parameterized
constructor and display the object count after each object is created. ( use static member and
method). Also display the content of each object. Modify program to create “n” object of the
student class. Accept details for each object. Define static method “shortStudent” which shorts
the array on the basis Of percentage.
import java.io.*;
class Student
{
int rollno;
String name;
float per;
static int count;
Student(){}
Student(String n,float p)
{
count++;
rollno=count;
name=n;
per=p;
void display()
{
}
}
class Slip21_1
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no. of Student:");
int n=Integer.parseInt(br.readLine());
Student p[]=new Student[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter Name:");
String name=br.readLine();
System.out.print("Enter percentage:");
float per=Float.parseFloat(br.readLine());
p[i]=new Student(name,per);
p[i].counter();
}
Slip 22_1: Write a menu driven program to perform following operations. Accept operation
accept the two number using input dialog box. GCD will compute the GCD of two numbers and
display in the message box and power operation will calculate the value of an and display it in
message box where “a” and “n” are two inputted values.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
int n;
int arr[]=new int [20];
int n1,n2,gcd,pow;
public Slip22_1()
{
mb=new JMenuBar();
m1=new JMenu("Operation");
m2=new JMenu("Compute");
String str[]={"Accept","Exit","GCD","Power"};
m=new JMenuItem[str.length];
for(int i=0;i<str.length;i++)
{
m[i]=new JMenuItem(str[i]);
m[i].addActionListener(this);
}
mb.add(m1);
mb.add(m2);
m1.add(m[0]);
m1.add(m[1]);
setLayout(new BorderLayout());
add(mb,BorderLayout.NORTH);
setSize(300,150);
setVisible(true);
setLocation(500,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // constructor
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand(); //return the name of menu
if(s.equals("Exit"))
System.exit(0);
if(s.equals("Accept"))
{
n1=Integer.parseInt(JOptionPane.showInputDialog("Enter 1st no for
Number"));
n2=Integer.parseInt(JOptionPane.showInputDialog("Enter 2nd no for
Number"));
}
if(s.equals("GCD"))
{
int min;
if(n1>=n2)
min=n1;
else min=n2;
for(int i=1;i<=min;i++)
{
if(n1%i==0 && n2%i==0)
gcd=i;
}
JOptionPane.showMessageDialog(null,"GCD = "+gcd);
}
if(s.equals("Power"))
{
pow=1;
for(int i=1;i<=n2;i++)
{
pow=pow*n1;
}
JOptionPane.showMessageDialog(null,"Power = "+pow);
import java.io.*;
interface CreditCard
{
void viewCreditAmount();
void increaseLimit()throws IOException;
void useCard()throws IOException ;
void payCard()throws IOException;
}
SliverCardCustomer()
{
name="Noname" ;
card_no=0;
creditAmount=0;
creaditLimit=50000;
}
}
else
System.out.println("You can't increse limit more than 3 times ");
}
}//end of class
}
else
System.out.println("You can't increse limit more than 3 times ");
}
}
class Slip23_1
{
public static void main(String args[])throws IOException
{
int ch ;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("0. exit") ;
System.out.println("Enter your choice : ") ;
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1: sc.useCard();
break;
case 2:sc.payCard();
break ;
case 3:sc.increaseLimit();
break;
case 4:gc.useCard();
break;
case 5:gc.payCard();
break ;
case 6:gc.increaseLimit();
break;
case 0 :break;
}
}while(ch!=0);
}
}
Slip 24_1: Create a class to create a super class vehicle having members company and price.
Drive two different classes LightMotorVehicle(mileage) and heavyMoterVehicle
(Capacity_in_tons). Accept the information for ‘n’ vehicle and .display the information in
appropriate form while taking data, ask user about the type of vehicle first.
import java.io.*;
class Vehicle
{
String cname;
float price;
void display()
{
System.out.println("Company Name : "+cname+"\nPrice : "+price);
}
super.accept();
void display()
{
super.display();
System.out.println("\nMileage : "+mileage);
}
super.accept();
void display()
{
super.display();
System.out.println("\nCapacity int tones : "+capacity);
}
class Slip24_1
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter type of vehicle \n1. for light motor vehicle \n2. for
heavy motor vehicle ");
int ch = Integer.parseInt(br.readLine());
int no;
switch(ch)
{
case 1 :System.out.println("Enter no of light motor");
no = Integer.parseInt(br.readLine());
LightMotorVehicle lv[] = new LightMotorVehicle[no];
Slip 25_1: Define a class employee having members –id, name, department, salary. Define
default and parameterized constructors. Create a subclass called manager with private member
bonus. Define methods accept and display in both the classes. Create “n” objects of the manager
class and display the details of the manager having the maximum total salary (salary+bonus).
import java.io.*;
class Employee
{
private int id;
private String name, department;
private float salary;
Employee()
{
id = 1;
name = "nived";
department = "bcs";
salary = 20000;
}
id = Integer.parseInt(br.readLine());
name= br.readLine();
department = br.readLine();
salary = Float.parseFloat(br.readLine());
}
void display()
{
System.out.println("\nId : "+id+"\nName : "+name+"\nDepartment :
"+department+"\nSalary : "+salary);
}
float getsalary()
{
return salary;
}
}
Manager()
{
super();
}
Manager(int id, String name, String department, float salary, float bonus)
{
super(id, name, department, salary);
this.bonus = bonus;
}
super.accept();
void display()
{
super.display();
System.out.println("\nBonus : "+bonus);
}
float getbonus()
{ return bonus;
}
}
class Slip25_1
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
calculate_salary(mn,no);
}
float max = 0;
for(int i = 0; i<n; i++)
{
if(sal[i] > max)
{
max = sal[i];
index = i;
}
}
System.out.println("Employee with max salary is: ");
mn[index].display();
}
}