Java Lab Record Final for Pint
Java Lab Record Final for Pint
19 Write a program to accept data and display output in key, value pair?
1. Write a ‘java’ Program to Demonstrate the concept of class Box with Constructors
import java.io.*;
class Box
Box()
try{
width=Double.parseDouble(br.readLine());
height=Double.parseDouble(br.readLine());
depth=Double.parseDouble(br.readLine());
catch(IOException ioe)
width=w;
height=h; depth=d;
double volume()
return (width*height*depth);
}
5
}
class BoxDemo
Box b1=new
Box(Double.parseDouble(args[0]),Double.parseDouble(args[1]),Double.parseDouble(
vol=b1.volume();
}}
Output
6
2. Write a java program to calculate salary of n employees using concept of classes with
constructor and method?
import java.util.*;
class Employee
System.out.println("Enter the employee id"); //taking all the inputs from the user
employeid=sc.next();
empname=sc.next();
basicsalary=sc.nextInt();
calculate();
HRA=(10/100)*basicsalary;
DA=(73/100)*basicsalary;
GS=basicsalary+DA+HRA;
incometax=(30/100)*GS;
netsalary=GS-incometax;
{
7
System.out.println("Employeeid : "+employeid);
System.out.println("Employename : "+empname);
System.out.println("HRA : "+HRA);
System.out.println("DA : "+DA);
System.out.println("GS : "+GS);
System.out.println("Incometax : "+incometax);
System.out.println("netsalary : "+netsalary);
class EmpRecord
emp.Employee();
emp.display();
}
8
Output:
import java.util.Scanner;
class StudentGrade
int i;
marks[i] = scanner.nextInt();
}
9
scanner.close();
avg = total/6;
if(avg>=80)
System.out.print("A");
System.out.print("B");
System.out.print("C");
else
System.out.print("D");
sg.student();
sg.stdGrade();
}}
10
Output:
3
11
class Base
void display()
void show()
System.out.println("\n***inheritance***\\\n");
d.show();
d.display();
}
3
Output: 12
3
13
5. Write a program to implement multilevel inheritance?
import java.io.*;
import java.lang.*;
class Base
int l,b;
void area()
{
void perimeter()
area();
l=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
perimeter();
}
{
3
System.out.println("\n /***Multilevel Inheritance***\\\n"); 14
Output:
3
6. Write a program to demonstrat abstract class and dynamic polymorphism? 15
import java.io.*;
class shape
double a,b;
shape(double a,double b)
this.a=a;
this.b=b;
Rectangle(double a,double b)
super(a,b);
void area()
System.out.println("area of rectangle="+(a*b));
Triangle(double a,double b)
super(a,b);
void area()
3
{ 16
class DynamicPolymorphism
System.out.println("Dynamic polymorphism");
shape ref;
ref=r;
ref.area();
ref=t;
ref.area();
Output
3
17
7. Write a program to implement packages?
package pack;
import java.util.*;
int n1=sc.nextInt();
int n2=sc.nextInt();
int sum=n1+n2;
// Access Package
import pack.DemoPackage;
class AccessPackage
obj.msg();
}
3
Output: 18
3
19
8. Write a program to demonstrate various arithmetic calculations using packages?
package demopack;
return x+y;
return x-y;
return x*y;
return (double)x/y;
return x%y;
}
3
Package Creations 20
import demopack.MyMaths;
import java.util.*;
}
3
Output: 21
3
9. Write a program to implement string handling methods? 22
class StrHdlDemo1
{
public static void main(String args[])
{
String s1="MCA";
String s2=" it department";
String st=s1.concat(s2);
int cmp=s1.compareTo(s2);
long hs=s2.hashCode();
int s=s2.indexOf('e',3);
int ln=s2.length();
String sb=s2.substring(4);
String lwr=s1.toLowerCase();
String upr=s2.toUpperCase();
String trm=s2.trim();
System.out.println("The concatenation of two strings is "+st);
System.out.println("he comparison of two strings is "+cmp);
System.out.println("The hash code of string 2 is "+ hs);
System.out.println("The index of E in string 2 is "+s);
System.out.println("The length of string 2 is "+ln);
System.out.println("Substring of string 2 = "+sb);
System.out.println("Lower case of string1 is ="+lwr);
System.out.println("UPPER case of string 2 = "+upr);
System.out.println("After trimming, string 2 ="+trm);
}
}
Output
3
23
class ExceptionDemo
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter first number");
int a = Integer.parseInt(br.readLine());
String name;Thread t;
Multithread(String s1)
name=s1;t=new Thread(this,name);
System.out.println("Multithread name:"+t);
t.start();
try
for(int i=0;i<=5;i++)
System.out.println(name+":"+i);
Thread.sleep(500);
catch(InterruptedException e)
System.out.println("Interrupted");
{
3
System.out.println("\n/******Multithreading by using Runnable interface ******\\\n"); 25
new Multithread("one");
new Multithread("two");
Output
3
12. Write a program to demonstrate mutual exclusion using thread 26
synchronization?
import java.io.*;
class Bank
int total=100;
if(total>=withdrawal){
total=total-withdrawal;
try{
Thread.sleep(1000);
catch(InterruptedException e)
e.printStackTrace();
else
try
Thread.sleep(1000);
catch(InterruptedException e)
{e.printStackTrace();
3
}}} 27
total=total+deposit;
try
Thread.sleep(1000);
catch(InterruptedException e)
e.printStackTrace();
}}}
class GFG
obj.withdrawn("Arnab",20);
obj.withdrawn("Monodwip",40);
obj.deposit("Mukta",35);
obj.withdrawn("Rinkel",80);
obj.withdrawn("Shubham",40);
}}
3
Output 28
3
13. Write a program to demonstrate Linked list class? 29
import java.io.*;
import java.util.*;
class LLdemo
ll.add("America");
ll.add("India");
ll.add("japan");
System.out.println("list = "+ll);
String element;
int position,choice=0;
while(choice<4)
System.out.println("4. Exit");
choice = Integer.parseInt(br.readLine());
switch(choice)
element = br.readLine();
ll.add(position-1,element);
break;
position = Integer.parseInt(br.readLine());
ll.remove(position-1);
break;
position = Integer.parseInt(br.readLine());
element=br.readLine();
ll.set(position-1,element);
break;
default: return;
System.out.println("list= "+ll);
Iterator it = ll.iterator();
while(it.hasNext());
System.out.println("it.next()"+" ");
}
3
Output: 31
3
14. Write a program to demonstrate Hash Set class? 32
import java.util.*;
class Hset
{
public static void main(String args[])
{
HashSet<String> hs = new HashSet();
hs.add("India");
hs.add("America");
hs.add("Japan");
hs.add("China");
hs.add("America");
System.out.println("Hash Set = "+hs);
Iterator<String> it = hs.iterator();
System.out.println("element using Iterator");
while(it.hasNext())
{
//String s = (String)it.next;
System.out.println(it.next());
}
}
}
Output:
3
15. Write a program to demonstrate Iterator class? 33
import java.util.*;
class Arraylistdemo
{
public static void main(String args[])
{
ArrayList arl = new ArrayList();
arl.add("apple");
arl.add("Mango");
arl.add("grapes");
arl.add("Gauva");
System.out.println("Contenets : "+arl);
arl.remove("apple");
arl.remove(1);
System.out.println("contents after removing :"+arl);
System.out.println("size of arraylist:"+arl.size());
System.out.println("Extracting using Enumerater interface");
//Enumeration e = arl.enumeration();
//while(e.hasMoreElements())
Iterator tr=arl.iterator();
while(tr.hasNext())
{
System.out.println(tr.next());
}
}
}
Output:
3
34
16. Write a program to demonstrate Enumeration interface?
import java.util.Enumeration;
import java.util.Vector;
public class EnumTest
{
public static void main(String[] args)
{
Vector v = new Vector();
for (int i = 0; i < 10; i++)
v.addElement(i);
System.out.println(v);
Enumeration e = v.elements();
while (e.hasMoreElements())
{
int i = (Integer)e.nextElement();
System.out.print(i + " ");
}
}
}
3
17. Write a program to demonstrate Comparator Interface? 35
import java.io.*;
import java.util.*;
abstact class Ascend implements Comparator
{
public int compare(Integer i1,Integer i2)
{ return i1.compareTo(i2); }
}
class Decend implements Comparator
{
public int compare(Integer i1,Integer i2)
{ return i2.compareTo(i1); }
}
class Array1
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("how many elements");
int size = Integer.parseInt(br.readLine());
Integer arr[] = new Integer[size];
for(int i=0;i<size;i++)
{
System.out.println("Enter int:");
arr[i]=Integer.parseInt(br.readLine());
}
Arrays.sort(arr,new Ascend());
System.out.println("\n sorted in Asending order:");
display(arr);
}
static void display(Integer arr[])
{
for(Integer i: arr)
System.out.print(i+"\t");
}
}
3
Ouput 36
3
37
18. Write a program to implement sring tokenizer?
import java.util.StringTokenizer;
class StringTokenizerDemo
{
public static void main(String args[])
{
StringTokenizer st = new StringTokenizer("Hello Everyone Have a nice day"," ");
System.out.println("Total number of Tokens: "+st.countTokens());
StringTokenizer sd = new StringTokenizer("Hello Everyone This is String Tokenizer file"," ");
while (sd.hasMoreTokens())
{
System.out.println(sd.nextElement());
}
}
}
OutPut:
3
19. Write a program to accept data and display output in key, value pair? 38
import java.util.*;
class Hashtable1{
public static void main(String args[]){
Hashtable<Integer,String> hm=new Hashtable<Integer,String>();
hm.put(100,"Amit");
hm.put(102,"Ravi");
hm.put(101,"Vijay");
hm.put(103,"Rahul");
for(Map.Entry m:hm.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
Output:
3
39
20. Write a program to create a registration form with different Controls?
import java.awt.*;
import java.awt.event.*;
class Mytext extends Frame implements ActionListener
{
TextField name,pass;
Mytext()
{
setLayout(new FlowLayout());
Label n = new Label("name :",Label.LEFT);
name = new TextField(20);
Label p = new Label("password : ",Label.LEFT);
pass = new TextField(20);
pass.setEchoChar('*');
name.setBackground(Color.pink);
name.setForeground(Color.blue);
Font f = new Font("Arial",Font.PLAIN,25);
name.setFont(f);
this.add(n);
this.add(name);
this.add(p);
this.add(pass);
name.addActionListener(this);
pass.addActionListener(this);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{ System.exit(0);}
});
}//constructor
public void actionPerformed(ActionEvent ae)
{
repaint();}
public void paint(Graphics g)
{
g.drawString("Name : "+name.getText(),10,200);
g.drawString("Password : "+pass.getText(),10,240);
}
public static void main(String args[])
{
Mytext mt = new Mytext();
mt.setTitle("my text field");
3
mt.setSize(400,400); 40
mt.setVisible(true);
}
}
Output:
3
21. Write a program to create a registration form with different menus? 41
import java.awt.*;
import java.awt.event.*;
class Menutest extends Frame implements ActionListener
{
MenuBar mb=new MenuBar();
public Menutest()
{
setTitle("Menu Window");
setSize(300,300);
setLocation(100,100);
mnu1.add(mi1);
mnu2.add(mi2);
mnu2.add(mi3);
mnu2.add(mi4);
mb.add(mnu1);
mb.add(mnu2);
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
setMenuBar(mb);
setVisible(true);
}
Output:
4
43
4
22. Write a program to create a registration form for demonstrating event handling? 44
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
Menu vehicles,colors;
TextField tf;
public menudemo()
setMenuBar(mb);
colors.add(new MenuItem("Pink"));
colors.add(new MenuItem("blue"));
colors.add(new MenuItem("green"));
colors.add(new MenuItem("yellow"));
mb.add(vehicles);
mb.add(colors);
vehicles.addActionListener(this);
colors.addActionListener(this);
tf = new TextField(15);
add(tf,"South");
setTitle("Menus in Action");
4
setSize(300,350); 45
setVisible(true);
new menudemo();
Output
4
46
23. Write a program to copy data from one file to another file?
import java.io.*;
class Copyfile
{
public static void main(String args[]) throws IOException
{
int ch;
FileInputStream fin = new FileInputStream(args[0]);
FileOutputStream fos = new FileOutputStream(args[1]);
while((ch=fin.read())!=-1)
fos.write(ch);
fin.close();
fos.close();
System.out.println("1 file is copied");
}
}
Output:
4
47
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
FileOutputStream fos = new FileOutputStream ("Objfile");
ObjectOutputStream oos= new ObjectOutputStream (fos);
System.out.print("how many objects ?");
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
Employee e1 = Employee.getdata();
oos.writeObject(e1);
}
oos.close();
}
}
Serializing an object into the file objfile
4
26. Write a program to retrieve web page using URL? 50
import java.io.*;
import java.net.*;
class Myurl
System.out.println("protocol : "+obj.getProtocol());
System.out.println("Host : "+obj.getHost());
System.out.println("File : "+obj.getFile());
System.out.println("Port : "+obj.getPort());
System.out.println("Path : "+obj.getPath());
}
Output:
4
51
//server
import java.io.*;
import java.net.*;
class Server1
Socket s =ss.accept();
System.out.println("Connection established");
ps.println(str);
ps.println("Bye");
ps.close();
ss.close();
}
4
//creating server for sending some string to the client 52
import java.io.*;
import java.net.*;
class Client1
InputStream obj=s.getInputStream();
String str;
while((str=br.readLine())!=null)
br.close();
s.close();
Server Compilation
4
53
Client Compilation
import java.awt.*;
import javax.swing.*;
JFrame f;
Border()
f = new JFrame();
// creating buttons
f.setSize(300, 300);
f.setVisible(true);
new Border();
}
4
} 55
Output
50
import java.awt.*;
import javax.swing.*;
JFrame frameObj;
// constructor
FlowLayoutExample()
frameObj.add(b9); frameObj.add(b10);
frameObj.setLayout(new FlowLayout());
frameObj.setSize(300, 300);
frameObj.setVisible(true);
// main method
new FlowLayoutExample();}}
Output
5
30. write a program to Demonstrate Key Listener? 58
import java.awt.*;
import java.awt.event.*;
String str;
public MyKeyEvents()
this.addWindowListener(new WindowAdapter()
System.exit(0);
}});
str=new String();
addKeyListener(this);
char ch=k.getKeyChar();
str+=ch;
repaint();
{ }
{ }
g.drawString(str,140,140);
f.setSize(550,550);
f.setVisible(true);
}}
Output