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++)
[Link](j + " ");
[Link]();
}
}
}
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 )
[Link](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 [Link];
public class Program3a {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Circle Radius: ");
double radius = [Link]();
[Link]("Area of Circle: " + ([Link] * radius * radius));
[Link]("Circumference of Circle: " + (2 * [Link] * 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 [Link];
public class Program3b {
public static void main(String[] args) {
[Link]("Enter a number to check prime or not: ");
Scanner sc = new Scanner([Link]);
int n = [Link]();
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)
[Link](n + " is a prime number");
else
[Link](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;
[Link]("Quotient: "+ (a/b));
}
catch (ArithmeticException e)
{
[Link]("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();
[Link]("Accessing from outer class");
[Link]("The value of outdata is " + outdata);
[Link]("The value of indata is " + [Link]);
[Link]();
}
class Inner {
private int indata = 20;
void inmethod() {
[Link]("Accessing from inner class");
[Link]("The sum of indata & outdata is " + (outdata + indata));
}
}
}
class Program5 {
public static void main(String args[]) {
Outer out = new Outer();
[Link]();
[Link] in = [Link] Inner();
[Link]();
}
}
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()
{
[Link]("Welcome");
}
Program6(String name)
{
[Link]("Welcome "+ name);
}
//Method Overloading
public void add(int a, int b)
{
[Link]("Sum of "+ a +" + "+ b +" = "+ a+b);
}
public void add(double a, double b)
{
[Link]("Sum of "+ a +" + "+ b +" = "+ (a+b));
}
public static void main(String[] args) {
Program6 p1 = new Program6();
Program6 p2 = new Program6("Yogeesh S");
[Link](5, 6);
[Link](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() {
[Link]("Circle Drawn");
}
public void drawCircle() {
[Link]("Rectangle Drawn");
}
}
public class Program7 {
public static void main(String args[]) {
Shapes s = new Shapes();
[Link]();
[Link]();
}
}
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 [Link].*;
import [Link].*;
import [Link].*;
//<applet code="[Link]" 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) {
[Link](msg, 150, 100);
}
public void keyReleased(KeyEvent ke) {
showStatus(key + " Key Released");
}
public void keyPressed(KeyEvent ke) {
int keycode = [Link]();
key = [Link](keycode);
repaint();
showStatus(key + " Key Pressed");
}
public void keyTyped(KeyEvent ke) {
char c = [Link]();
msg += c;
key = [Link](c);
repaint();
}
}
11 MCA, AIT, Bangalore
Java Programming Laboratory 22MCAL28
Output
12 MCA, AIT, Bangalore