Practical Java Programs
Practical Java Programs
Practical Java Programs
class big3
{
public static void main (String[] args)
{
int a=100;
int b=150;
int c=200;
System.out.println(“Value of A is "+a);
System.out.println(“Value of B is "+b);
System.out.println(“Value of C is "+c);
if (a>b && a>c)
{
System.out.println("A is bigger");
} else if(b>c)
{
System.out.println("B is biger");
} else
{
System.out.println("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:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid Number");
}
}
}
//Factorial
class Factorial
{
public static void main(String args[])
{
int fact=0;
for(int i=0;i<=5;i++)
{
fact=fact+i;
System.out.println(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
{
System.out.println(i++);
}
while (i<=10);
}
}
}
}
Develop a program for implementation of Constructor.
class Person
{
String name;
int age;
Person(String n, int a)
{
name=n;
age=a;
}
void display()
{
System.out.println("Name="+name);
System.out.println("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();
System.out.println("Designation="+designation);
System.out.println("Salary=" +salary);
}
int b = 55;
String str= "45";
// Construct two Integer objects
int a=Integer.parseInt(str);
Integer x = new Integer(b);
Integer y = new Integer(str);
}
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;
System.out.println(i.intValue());
System.out.println(d.doubleValue());
System.out.println(c.charValue());
System.out.println(b.byteValue());
System.out.println(s.shortValue());
}
}
Write a program to make use of Character Wrapper class methods.
public class CharacterWrapper {
System.out.println("isLetter");
System.out.println(Character.isLetter('A'));
System.out.println(Character.isLetter('0'));
System.out.println("isWhitespace");
System.out.println(Character.isWhitespace('A'));
System.out.println(Character.isWhitespace(' '));
System.out.println(Character.isWhitespace('\n'));
System.out.println(Character.isWhitespace('\t'));
System.out.println("isUpperCase");
System.out.println(Character.isUpperCase('A'));
System.out.println(Character.isUpperCase('a'));
System.out.println("LowerCase");
System.out.println(Character.isLowerCase('A'));
System.out.println(Character.isLowerCase('a'));
System.out.println("toUpperCase");
System.out.println(Character.toUpperCase('a'));
System.out.println(Character.toUpperCase(97));
System.out.println("toLowerCase");
System.out.println(Character.toLowerCase('A'));
System.out.println(Character.toLowerCase(65));
System.out.println("toString");
System.out.println(Character.toString('x'));
System.out.println(Character.toString('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;
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(System.in));
System.out.print("Enter Emp number : ");
empid=Integer.parseInt(obj.readLine());
System.out.print("Enter Emp Name : ");
name=obj.readLine();
System.out.print("Enter Emp Salary : ");
salary=Double.parseDouble(obj.readLine());
}
void show()
{
System.out.println("Emp ID : " + empid);
System.out.println(“Name : " + name);
System.out.println(“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();
}
System.out.println(" Employee Details are : ");
for(inti=0; i<3;i++)
{
e[i].show();
}
}
try
{
int i = arr[4];
// this statement will never execute
// as exception is raised by above statement
System.out.println("Inside try block");
}
catch(ArrayIndexOutOfBoundsException ex)
{
System.out.println("Exception caught in catch block");
}
finally
{
System.out.println("finally block executed");
}
}
}
{
public static void main (String[] args)
{
try
{
int m1=Integer.parseInt(args[0]);
int m2=Integer.parseInt(args[1]);
int m=m1+m2;
System.out.println(“Sum is:”+m);
}
catch(NumberFormatException ex)
{
System.out.println(ex);
}
finally
{
System.out.println(“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 java.io.*;
import java.lang.Thread;
class Even extends Thread
{
public void run()
{
for(int i=1;i<=20;i++)
{
if(i%2==0)
{
System.out.println("Even Thread i="+i);
}
}
System.out.println("Exit from Even");
}
}
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()
{
System.out.println("Length="+length);
System.out.println("Width="+width);
System.out.println("Height="+height);
}
}
class AreaVolume extends Room
{
AreaVolume(int l,int w,int h)
{
super(l,w,h);
}
void calculate()
{
System.out.println("Area of room="+length*width);
System.out.println("Volume of room="+length*width*height);
}
}
class RoomDemo
{
public static void main(String args[])
{
AreaVolume av=new AreaVolume(50,40,30);
av.display();
av.calculate();
}
}
classBankInterestRate
{
public static void main(String args[])
{
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
//Write a program to input name and age of person and throws user defined
exception, if entered age is negative.
import java.util.*;
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(System.in);
System.out.println("enter age and name of person");
try
{
age=sc.nextInt();
name=sc.nextLine();
if(age<0)
throw new Negative("age is negative");
else
throw new Negative("age is positive");
}
catch(Negative n)
{
System.out.println(n);
}
}
}
Program to display string in applet
import java.applet.*;
import java.awt.*;
public class WelcomeToApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Welcome to the World of Applet", 25, 50);
}
}
/* <applet code="WelcomeToApplet.class" width=400 height=200></applet> */
class ByteToFile
{
public static void main(String args[])
{
try
{
FileOutputStream fout= new FileOutputStream("Output.txt");
String s= "Welcome to JAVA Programming! This is an example of Java program to write Bytes
using ByteStream. into file";
fout.write(b);
System.out.print("Copied Successfully");
fout.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
Output:
Copied Successfully
int b=0;
while(b!=-1)
{
b=fis.read();
fos.write(b);
}
System.out.println("File copied Successfully");
fis.close();
fos.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Output:
File copied Successfully
int ch=0;
while(ch!=-1)
{
ch=fr.read();
fw.write((char)ch);
}
System.out.println("File copied Successfully");
fr.close();
fw.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Output:
File copied Successfully
g.drawRect(80,80,100,100);
g.drawOval(100, 100, 60, 60);
g.drawOval(200,80,100,100);
g.drawRect(220,100,60,60);
}
}
import java.io.*;
import java.awt.*;;
import java.applet.*;
/* To draw an cone*/
g.drawOval(200,80,200,50);
g.drawLine(200,100,300,500);
g.drawLine(400,100,300,500);
/* To draw a cyclinder*/
g.drawOval(500,60,200,50);
g.drawLine(500,80,500,300);
g.drawLine(700,80,700,300);
g.drawOval(500,280,200,50);
/* To draw a cube*/
g.drawRect(500,400,100,100);
g.drawRect(550,450,100,100);
g.drawLine(500,400,550,450);
g.drawLine(500,500,550,550);
g.drawLine(600,400,650,450);
g.drawLine(650,550,600,500);
}
}