OOPS Using Java Laboratory MMCL206
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
OOPS Using Java Laboratory MMCL206
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
OOPS Using Java Laboratory MMCL206
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
OOPS Using Java Laboratory MMCL206
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
OOPS Using Java Laboratory MMCL206
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
OOPS Using Java Laboratory MMCL206
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 cant be divided by zero
6
OOPS Using Java Laboratory MMCL206
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
OOPS Using Java Laboratory MMCL206
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
OOPS Using Java Laboratory MMCL206
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 = 11
Sum of 5.2 + 6.4 = 11.600000000000001
9
OOPS Using Java Laboratory MMCL206
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 rectangleArea(double w,double h);
}
interface Triangle{
void triangleArea(double b,double h);
}
class Shapes implements Rectangle,Triangle{
public void rectangleArea(double w, double h) {
[Link]("Rectangle Area is: "+(w*h));
}
public void triangleArea(double b, double h) {
[Link]("Triangle Area is: "+(0.5*b*h));
}
}
class Program7 {
public static void main(String[] args){
Shapes s = new Shapes();
[Link](5,6);
[Link](4,3);
}
}
Output
Rectangle Area is: 30.0
Triangle Area is: 6.0
10
OOPS Using Java Laboratory MMCL206
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 = "";
char 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) {
key = [Link]();
repaint();
showStatus(key + " Key Pressed");
}
public void keyTyped(KeyEvent ke) {
msg += key;
repaint();
}
}
11
OOPS Using Java Laboratory MMCL206
To Execution
C:\Users\Yogeesh S\Desktop\Programs>javac [Link]
C:\Users\Yogeesh S\Desktop\Programs>appletviewer [Link]
Output
12
OOPS Using Java Laboratory MMCL206
9. Write a Java Program to create a window when we press
➢ M or m the window displays Good Morning
➢ A or a the window displays Good After Noon
➢ E or e the window displays Good Evening
➢ N or n the window displays Good Night
Program
import [Link].*;
import [Link].*;
public class Program9 extends Frame implements KeyListener {
Label lbl;
Program9() {
addKeyListener(this);
requestFocus();
lbl = new Label();
[Link](100, 100, 200, 40);
[Link](new Font("Calibri", [Link], 16));
add(lbl);
setSize(400, 300);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e) {
if ([Link]() == 'M' || [Link]() == 'm')
[Link]("Good morning");
else if ([Link]() == 'A' || [Link]() == 'a')
[Link]("Good afternoon");
else if ([Link]() == 'E' || [Link]() == 'e')
[Link]("Good evening");
else if ([Link]() == 'N' || [Link]() == 'n')
[Link]("Good night");
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
new Program9();
}
}
13
OOPS Using Java Laboratory MMCL206
Output
14
OOPS Using Java Laboratory MMCL206
10. Write a Java program to implement a Queue using user defined
Exception Handling (also make use of throw, throws). a. Complete the
following: b. Create a package named shape. c. Create some classes in the
package representing some common shapes like Square, Triangle, and
Circle. d. Import and compile these classes in other program.
Program ([Link])
package Shape;
public class Rectangle1 {
private double length, breadth;
public void setRectangle(double len, double br) {
length = len;
breadth = br;
}
public void area() {
double area = length * breadth;
[Link]("Area of Rectangle =" + area);
}
}
Program ([Link])
package Shape;
public class Square {
private double side;
public void setSquare(double val) {
side = val;
}
public void area() {
[Link]("Area of Square=" + (side * side));
}
}
15
OOPS Using Java Laboratory MMCL206
Program([Link])
package Shape;
public class Circle1 {
private double rad;
public void setCircle(double radius) {
rad = radius;
}
public void area() {
double area = (0.5) * 3.14 * rad * rad;
[Link]("Area of Rectangle =" + area);
}
}
Program([Link])
import Shape.Rectangle1;
import [Link];
import Shape.Circle1;
public class Program10 {
public static void main(String args[]) {
Rectangle1 rect = new Rectangle1();
[Link](5.6, 6.4);
[Link]();
Square sq = new Square();
[Link](10.5);
[Link]();
Circle1 round = new Circle1();
[Link](5.6);
[Link]();
}
}
16
OOPS Using Java Laboratory MMCL206
To Execute
C:\Users\Yogeesh S\Desktop\Programs>javac -d . [Link]
C:\Users\Yogeesh S\Desktop\Programs>javac -d . [Link]
C:\Users\Yogeesh S\Desktop\Programs>javac -d . [Link]
C:\Users\Yogeesh S\Desktop\Programs>javac [Link]
C:\Users\Yogeesh S\Desktop\Programs>java Program10
Output
Area of Rectangle =35.839999999999996
Area of Square=110.25
Area of Rectangle =49.2352
17