Lab Insttructions
Lab Insttructions
Lab Insttructions
Program 1:Write a simple java application, to print the message, “hELLO WORLD”
package java_lab_manual;
Program 2:Write a program to display the month of a year. Months of the year should be held in an
array.
package java_lab_manual;
import java.util.*;
public class CalenderMonth {
String[] month={"jan","feb","mar","apr","may","jun","jul",
"aug","sep","oct","nov","dec" };
for(int i=0;i<12;i++){
System.out.println(month[i]);
}
Program 3:Write a program to demonstrate a division by zero exception
package java_lab_manual;
import java.util.*;
try {
int result=num/deno;
System.out.println(result);
}
catch (ArithmeticException e){
System.out.println("Arithmatic exception division by zero not
allowed");
}
}
}
Program 4:Write a program to create a user defined exception say Pay Out of Bounds. .
package java_lab_manual;
import java.util.*;
System.out.print(pay);
}
catch(PayOutOfBounds e) {
e.showError();
}
}
}
Program 5: Write a java program to add two integers and two float numbers. When no arguments
are supplied, give a default value to calculate the sum. Use function overloading.
package java_lab_manual;
Program 6: Write a program to perform mathematical operations. Create a class called AddSub with
methods to add and subtract. Create another class called MulDiv that extends from AddSub class to
use the member data of the super class. MulDiv should have methods to multiply and divide A main
function should access the methods and perform the mathematical operations.
package java_lab_manual;
class addsub
{
int num1,num2;
addsub(int n1, int n2)
{
num1 = n1;
num2 = n2;
}
int add()
{
return num1+num2;
}
int sub()
{
return num1-num2;
}
}
class multdiv extends addsub
{
public multdiv(int n1, int n2)
{
super(n1, n2);
}
int mul()
{
return num1*num2;
}
float div()
{
return num2/num1;
}
}
public class InheritanceDemo
{
public static void main(String arg[])
{
addsub r1=new addsub(50,20);
int ad = r1.add();
int sb = r1.sub();
System.out.println("Addition =" +ad);
System.out.println("Subtraction =" +sb);
multdiv r2 =new multdiv(4,20);
int ml = r2.mul();
float dv =r2.div();
System.out.println("Multiply =" +ml);
System.out.println("Division =" +dv);
}
}
Program 7: Write a program with class variable that is available for all instances of a class. Use static
variable declaration. Observe the changes that occur in the object’s member variable values.
package java_lab_manual;
class Staticvar
{
public static int a,b;
public void display()
{
System.out.println(" A value ="+a+" B valu ="+b);
}
}
class Demo
{
public static void main(String args[])
{
Staticvar sv=new Staticvar();
sv.a=10;
sv.b=20;
sv.display();
Staticvar sv1=new Staticvar();
sv1.display();
}
}
Program 8: Write a small Program to catch Negative Array Size Exception. This exception is caused
when the array is initialized to negative values.
package java_lab_manual;
Program 9: Write a Program to handle Null Pointer Exception and use the “Finally” method to display
message to the user.
package java_lab_manual;
A) Create a default class Student in the above package with the following attributes: Name, age,
Gender
}
package java_lab_manual;
import student.Fulltime.BCA.*;