JAVA MANUAL-1
JAVA MANUAL-1
1.Develop a JAVA program to add TWO matrices of suitable order N (The value of N
should be read from command line arguments).
package sample;
import java.util.Scanner;
class Main
{
public static void main(String a[])
{
int i,j;
Scanner in = new Scanner(System.in);
int N=Integer.parseInt(a[0]) ;
System.out.println("The order of matrices (N): "+N);
System.out.println();
}
System.out.println("Enter the elements of matrix2");
System.out.println();
}
2. Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop a
JAVA main method to illustrate Stack operations.
public class Stack {
private int maxSize;
private int top;
private int[] stackArray;
return value;
} else {
System.out.println("Stack is empty. Cannot pop.");
return -1; // You can choose a different value to represent an empty stack
}
}
Pushed: 30
Pushed: 40
Pushed: 50
Pushed: 60
Pushed: 70
Pushed: 80
Pushed: 90
Pushed: 100
Is the stack empty? false
Is the stack full? true
Popped: 100
Popped: 90
Popped: 80
Popped: 70
Popped: 60
3. A class called Employee, which models an employee with an ID, name and salary, is designed as
shown in the following class diagram. The method raiseSalary (percent) increases the salary by the
given percentage. Develop the Employee class and suitable main method for demonstration.
public class Employee {
// Instance variables
private int id;
private String name;
private double salary;
// Constructor
public Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
// Getter methods
public int getId() {
return id;
}
ID: 101
Name: John Doe
Salary: $55000.0
4. Develop a JAVA program to create a class named shape. Create three sub classes namely: circle,
triangle and square, each class has two member functions named draw () and erase (). Demonstrate
polymorphism concepts by developing suitable methods, defining member data and main program.
class Shape {
// Member functions
public void draw() {
System.out.println("Drawing a shape");
}
// Circle class
class Circle extends Shape {
// Override draw method
@Override
public void draw() {
System.out.println("Drawing a circle");
}
// Triangle class
class Triangle extends Shape {
// Override draw method
@Override
public void draw() {
System.out.println("Drawing a triangle");
}
// Square class
class Square extends Shape {
// Override draw method
@Override
public void draw() {
System.out.println("Drawing a square");
}
// Main program
public class lab {
public static void main(String[] args) {
// Creating objects of different shapes
Shape circle = new Circle();
Shape triangle = new Triangle();
Shape square = new Square();
// Demonstrating polymorphism
// Calls to overridden draw and erase methods
circle.draw();
circle.erase();
triangle.draw();
triangle.erase();
square.draw();
square.erase();
}
}
5. Develop a JAVA program to create an interface Resizable with methods resizeWidth(int width) and
resizeHeight(int height) that allow an object to be resized. Create a class Rectangle that implements
the Resizable interface and implements the resize methods
// Implement the Resizable interface in the Rectangle class
class Rectangle implements Resizable {
private int width;
private int height;
this.width = width;
}
OUTPUT:
Initial Width: 10
Initial Height: 5
Resized Width: 15
Resized Height: 8
6. Develop a JAVA program to create an outer class with a function display. Create another class
inside the outer class named inner with a function called display and call the two functions in the
main class.
// Outer class
class Outer {
void display() {
System.out.println("Outer class display");
}
// Inner class
class Inner {
void display() {
System.out.println("Inner class display");
}
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Create an instance of the outer class
Outer outerObj = new Outer();
// Create an instance of the inner class using the outer class instance
Outer.Inner innerObj = outerObj.new Inner();
7. Develop a JAVA program to raise a custom exception (user defined exception) for DivisionByZero
using try, catch, throw and finally.
// Custom exception class
class DivisionByZeroException extends Exception {
8. Develop a JAVA program to create a package named mypack and import & implement it in a
suitable class.
9. Write a program to illustrate creation of threads using runnable class. (start method start each of
the newly created thread. Inside the run method there is sleep() for suspend the thread for 500
milliseconds).
}
}
}
10. Develop a program to create a class MyThread in this class a constructor, call the base class
constructor, using super and start the thread. The run method of the class starts after this. It can be
observed that both main thread and created child thread are executed concurrently.