Java Program Codeing
Java Program Codeing
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;
do
{
System.out.println();
System.out.println("1. Square");
System.out.println("2. Rectangle");
System.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:
case 3:
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
1. Square
2. Rectangle
3. Circle
4. 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
1. Square
2. Rectangle
3. Circle
4. 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
1. Square
2. Rectangle
3. Circle
4. 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
1. Square
2. Rectangle
3. Circle
4. Exit
Please select your choice:
4
2.PROGRAM TO SORT THE LIST OF NUMBERS USING COMMAND LINE
ARGUMENTS
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
NOTE :
o We have to enter the numbers that we want to be Sorted, after the command. That is
[java Sorting] (numbers).
o Also ,we have to leave space between two numbers, Otherwise it will take the given
numbers as one single number.
3. PROGRAM TO MULTIPLY GIVEN TWO MATRICES
import java.util.Scanner;
public class MatrixMultiplication
{
System.out.println("Given Matrix:");
System.out.println("First Matrix:");
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();
}
NOTE :
o We have to enter the numbers one by one in the command line “Enter the Elements of
matrices”.
o Also ,We need to leave space between two numbers in the command “First Matrix:”
and “Second Matrix”.
4. BANKING DETAILS USING CLASS AND OBJECTS
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;
}
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:
case 2:
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 informations to create a new account
Thank you Kumar your new account with us has been created successfully.
************************************************************
Acc No Name Account Type Balance
1000 Kumar Savings 5000.0
************************************************************
1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit
1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit
1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit
1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit
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
NOTE :
o First We have to create a folder named Calc, Then create two java files.
o One with Sourse code named Arithmetic.java
o Next with PackageDemo.java.
6. DEMONSTRATION OF EXCEPTION HANDLING
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
NOTE :
o We have to leave space between two numbers in the command line, Otherwise it
will take the given numbers as one single number.
7. USES OF MULTITHREADS
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
8. STUDENT REGISTRATION FORM USING APPLETS
mport java.awt.*;
import java.applet.*;
import java.awt.event.*;
{
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
NOTE :
o If you spot any Errors indicating the line with “l1” .
o Check them You may have typed them as 11 or LL, Because they seems like
eachother.
o It is actually “L1” means ‘Line 1’.
9. DRAWING VARIOUS SHAPES USING GRAPHICS CLASS
import java.awt.*;
import java.applet.*;
GA.drawOval(10,120,155,95);
GA.setColor(Color.yellow);
GA.fillOval(700,140,50,150);
GA.setColor(Color.green);
GA.drawLine(340,100,340,350);
GA.setColor(Color.magenta);
GA.fillRoundRect(510,400,90,100,20,20);
}
}
OUTPUT :
C:\JavaLab>javac graphics.java
C:\JavaLab>appletviewer graphics.java
10. 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.
COMPAUTE AND PRINT THE TOTAL VALUE OF ALL THE FIVE PRODUCTS.
class file
FileReader intfile=null;
FileWriter outfile=null;
try
outfile=new FileWriter("Products.txt");
for(int i=0;i<5;i++)
outfile.write(br.readLine());
catch(FileNotFoundException e)
catch(IOException e)
System.out.println(e);
}
finally
try
outfile.close();
catch(IOException e)
System.out.println("Error");
}
OUTPUT :