Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
7.8 kB
6
Indexable
Never
LAB 1A: Program to check x and y values using if statement and displaying message whether x is greater than y

package LabPgm1;

public class Pgm1 
{
	public static void main(String args[])
	{
		int x=8, y=3;
		
		System.out.println("X = " +x);
		System.out.println("Y = " +y);
		
		if( x > y )
			System.out.println("X is greater than Y");
		else
			System.out.println("Y is greater than X");
	}

}



OUTPUT:

X = 8
Y = 3
X is greater than Y



LAB 2A: Program to list the factorial of the numbers from 1 to 10 using while loop

package LabPgm2;

public class Pgm2 
{
	public static void main(String[] args) {
		int fact, i, j;
		
		for(i=1;i<=10;i++)
		{
			System.out.print("\nFactorial of " + i + " is: ");
			fact=1;
			j=1;	
			while(j<=i)
			{
				fact = fact * j;
				j++;
			}
			System.out.print(fact);
		}

	}

}



OUTPUT:

Factorial of 1 is: 1
Factorial of 2 is: 2
Factorial of 3 is: 6
Factorial of 4 is: 24
Factorial of 5 is: 120
Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320
Factorial of 9 is: 362880
Factorial of 10 is: 3628800






LAB 3A: Program to find the area and circumference of the circle by accepting the radius from the user

package LabPgm3;
import java.util.*;

public class Pgm3 
{
	public static void main(String args[])
	{
		double radius, area, circum;
		Scanner in = new Scanner(System.in);
		
		System.out.println("Enter radius value: ");
		radius = in.nextDouble();
		
		area=Math.PI * radius * radius;
		circum = 2 * Math.PI * radius;
		
		System.out.println("Area = " +area);
		System.out.println("Circumference = " +circum);
	}

}

OUTPUT:

Enter radius value: 
2.6
Area = 21.237166338267002
Circumference = 16.336281798666924




LAB 4A: Program to add two integer and two float numbers. Use function overloading

package LabPgm4;

public class AddDemo 
{
	int a=2, b=6, res;
	double c=3.5, d=5.6, res1;
	void addInt()
	{
		res = a+b;
		System.out.println("Addition of default Integer values" +a+ " and  " +b+ " is: " + res);
	}
	void addInt(int x, int y)
	{
		a=x;
		b=y;
		res = a+b;
		System.out.println("Addition of Integer parameters values "+a+" and "+b+" is: "+res);
	}
	void addFloat()
	{
		res1 = c+d;
		System.out.println("\nAddition of default Float values " +c+ " and  "+d+ " is: " +res1);
	}
	void addFloat(double x, double y)
	{
		c=x;
		d=y;
		res1 = c+d;
		System.out.println("Addition of Float parameters values "+c+" and "+d+" is: " +res1);
	}

}

package LabPgm4;

public class Pgm4 
{
	public static void main(String[] args) 
{
		AddDemo ad = new AddDemo();
		Scanner in = new Scanner(System.in);
		
		int a,b;
		double c,d;
		
		System.out.println("Enter 2 integer values" );
		a=in.nextInt();
		b=in.nextInt();	
		
		System.out.println("Enter 2 float values" );
		c=in.nextDouble();
		d=in.nextDouble();	
		
		ad.addInt();
		ad.addInt(a,b);
		ad.addFloat();
		ad.addFloat(c,d);

	}

}



OUTPUT:

Enter 2 integer values
3
6
Enter 2 float values
2.3
6.5

Addition of default Integer values 2 and 6 is: 8
Addition of Integer parameters values 3 and 6 is: 9

Addition of default Float values 3.5 and 5.6 is: 9.1
Addition of Float parameters values 2.3 and 6.5 is: 8.8





LAB 5A: Program to perform arithmatic operations by extending the classes

package LabPgm5;
import java.util.*;

public class AddSub 
{
	int a, b;
	int add, sub;
	Scanner in = new Scanner(System.in); 
	void getdata()
	{
		System.out.println("Enter a value:");
		a= in.nextInt();
		System.out.println("Enter b value:");
		b= in.nextInt();
	}
	void add1()
	{
		add = a + b;
		System.out.println("Addition: "+add);
	}
	void sub1()
	{
		sub = a - b;
		System.out.println("Subtraction: "+sub);
	}
}

package LabPgm5;
public class MulDiv extends AddSub 
{
	int mul;
	double div;
	
	void mul1()
	{
		mul = a * b;
		System.out.println("Multiplication: "+mul);
	}
	void div1()
	{
		div = a / b;
		System.out.println("Division: "+div);
	}
}

package LabPgm5;
public class Pgm5 
{
	public static void main(String[] args)
	{
		MulDiv obj = new MulDiv();
		
		obj.getdata();
		obj.add1();
		obj.sub1();
		obj.mul1();
		obj.div1();
	}

}



OUTPUT:


Enter a value:
6

Enter b value:
2

Addition: 8
Subtraction: 4
Multiplication: 12
Division: 3.0



Part B


LAB 1B: Program to catch Negative Array Size Exception. This exception is caused when the array size is initialized to negative values.

public class Pgm1b 
{
	public static void main(String[] args) 
	{
		try
		{
			int[] array = new int[-2];
		}
		catch(NegativeArraySizeException e)
		{
			System.out.println("ArraySize Error");
			e.printStackTrace();
		}
		
		System.out.println("Continuing execution");
	}

}


OUTPUT:

ArraySize Error
java.lang.NegativeArraySizeException: -2
	at Pgm1b.main(Pgm1b.java:8)
Continuing execution




LAB 2B: Program to demonstrate exception handling with try, catch and finally.

import java.util.Scanner;
public class Pgm2b 
{
	public static void main(String[] args) 
	{
		int a,b;
		double c;
		Scanner in = new Scanner(System.in);
		
		System.out.println("Enter 1st number");
		a = in.nextInt();
		
		System.out.println("Enter 2nd number");
		b = in.nextInt();
		try
		{
			c = a / b;
			System.out.println("Division = " + c);
		}
		catch(ArithmeticException e)
		{
			System.out.println("Divide by zero error");
			System.out.println(e.getLocalizedMessage());
		}
		finally
		{
			System.out.println("Inside finally block");
		}
		
		System.out.println("Outside try-catch-finally block");
	}

}



OUTPUT:

Enter 1st number
6
Enter 2nd number
3
Division = 2.0
Inside finally block
Outside try-catch-finally block


Enter 1st number
5
Enter 2nd number
0
Divide by zero error
/ by zero
Inside finally block
Outside try-catch-finally block




LAB 3B: Program which create and displays a message on the window

package LabPgm3b;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Pgm3b implements ActionListener
{
	public static void main(String[] args) 
	{
		JFrame frame = new JFrame("Original Frame");
		frame.setSize(300,300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Pgm3b obj = new Pgm3b();
		JButton button = new JButton("View Message");
		frame.add(button);
		button.addActionListener(obj);
		frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		JFrame sub_frame = new JFrame("Sub Frame");
		sub_frame.setSize(200,200);
		JLabel label = new JLabel("!!! HELLO !!!");
		sub_frame.add(label);
		sub_frame.setVisible(true);
	}
}



OUTPUT







LAB 5B: Program to create a 4×4 grid and fills it in with 15 buttons, each 1 labelled with its index.

package LabPgm5b;

import java.awt.*;    
import javax.swing.*; 

public class Pgm5b 
{
	JFrame frameObj;    
	Pgm5b()  
	{    
		frameObj = new JFrame();    
		   
		JButton btn1 = new JButton("1");    
		JButton btn2 = new JButton("2");    
		JButton btn3 = new JButton("3");    
		JButton btn4 = new JButton("4");    
		JButton btn5 = new JButton("5");    
		JButton btn6 = new JButton("6");    
		JButton btn7 = new JButton("7");    
		JButton btn8 = new JButton("8");    
		JButton btn9 = new JButton("9"); 
		JButton btn10 = new JButton("10"); 
		JButton btn11 = new JButton("11"); 
		JButton btn12 = new JButton("12"); 
		JButton btn13 = new JButton("13"); 
		JButton btn14 = new JButton("14"); 
		JButton btn15 = new JButton("15"); 
		
		frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);  
		frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);  
		frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);
		frameObj.add(btn10);	frameObj.add(btn11);	frameObj.add(btn12);
		frameObj.add(btn13);	frameObj.add(btn14);	frameObj.add(btn15);

		frameObj.setLayout(new GridLayout(4,4));    
		frameObj.setSize(300, 300);    
		frameObj.setVisible(true);    
	}  
	  
	public static void main(String args[])   
	{    
		new Pgm5b();    
	}    
}



OUTPUT