0% found this document useful (0 votes)
127 views23 pages

Practical Java Programs

The document discusses developing Java programs to demonstrate various programming concepts like if-else statements, loops, arrays, exceptions, wrappers classes etc. It provides example code snippets for implementing different concepts through classes and methods.

Uploaded by

Vedant Nagul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views23 pages

Practical Java Programs

The document discusses developing Java programs to demonstrate various programming concepts like if-else statements, loops, arrays, exceptions, wrappers classes etc. It provides example code snippets for implementing different concepts through classes and methods.

Uploaded by

Vedant Nagul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

 Develop programs to demonstrate use of if statements and its different forms

class big3
{
public static void main (String[] args)
{
int a=100;
int b=150;
int c=200;
[Link](“Value of A is "+a);
[Link](“Value of B is "+b);
[Link](“Value of C is "+c);
if (a>b && a>c)
{
[Link]("A is bigger");
} else if(b>c)
{
[Link]("B is biger");
} else
{
[Link]("C is bigger");
}
}
}
 Develop programs to demonstrate use of- switch and conditional operator
Switch Case (a).
class week
{
public static void main(String[] args)
{
int a=3;
switch(a)
{
case 1:
[Link]("Sunday");
break;
case 2:
[Link]("Monday");
break;
case 3:
[Link]("Tuesday");
break;
case 4:
[Link]("Wednesday");
break;
case 5:
[Link]("Thursday");
break;
case 6:
[Link]("Friday");
break;
case 7:
[Link]("Saturday");
break;
default:
[Link]("Invalid Number");
}
}
}

Switch Case (b).


class sw1
{
public static void main(String[] s)
{
char ch='B';
switch (ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':[Link](ch+" is an Vowel");
break;
default:
[Link](ch+" is a Constant");
break;
}
}
}
 Develop programs to demonstrate use of Looping Statement 'for'
Nested Loop (Stat peramid)
class star
{
public static void main(String[] args)
{
for (int i=0;i<=4;i++)
{
for (int j=1;j<=i;j++)
{
[Link]("* "); // “j” print 2nd pattern and “i” print 3rd pattern.
}
[Link]();
}
}
}

Print Odd numbers from 1 to 9.


class odd
{
public static void main(String[] args)
{
[Link]("Odd table is ");
for (int i=1;i<=10;i++)
{
[Link](i);
i=i+1;
}
}}

//Factorial
class Factorial
{
public static void main(String args[])
{
int fact=0;
for(int i=0;i<=5;i++)
{
fact=fact+i;
[Link](fact);
}
}
}
 Develop programs to demonstrate use of Looping Statement 'while' and 'do-while'
Print 1 to 10 counting (do while loop).
class count
{
public static void main(String[] args)
{
int i=1;
do
{
[Link](i++);
}
while (i<=10);
}
}

Print 1 to 10 counting (while loop).


class while1
{
public static void main(String[] args)
{
int i=1;
while (i<=10)
{
[Link](i++);
}
}
}

 Develop a program for implementation of explicit type conversion


class TypeCast
{
public static void main(String args[])
{
byte b;
int i=257;
double d = 100.78d;
float f=44.56f;

[Link]("Convertion of int to byte:");


b=(byte)i;
[Link](b+" "+i);

[Link]("Convertion of double to int:");


i=(int)d;
[Link](d+" "+i);

[Link]("Convertion of double to Float:");


d=(double)f;
[Link](d+" "+f);

}
}
 Develop a program for implementation of Constructor.
class Person
{
String name;
int age;

Person(String n, int a)
{
name=n;
age=a;
}
void display()
{
[Link]("Name="+name);
[Link]("Age=" +age);
}
}
class Employee extends Person
{
String designation;
int salary;
Employee(String d,int s)
{
//super(n,a);
designation=d;
salary=s;
}
void display1()
{
//display();
[Link]("Designation="+designation);
[Link]("Salary=" +salary);
}

public static void main(String args[])


{
Employee e1=new Employee("Manager",30);
e1.display1();
}
}
 Develop a program for implementation of Arrays in Java

public class Array {

public static void main(String[] args) {


int[] array = {1, 2, 3, 4, 5};

for (int element: array) {


[Link](element);
}
}
}

 Develop a program for implementation of Vectors in Java

 Develop a program for implementation of Wrapper Class to convert primitive into


object.
class IntegerWrapper
{
public static void main(String args[])
{

int b = 55;
String str= "45";
// Construct two Integer objects

int a=[Link](str);
Integer x = new Integer(b);
Integer y = new Integer(str);

[Link]("toString(b) = " + [Link](b));


[Link]("toHexString(b) =" + [Link](b));
[Link]("toOctalString(b) =" + [Link](b));
[Link]("toBinaryString(b) =" + [Link](b));
[Link](a);

}
 Develop a program for implementation of Wrapper Class to convert object into
primitive
public class IntegerObjectWrapper
{
public static void main(String args[])
{
Byte b=1;
Integer i = 5;
Double d = 5.99;
Character c = 'A';
Short s=123;

[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}
 Write a program to make use of Character Wrapper class methods.
public class CharacterWrapper {

public static void main(String[] args) {

[Link]("isLetter");
[Link]([Link]('A'));
[Link]([Link]('0'));

[Link]("isWhitespace");
[Link]([Link]('A'));
[Link]([Link](' '));
[Link]([Link]('\n'));
[Link]([Link]('\t'));

[Link]("isUpperCase");
[Link]([Link]('A'));
[Link]([Link]('a'));

[Link]("LowerCase");
[Link]([Link]('A'));
[Link]([Link]('a'));
[Link]("toUpperCase");
[Link]([Link]('a'));
[Link]([Link](97));

[Link]("toLowerCase");
[Link]([Link]('A'));
[Link]([Link](65));

[Link]("toString");
[Link]([Link]('x'));
[Link]([Link]('Y'));
}
}

 Define a class student with int id and string name as data members and a method
void SetData ( ). Accept and display the data for five students.-4 Mark [Methods
2-Marks & 2-Marks Program]
public class student
{
int id;
string name;

public void setdata()


{
Scanner s1=new Scanner([Link]);
[Link]("student name=");
name=[Link]();
[Link]("student id=");
id=[Link]();
}
public void display()
{
student s1=new student[5];
int i;
for(i=0;i<5;i++);
{
s[i]=new student();
}
for(i=o;i<5;i++);
{
s[i].setdata();
}
for(i=0;i<5;i++)
{
s[i].display();
}
}
}

 Define a class employee with data members 'empid , name and salary. Accept
data for three objects and display it

class employee
{
int empid;
String name;
double salary;
void getdata()
{
BufferedReader obj = new BufferedReader (new InputStreamReader([Link]));
[Link]("Enter Emp number : ");
empid=[Link]([Link]());
[Link]("Enter Emp Name : ");
name=[Link]();
[Link]("Enter Emp Salary : ");
salary=[Link]([Link]());
}
void show()
{
[Link]("Emp ID : " + empid);
[Link](“Name : " + name);
[Link](“Salary : " + salary);
}
}
classEmpDetails
{
public static void main(String args[])
{
employee e[] = new employee[3];
for(inti=0; i<3;i++)
{
e[i] = new employee();
e[i].getdata();
}
[Link](" Employee Details are : ");
for(inti=0; i<3;i++)
{
e[i].show();
}
}

 Develop a program for implementation of try, catch and finally block.


class ExceptionHandling
{
public static void main (String[] args)
{
// array of size 4.
int[] arr = new int[4];

try
{
int i = arr[4];
// this statement will never execute
// as exception is raised by above statement
[Link]("Inside try block");
}

catch(ArrayIndexOutOfBoundsException ex)
{
[Link]("Exception caught in catch block");
}

finally
{
[Link]("finally block executed");
}

// rest program will be executed


[Link]("Outside try-catch-finally clause");
}
}

 Program to throw an exception ”Authentication Failure” if password is


incorrect.
import [Link].*;
class PasswordException extends Exception
{
PasswordException(String msg)
{
super(msg);
}
}
class PassCheck
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
try
{
[Link]("Enter Password : ");
if([Link]().equals("vvp"))
{
[Link]("Authenticated ");
}
else
{
throw new PasswordException("Authentication failure");
}
}
catch(PasswordException e)
{
[Link](e);
}

}
}

 The Program calculates sum of two numbers inputted as command line


arguments when will it give an exception.
class TwoNumber

{
public static void main (String[] args)
{
try
{
int m1=[Link](args[0]);
int m2=[Link](args[1]);
int m=m1+m2;
[Link](“Sum is:”+m);
}
catch(NumberFormatException ex)
{
[Link](ex);
}
finally
{
[Link](“Your inputted a correct integer number”);
}

}
}

 /*Develop a program to create two threads such that one thread will print odd
numbers and the other will print even numbers between 1 to 20 numbers.*/

import [Link].*;
import [Link];
class Even extends Thread
{
public void run()
{
for(int i=1;i<=20;i++)
{
if(i%2==0)
{
[Link]("Even Thread i="+i);
}
}
[Link]("Exit from Even");
}
}

class Odd extends Thread


{
public void run()
{
for(int j=1;j<=20;j++)
{
if(j%2!=0)
{
[Link]("Odd Thread j="+j);
}
}
[Link]("Exit from Odd");
}
}
class EvenOddThread
{
public static void main(String args[])
{
Even ThEven=new Even();
Odd ThOdd=new Odd();
[Link]();
[Link]();
}
}

 Define a package names let_me_calculate include class named as calculator and a


method named add to add two integer numbers. Import let_me_calculate
package in another program named Demo to add two numbers.
package let_me_calculate;
public class Calculator
{
public void add(int a,int b)
{
[Link]("Addition="+(a+b));
}
}
import let_me_calculate.*;
class Demo
{
public static void main(String args[])
{
Calculator cl=new Calculator();
[Link](12,5);
}
}

 Develop program to calculate room area and volume using Single inheritance
class Room
{
int length,width,height;
Room(int l,int w,int h)
{
length=l;
width=w;
height=h;
}
void display()
{
[Link]("Length="+length);
[Link]("Width="+width);
[Link]("Height="+height);
}
}
class AreaVolume extends Room
{
AreaVolume(int l,int w,int h)
{
super(l,w,h);
}
void calculate()
{
[Link]("Area of room="+length*width);
[Link]("Volume of room="+length*width*height);
}
}
class RoomDemo
{
public static void main(String args[])
{
AreaVolume av=new AreaVolume(50,40,30);
[Link]();
[Link]();
}
}

 Program to find area of rectangle and circle using interfaces.


interface Area
{
float pi=3.14F;
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute (float x, float y)
{
return(x*y);
}
}
class Circle implements Area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}
classRCArea
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area a;
a=rect;
[Link]("Area of rectangle : "+[Link](5,10));
a=cir;
[Link]("Area of circle : "+[Link](5,0));
}
}

 Develop program which implements the concept of overriding.


class Bank
{
intgetRateOfInterest(){return 0;}
}
class SBI extends Bank
{
intgetRateOfInterest(){return 8;}
}
class ICICI extends Bank
{
intgetRateOfInterest(){return 7;}
}
class AXIS extends Bank
{
intgetRateOfInterest(){return 9;}

classBankInterestRate
{
public static void main(String args[])
{
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
[Link]("SBI Rate of Interest: "+[Link]());
[Link]("ICICI Rate of Interest: "+[Link]());
[Link]("AXIS Rate of Interest: "+[Link]());
}
}

 /*Define an Exception called “NoMatchException” that is thrown when a string


is not equal to “India”. Write a program that uses this exception.*/

class NoMatchException extends Exception


{
String m;
NoMatchException(String message)
{
m = message;
}
void printmsg()
{
[Link](m);
}
}
class NoMatch
{
public static void main(String args[])
{
if([Link]!=1)
{
[Link]("\nEnter only one String argument");
}
else
{
try
{
if(args[0].compareTo("India") == 0)
{
[Link]("\nString is equal to 'India'.");
}
else
{
throw new NoMatchException("Arguments is not equal to 'India'.");
}
}
catch(NoMatchException e)
{
[Link]("\nException Caught: ");
[Link]();
}
}
}
}

 //Write a program to input name and age of person and throws user defined
exception, if entered age is negative.
import [Link].*;
class Negative extends Exception
{
Negative(String msg)
{
super(msg);
}
}
class Negativedemo
{
public static void main(String ar[])
{
int age=0;
String name;
Scanner sc=new Scanner([Link]);
[Link]("enter age and name of person");
try
{
age=[Link]();
name=[Link]();

if(age<0)
throw new Negative("age is negative");
else
throw new Negative("age is positive");
}
catch(Negative n)
{
[Link](n);
}
}
}
 Program to display string in applet
import [Link].*;
import [Link].*;
public class WelcomeToApplet extends Applet
{
public void paint (Graphics g)
{
[Link] ("Welcome to the World of Applet", 25, 50);
}
}
/* <applet code="[Link]" width=400 height=200></applet> */

 Program to use control loops in applet


import [Link].*;
import [Link].*;
/* <applet code= [Link] width=100 height=100></applet>*/
public class loopsinapplet extends Applet
{
public void paint(Graphics g)
{
int row;
int col;
int x,y;
for ( row = 0; row < 5; row++ )
{
for ( col = 0; col < 5; col++)
{
x = col * 20;
y = row * 20;
if ( (row % 2) == (col % 2) )
{
[Link]([Link]);
}
else
{
[Link]([Link]);
}
[Link](x, y, 20, 20);
}
}
}
}

 Develop a program to write Bytes to file.


import [Link];
import [Link];

class ByteToFile
{
public static void main(String args[])
{

try
{
FileOutputStream fout= new FileOutputStream("[Link]");

String s= "Welcome to JAVA Programming! This is an example of Java program to write Bytes
using ByteStream. into file";

byte b[] = [Link]();

[Link](b);
[Link]("Copied Successfully");

[Link]();
}
catch (IOException e)
{
[Link](e);
}
}
}
Output:
Copied Successfully

 Develop a program to copy one file to another using Byte Stream.


import [Link].*;
class CopyByte
{
public static void main(String args[])
{
try
{ FileInputStream fis=new FileInputStream("[Link]");
FileOutputStream fos=new FileOutputStream("[Link]");

int b=0;
while(b!=-1)
{
b=[Link]();
[Link](b);
}
[Link]("File copied Successfully");
[Link]();
[Link]();
}
catch(IOException e)
{
[Link](e);
}
}
}
Output:
File copied Successfully

 Develop a program to copy one file to another using Character Stream.


import [Link].*;
class CopyChar
{
public static void main(String args[])
{
try
{
FileReader fr=new FileReader("[Link]");
FileWriter fw=new FileWriter("[Link]");

int ch=0;
while(ch!=-1)
{
ch=[Link]();
[Link]((char)ch);
}
[Link]("File copied Successfully");
[Link]();
[Link]();
}
catch(IOException e)
{
[Link](e);
}
}
}
Output:
File copied Successfully

 Develop a program to draw


a. Square Inside a Circle
b. Circle inside a square
import [Link].*;
import [Link];
import [Link];

public class GraphicsExcercise extends Applet {

public void paint(Graphics g) {

[Link](80,80,100,100);
[Link](100, 100, 60, 60);

[Link](200,80,100,100);
[Link](220,100,60,60);
}
}

/* <applet code="[Link]" width="320" height="320">


</applet>
*/
 Applet program to draw a cone,cylinder and cube

import [Link].*;
import [Link].*;;
import [Link].*;

//<Applet code="Cone" WIDTH="800" HEIGHT="400"></Applet>

public class Cone extends Applet


{
public void paint(Graphics g)
{
setBackground([Link]);
[Link]([Link]);

/* To draw an cone*/
[Link](200,80,200,50);
[Link](200,100,300,500);
[Link](400,100,300,500);

/* To draw a cyclinder*/
[Link](500,60,200,50);
[Link](500,80,500,300);
[Link](700,80,700,300);
[Link](500,280,200,50);

/* To draw a cube*/
[Link](500,400,100,100);
[Link](550,450,100,100);
[Link](500,400,550,450);
[Link](500,500,550,550);
[Link](600,400,650,450);
[Link](650,550,600,500);
}
}

 Program to draw polygon in applet


import [Link].*;
import [Link];

public class DrawFillPolygon extends Applet


{
int xCoords[] = { 50, 200, 300, 150, 50, 50 };
int yCoords[] = { 100, 0, 50, 300, 200, 100 };
int xFillCoords[] = { 450, 600, 700, 550, 450, 450 };
public void paint(Graphics g)
{
[Link](xCoords, yCoords, 6);
[Link](xFillCoords, yCoords, 6);
}
}
/* <applet code=DrawFillPolygon width=300 height=300> </applet> */

You might also like