Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Java Program Codeing

Uploaded by

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

Java Program Codeing

Uploaded by

kamarajlove5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1.

PROGRAM TO FIND AREA OF SQUARE, RECTANGLE AND CIRCLE


USING METHOD OVERLOADING

FILE NAME : 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");
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:

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
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

FILE NAME : 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 Commandline Arguments:


Original Order:
9,6,0,7,5,3,1;Ascending Order:
0,1,3,5,6,7,9

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

FILE NAME : 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:
12
34
Second Matrix:
12
34
Multiplying the matrices.....
The product is:
7 10
15 22

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

FILE NAME : 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 informations 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
************************************************************
1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit

Enter your choice:


1
How many rupees do you want to deposit?
2000
Your deposit was successful.

1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit

Enter your choice:


3
************************************************************
Acc No Name Account Type Balance
1000 Kumar Savings 7000.0
************************************************************
1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit

Enter your choice:


2
How many rupees do you want to withdraw?
3000
Your withdraw Rs :3000.0 was successful.

1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit

Enter your choice:


3
************************************************************
Acc No Name Account Type Balance
1000 Kumar Savings 4000.0
************************************************************

1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit

Enter your choice:


4
5. DEMONSTRATION OF USER DEFINED PACKAGES

FILE NAME : Arithmetic.java

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

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

FILE NAME : 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

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

FILE NAME : 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
8. STUDENT REGISTRATION FORM USING APPLETS

FILE NAME : Student.java

mport 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

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

FILE NAME : graphics.java

import java.awt.*;
import java.applet.*;

/*<applet code="graphics" width="800" height="600">


</applet>*/

public class graphics extends Applet


{
Font f1=new Font("Courier New",Font.BOLD,20);

public void paint(Graphics GA)


{
GA.setColor(Color.blue);
GA.setFont(f1);
GA.drawString("Illustration of methods of Graphics class",130,20);
GA.fillRect(450,55,200,260);
GA.drawRect(300,375,110,160);

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.

FILE NAME: FILE.JAVA


import java.io.*;

class file

public static void main(String args[])

FileReader intfile=null;

FileWriter outfile=null;

try

outfile=new FileWriter("Products.txt");

System.out.println("Enter Products code:cost \n number of items for five Products");

for(int i=0;i<5;i++)

InputStreamReader r=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(r);

outfile.write(br.readLine());

catch(FileNotFoundException e)

System.out.println("\n\n\n\n\t\t File not found");

catch(IOException e)

System.out.println(e);

}
finally

try

outfile.close();

catch(IOException e)

System.out.println("Error");

}
OUTPUT :

Computer 223 : 222

Graphics card 221: 3334

Mouse 112: 2234

Keyboard 113: 2345

Pen drive 114: 9876

You might also like