Ex. No.
1
TO FIND AREA OF SQUARE, RECTANGLE AND CIRCLE USING
Date: METHOD OVERLOADING
AIM
To write a program to find the area of Square, Rectangle and Circle using Method Overloading.
ALGORITHM
Step 1: Start the Program
Step 2: Import necessary header file to the program
Step 3: Create valid class name with necessary functions and variables
Step 4: Create method overloading concept with different parameters by using Switch
Statement
Step 5: Compile the program
Step 6: Run the Java program with appropriate input values
Step 7: Display the Output
Step 8: Stop the Program
PROGRAM
Source Code:
area.java import java.io.*;
class area
{
void findarea(int a)
{
System.out.println("The area of Square with a side value "+a+ " is:"+a*a);
}
void findarea(int a,int b)
{
System.out.println("The Area of rectangle with Width "+a+ " and Length "+b+" is:"+a*b);
}
void findarea(float a)
{
System.out.println("The area of Circle with the Radius value "+a+" is: "+((3.14)*(a*a)));
}
public static void main(String args[])throws IOException
{
area d=new area();
int choice;
BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
DataInputStream Br=new DataInputStream(System.in);
System.out.println("Finding the areas of different shape Using Method Overloading");
do
{
System.out.println();
System.out.println("1. Square");
System.out.println("2. Rectangle"); S
ystem.out.println("3. Circle");
System.out.println("4. Exit");
System.out.println();
System.out.println("Please select your choice");
choice=Integer.parseInt(Br.readLine()); switch(choice)
{
case 1:
System.out.println("Enter a side value of a square:");
int a=Integer.parseInt(Br.readLine());
d.findarea(a);
break;
case 2:
System.out.println("Enter the Width of the Rectangle:");
int x=Integer.parseInt(Br.readLine());
System.out.println("Enter the Length of the Rectangle:");
int y=Integer.parseInt(Br.readLine());
d.findarea(x,y);
break;
case 3:
System.out.println("Enter the radius of circle in float :");
float r=Float.parseFloat(Br.readLine());
d.findarea(r);
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
while(choice<=4);
}
}
OUTPUT
C:\JavaLab>javac area.java
Note: area.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for
details. C:\JavaLab>java area
Finding the areas of different shape Using Method Overloading
Square
Rectangle
Circle
Exit
Please select your choice 1
Enter a side value of a square:
5
The area of Square with a side value 5 is:25
Square
Rectangle
Circle
Exit
Please select your choice 2
Enter the Width of the Rectangle:
4
Enter the Length of the Rectangle:
3
The Area of rectangle with Width 4 and Length 3 is:12
Square
Rectangle
Circle
Exit
Please select your choice 3
Enter the radius of circle in float :
5.4
The area of Circle with the Radius value 5.4 is: 91.56240550994873
Square
Rectangle
Circle
Exit
Please select your choice 4
Result
Thus the above Java program to find area of square, rectangle and circle using method
overloading has been compiled and executed successfully.
Ex. No. 2
TO SORT THE LIST OF NUMBERS USING COMMAND LINE
ARGUMENTS
Date:
AIM
To write a program to sort the list of numbers using command line Arguments
ALGORITHM
Step 1: Start the Program
Step 2: Import necessary header file to the program
Step 3: Create valid class name with necessary functions and variables
Step 4: Create the command line arguments for sort the list of numbers
Step 5: Compile the program
Step 6: Run the Java program with appropriate input values
Step 7: Display the Output
Step 8: Stop the Program
CODING
Source Code: Sorting.java
public class Sorting
{
public static void main(String args[])
{
if(args.length<=0)
{
System.out.println("Error: Enter some integer as command line arguments");
System.exit(0);
}
System.out.println("Sorting Set of Numbers Using Commandline Arguments");
int n=args.length;
int a[]=new int[n]; int temp;
System.out.println("Original Order");
for (int i = 0; i < n; i++)
{
a[i] = Integer.parseInt(args[i]);
System.out.print(a[i]+",");
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i]> a[j])
{
temp = a[i]; a[i] = a[j]; a[j] = temp;
}
}
}
System.out.println("Ascending Order:"); for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
OUTPUT
C:\JavaLab>javac Sorting.java
C:\JavaLab>java Sorting 9 6 0 7 5 3 1
Sorting Set of Numbers Using Command line Arguments
Original Order
9,6,0,7,5,3,1
Ascending Order:
0,1,3,5,6,7,9
RESULT
Thus the above Java program to sort the list of numbers using command line arguments
has been compiled and executed successfully.
Ex. No. 3
TO MULTIPLY THE GIVEN TWO MATRICES
Date:
AIM
To write a program to multiply the given two matrices.
ALGORITHM
Step 1: Start the Program
Step 2: Import necessary header file to the program
Step 3: Create valid class name with necessary functions and variables
Step 4: Create the necessary matrices to perform multiplication
Step 5: Compile the program
Step 6: Run the Java program with appropriate input values
Step 7: Display the Output
Step 8: Stop the Program
CODING
Source Code: MatrixMultiplication.java
import java.util.Scanner;
public class MatrixMultiplication
{
public static void main(String args[])
{
int n;
Scanner input=new Scanner(System.in); System.out.println("Enter the base of squared matrices");
n=input.nextInt();
int[][] a=new int[n][n];
int[][] b=new int[n][n];
int[][] c=new int[n][n];
System.out.println("Enter the elements of 1st matrix row wise\n"); for(int i=0; i<n;i++)
{
for(int j=0; j<n;j++)
{
a[i][j]=input.nextInt();
}
}
System.out.println("Enter the elements of 2nd matrix row wise\n"); for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
b[i][j]=input.nextInt();
}
}
System.out.println("Given Matrix"); System.out.println("First Matrix"); for(int i=0; i<n; i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("Second Matrix"); for(int i=0; i<n; i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("Multiplying the matrices.");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
System.out.println("The product is:"); for(int i=0; i<n; i++)
{
for(int j=0;j<n;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
input.close();
}
}
Output:
C:\JavaLab>javac MatrixMultiplication.java
C:\JavaLab>java MatrixMultiplication
Enter the base of squared matrices
2
enter the elements of 1st matrix row wise
1
2
3
4
enter the elements of 2nd matrix row wise
1
2
3
4
Given Matrix First Matrix
1 2
3 4
Second Matrix
1 2
3 4
Multiplying the matrices.....
the product is:
7 10
15 22
RESULT
Thus the above Java program to multiply the given two matrices has been compiled and
executed successfully.
Ex. No. 4
BANKING DETAILS USING CLASS AND OBJECTS
Date:
AIM
To write a program to design a class to represent a bank account by using data members and
methods.
ALGORITHM
Step 1: Start the Program
Step 2: Import necessary header file to the program
Step 3: Create valid class name , Data members and methods.
Data Members: i) Name of the depositor, ii) Account Number iii) Type of Account
iv) Balance Amount
Methods: i) To assign initial values, ii) To deposit an amount, iii) To withdraw an
amount after checking banking ( Balance), iv) To display the name and balance
Step 4: To write the Java Program by using above mentioned Data members and Methds.
Step 5: Compile the program
Step 6: Run the Java program with appropriate input values
Step 7: Display the Output
Step 8: Stop the Program
CODING
Source Code: banking.java
import java.io.*;
class account
{
int Acc_Number;
String Cus_Name,Acc_Type;
double balance;
account(String name, String type, double amount)
{
Acc_Number=1000;
Cus_Name=name;
Acc_Type=type;
balance=amount;
}
public void display()
{
System.out.println("*******************************************************");
System.out.println("Acc No\tName\t\tAccount Type\tBalance");
System.out.println(Acc_Number+"\t"+Cus_Name+"\t\t"+Acc_Type+"\t\t"+balance);
System.out.println("*********************************************************");
}
public void deposit(double amt)
{
balance+=amt;
System.out.println("Your deposit was successful.");
}
public void withdraw(double money)
{
if(balance<money)
{
System.out.println("Insufficient money");
}
else
{
balance-=money;
System.out.println("Your withdraw Rs :"+money+" was successful.");
}
}
}
class banking
{
public static void main(String args[])throws IOException
{
DataInputStream dis=new DataInputStream(System.in);
int Choice;
System.out.println("\t***************************");
System.out.println("\tABC Banking Private Limited");
System.out.println("\t***************************");
System.out.println("Kindly Provide the following informations to create a new
account"
); System.out.println("Enter your name"); String name=dis.readLine();
System.out.println("Enter your account type");
String type=dis.readLine();
System.out.println("What is your initial deposit amount?");
double amount=Double.parseDouble(dis.readLine());
account obj=new account(name,type,amount);
System.out.println();
System.out.println("Thank you "+obj.Cus_Name+" your new account with us has
been created successfully.");
System.out.println();
obj.display();
do
{
System.out.println();
System.out.println("1. To Deposit Money"); System.out.println("2. To Withdraw Money");
System.out.println("3. To Check Account Balance");
System.out.println("4. To Exit");
System.out.println(); System.out.println("Enter your choice");
Choice=Integer.parseInt(dis.readLine()); switch(Choice)
{
case 1:
System.out.println("How many rupees do you want to deposit");
amount=Double.parseDouble(dis.readLine()); obj.deposit(amount);
break; case 2:
System.out.println("How many rupees do you want to withdraw");
amount=Double.parseDouble(dis.readLine()); obj.withdraw(amount);
break; case 3:
obj.display(); break;
case 4:
System.exit(0); default:
System.out.println("Your choice was wrong");
}
}while(Choice<=4);
}
}
OUTPUT:
C:\JavaLab>javac banking.java
Note: banking.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for
details.
C:\JavaLab>java banking
*************************** ABC Banking Private Limited
***************************
Kindly Provide the following information’s to create a new account Enter your name
Kumar
Enter your account type Savings
What is your initial deposit amount? 5000
Thank you Kumar your new account with us has been created successfully.
Acc No Name Account Type Balance
1000 Kumar Savings 5000.0
************************************************************
To Deposit Money
To Withdraw Money
To Check Account Balance
To Exit
Enter your choice 1
How many rupees do you want to deposit 2000
Your deposit was successful.
To Deposit Money
To Withdraw Money
To Check Account Balance
To Exit
Enter your choice 3
************************************************************ Acc No Name
Account Type Balance
1000 Kumar Savings 7000.0
************************************************************
To Deposit Money
To Withdraw Money
To Check Account Balance
To Exit
Enter your choice 2
How many rupees do you want to withdraw 3000
Your withdraw Rs :3000.0 was successful.
To Deposit Money
To Withdraw Money
To Check Account Balance
To Exit
Enter your choice
3
************************************************************
Acc No Name Account Type Balance
1000 Kumar Savings 4000.0
************************************************************
To Deposit Money
To Withdraw Money
To Check Account Balance
To Exit
Enter your choice 4
RESULT
Thus the above Java program to design a class to represent a bank account with help of Data
Members and Methods has been compiled and executed successfully
Ex. No. 5
PACKAGE
Date:
AIM
To write a program that import the user defined package and access the member variable of
classes that contained by Package.
ALGORITHM
Step 1: Start the Program
Step 2: Import necessary header file to the program
Step 3: To create user defined package with member variable
Step 4: To import user defined package and access the member variable of classes that
contained by Package
Step 5: Compile the program
Step 6: Run the Java program with appropriate input values
Step 7: Display the Output
Step 8: Stop the Program
PROGRAM
Source Code: Arithmetic.java
package Calc;
public class Arithmetic
{
public int addition(int num1,int num2)
{
return (num1+num2);
}
public int subtraction(int num1,int num2)
{
return (num1-num2);
}
public int multiplication(int num1,int num2)
{
return (num1*num2);
}
public int division(int num1,int num2)
{
return (num1/num2);
}
}
PackageDemo.java
import java.io.*;
import Calc.Arithmetic;
class PackageDemo
{
public static void main(String args[])throws IOException
{
DataInputStream dis=new DataInputStream(System.in);
int num1,num2,add,sub,mul,div;
Arithmetic obj=new Arithmetic();
System.out.println("\t*********************");
System.out.println("\tPackage Demonstration");
System.out.println("\t*********************");
System.out.println("Enter two integer number");
num1=Integer.parseInt(dis.readLine());
num2=Integer.parseInt(dis.readLine());
add=obj.addition(num1,num2);
sub=obj.subtraction(num1,num2);
mul=obj.multiplication(num1,num2);
div=obj.division(num1,num2);
System.out.println("The Addition is: "+add);
System.out.println("The Subtraction is: "+sub);
System.out.println("The Multiplication is: "+mul); System.out.println("The Division is: "+div);
}
}
OUTPUT
C:\JavaLab>cd Calc C:\JavaLab\Calc>javac Arithmetic.java C:\JavaLab\Calc>cd..
C:\JavaLab>javac PackageDemo.java
Note: PackageDemo.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation
for details.
C:\JavaLab>java PackageDemo
********************* Package Demonstration
********************* Enter two integer number
10
5
The Addition is: 15
The Subtraction is: 5
The Multiplication is: 50
The Division is: 2
RESULT
Thus the above Java program to import the user defined package and access the member
variable of classes that contained by package has been compiled and executed successfully
Ex. No. 6
EXCEPTION HANDLING USING TRY AND MULTIPLE CATCH
Date: BLOCKS
AIM
To write a program to handle the Exception using try and multiple catch blocks.
ALGORITHM
Step 1: Start the Program
Step 2: Import necessary header file to the program
Step 3: To create Exception handling with multiple try and catch blocks
Step 4: Compile the program
Step 5: Run the Java program with appropriate input values
Step 6: Display the Output
Step 7: Stop the Program
CODING
Source Code: ExceptionHandling.java
import java.util.*;
import java.io.*;
class ExceptionHandling
{
public static void main(String args[])
{
try
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
System.out.println("Answer:"+a/b);
}
catch(ArithmeticException e)
{
System.out.println("\n\tDivision Error");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("\n\tError in index value");
}
catch(NumberFormatException e)
{
System.out.println("\n\tData type error");
}
finally
{
System.out.println("\n\tFinally block executed");
}
}
}
Output:
C:\JavaLab>javac ExceptionHandling.java C:\JavaLab>java ExceptionHandling
10
5
Answer:2
Finally block executed
C:\JavaLab>java ExceptionHandling
10
0
Division Error
Finally block executed C:\JavaLab>java ExceptionHandling
Error in index value Finally block executed
C:\JavaLab>java ExceptionHandling
10
a
Data type error
Finally block executed
RESULT
Thus the above Java program to handle the Exception using try and multiple catch blocks
has been compiled and executed successfully
Ex. No. 7
MULTITHREADS
Date:
AIM
To write a program to illustrate the use of Multithreads
ALGORITHM
Step 1: Start the Program
Step 2: Import necessary header file to the program
Step 3: To create necessary multithreads concept to implement the program
Step 4: Compile the program
Step 5: Run the Java program with appropriate input values
Step 6: Display the Output
Step 7: Stop the Program
CODING
Source Code: MultiThread.java
import java.io.*;
class Even extends Thread
{
void Even()
{
start();
}
public void run()
{
for(int j=2;j<10;j=j+2)
{
System.out.println("\n\t\t Even number:"+j);
try
{
sleep(2000);
}
catch(InterruptedException e)
{
}
}}}
class Odd extends Thread
{
void Odd()
{
start();
}
public void run()
{
for(int i=1;i<10;i=i+2)
{
System.out.println("\n\t\t Odd number:"+i); try
{
sleep(1000);
}
catch(InterruptedException e)
{
}
}}}
class MultiThread
{
public static void main(String args[])
{
System.out.println("\n\t\t Multithread Programming\n"); Even r=new Even();
Odd b=new Odd(); r.Even();
b.Odd();
}
}
OUTPUT
C:\JavaLab>javac MultiThread.java C:\JavaLab>java MultiThread
Multithread Programming
Odd number:1
Even number:2
Odd number:3
Odd number:5
Even number:4
Odd number:7
Odd number:9
Even number:6
Even number:8
RESULT
Thus the above Java program to illustrate the use of Multithreads has been compiled and
executed successfully
Ex. No. 8
STUDENT REGISTRATION FORM USING APPLET
Date:
AIM
To write a program to create student registration form using applet with Name, Address, Sex, Class,
Email-id.
ALGORITHM
Step 1: Start the Program
Step 2: Import necessary header file to the program
Step 3: To create Applet concept for registration.
Step 4: Compile the program
Step 5: Run the Java program with appropriate input values
Step 6: Display the Output
Step 7: Stop the Program
Source Code: student.java
import java.awt.*; import java.applet.*; import java.awt.event.*;
/* <applet code="Student.class" width=600 height=500>
</applet> */
public class Student extends Applet
{
TextField t3,t4,t5; Button b1,b2;
Checkbox c1,c2,c3,c4,m,f; CheckboxGroup cbg;
List l1;
Label l2,l3,l4,l5,l6; public void init()
{
setLayout(null);
l2=new Label("NAME"); l2.setBounds(0,0,50,50); add(l2);
t3=new TextField(20); t3.setBounds(130,10,150,20); add(t3);
l3=new Label("ADDRESS"); l3.setBounds(0,40,70,50); add(l3);
t4=new TextField(20); t4.setBounds(130,50,150,20); add(t4);
l4=new Label("SEX"); l4.setBounds(0,80,70,50); add(l4);
cbg=new CheckboxGroup();
m=new Checkbox("Male",false,cbg); m.setBounds(130,90,75,20);
add(m);
f=new Checkbox("Female",false,cbg); f.setBounds(225,90,75,20);
add(f);
l5=new Label("Class"); l5.setBounds(0,120,120,50);
add(l5);
l6=new Label("E-Mail"); l6.setBounds(0,160,120,50); add(l6);
t5=new TextField(20); t5.setBounds(130,175,150,20); add(t5);
l1=new List(1,false); l1.add("I Year");
l1.add("II Year");
l1.add("III Year"); l1.setBounds(130,130,100,20); add(l1);
b1= new Button("SUBMIT"); b1.setBounds(80,250,70,20); add(b1);
b2= new Button("RESET"); b2.setBounds(200,250,70,20); add(b2);
}
}
Output:
C:\JavaLab>javac Student.java C:\JavaLab>appletviewer Student.java
RESULT
Thus the above Java program to Student registration form using Applet has been compiled and
executed successfully
Ex. No. 9
DRAWING VARIOUS SHAPES USING GRAPHICS METHOD
Date:
AIM
To write a program to draw the line, rectangle, oval, text using the graphics method.
ALGORITHM
Step 1: Start the Program
Step 2: Import necessary header file to the program
Step 3: To create graphics method for drawing various shapes.
Step 4: Compile the program
Step 5: Run the Java program with appropriate method
Step 6: Display the Output
Step 7: Stop the Program
Program:
import java.awt.*;
import java.applet.*;
/*<applet code="sampface"width=400 height=60>
</applet>
*/
public class sampface extends Applet
public void paint(Graphics g)
g.setColor(Color.green);
g.drawRect(20,20,165,180);
g.setColor(Color.red);
g.drawOval(40,40,120,150);
g.setColor(Color.yellow);
g.drawOval(57,75,30,20);
g.drawOval(110,75,30,20);
g.setColor(Color.black);
g.fillOval(68,81,10,10);
g.fillOval(121,81,10,10);
g.setColor(Color.orange);
g.drawOval(85,100,30,30);
g.setColor(Color.blue);
g.fillArc(60,125,80,40,180,180);
g.setColor(Color.green);
g.drawOval(25,92,15,30);
g.drawOval(160,92,15,30);
}
Output:
RESULT
Thus the above Java program using the graphics has been compiled and executed successfully
Ex. No. 10
SEQUENTIAL FILE
Date:
AIM
To write a program to create a sequential file that could store details about five products.
Details include product code, cost, and number of items available and are provided through the
keyboard. Compute and print the total value of all the five products
ALGORITHM
Step 1: Start the Program
Step 2: Import necessary header file to the program
Step 3: To get the details of the product code, cost and number of items available in particular
Store.
Step 4: To compute the total value of the entire product
Step 5: Run the Java program with appropriate method
Step 6: Display the Output
Step 7: Stop the Program
PROGRAM
import java.util.*;
import java.io.*;
class Inventory
static DataInputStream din=new DataInputStream(System.in);
static StringTokenizer st;
public static void main(String args[])throws IOException
DataOutputStream des=new DataOutputStream(new FileOutputStream("invent.txt"));
System.out.println("Enter code Number:");
st=new StringTokenizer (din.readLine());
int code=Integer.parseInt(st.nextToken());
System.out.println("Enter No of Items:");
st=new StringTokenizer(din.readLine());
int Items=Integer.parseInt(st.nextToken());
System.out.println("Enter cost:");
st=new StringTokenizer(din.readLine());
double cost=new Double(st.nextToken()).doubleValue();
des.writeInt(code);
des.writeInt(Items);
des.writeDouble(cost);
des.close();
DataInputStream dis=new DataInputStream(new FileInputStream("invent.txt"));
int codeNumber=dis.readInt();
int totalItem=dis.readInt();
double itemCost=dis.readDouble();
double totalCost=totalItem*itemCost;
dis.close();
System.out.println();
System.out.println("Code Number :"+codeNumber);
System.out.println("Item cost:"+itemCost);
System.out.println("total Items:"+totalItem);
System.out.println("total cost:"+totalCost);
}
OUTPUT:
Enter code number:
123
Enter no.of Items:
Enter cost :
100
Code number : 123
Item cost : 100
Total items : 5
Total cost : 500
RESULT
Thus the above Java program using the sequential file has been compiled and executed
successfully