Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

SLIP 1

1. Write a Java program to accept two numbers using command line argument and calculate
addition, subtraction, multiplication and division
class Command1
public static void main(String args[])
{
int a= Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int add,sub,mul,div;
add=a-b;
System.out.println("Additon -"+add);
sub=a-b;
System.out.println("Substraction ="+sub);
mul=a*b;
System.out.println("Multiplication ="+mul);
div=a/b;
System.out.println("Division ="+div);
}

2. Design a screen in Java to handle the Mouse Event such as MOUSE_CLICK and display
the position of the Mouse_Click in a TextField
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MouseDemo extends JFrame implements MouseListener,MouseMotionListener
{
JTextField t1;
public MouseDemo()
{
setLayout(null);
t1=new JTextField();
t1.setBounds(100,100,100,30);
add(t1);
setSize(200,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
t1.setText(me.getX() + " " + me.getY());
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseEntered(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{
}
public void mousePressed(MouseEvent me)
{
}
public void mouseDragged(MouseEvent me)
{
}

public void mouseMoved(MouseEvent me)


{}
public static void main(String args[])
{
new MouseDemo();
}

SLIP2
1.Write a Java program to accept a number from command prompt and generate
multiplication table of that number
import java.io.*;
class Multi
public static void main(String args[]) throws Exception
{
BufferedReader B =new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number: ");
int n = Integer.parseInt(B.readLine());
for (int i=1;i<=10;i++)
{
System.out.println(n+"="+i+"="+n*i);
}
}

2.Write a java program to create and implement the following GUI using Swing components.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Calculator extends JFrame implements ActionListener
{
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
JTextField t1;
JPanel p1;
char op;
float no1,no2,ans;
Calculator()
{
setLayout(new BorderLayout());
p1=new JPanel();
t1=new JTextField();
add(t1,BorderLayout.NORTH);
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b10=new JButton("0");
b11=new JButton("+");
b12=new JButton("-");
b13=new JButton("*");
b14=new JButton("/");
b15=new JButton(".");
b16=new JButton("=");
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b11);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b12);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b13);
p1.add(b10);
p1.add(b15);

p1.add(b16);
p1.add(b14);
p1.setLayout(new GridLayout(4,4));
add(p1,BorderLayout.CENTER);
setSize(400,400);
setTitle("Calculator");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
JButton b=(JButton)ae.getSource();
if(b==b1||b==b2||b==b3||b==b4||b==b5||b==b6||b==b7||b==b8||b==b9||b==b10||b==b1
5)
{
t1.setText(t1.getText() + " "+ b.getLabel());
}
if(b==b11||b==b12|| b==b13||b==b14)
{
op=(b.getLabel()).charAt(0);
no1=Float.valueOf(t1.getText());

t1.setText(" ");
t1.requestFocus();
}
if(b==b16)
{
no2=Float.valueOf(t1.getText());
switch(op)
{
case '+':
ans=no1+no2;
break;
case '-':
ans=no1-no2;
break;
case '*':
ans=no1*no2;
break;
case '/':
ans=no1/no2;
break;
}
t1.setText(Float.toString(ans));
}
}
public static void main(String args[])
{
Calculator C=new Calculator();
}
}

SLIP3

1. Write a Java Program to accept a number using BufferedReader and print reverse of that
number.
class Reverse
{
public static void main(String args[])
{
int n= Integer.parseInt(args[0]);
int rem, reverse=0,num;
n=num;
while(n!=0)
{
rem=n%10;
reverse= reverse*10+rem;
n=n/10;
}
System.out.println("Reverse: "+reverse);
}

2.Write a java program to design and implement event handling for following GUI. Use
appropriate Layout and Components. Display selected Name & Vaccine. If 1st Dose is
taken then write Yes otherwise write No

SLIP4
1. Write a Java program to accept a number from command prompt and print the factors of
that number using BufferedReader
import java.io.*;
class Factor
{
public static void main(String args[]) throws Exception
{
BufferedReader B = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a
number: ");
int n =Integer.parseInt(B.readLine());
int fact=1;
for (int i=1;i<=n;i++)
{
if(n%i==0)
{
System.out.println(i);
}
}
}
2. Create a Person table with fields (Pid, Pname, and Paddress). Write a menu driven program
to perform the following operations on Person table.
a. Insert
b. Update address of ‘Mr. Patil’ to Mumbai.
c. Display
d. Exit
import java.io.*;
import java.sql.*;
class Emp1
{
public static void main(String args[]) throws Exception
{
int pid,k,ch;
ResultSet rs;
String pname,paddress;
Class.forName("org.postgresql.Driver");
Connection cn=DriverManager.getConnection("jdbc:postgresql://localhost:5432/fybcsd","postgres","");
Statement st=cn.createStatement();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("1.Create Table");
System.out.println("2.Insert");
System.out.println("3.Update");
System.out.println("4.Display");
System.out.println("5.Exit");
System.out.println("Enter Ur Choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
sql="create table emp2(pid int,pname text,paddress text)";
st.execute(sql);
System.out.println("Table Is created");
break;

case 2:
System.out.println("pid PName Paddress");
pid=Integer.parseInt(br.readLine());
pname=br.readLine();
paddress=br.readLine();
sql="insert into emp2 values(" + pid + ",'" + pname +"'," + paddress+ ")";
k=st.executeUpdate(sql);
if(k>0)

System.out.printf("Record Is Added...!");

break;
case 3:
System.out.println("pid paddress");
pid=Integer.parseInt(br.readLine());
paddress=br.readLine();
sql="update emp2 set address="+ paddress + " where eno=" + pid;
k=st.executeUpdate(sql);
if(k>0)
System.out.printf("Record Is Updated...!");
break;
case 4:
rs=st.executeQuery("select * from emp2");
while(rs.next())
{
System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3))
}
case 5:
System.exit(0);
}
}
while(ch!=5);
}
}

SLIP5

1. Write a Java program which accepts three integer values using Scanner and prints the
maximum and minimum.
import Java.util.*;
class Greatest
{
public static void main (string args [])
{
Scanner s= new Scanner (systuen. In);
int num1, num2, num3;
System.out.println("Enter three numbers :");
num1 = s.nextInt();
num2 =s.nextInt();
num3 = s.nextInt();
if (num1 > num2 && num1>num3)
{
System.out.println(num1+" num is "greater"):
}
else if (num2 >num1 && num2>num3)
{
Systim out printin (num2 + " num is greater"):
}
else
{
System.out.printin (num 3 + "numg is greater"):
}
}
}

2. Write a Program to design following GUI by using swing component JComboBox. On


click of show button display the selected language on JLabel.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SwingComboEventdemo extends JFrame implements ItemListener
{
JComboBox c1;
JTextField t1;

SwingComboEventdemo()
{
t1= new JTextField();
c1=new JComboBox();
setLayout(null);
add(c1);
add(t1);
c1.setBounds(100,100, 100,50);
t1.setBounds (220, 100,100,50);
c1.addItem("PHP");
c1.addItem("DotNet");
c1.addItem("Java");
c1.addItem("CPP");
setSize(800,800);
setTitle(Combo Event Demo);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c1.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)
{
JComboBox C=(JComboBox)e.getSource();
if(C==c1)
{
t1.setText((String)c1.getSelectedItem());
}
}
public static void main(String args[])
{
SwingComboEventdemo Obj=new SwingComboEventdemo();
}
}

SLIP 6
1. Write a Java program to read an array and print the sum of elements of the array. Also
display array elements in ascending order.
import java.util.*;
class Ascending
{
public static void main(String args[])
{
int no, temp=0;
Scanner S= new Scanner(System.in);
System.out.println("Inter no of element in array:");
no=s.nextInt();
int a[]=new int[no];
System.out.println("Enter element of array:");
for( int i=0; i<no; i++)
{
a[i]=s.nextInt();
}
//sum, of array elemnt
int sum=0;
for(int i=0;i<a.iength;i++)
{
sum=sum+a[i];
}
System.out.println("sum of Array elements :" + sum);
// sort array in ascending order.
for(int i=0; i<a.length; i++)
{
for(int j=i+1;j<a.length;a++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.ou.println("display sorted array");
for(int i=0;i<a.length;i++)
{
System.out.println("assending order of array:"+a[i]);
}
}
}
2. Write a java program to create 2 files File1 & File2. Copy the contents of File1 by changing
the case into File2. Also add the additional comment “End of File” at the end of File2.
import java.io.*;
class FileCopyF1F2F3
{
public static void main(String args[]) throws Exception
{
int b;
char ch,ch1;
File f1=new File("F1.txt");
File f2=new File("F2.txt");
File f3=new File("F3.txt");
if (!( f2.exists()) ||(!(f3.exists())))
{
f2.createNewFile();
f3.createNewFile();
}
FileInputStream fin1=new FileInputStream("F1.txt");
FileOutputStream fout2=new FileOutputStream("F2.txt");
while((b=fin1.read())!=-1)
{
ch=(char)b;
if(Character.isLetter(ch))
{
if(Character.isUpperCase(ch))
ch1=Character.toLowerCase(ch);
else
ch1=Character.toUpperCase(ch);
fout2.write(ch1);
}
}
fin1.close();
fout2.close();
FileInputStream fin11=new FileInputStream("F1.txt");
FileInputStream fin2=new FileInputStream("F2.txt");
FileOutputStream fout3=new FileOutputStream("F3.txt");
while((b=fin11.read())!=-1)
{
fout3.write(b);
}
while((b=fin2.read())!=-1)
{
fout3.write(b);
}
fout3.close();
FileInputStream fin3=new FileInputStream("F3.txt");
while((b=fin3.read())!=-1)
{
System.out.print((char)b); ;
}
}
}

SLIP7
1. Write a Java Program to create a class Employee with data member as Eid, Ename and
Salary. Store the information of ‘n’ employees and Display the name of employee having
maximum salary (Use array of object).
import java.util.*;
class Employee
{
int id;
string name;
float salary;
void accept()
{
Scanner s= new Scanner(System.in);
System.out.println("Enter id: ");
id=s.nextInt();
System.out.println("Enter name :");
name=s.next();
System.out.println("Enter salary:");
salary = s.nextFloat();
void display()
{
System.out.println("Id: "+id);
System.out.println("Name "+ name);
System.out.println("salary: "+ salary);
}
float salary()
{
System.out.println(salary);
}
static float max(Employee e[],int n)
{
float max=0;
int t=0;
for(int i=0;i<n;i++)
{
if(max <(m[i].salary())
{
max=m[i].salary();
t=i;
}
}
System.out.println("max salary:"+max);
return t;
}
}
class EmployeeDemo
{
public static void main( string args[])
{
int n, i,ans;
Scanner s= new Scanner (system.in);
System.out.println("Enter how many records:")
n=s.nextInt();
Employee e=new Employee[n];
for(i=0;i<n; i++)
{
e[i]=new Employee();
e[i].accept();
}
for(i=0; i<n;i++)
{
e[i].display();
}
ans = Employee.max(e, n);
e[ans].display();
}
}
2. Cretae a Person(Pid, Pname, Salary) table. Write a menu driven program to perform the
following operations on Person table.
a. Insert
b. Delete all records of employee whose salary is greater than 50000.
c. Display
d. Exit
import java.io.*;
import java.sql.*;
class person
{
public static void main(String args[]) throws Exception
{
int pid,sal,k,ch;
ResultSet rs;
String pn,sql;
Class.forName("org.postgresql.Driver");
Connection cn=DriverManager.getConnection("jdbc:postgresql://localhost:5432/fybcsd","postgres","");
Statement st=cn.createStatement();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("1.Create Table");
System.out.println("2.Insert");
System.out.println("3.Display");
System.out.println("4.Delete");
System.out.println("5.Exit");
System.out.println("Enter Ur Choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
sql="create table person2(pid int,pname text,sal int)";
st.execute(sql);
System.out.println("Table Is created");
break;

case 2:
System.out.println("pid pname Salary");
pid=Integer.parseInt(br.readLine());
pn=br.readLine();
sal=Integer.parseInt(br.readLine());
sql="insert into person2 values(" + pid + ",'" + pn +"'," + sal+ ")";
k=st.executeUpdate(sql);
if(k>0)
System.out.printf("Record Is Added...!");
break;

case 3:
rs=st.executeQuery("select * from person2");
while(rs.next())
{
System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
}
break;

case 4:
System.out.println("pid ");
pid=Integer.parseInt(br.readLine());
sql="delete from person2 where pid="+ pid;
k=st.executeUpdate(sql);
if(k>0)
System.out.printf("Record Is Deleteed...!");
break;

case 5:
System.exit(0);
}
}
while(ch!=7);
}
}

SLIP 8
1. Write a Java Program to create a class Product with data member as Pid, Pname and Price.
Store the information of ‘n’ products and Display the name of product having maximum
price (Use array of object).
import java.util.*;
class Product
{
int pid;
string pname;
float price;
void accept()
{
Scanner s= new Scanner(System.in);
System.out.println("Enter pid: ");
pid=s.nextInt();
System.out.println("Enter name :");
pname=s.next();
System.out.println("Enter salary:");
price=s.nextFloat();
void display()
{
System.out.println("Id: "+pid);
System.out.println("Name "+ pname);
System.out.println("price: "+ price);
}
float price()
{
System.out.println(price);
}
float max(Product p[],int n)
{
float max=0;
int t=0;
for(int i=0;i<n;i++)
{
if(max <(p[i].price())
{
max=p[i].price();
t=i;
}
}
System.out.println("max price:"+max);
return t;
}
}
class EmployeeDemo
{
public static void main( string args[])
{
int n, i,ans;
Scanner s= new Scanner (system.in);
System.out.println("Enter how many records:")
n=s.nextInt();
Product p=new Product[n];
for(i=0;i<n; i++)
{
p[i]=new Product();
p[i].accept();
}
for(i=0; i<n;i++)
{
p[i].display();
}
ans=Product.max(p, n);
p[ans].display();
}
}

2. Write a menu driven program to perform the following operations on Student(Sid, Sname,
Percentage) table.
a. Insert
b. Display a record of student whose sid is “10001”.
c. Display all records of student.
d. Exit
import java.io.*;
import java.sql.*;
class student
{
public static void main(String args[]) throws Exception
{
int sid,per,k,ch;
ResultSet rs;
String sname,sql;
Class.forName("org.postgresql.Driver");
Connection cn=DriverManager.getConnection("jdbc:postgresql://localhost:5432/fybcsd","postgres","");
Statement st=cn.createStatement();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("1.Create Table");
System.out.println("2.Insert");
System.out.println("3.Display");
System.out.println("4.Search");
System.out.println("5.Exit");
System.out.println("Enter Ur Choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
sql="create table student(sid int,sname text,per int)";
st.execute(sql);
System.out.println("Table Is created");
break;

case 2:
System.out.println("sid sname percentage");
sid=Integer.parseInt(br.readLine());
sname=br.readLine();
per=Integer.parseInt(br.readLine());
sql="insert into student values(" + sid+ ",'" + sname +"'," + per+ ")";
k=st.executeUpdate(sql);
if(k>0)
System.out.printf("Record Is Added...!");
break;

case 3:
rs=st.executeQuery("select * from person2");
while(rs.next())
{
System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
}
break;
case 4:
System.out.println("sid For Search");
sid=Integer.parseInt(br.readLine());
rs=st.executeQuery("select * from student where eno="+ sid);
while(rs.next())
{
System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
}
break;

case 5:
System.exit(0);
}
}
while(ch!=5);
}
}

SLIP 8

1. Define a class student having Rollno, Name and Percentage. Define Default and
parameterized constructor. Overload the constructor. Accept the 3 student details and
display it. (use this keyword).
import java.util.*;
class Student
{
int rollno;
String name;
Float per;
Student()
{
rollno=0;
name=aa;
per=0;
}
Student(int r,String n,float p)
{
rollno=r;
name=n;
per=p;
}
}
class StudentDemo
{
public static void main(String []args)
{
Student s1=new Student(1,"aaa",60);
Student s2=new Student(2,"bbb",75);
Student s3=new Student(3,"ccc",80);
}
}
2. Create a Hash table containing Employee name and Salary. Display the details of the hash
table. Also search for a specific Employee and display Salary of that Employee.
import java.util.*;
class HashEmp
{
public static void main( string args [])
{
Scanner s = new Scanner(System.in);
Hashtable bt = new Hashtable ();
flow salary;
String name;
system.out.println("Enter the no of Employee");
int n=s.nextInt();
for (int i=0; i<n;i++)
{
System.out.println("Enter name of Employee:");
name=s.next();
System.out.println("Enter salary of Employer!');
salary = s.nextfloat();
ht.put(name, salary);
}
System.out.println("Hash Table="+ ht);
Enumerator k =ht.keys();
Enumerater v=ht.elements();
System.out.println("Enter name to the teached :");
String enum=s.next();
int cnt=0;
while (k.hasMoreElements())
{
name = (string) k.nextElements();
if(enum.equals(name))
{
cnt++;
System.out.println("salary:" tht.get(na);
}
if (cnt ==0)
System.out.println("Employee not found");
}
}

SLIP 9
1. Write a Java program to create class as MyDate with dd,mm,yy as data members. Write
default and parameterized constructor. Display the date in dd-mm-yy format.(Use this
keyword)
import java.util.*;
class MyDate
{
int dd, mm, yy;
MyDate()
{
}
MyDate(int dd, int mm, int yy)
{
this.dd=dd;
this.mm=mm;
this.yy=yy;
}
void display()
{
System.out.println("myDade: " + dd + "/ " +mm+"/"+yy);
}
}
class main
{
public static void main(String args[])
{
Scanner S= new Scanner(system.in);
MyDate d=new MyDate(10,05,2022);
d.display();
}
}
2. Write a java program to accept Person name from the user and check whether it is valid or
not. (It should not contain digits and special symbol) If it is not valid then throw user
defined Exception - Name is Invalid -- otherwise display it
import java.io.*;
class DigitSymbolException extends Exception
{
class Person
{
public static void main(String args[]) throws Exception
{
try
{
int i, len, cnt=0;
BufferedReader b= new BufferedReaded (new InputStreamReader(system.in));
System.out.println("Enter Person Name:");
String name=b.readLine();
len=name.length();
for(i=0; i<len; i++)
{
char a=name.CharAt(i);
if (character.isletter(a) && ! character.is(a) && whitespace(a))
{
cnt++;
throw new DigitSymbolException();
}
}
if (cnt==0)
{
System.out.println("Person name:"+name);
}
}
Catch (DigitSymbolException E)
{
System.out.println("Exception- -Invalid Name");
}
}
}

SLIP 10

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 AgeException extends Exception
{
}
class NameException extends Exception
{
}
class Student
{
int rno,a;
String n,c;
Student(int rollno int age,String name, string course)
{
rno=rollno;
a=age;
n=name;
c=course;
}
try
{
if(a<15 || a>21)
throw new AgeException();
int len=n.length();
for(int i=0;i<len;i++)
{
char ch=n.charAt(i);
if ( ! character.isLetter(ch) && !character.isWhiteSpace(ch))
throw new NameException();
}
System.out.println("Student Name = "+ n);
system.out.println(" student age=11+a);
system.out.println(" student roling & course = "+ Eno+c)
}
Catch (AgeException E)
{
System.out.println("Exception-Age Not valid ");
}
Catch(NameException E)
{
System.out.println("Exception - Name not valid");
}
Class StudentAgeNagm
{
public static void main(string args[])
{
int rollno,age;
string name, course;
Buffered Reader b=new BufferedReader(new InputstreamReader (system.in));
System.out.println("Enter name :");
name = b.readLine();
System.out.println("Enter age = ");
age=Integer.parseInt(b.readLine());
System.out.println("Enter fall no :");
rollno=Integer.parseInt(b.readLine());
System.out.println("Enter the course=");
course=b.readLine();
Student s=new Student(rollno,age,name,course);
}
}

2. Create a package named “Series” having three different classes to print series:
a. Fibonacci series
b. Cube of numbers
c. Square of numbers
Write a java program to generate “n” terms of the above series. Accpet n from use
package series;
import java.util.*;
public class cube
{
public void cb()
{
int n;
Scanner s= new Scanner(System.in);
System.out.println("Enter no:");
n = s.nextInt();
int ans = n*n*n;
System.out.println("Cube="+ans);
}
}//vim cube java
package series;
import java.util.*;
public class fibo
{
public void fb()
{
int n,a=0, b=1,c;
Scanner s = new Scanner(System.in);
System.out.println("Enter no:");
n = s.nextInt();
system.out.print in (a);
System.out.print in (b);
for(int i=1;i<n; i++)
{
c =a+b;
System.out.println(c);
a=b;
b=c;
}
}
}// vim fibo.ava
package series;
import java.util.*;
public class Square
{
public void sq()
{
int n;
Scanner s= new Scanner(System.in);
System.out.println("Enter no:");
n = s.nextInt();
int ans = n*n;
System.out.println("square="+ans);
}
}//vim square.java
import series.*;
class seriespkg
{
public static void main(String args[])
{
Square s=new Square();
s.sq();
Cube c=new Cube();
c.cb();
fibo f=new fibo();
f.fb();
}
}

SLIP 11
1. Define a “Point” class having members – x, y(coordinates). Define default constructor and
parameterized constructors. Define two subclasses “ColorPoint” with member as color and
subclass “Point3D” with member as z (coordinate). Write display method to display the
details of different types of Points
class Point
{
int x,y;
point()
{
x=10;
y=20;
}
Point(int x, int y)
{
this.x=x;
this.y= y;
}
void display()
{
System.out.println(x);
System.out.println (y);
}
class colorpoint extends point
{
String color;
Colorpoint ()
{
color=red;
}
Color Point (int x, int y, string color)
{
Super(x,y);
this.color= color;
}
void display()
{
super.display();
System.out.println(Color);
}
}
}
class Point3D extends Point
{
int z;

Point3D()
{
z=30;
}
paint 3D (int x, int y,int z)
{
Super(x,y);
this.z=z;
}
void display()
{
super.display();
System.out.println(z);
}
}
class PointDemo
{
public static void main(String args[])
{
colorpoint c=new Colorpoint(10,20,red);
c.display();
Point3D d=new Point3D(20,30,40);
d.display();
}
}

2. Write a program to design and implement following GUI // MENUBAR


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class SwingJMenuDemo extends JFrame
{
JMenuBar mb;
JMenu x;
JMenuItem m1, s2,s3,s4 ,s1;
JMenu s;
JMenu h
SwingJMenuDemo()
{
setLayout(null);
setTitle("My Swing Demo");
setSize(400,400);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
mb = new JMenuBar(); // create a menu bar
x = new JMenu("File"); // create a menu
S=new JMenu("Edit");
h=new JMenu("Help");

m1 = new JMenuItem("New"); // create menuitems


s1= new JMenuItem("Cut");
s2 = new JMenuItem("Copy");
s3=new JMenuItem("Paste");
s4=new JMenuItem("Select All");

x.add(m1); // add menu items to menu


mb.add(x); // add menu to menu bar
S.add(s1);
S.add(s2);
S.add(s3);
S.add(s4);

mb.add(S);
mb.add(h);

setJMenuBar(mb); // add menubar to frame


}
public static void main(String args[])
{
SwingJMenuDemo b=new SwingJMenuDemo();
}
}

SLIP 12
1. Write a java program that displays the number of characters, lines and words of a file.
import java.io.*;
class FileDemo
{
public static void main(String args[])
{
try
{
int lines=0,chars=0,words=0;
int code=0;
FileInputStream fis = new FileInputStream("sample.txt");
while(fis.available()!=0)
{
code = fis.read();
if(code!=10)
chars++;
if(code==32)
words++;
if(code==13)
{
lines++;
words++;
}
}
System.out.println("No.of characters = "+chars);
System.out.println("No.of words = "+(words+1));
System.out.println("No.of lines = "+(lines+1));
fis.close();
}
catch(FileNotFoundException e)
{
System.out.println("Cannot find the specified file...");
}
catch(IOException i)
{
System.out.println("Cannot read file...");
}
}

2. Write a Java program to create a super class Employee (members – name, salary). Derive
a sub-class as Developer (member – projectname). Derive a sub-class Programmer
(member – proglanguage) from Developer. Create object of Developer and display the
details of it. Implement this multilevel inheritance with appropriate constructor and
methods

Class Employee
{
String name;
float salary;
Employee (String name, float sulary)
{
this .name name;
this .salary= salary;
}
void display()
{
System.out.println("Emp Name: "'+ name);
System.out.println("Emp salary: "+ salary);
}
}
class Developer extends Employee
{
string pname;
Developer (string name, float salary, string pname)
{
Super (name, salary);
thispname pname;
}

void display ()
{
Super .display();
system.out.println("project name :"+ pname);
}
}
Closs programmer extends Developer.
{
string proglang;
Programmer(string name, float salary, String pnamestring proglang)
{
super.name = name:
super. salary = salary;
Super.pname=pname;
this .proglang = progllanguage;
}
void display()
{
Super.display()
System.out.printIn ("program language:" + proglang);
}
}
class multilevelInheritanceDemo
{
public static void main(string args[])
{
programmer P = new programmes ("aaa",40000,”xyz”,”java”);

p.display();
}
}
SLIP 13

1. Write a java program to accept a string from the user using command line argument. If it
is a file, display all information about the file (path, size, attributes etc.)
import java.io.*;
class FileInfo
{
public static void main(String args[]) throws Exception
{
File F=new File(args[0]);
if(F.exists())
{
System.out.println("File Name " + F.getName());
System.out.println("File Length " + F.length());
System.out.println("File Path" + F.getPath());
System.out.println("File Absolute Path " + F.getAbsolutePath());
System.out.println("File Parent " + F.getParent());
}
else
System.out.println(F.getName()+ " " + " doesnot Exists");
}
}

2. Construct linked List containing names of colours: red, blue, yellow and orange. Then
extend your program to do the following:
i. Display the contents of the List using an Iterator.
ii. Display the elements of LinkedList in reverse order using ListIterator.
iii. Create another list containing pink and green. Insert the elements of this list between
blue and yellow.

import java.util.*;
dass linkedList Demo
{
public static void main(String args[])
{
List Colcarlist = new linked List ();
colorlist.add("red");
Colorlist.add("blue");
Colorlist.add("yellow");
colortis).add("orange");

Iterator = colo list. Iterator();


system.out.println("Colors List = ");
while(i.hasNext())
system.out.println("-next());
}
int n= cobrlist.size();
ListIterator ri= colorlist-ListIterator(n);
System.out.println("Reverse colour List:");
while (ri. hasprevious ())
{
System.out.println(ri-previous ());
}
List colorlist= new linkedlist();
Colorlist.add("pink");
colorlist.add("green");
Colorlist .addAll(2, colorlists);
system.out.println("modified color List:" +colorlist);
}
}

SLIP 14

1. Define a class Number having one integer data member. Write a default constructor
initialize it to 0 and another constructor to initialize it to a value. Write methods
isNegative(), isPositive(). Use command line argument to pass a value to the object and
perform the specified operations.

public class MyNumber {


private int x;
public MyNumber(){
x=0;
}
public MyNumber(int x){
this.x=x;
}
public boolean isNegative(){
if(x<0)
return true;
else return false;
}
public boolean isPositive(){
if(x>0)
return true;
else return false;
}
public boolean isZero(){
if(x==0)
return true;
else return false;
}
public boolean isOdd(){
if(x%2!=0)
return true;
else return false;
}
public boolean isEven(){
if(x%2==0)
return true;
else return false;
}

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


{
int x=Integer.parseInt(args[0]);
MyNumber m=new MyNumber(x);
if(m.isNegative())
System.out.println("Number is Negative");
if(m.isPositive())
System.out.println("Number is Positive");
if(m.isEven())
System.out.println("Number is Even");
if(m.isOdd())
System.out.println("Number is Odd");
if(m.isZero())
System.out.println("Number is Zero");
}
}
2. Write a program to design following GUI using JTextArea. Write a code to display number
of words and characters of text in JLabel. Use JScrollPane to get scrollbars for JTextArea.

import javax.swing.*;
import java.awt.*;
import static javax.swing.ScrollPaneConstants.*;

public class Test {


public static void main(String[] args)
{
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new JLabel("text"));
panel.add(new JLabel("text"));
panel.add(new JLabel("text"));

JTextArea textArea = new JTextArea();


textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setText("This text fits");
panel.add(textArea);

JScrollPane scrollPane =
new JScrollPane(panel, VERTICAL_SCROLLBAR_AS_NEEDED,
HORIZONTAL_SCROLLBAR_NEVER);

JFrame frame = new JFrame("Title");


frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollPane);

frame.pack();
frame.setVisible(true);
}
}

SLIP 15
1. Define a class Number having one integer data member. Write a default constructor
initialize it to 0 and another constructor to initialize it to a value. Write methods isOdd(),
isEven(). Use command line argument to pass a value to the object and perform the given
operations.
public class MyNumber {
private int x;
public MyNumber(){
x=0;
}
public MyNumber(int x){
this.x=x;
}
public boolean isNegative(){
if(x<0)
return true;
else return false;
}
public boolean isPositive(){
if(x>0)
return true;
else return false;
}
public boolean isZero(){
if(x==0)
return true;
else return false;
}
public boolean isOdd(){
if(x%2!=0)
return true;
else return false;
}
public boolean isEven(){
if(x%2==0)
return true;
else return false;
}

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


{
int x=Integer.parseInt(args[0]);
MyNumber m=new MyNumber(x);
if(m.isNegative())
System.out.println("Number is Negative");
if(m.isPositive())
System.out.println("Number is Positive");
if(m.isEven())
System.out.println("Number is Even");
if(m.isOdd())
System.out.println("Number is Odd");
if(m.isZero())
System.out.println("Number is Zero");
}
}

2. Write a program to design and implement event handling for following GUI. Verify
username and password in 3 attempts. Display dialog box "Login successful" on success
or display "Username or Password is in correct". After 3 attempts display "Login Failed".
On reset button clear the fields of text box.

SLIP 16
1. Write a java program to accept ‘n’ integers from user and store them in an ArrayList
Collection. Display the elements of ArrayList in reverse order.(Use ListIterator)

import java.util.*;
class array
{
public static void main(String a[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Limit of ArrayList :");
int n=sc.nextInt();
ArrayList alist=new ArrayList();
System.out.println("Enter Elements of ArrayList :");
for(int i=0;i<n;i++)
{
String elmt=sc.next();
alist.add(elmt);
}
System.out.println("Original ArrayList is :"+alist);
Collections.reverse(alist);
System.out.println("Reverse of a ArrayList is :"+alist);
}
}
import java.util.*;
class array
{
public static void main(String a[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Limit of ArrayList :");
int n=sc.nextInt();
ArrayList alist=new ArrayList();
System.out.println("Enter Elements of ArrayList :");
for(int i=0;i<n;i++)
{
String elmt=sc.next();
alist.add(elmt);
}
System.out.println("Original ArrayList is :"+alist);
Collections.reverse(alist);
System.out.println("Reverse of a ArrayList is :"+alist);
}
}

2. Write a java program to create class Account (accno, accname, balance). Create an array
of “n” Account objects. Define static method “sortAccount” which sorts the array on the
basis of balance. Display account details in sorted order.

import java.util.*;
class Account
{
int accno ;
string accname;
float balance;
Void accept ()
{
Scanner s= new Scanner(System.in);
System.out.println("* Enter Account no");
accno=s.nextInt();
System.out.println("Enter Account Name:");
accname = s.next();
System.out.println("Enter Balance:");
balance=s.nextFloat();
}
void display()
{
System.out.println("Account No:" + accno);
System.out.println("Account Name :"+ accname);
System.out.println("Balance"'+ balance);
}
Static void SortAccount (Account a[])
{
for (int i=0; i< a. length; i++)
{
for(int j=i+1;j<a.length; j++)
{
Account temp;

if (a[i]). balance > a[j].balance)


{
temp = a[i];
a[i[=a[j];
}
}
}
}
}
class Account Demo
{
public static void main (string args [])
{
Scanner s = new scanner (system.in);
System.out.println("HOW many accounts:”);
int n = s.nextInt();
Account a[] = new Account [n];
for (int i=0; i<n; i++)
{
A[i] = new Account [];
a[i].accept();
}
System.out.print In ("Before Sort: ");
for(int i=0; i<n;i++)
{
a[i].display();
}
Account.sortAccount (a);
System.out.println("After sort :");
for (int i=0; i<n; i++)
{
a[i].display();
}
}
}

SLIP 17

1. Write a program to display information about the ResultSet like number of columns
available in the ResultSet and SQL type of the column. Use Person table. (Use
ResultSetMetaData)

import java.sql.*;
class person
{
public static void main(String args[]) throws Exception
{
Class.forName("org.postgresql.Driver");
Connection
cn=DriverManager.getConnection("jdbc:postgresql://localhost:5432/fybcsd","postgres","");
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select * from emp");
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("Total Columns : "+rsmd.getColumnCount());
System.out.println("Column name of first columns : "+rsmd.getColumnName(1));
System.out.println("Column Type Name : "+rsmd.getColumnTypeName(1));
}
}

2.Define an interface “Operation” which has methods area(),volume(). Define a constant PI


having a value 3.142. Create a class circle (member – radius), cylinder (members – radius,
height) which implements this interface. Calculate and display the area and volume.

interface Operation
{
final float PI = 3.142f;
abstract void area();
abstract vold volume();
}
class circle implements operation
{
float radius;
circle (float r)
{
radius = r;
}
public void area()
{
float aoc;
aoc = pi*radius*radius;
System.out.println(" Area of circle="'+aoc);
}
public void volume()
{
float voc;
voc= 4/3* PI* radius * radius* radius;
System.out.println(" volume of circle = "+ voc);
}
Class Cylinder implements operation
{
float radius,height;
Cylinder (float r, float h);
{
radius=r;
height=h;
}
public void area()
{
float area;

area = 2* PI* radius*height +2 PI * radius * radius;


System.out.println("area of cylinder = "'+ area);
}
public void volume U
{
float volume;
volume = PI *radius * radius * height;
System.out.println("volume of cylindes="+ volume);
}
class Interfacefloss
{
public static void main(String args(1)
{
Circle c1 = new.Circle (2.200);

c1.area();
c1. Volume();

Cylinder c2=new cylinder (2.5f,2.7f)


C2.area();
c2.volume();
}
}
SLIP 18
1. Write a Java program to display information about database (Use DatabaseMetaData)

import java.sql.*;
class person
{
public static void main(String args[]) throws Exception
{
Class.forName("org.postgresql.Driver");
Connection
cn=DriverManager.getConnection("jdbc:postgresql://localhost:5432/fybcsd","postgres","");
Statement st=cn.createStatement();
DatabaseMetaData dbmd=cn.getMetaData();
System.out.println("Driver Name : "+dbmd.getDriverName());
System.out.println("Driver Version : "+dbmd.getDriverVersion());
System.out.println("User Name : "+dbmd.getUserName());
System.out.println("DB Product Name : "+dbmd.getDatabaseProductName());
}
}

2. Define a class Employee having members – id, name, salary. Define default constructor.
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 worker having the maximum total salary (salary + bonus).
Import java.util.*;
Class Employee
{
Int id;
string name;
float salary;
Employee (int eid, stating ename, float salary)
{
Id=eid;
name= ename;
Salary = esalary;
}
void accept ()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter id: ");
id=s.nextInt();
System.out.println("Enter name:"");
name=s.next();
System.out.println("Enter salary: ");
salary = s.nextFloat();
}
void display()
{
System.out.println("did: "+id);
System.out.println("Name :"+ name);
System.out.println("salary: "+ salary);
}

float salary()
{
System.out.println(salary);
}
Class Manager extends Employee
{
private float bonus;
void accept ()
{
Super. Accept()
Scanner s=new scanner(Syste.in);
System.out.println("Enter a bonus:");
bonus = s.next Float();
void display()
{
super, display System.out.println("Bonus is: "'+ bonus);
static float max (manager m[], int n)
float max=0;
int t=0;
for (int i=0;i<n; i++)
{
if(max < (m[i].salary() + m[i] .bonus))
{
max = m[i].salary + m[i].bonus;
t=i;
System.out.println("max sulary:" +
return t;
}
}
class EmployeeDemo
{
public static void main( string args[])
{
int n, i,ans;
Scanner s = new Scanner (System.in);
System.out.println("Enter how many records:");
n-s.nextInt();
Manager m[] = new Manager [n];
for (i=0; i<n; i++)
{
m[i]=new manager();
m[i]-accept();
}
for(i=0; i<n;i++);
{
m[i]. display();
}
Manager.max (m, n);
m[ans]. display();
}
} SLIP 19
1. Accept n integers from the user and store them in a collection. Display them in the sorted
order. The collection should not accept duplicate elements. (Use a suitable collection).
Search for a particular element using predefined search method in the Collection
framework.
import java.util.*;
import java.io.*;
class sortedNumbers
{
public static void main(string args[]) throws Exception
{
BufferedReader b= new BufferedReader(new InputStreamReader(system.in));
set s=new Treeset();
System.out.println("Enter no of integers: ");
Int n= Integer.parseInt(b.readLine();
For(int i=0; in; itt).
{
system.out.println("Enter Number :");
int x = Integer.parseInt(b.sead Line ());
s.add(x);
}
Iterator i = s.iterator();
while (i.hasNext()).
{
System.out.println("i.next());
}
System.out.println("Enter element to be sorted:");
int no = Integer.parseInt(b,read Line ());
if(s.contains(no))
{
System.out.println("Number "+no + "found");
}
else
{
System.out.println("Number "+no+ "not found ");
}
}
2. Define an abstract class Staff with members name and address. Define two sub-classes of
this class – FullTimeStaff (members - department, salary, hra - 8% of salary, da – 5% of
salary) and PartTimeStaff (members - number-of-hours, rate-per-hour). Define appropriate
constructors. Write abstract method as calculateSalary() in Staff class. Implement this
method in subclasses. Create n objects which could be of either FullTimeStaff or
PartTimeStaff class by asking the user‘s choice. Display details of all FullTimeStaff objects
and all PartTimeStaff objects along with their salary.
import java.io.*;
abstract class Staff
{
String name,address;
Abstract void calculatesalary();
}
class FullTimeStaff extends Staff
{
String department;
double salary;
FullTimeStaff(String name,string address,string department,double salary)
{
this.name=name;
this.address=address;
this.department=department;
this.salary=salary;
}
public void display()
{
System.out.println("Name: "+name);
System.out.println("Address: "+address);
System.out.println("Department: "+department);
System.out.println("Salary: "+salary);
System.out.println(“HRA=”+salary*0.08 );
System.out.println(“DA=”+salary*0.05);
}
Void calculatesalary()
{
double sal=salary+(salary*0.08)+(salary*0.05);
System.out.println(“total salary=”+sal);
}
}
class PartTimeStaff extends Staff
{
int noofdays;
float rate;
PartTimeStaff(String name,String address,int noofdays,float rate)
{
this.name=name;
this.address=address;
this.noofdays=noofdays;
this.rate=rate;
}
public void display()
{
System.out.println("Name: "+name);
System.out.println("Address: "+address);
System.out.println("No of days: "+noofdays);
System.out.println("Rate per hour: "+rate);

}
Void calculatesalary()
{
float sa=noofdays*rate;
System.out.println(“total sarary=”+sal);
}
}
public class emp
{
public static void main(String [] args) throws IOException
{
int i,ch=0,n=0,noofdays;
String name,address,department;
float salary,rate;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("Select Any One: ");
System.out.println("1.Full Time Staff");
System.out.println("2.Part Time Satff");
System.out.println("3.exists ");
System.out.println("enter ur choice: ");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter the number of Full Time Staff: ");
int n=Integer.parseInt(br.readLine());
FullTimeStaff f[]=new FullTimeStaff[n];
for(i=0;i<n;i++)
{
f[i]=new FullTimeStaff();
f[i].accept();
}
for(i=0;i<n;i++){
l[i].display();
}
break;
case 2:
System.out.println("Enter the number of Part Time Staff: ");
int m=Integer.parseInt(br.readLine());
PartTimeStaff [] h=new PartTimeStaff[m];
for(i=0;i<m;i++){
h[i]=new PartTimeStaff();
h[i].accept();
}
for(i=0;i<m;i++){
h[i].display();
}
break;

}
}
}

You might also like