0% found this document useful (0 votes)
85 views

Ex No: 1 Substring Removal From A String Using String Buffer Class Date

This document contains code for generating an electricity bill. The code imports necessary Java classes and defines an Ebill class with a main method. The main method creates a File object pointing to an "Electricity.txt" file and opens it for writing using a FileOutputStream. It then prompts the user to enter meter reading, unit consumed, and rate per unit via the keyboard. Based on the input, it calculates the bill amount and writes the output to the "Electricity.txt" file.

Uploaded by

Vinoth M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Ex No: 1 Substring Removal From A String Using String Buffer Class Date

This document contains code for generating an electricity bill. The code imports necessary Java classes and defines an Ebill class with a main method. The main method creates a File object pointing to an "Electricity.txt" file and opens it for writing using a FileOutputStream. It then prompts the user to enter meter reading, unit consumed, and rate per unit via the keyboard. Based on the input, it calculates the bill amount and writes the output to the "Electricity.txt" file.

Uploaded by

Vinoth M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

EX NO: 1

DATE:

Substring Removal from a String Using String Buffer class

Aim:

Procedure:

Coding:
import java.io.*;
class strbuf
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("jav");
System.out.println("Before insert");
System.out.println(sb);
System.out.println("After insert");
System.out.println(sb.insert(0,"Welcome to "));
System.out.println("After append");
System.out.println(sb.append('a'));
System.out.println("Length of the string");
System.out.println(sb.length());
System.out.println("Change tyhe char");
System.out.println(sb.substring(0,5));
System.out.println("After delete"+sb.delete(0,5));
System.out.println("After replace:"+sb.replace(0,2,"Welcome"));
}
}

OUTPUT:

Aim:
3

EX NO: 2
DATE:

Determining the Perimeter and Area of Triangle Using Stream Class

Procedure:

Coding:
import java.io.*;
import java.lang.*;
4

class test
{
void value(int h,int b)
{
System.out.println("Area of Triangle:"+0.5*b*h);
}
void value(int a,int b,int c)
{
System.out.println("Perimeter of triangle"+(a+b+c));
}
}
class triangle
{
public static void main(String args[])
{
String s;
try
{
test t=new test();
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter breath and height value");
s=br.readLine();
int b=Integer.parseInt(s);
s=br.readLine();
int h=Integer.parseInt(s);

t.value(b,h);
5

System.out.println("Enter a,b,c sides values:");


s=br.readLine();
int a=Integer.parseInt(s);
s=br.readLine();
b=Integer.parseInt(s);
s=br.readLine();
int c=Integer.parseInt(s);
t.value(a,b,c);
}
catch(Exception ie){}
}
}

OUTPUT:

EX NO:3
7

DATE:

Determining the Order of Numbers Generated Randomly Using


Random Class

Aim:

Procedure:

Coding:
8

import java.util.*;
public class rand
{
Random r=new Random();
int i,j;
int a[]=new int[10];
public void gen_no()
{
System.out.print("\n\n\t Generated Random Number are...");
for(i=0;i<5;i++)
{
a[i]=r.nextInt(20);
System.out.print("\n\t"+a[i]);
}
}
public void sort()
{
int temp;
for(i=0;i<8;i++)
{
for(j=1;j<8-i;j++)
{
if(a[j-1]>a[j])
{
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
}}}

public void show()


{
System.out.print("\n\n\t Ascending Form");
System.out.print("\n\n\t **************\n");
for(i=0;i<8;i++)
System.out.print("\n\t"+a[i]);
System.out.print("\n\n\t Descending Form");
System.out.print("\n\n\t ***************\n");
for(i=7;i>=0;i--)
System.out.print("\n\t"+a[i]);
}
public static void main(String args[])
{
System.out.print("\n Ordering Number using random class");
rand r1=new rand();
r1.gen_no();
r1.sort();
r1.show();
}
}

10

OUTPUT:

11

EX NO 4
DATE:

Usage of Calendar Class and Manipulation

Aim:

Procedure:

12

Coding:
import java.util.*;
class calendardemo
{
public static void main(String args[])
{
String
months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Jul","Aug","Sep","Oct","Nov","Dec
"};
System.out.print("\n\t\t Implementation of calender class");
System.out.print("\n\t\t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Calendar c=Calendar.getInstance();
GregorianCalendar gc=new GregorianCalendar();
System.out.print("\n\t Date-->");
System.out.print(months[c.get(Calendar.MONTH)]);
System.out.print("-"+c.get(Calendar.DATE));
System.out.print("-"+c.get(Calendar.YEAR));
System.out.print("\n\n\t Time-->");
System.out.print(c.get(Calendar.HOUR)+":");
System.out.print(c.get(Calendar.MINUTE)+":");
System.out.print(c.get(Calendar.SECOND));
if(gc.isLeapYear(c.get(Calendar.YEAR)))
System.out.print("\n\n\t"+c.get(Calendar.YEAR)+"Is a Leap year");
else
System.out.print("\n\n\t"+c.get(Calendar.YEAR)+"Is not a Leap year");
}
}

Output:
13

14

EX NO: 5
DATE:

String Manipulation using Character Array

Aim:

Procedure:

15

Coding:
import java.io.*;
import java.lang.*;
class stringcompare
{
public static void main(String arg[])throws IOException
{
String s=new String();
char chars[];
char vowels[]={'a','e','i','o','u','A','E','I','O','U'};
int v=0,n=0,sp=0;
System.out.println("String Manipulation Using Character Array");
System.out.println("*****************************************");
System.out.println("Enter the String");
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
s=br.readLine();
System.out.println(s);
chars=s.toCharArray();
for(int j=0;j<s.length();j++)
{
if((chars[j]>47)&&(chars[j]<58))
n++;
else
if(((chars[j]>=65)&&(chars[j]<95))||((chars[j]>95)&&(chars[j]<122)))
{
for(int k=0;k<10;k++)
{
if(chars[j]==vowels[k])
v++;
}
}
else
sp++;
}
System.out.println("\n\n Vowels"+v);
System.out.println("\n\n Digits"+n);
System.out.println("\n\n Special character"+sp);
}}

16

Output:

17

EX NO:6
DATE:

Usage Of Vector Class

Aim:

Procedure:

18

Coding:
import java.io.*;
import java.util.*;
class vectordemo
{
public static void main(String fr[])throws IOException
{
Vector vec=new Vector();
int n,i,j,k,pos=0,s;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n\n\t\t Implementation of vector class");
System.out.print("\n\t\t*********************************");
System.out.print("\n\n\t Enter the no.of elements:");
n=Integer.parseInt(br.readLine());
for(i=0;i<n;i++)
{
System.out.print("\n\n\t Enter the #"+(i+1)+"elements:");
k=Integer.parseInt(br.readLine());
vec.addElement(new Integer(k));
}
System.out.print("\n\n\t Displaying the vector:"+vec);
System.out.println("\n\t First Element in the vector:"+vec.firstElement());
System.out.println("\n\t Last Element in the vector:"+vec.lastElement());
System.out.println("\n\t Enter the element to be inserted:");
s=Integer.parseInt(br.readLine());
System.out.println("\n\t Enter the position to be inserted:");
pos=Integer.parseInt(br.readLine());
vec.insertElementAt(new Integer(s),pos-1);
System.out.println("\n\t Displaying vector after insertion:"+vec);
System.out.println("\n\t Enter the position to remove:");
pos=Integer.parseInt(br.readLine());
vec.removeElementAt(pos-1);
System.out.println("\n\t Vector after removal:"+vec);
System.out.println("\n Displaying using Enumeration...");
Enumeration e=vec.elements();
while(e.hasMoreElements())
System.out.println("\n\t\t\t"+e.nextElement());
}
}

19

Output:

20

EX NO: 7
Interface And Package

DATE:
Aim:

Procedure:

21

Coding:
Impinp package
import java.io.*;
import pack.*;
class impinp extends student implements marks
{
int m1,m2,m3,tot;
public void gets(int no1,int no2,int no3)
{
m1=no1;
m2=no2;
m3=no3;
tot=no1+no2+no3;
}
public void view()
{
System.out.print("\n\n\t Mark 1="+m1);
System.out.print("\n\n\t Mark 2="+m2);
System.out.print("\n\n\t Mark 3="+m3);
System.out.print("\n\n\t Total="+tot);
}
public static void main(String args[])throws IOException
{
System.out.println("\t\t\t Interfaces And Package");
System.out.println("\t\t\t*****************");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n Enter the Student Number: ");
int no=Integer.parseInt(br.readLine());
System.out.println("\n Enter the Student name: ");
String name=br.readLine();
System.out.println("\n\t Enter the mark 1:");
int m1=Integer.parseInt(br.readLine());
System.out.println("\n\t Enter the mark 2:");
int m2=Integer.parseInt(br.readLine());
System.out.println("\n\t Enter the mark 3:");
int m3=Integer.parseInt(br.readLine());
impinp imp=new impinp();
imp.get(no,name);
imp.display();
imp.gets(m1,m2,m3);
imp.view();
}}

Student pack
22

package pack;
public class student
{
int no;
String name;
public void get(int n,String s)
{
no=n;
name=s;
}
public void display()
{
System.out.println("\n\t The Student number is: "+no);
System.out.println("\n\t The Student name is: "+name);
}
public interface marks
{
public void gets(int no1,int no2,int no3);
public void view();
}
}

23

Output:

24

EX NO:9
DATE:

Implementing Thread Based Application and Exception Handling

Aim:

Procedure:

Coding:
25

import java.lang.*;
class newthread implements Runnable
{
newthread()
{
Thread t=new Thread(this,"Demo Thread");
System.out.print("Child:"+t);
t.start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.print("\n\n\t Child:"+i);
Thread.sleep(500);
}
}
catch(InterruptedException i)
{
}
System.out.print("\n\n\t Child is in exist");
}
}
class threaddemo
{
public static void main(String arg[])
{
new newthread();
try
{
for(int i=5;i>0;i--)
{
System.out.print("\n\n\t Main:"+i);
Thread.sleep(100);
}
}
catch(InterruptedException i)
{
}
System.out.print("\n\n\t Main is in exist");
}
}

26

Output:

27

EX NO: 9
DATE:

Application using Synchronization

Aim:

Procedure:

28

Coding;
import java.lang.*;
class inventory
{
static int q=100,r=0;
static public synchronized void request(int order)
throws InterruptedException
{
for(int i=1;i<=3;i++)
{
Thread.sleep(300);
if(order<=q)
{
System.out.println("\n\t Quantity Ordered:"+order);
q=q-order;
r=r+order;
System.out.println(" Quantity in hand:"+q);
System.out.println("\n\t Total order taken:"+r);
}
else
{
System.out.println("\n\t Ordered quantity more than in");
System.out.println("hand");
}
order=((int)(Math.random()*100));
}
}
public static void main(String args[ ]) throws InterruptedException
{
System.out.println("\t\t\t APPLICATION USING");
System.out.print("SYNCHRONIZATION");
System.out.println("\t\t\t ......................................");
System.out.print(".");
new ourthread();
Thread.sleep(50);
inventory.request((int)(Math.random()*100));
System.out.println("\n\t Existing from main thread...");
}
29

}
class ourthread extends Thread
{
ourthread()
{
super("Test thread");
System.out.println("\n\t Child thread:"+this);
start();
}
public void run()
{
try
{
inventory.request((int)(Math.random()*100));
}
catch(InterruptedException e)
{
System.out.println("\n\t"+e);
}
System.out.println("\n\t Child thread existing");
}
}

Output:
30

EX NO: 10
31

DATE:

Electricity Bill Preparation

Aim:

Procedure:

Coding:
import java.io.*;
32

class Ebill{
public static void main(String args[]) throws IOException {
File f = new File("Electricity.txt");

FileOutputStream fos = new FileOutputStream(f);


DataOutputStream dos = new DataOutputStream(fos);
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the current meter reading :");
String s = br.readLine();
int cr =Integer.parseInt(s);
System.out.println("Enter the previous meter reading:");
s=br.readLine();
int pr=Integer.parseInt(s);
int tot = cr-pr;
double amount;
int i;
if(tot>=1000)
{ i=5;
amount = tot*i; }
else if(tot>=500 && tot<=999)
{ i=4;
amount = tot*i; }
else if(tot>=200 && tot <=499)
{ i=3;
amount=tot*i; }
else
{ i=2;
amount=tot*i; }
dos.writeInt(cr);
dos.writeInt(pr);
dos.writeInt(tot);

dos.writeDouble(amount);
dos.close();
33

FileInputStream fis=new FileInputStream(f);


DataInputStream dis = new DataInputStream(fis);
System.out.println( "Current meter reading : "+ dis.readInt());
System.out.println("Previous meter reading : "+dis.readInt());
System.out.println("Total units charged : "+dis.readInt());
System.out.println("Charges per Unit : "+ i);
System.out.println("Total amount to be paid : "+dis.readDouble());
dis.close();
fis.close();
}
}

Output:

34

35

EX NO: 11
Telephone Bill Preparation

DATE:

Aim:

Procdure:

Coding:
36

//Data File Creation for Telephone Billing


import java.io.*;
class TPbill
{
public static void main(String args[]) throws IOException
{
File f=new File("tel.dat");
FileOutputStream fos=new FileOutputStream(f);
DataOutputStream dos=new DataOutputStream(fos);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s;
System.out.println("Enter the telephone no:");
s=br.readLine();
int tel=Integer.parseInt(s);
System.out.println();
System.out.println("Enter the total calls made:");
s=br.readLine();
int tc=Integer.parseInt(s);
System.out.println();
int totf=100;
int tot=tc-totf;
double amount=0;
if(tot>=0)
amount=tot*1;
else
amount=50;
double sertax=amount*20/100;
System.out.println("service tax="+sertax);
double tamt=amount+sertax;
dos.writeInt(tel);
dos.writeInt(tc);
dos.writeInt(totf);
dos.writeInt(tot);
dos.writeDouble(amount);
dos.writeDouble(sertax);
dos.writeDouble(tamt);
dos.close();
37

FileInputStream fis=new FileInputStream(f);


DataInputStream dis=new DataInputStream(fis);
System.out.println("\n Customer telephone Billing:");
System.out.println("\n-----------------------------------");
System.out.println("\n Telephone no :"+dis.readInt());
System.out.println("\n No of calls made :"+dis.readInt());
System.out.println("\n No of free calls :"+dis.readInt());
System.out.println("\n No of calls to be charged :"+dis.readInt());
System.out.println("\n Amount for total calls :"+dis.readDouble());
System.out.println("\n Service tax charge :"+dis.readDouble());
System.out.println("\n total amount to be paid :"+dis.readDouble());
dis.close();
fis.close();
}
}

Output:
38

39

EX NO:14
EMAIL CREATION USING DATABASE
DATE:

Aim

Procedure:

Coding:
import java.sql.*;
40

import java.sql.DriverManager.*;
import java.io.*;
class inst
{
Connection con;
PreparedStatement ps;
Statement st;
ResultSet r;
int rs;
String str;
void inst1()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Test");
Statement stm=con.createStatement();
}
catch(Exception e)
{
System.out.println("\n\t Connection Error");
}
}
void addrec(int id,String str,int age,String emid)
throws SQLException
{
ps=con.prepareStatement("Insert into email values(?,?,?,?)");
ps.setInt(1,id);
ps.setString(2,str);
ps.setInt(3,age);
ps.setString(4,emid);
ps.executeUpdate();
System.out.println("\n\t Inserted");
con.setAutoCommit(true);
con.close();
}
41

void update(int pid,String name,int age,String emid)


throws SQLException
{
st=con.createStatement();
String Upstring="Update e-mail set
name="+name+"Age="+age+"emid="+emid+"where id="+pid;
st.executeUpdate(Upstring);
con.setAutoCommit(true);
System.out.println("\n\t Record Updated");
con.close();
}
void disp() throws SQLException
{
st=con.createStatement();
r=st.executeQuery("Select*from Email");
System.out.println("\n\t Id\n\n\t Age\n\n\t Email-id");
while(r.next())
{
System.out.println(r.getString(1)+"\n\n\t"+r.getString(2)
+"\n\n\t"+r.getString(3)+"\n\n\t "+r.getString(4));
}
r.close();
st.close();
con.close();
}
void del(int pid) throws SQLException
{
st=con.createStatement();
st.executeUpdate("Delete from email where id="+pid+"");
con.setAutoCommit(true);
System.out.println("\n\t Record Deleted");
con.close();
}
}
class email
{
42

public static void main(String arg[ ])


throws IOException,SQLException
{
String s,name,emid;
int choice,pid,age;
inst i=new inst();
System.out.println("\t\t\t DATABASE MANIPULATION");
System.out.println(\t\t\t ------------------------------------------);
BufferedReader br=new BufferedReader
(new InputStreamReader(System.in));
do
{
System.out.println("\n\n\t\t 1. Insert");
System.out.println("\n\n\t\t 2. Update");
System.out.println("\n\n\t\t 3. Delete");
System.out.println("\n\n\t\t 4. Display");
System.out.println("\n\n\t\t 5. Exit");
System.out.println("\n\n Enter your choice:");
s=br.readLine();
choice=Integer.parseInt(s);
switch(choice)
{
case 1:
{
i.inst1();
System.out.println("\n\nt\t\t Enter the);
System.out.print(person" ID,Name,);
System.out.print(Age,Email ID:");
s=br.readLine();
pid=Integer.parseInt(s);
name=br.readLine();
age=Integer.parseInt(s);
emid=br.readLine();
i.addrec(pid,name,age,emid);
break;
}
43

case 2:
{
i.inst1();
System.out.println("\n\n\t\t Enter the);
System.out.print(personID to update:");
s=br.readLine();
pid=Integer.parseInt(s);
System.out.println("\n\n\t\t Enter the);
System.out.print(person ID,Name,Age,);
System.out.print(Email ID:");
name=br.readLine();
age=Integer.parseInt(s);
emid=br.readLine();
i.update(pid,name,age,emid);
break;
}
case 3:
{
i.inst1();
System.out.println("\n\n\t\t Enter the);
System.out.print(person ID to delete);
System.out.print(records...");
s=br.readLine();
pid=Integer.parseInt(s);
i.del(pid);
}
case 4:
{
i.inst1();
i.disp();
break;
}
case 5:
System.out.println(\t ***END***);
break;
default:
44

System.out.println("\n\t Invalid Selection");


}
}
while(choice!=5);
}
}

Output:
DATABASE MANIPULATION
45

-----------------------------------------

1. INSERT
2. UPDATE
3. DELETE
4. DISPLAY
5. Exit
Enter your choice: 1
Enter the person ID, Name, Age, Email ID
2100
john
18
[email protected]
Inserted
1. INSERT
2. UPDATE
3. DELETE
4. DISPLAY
5. EXIT
Enter your choice: 1
Enter the person ID, Name, Age, Email ID
2101
gnp
19
[email protected]
Inserted

1. INSERT
46

2. UPDATE
3. DELETE
4. DISPLAY
5. EXIT
Enter your choice: 4
Id

Name Age

Email-Id

2100 john 18

[email protected]

2101 gnp

[email protected]

19

1. INSERT
2. UPDATE
3. DELETE
4. DISPLAY
5. EXIT
Enter your choice: 3
Enter the person ID to delete records: 2100
Id

Name Age

2100 john 18

Email-Id
[email protected]

record deleted
1. INSERT
2. UPDATE
3. DELETE
4. DISPLAY
5. EXIT
Enter your choice: 5
***END***

47

You might also like