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

22MCAL28 - Java Programming Laboratory - Experiments

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Java Programming Laboratory 22MCAL28

1. Write a Java program to print the following triangle of numbers


1
12
123
1234
12345

Program

class Program1 {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
}
}

Output

1
12
123
1234
12345

1 MCA, AIT, Bangalore


Java Programming Laboratory 22MCAL28

2. Write a Java program to list the factorial of the numbers 1 to 10. To


calculate the factorial value, use while loop. (Hint Fact of 4 = 4*3*2*1)
Program

class Program2 {
public static void main(String[] args) {
int fact = 1, i = 1;
while( i <= 10 )
System.out.println(i + "! = " + (fact = fact * i++));
}
}

Output

1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800

2 MCA, AIT, Bangalore


Java Programming Laboratory 22MCAL28

3. Write a Java program


 To find the area and circumference of the circle by accepting the
radius from the user.
 To accept a number and find whether the number is Prime or not

3.1 To find the area and circumference of the circle by accepting the radius
from the user.

Program

import java.util.Scanner;

public class Program3a {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Circle Radius: ");
double radius = sc.nextDouble();
System.out.println("Area of Circle: " + (Math.PI * radius * radius));
System.out.println("Circumference of Circle: " + (2 * Math.PI * radius));
}
}

Output

Enter Circle Radius: 5


Area of Circle: 78.53981633974483
Circumference of Circle: 31.41592653589793

3 MCA, AIT, Bangalore


Java Programming Laboratory 22MCAL28

3.2 To accept a number and find whether the number is Prime or not

Program

import java.util.Scanner;

public class Program3b {


public static void main(String[] args) {
System.out.print("Enter a number to check prime or not: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int flag = 0, m = n/2;
if(n!=0 && n!=1)
{
for (int i = 2; i <= m; i++){
if(n%i==0)
flag = 1;
}
}
else
flag = 1;
if(flag == 0)
System.out.println(n + " is a prime number");
else
System.out.println(n + " is not a prime number");
}
}

4 MCA, AIT, Bangalore


Java Programming Laboratory 22MCAL28

Output 1

Enter a number to check prime or not: 5


5 is a prime number

Output 2

Enter a number to check prime or not: 0


0 is not a prime number

Output 3

Enter a number to check prime or not: 10


10 is not a prime number

5 MCA, AIT, Bangalore


Java Programming Laboratory 22MCAL28

4. Write a Java program to demonstrate a division by zero exception

Program

public class Program4 {


public static void main(String[] args) {
try
{
int a = 5, b = 0;
System.out.println("Quotient: "+ (a/b));
}
catch (ArithmeticException e)
{
System.out.println("Number cant be divided by zero ");
}
}
}

Output

Number can’t be divided by zero

6 MCA, AIT, Bangalore


Java Programming Laboratory 22MCAL28

5. Write a Java program to implement Inner class and demonstrate its


Access protection.

Program

class Outer {
private int outdata = 10;

void display() {
Inner in = new Inner();
System.out.println("Accessing from outer class");
System.out.println("The value of outdata is " + outdata);
System.out.println("The value of indata is " + in.indata);
System.out.println();
}

class Inner {
private int indata = 20;

void inmethod() {
System.out.println("Accessing from inner class");
System.out.println("The sum of indata & outdata is " + (outdata + indata));
}
}
}

class Program5 {
public static void main(String args[]) {
Outer out = new Outer();
out.display();
Outer.Inner in = out.new Inner();
in.inmethod();
}
}

7 MCA, AIT, Bangalore


Java Programming Laboratory 22MCAL28

Output

Accessing from outer class


The value of outdata is 10
The value of indata is 20

Accessing from inner class


The sum of indata & outdata is 30

8 MCA, AIT, Bangalore


Java Programming Laboratory 22MCAL28

6. Write a Java program to demonstrate Constructor Overloading and


Method Overloading
Program
public class Program6 {
//Constructor Overloading
Program6()
{
System.out.println("Welcome");
}

Program6(String name)
{
System.out.println("Welcome "+ name);
}
//Method Overloading
public void add(int a, int b)
{
System.out.println("Sum of "+ a +" + "+ b +" = "+ a+b);
}

public void add(double a, double b)


{
System.out.println("Sum of "+ a +" + "+ b +" = "+ (a+b));
}
public static void main(String[] args) {
Program6 p1 = new Program6();
Program6 p2 = new Program6("Yogeesh S");
p1.add(5, 6);
p1.add(5.2, 6.4);
}
}

Output
Welcome
Welcome Yogeesh S
Sum of 5 + 6 = 56
Sum of 5.2 + 6.4 = 11.600000000000001

9 MCA, AIT, Bangalore


Java Programming Laboratory 22MCAL28

7. Write a JAVA program to demonstrate Inheritance. Simple Program on


Java for the implementation of Multiple inheritance using interfaces to
calculate the area of a rectangle and triangle.
Program
interface Rectangle {
void drawRect();
}
interface Circle {
void drawCircle();
}
class Shapes implements Rectangle, Circle {
public void drawRect() {
System.out.println("Circle Drawn");
}
public void drawCircle() {
System.out.println("Rectangle Drawn");
}
}
public class Program7 {
public static void main(String args[]) {
Shapes s = new Shapes();
s.drawRect();
s.drawCircle();
}
}

Output

Circle Drawn
Rectangle Drawn

10 MCA, AIT, Bangalore


Java Programming Laboratory 22MCAL28

8. Write a Java applet program, which handles keyboard event.

Program

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

//<applet code="Program8.java" width=600 height=200></applet>

public class Program8 extends Applet implements KeyListener {


String msg = "", key;

public void init() {


addKeyListener(this);
}

public void paint(Graphics g) {


g.drawString(msg, 150, 100);
}

public void keyReleased(KeyEvent ke) {


showStatus(key + " Key Released");
}

public void keyPressed(KeyEvent ke) {


int keycode = ke.getKeyCode();
key = ke.getKeyText(keycode);
repaint();
showStatus(key + " Key Pressed");
}

public void keyTyped(KeyEvent ke) {


char c = ke.getKeyChar();
msg += c;
key = String.valueOf(c);
repaint();
}
}

11 MCA, AIT, Bangalore


Java Programming Laboratory 22MCAL28

Output

12 MCA, AIT, Bangalore

You might also like