3rd sem aiml notes
3rd sem aiml notes
Course Objectives:
1. To learn primitive constructs JAVA programming language.
2. To understand Object Oriented Programming Features of JAVA.
3. To gain knowledge on: packages, multithreaded programing and exceptions.
Course Outcomes
1. Demonstrate proficiency in writing simple programs involving branching and looping structures.
2. Design a class involving data members and methods for the given scenario.
3. Apply the concepts of inheritance and interfaces in solving real world problems.
4. Use the concept of packages and exception handling in solving complex problem
5. Apply concepts of multithreading, autoboxing and enumerations in program development
Laboratory Requirement
Software Requirement:
• Operating System : Windows- 10/8/7(64 Bit)
• JDK 1.9
Sl.no Progams
Develop a JAVA program to add TWO matrices of suitable order N (The value of N should be
1
read from command line arguments).
Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop a
2
JAVA main method to illustrate Stack operations.
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.
1. A class called MyPoint, which models a 2D point with x and y coordinates, is designed as
4
follows:
● Two instance variables x (int) and y (int).
● A default (or "no-arg") constructor that construct a point at the default location of (0, 0).
● A overloaded constructor that constructs a point with the given x and y coordinates.
● A method setXY() to set both x and y.
● A method getXY() which returns the x and y in a 2-element int array.
● A toString() method that returns a string description of the instance in the format "(x, y)".
● A method called distance(int x, int y) that returns the distance from this point to another point
at the
given (x, y) coordinates
● An overloaded distance(MyPoint another) that returns the distance from this point to the
given
MyPoint instance (called another)
● Another overloaded distance() method that returns the distance from this point to the origin
(0,0)
Develop the code for the class MyPoint. Also develop a JAVA program (called TestMyPoint)
to test all the methods defined in the class.
Develop a JAVA program to create a class named shape. Create three sub classes namely:
5
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.
10 Develop a JAVA program to create a package named mypack and import & implement it in a
suitable class.
Write a program to illustrate creation of threads using runnable class. (start method start each of
11
the newly created thread. Inside the run method there is sleep() for suspend the thread for 500
milliseconds)
12 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.
}
}
System.out.println("Matrix after addition:");
for (int i = 0; i < p; i++)
{
2. Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop a JAVA
main method to illustrate Stack operations.
class Stack {
int[] stck = new int[10];
int tos;
// Initialize top-of-stack
Stack() {
tos = -1;
}
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.
class Employee
{
int id;
}
void printdetails()
{
System.out.println("id is "+id);
System.out.println("name is "+name);
System.out.println("the salary is "+salary);
}
}
}
Output:
5. 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 {
public void draw() {
System.out.println("Drawing a shape");
}
@Override
public void erase() {
System.out.println("Erasing the square");
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Shape();
Circle circle = new Circle();
Triangle triangle = new Triangle();
Square square = new Square();
double calculateArea() {
return Math.PI * dim1 * dim1;
}
double calculatePerimeter() {
return 2 * Math.PI * dim1;
}
}
double calculatePerimeter() {
return dim1 + dim2 + Math.sqrt(dim1 * dim1 + dim2 * dim2);
}
}
7. 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
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
@Override
public void resizeWidth(int width) {
this.width = width;
}
@Override
public void resizeHeight(int height) {
this.height = height;
}
rectangle.resizeWidth(15);
System.out.println("\nResized Dimensions:");
rectangle.display();
}
}
Output:
8. 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.
class OuterClass {
void display() {
System.out.println("OuterClass - Display method");
class Inner {
void display() {
System.out.println("Inner - Display method");
}
}
}
9. Develop a JAVA program to raise a custom exception (user defined exception) for
DivisionByZero using try, catch, throw and finally.
import java.util.Scanner;
class DivisionByZeroExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter numerator: ");
int numerator = scanner.nextInt();
if (denominator == 0) {
throw new RuntimeException("Division by zero is not allowed.");
}
} catch (RuntimeException e) {
System.err.println("Error: " + e.getMessage());
} finally {
System.out.println("Finally block executed.");
scanner.close();
}
}
}
10. Develop a JAVA program to create a package named mypack and import & implement it in a
suitable class.
package mypack;
11. 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).
class MyRunnable implements Runnable
{
public void run() {
try {
System.out.println(Thread.currentThread().getName() +
" is running.");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "
is finished.");
}
}
class RunnableExample {
public static void main(String[] args) {
MyRunnable myRunnable1 = new MyRunnable();
MyRunnable myRunnable2 = new MyRunnable();
thread1.start();
thread2.start();
12. 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. \
class MyThread extends Thread {
public MyThread(String threadName) {
super(threadName);
}
class ThreadExample {
public static void main(String[] args) {
new MyThread("ChildThread").start();