Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Insttructions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

JAVA LAB MANUAL

Program 1:Write a simple java application, to print the message, “hELLO WORLD”
package java_lab_manual;

public class HelloWorld {

public static void main(String[] args) {


System.out.println("HELLO WORLD");

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 {

public static void main(String[] args) {

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.*;

public class DivisionByZero {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter numerator");
int num=sc.nextInt();
System.out.println("Enter denominator");
int deno=sc.nextInt();

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.*;

class PayOutOfBounds extends Exception{


public void showError() {
System.out.print("Invalid Pay");
}
}

public class ErrorTest {

public static void main(String[] args) throws Exception {


Scanner sc = new Scanner(System.in);
try {
System.out.print("Enter The Pay\n");
float pay=sc.nextFloat();
if(pay>10000)
throw new PayOutOfBounds();

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;

public class OverloadingTest1 {


void addition(int a, int b) {
int sum=a+b;
System.out.println("the sum is" +sum);
}
void addition(double a, double b) {
double sum=a+b;
System.out.println("the sum is" +sum);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
OverloadingTest1 ov1= new OverloadingTest1();
ov1.addition(10,20);
ov1.addition(1.5,2.5);
}

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;

public class NegativeArraySizeExceptionExample {


public static void main(String[] args) {
// TODO Auto-generated method stub
try {
int[] array = new int[-5];
} catch (NegativeArraySizeException nase) {
nase.printStackTrace();
}
System.out.println("Continuing execution...");

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;

public class TestFinallyBlock {

public static void main(String[] args) {


// TODO Auto-generated method stub
try {
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e) {
System.out.println(e);
}
finally {
System.out.println("Finally Block is always executed");
}
System.out.println("rest of the code.....");
}

10. Create a Package student.Fulltime.BCA in your current working directory

A) Create a default class Student in the above package with the following attributes: Name, age,
Gender

B) Have methods for Storing as well as displaying


package student.Fulltime.BCA;

public class Studentinfo {


int age;
char gender;
String name;
public Studentinfo(String a,int e,char f) {
name=a;
age=e;
gender=f;
}
public void display() {
System.out.println("STUDENT PERSONAL DETAILS");
System.out.println("NAME OF THE STUDENT IS:"+name);
System.out.println("Age="+age);
System.out.println("STUDENT is"+gender);
}

}
package java_lab_manual;
import student.Fulltime.BCA.*;

public class PackDemo {

public static void main(String[] args) {


// TODO Auto-generated method stub
Studentinfo st1=new Studentinfo("Rita",18,'f');
st1.display();
}

You might also like