0% found this document useful (0 votes)
4 views

javajournal

Uploaded by

Sahim Tambadi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

javajournal

Uploaded by

Sahim Tambadi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 01

Develop a JAVA program to add two matrices of suitable order N (The


value of N should be read from arguments).

Add.java
import java.util.Scanner;

public class Add

public static void main(String args[])

int m,n;

Scanner scan = new Scanner(System.in);

System.out.print ("Enter the numbers of rows in matrix:");

m = scan.nextInt();

System.out.print("Enter the numbers of columns in matrix:");

n = scan.nextInt();

int a[][] = new int[m][n];

int b[][] = new int[m][n];

int c[][] = new int[m][n];

System.out.println("Enter all the elements of the first matrix:");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

a[i][j] = scan.nextInt();

CSE, MMEC, Belgavi. Page 1


Name: Mabrura A Sangoli USN: 2MM23CS025

System.out.println("");

System.out.println("Enter all the elements of second matrix:");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

b[i][j] = scan.nextInt();

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

c[i][j] = a[i][j] + b[i][j];

System.out.println("Matrix after addition:");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

System.out.print(c[i][j]+" ");

System.out.println("");

CSE, MMEC, Belgavi. Page 2


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025>javac Add.java

C:\Users\LENOVO\Documents\mabrura025>java Add

Enter the numbers of rows in matrix: 2

Enter the numbers of columns in matrix: 2

Enter all the elements of the first matrix:

12

34

Enter all the elements of second matrix:

56

78

Matrix after addition:

68

10 12

CSE, MMEC, Belgavi. Page 3


Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 02

Develop a stack class to hold a maximum of 10 integers with suitable


method. Develop a JAVA main to illustrate stack operations.

Stack.java
import java.util.*;

public class Stack

int s[] = new int[10]; int top = -1;

int size = 3;

void push(int i)

if(top==size-1)

System.out.println("Stack Overflow");

else

s[++top]=i;

void pop()

if(top==-1)

System.out.println("Stack Underflow");

else

CSE, MMEC, Belgavi. Page 4


Name: Mabrura A Sangoli USN: 2MM23CS025

System.out.println("Popped Element="+ s[top]);

top--;

void display()

if(top==-1)

System.out.println("Stack is empty\n");

else

System.out.println("Stack Elements are:\n");

for(int i=top;i>=0;i--)

System.out.println(s[i]);

public static void main(String args[])

Scanner scan = new Scanner(System.in);

Stack stk = new Stack();

for(;;)

System.out.println("\n--Stack Operation---");

System.out.println("1.Push");

CSE, MMEC, Belgavi. Page 5


Name: Mabrura A Sangoli USN: 2MM23CS025

System.out.println("2.Pop");

System.out.println("3.Display");

System.out.println("4.Exit");

System.out.println("Enter your choice:\n");

int choice = scan.nextInt();

switch(choice)

case 1:

System.out.println("Enter the element to push");

stk.push(scan.nextInt());

break;

case 2:

stk.pop();

break;

case 3:

stk.display();

break;

case 4:

System.exit(0);

default :

System.out.println("Invalid choice\n");

break;

CSE, MMEC, Belgavi. Page 6


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025>javac Stack.java

C:\Users\LENOVO\Documents\mabrura025>java Stack

--Stack Operation---

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:1

Enter the element to push

11

--Stack Operation---

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:1

Enter the element to push

12

--Stack Operation---

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:1

Enter the element to push

CSE, MMEC, Belgavi. Page 7


Name: Mabrura A Sangoli USN: 2MM23CS025

13

--Stack Operation---

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:1

Enter the element to push

14

Stack Overflow

--Stack Operation---

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:2

Popped Element=13

--Stack Operation---

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:3

Stack Elements are:

12

11

--Stack Operation---

CSE, MMEC, Belgavi. Page 8


Name: Mabrura A Sangoli USN: 2MM23CS025

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:5

Invalid c

CSE, MMEC, Belgavi. Page 9


Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 03

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.

Employee.java
import java.util.Scanner;

public class Employee

int id;

String name;

double salary;

public Employee(int id, String name, double salary)

this.name = name;

this.id = id;

this.salary = salary;

public void display()

System.out.println("Id: " + id);

System.out.println("Name: " + name);

System.out.println("Salary: " + salary);

public void raiseSalary(double percentage)

{
CSE, MMEC, Belgavi. Page 10
Name: Mabrura A Sangoli USN: 2MM23CS025

salary = salary+(salary*percentage/100);

public static void main(String[] args)

int p;

Employee e1=new Employee(8,"Rakesh",2500);

e1.display();

Scanner scan = new Scanner(System.in);

System.out.println("\nEnter the percentage to raise the salary");

p=scan.nextInt();

e1.raiseSalary(p);

System.out.println("\nAfter raising salary");

e1.display();

CSE, MMEC, Belgavi. Page 11


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025>javac Employee.java

C:\Users\LENOVO\Documents\mabrura025>java Employee

Id: 8

Name: Rakesh

Salary: 2500.0

Enter the percentage to raise the salary

12

After raising salary

Id: 8

Name: Rakesh

Salary: 2800.0

CSE, MMEC, Belgavi. Page 12


Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 04

A class called MyPoint, which models a 2D points with X and Y


coordinates, is designed as 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 overload constructor that construct a point with 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 overload distance (MyPoint another) that returns the distance
from this point to the given MyPoint instance (called another).
 Another Overload 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.

MyPoint.java
public class MyPoint

private int x = 0;

private int y = 0;

public MyPoint()

this.x = 0;

this.y = 0;

CSE, MMEC, Belgavi. Page 13


Name: Mabrura A Sangoli USN: 2MM23CS025

public MyPoint(int x,int y)

this.x = x;

this.y = y;

public double distance(int x,int y)

int xDiff = this.x-x;

int yDiff = this.y-y;

return Math.sqrt(xDiff*xDiff + yDiff*yDiff);

public double distance(MyPoint another)

int xDiff = this.x-another.x;

int yDiff = this.y-another.y;

return Math.sqrt(xDiff*xDiff + yDiff*yDiff);

public double distance()

return Math.sqrt(x*x + y*y);

CSE, MMEC, Belgavi. Page 14


Name: Mabrura A Sangoli USN: 2MM23CS025

public int getX()

return x;

public void setX(int x)

this.x = x;

public int getY()

return y;

public void setY(int y)

this.y = y;

public void setXY(int x,int y)

this.x = x;

this.y = y;

public String toString()

CSE, MMEC, Belgavi. Page 15


Name: Mabrura A Sangoli USN: 2MM23CS025

return "("+ x +","+ y +")";

public static void main(String[] args)

MyPoint point1 = new MyPoint(3,4);

MyPoint point2 = new MyPoint(6,8);

System.out.println("Point 1 coordinates: " +point1.toString());

System.out.println("Point 2 coordinates: " +point2.toString());

System.out.println("Distance from Point 1 to (6,8) : " +point1.distance(6,8));

System.out.println("Distance from Point 1 to point 2 : " +point1.distance(point2));

System.out.println("Distance from Point 1 to origin : " +point1.distance());

CSE, MMEC, Belgavi. Page 16


Name: Mabrura A Sangoli USN: 2MM23CS025

Output:
C:\Users\LENOVO\Documents\mabrura025>javac MyPoint.java

C:\Users\LENOVO\Documents\mabrura025>java MyPoint

Point 1 coordinates: (3,4)

Point 2 coordinates: (6,8)

Distance from Point 1 to (6,8) : 5.0

Distance from Point 1 to point 2 : 5.0

Distance from Point 1 to origin : 5.0

CSE, MMEC, Belgavi. Page 17


Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 05

Inheritance and Polymorphism- Shape class

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.

ShapeDemo.java
class Shape

protected String name;

public Shape(String name)

this.name=name;

public void draw()

System.out.println("Drawing a" +name);

public void erase()

System.out.println("Erasing a" +name);

class Circle extends Shape


CSE, MMEC, Belgavi. Page 18
Name: Mabrura A Sangoli USN: 2MM23CS025

private double radius;

public Circle(String name, double radius)

super(name);

this.radius = radius;

@Override

public void draw()

System.out.println("Drawing a circle with radius" +radius);

@Override

public void erase()

System.out.println("Erasing a circle with radius" +radius);

class Triangle extends Shape

private double base;

private double height;

public Triangle(String name,double base,double height)

CSE, MMEC, Belgavi. Page 19


Name: Mabrura A Sangoli USN: 2MM23CS025

super(name);

this.base=base;

this.height=height;

@Override

public void draw()

System.out.println("Drawing a triangle with base" +base+ "andheight" +height);

@Override

public void erase()

System.out.println("Erasing a triangle with base" +base+ "andheight" +height);

class Square extends Shape

private double side;

public Square(String name,double side)

super(name);

this.side=side;

@Override

public void draw()

CSE, MMEC, Belgavi. Page 20


Name: Mabrura A Sangoli USN: 2MM23CS025

System.out.println("Drawing a square with side length" +side);

@Override

public void erase()

System.out.println("Erasing a square with side length" +side);

public class ShapeDemo

public static void main(String[] args)

Shape[] shapes=new Shape[3];

shapes[0]=new Circle("Circle",5.0);

shapes[1]=new Triangle("Triangle",4.0,6.0);

shapes[2]=new Square("Square",3.0);

for(Shape shape:shapes)

shape.draw();

shape.erase();

System.out.println();

CSE, MMEC, Belgavi. Page 21


Name: Mabrura A Sangoli USN: 2MM23CS025

Output:
C:\Users\LENOVO\Documents\mabrura025>javac ShapeDemo.java

C:\Users\LENOVO\Documents\mabrura025>java ShapeDemo

Drawing a circle with radius 5.0

Erasing a circle with radius 5.0

Drawing a triangle with base4.0andheight 6.0

Erasing a triangle with base4.0andheight 6.0

Drawing a square with side length 3.0

Erasing a square with side length 3.0

CSE, MMEC, Belgavi. Page 22


Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 06

Develop a JAVA program to create an abstract class Shape with abstract


methods calculateArea() and calculatePerimeter(). Create subclass Circle
and Triangle that extend the shape class and implemented the respective
method to calculate the area and perimeter of each shape.

ShapeDemo1.java
abstract class Shape

abstract double calculateArea();

abstract double calculatePerimeter();

class Circle extends Shape

private double radius;

public Circle(double radius)

this.radius=radius;

@Override

double calculateArea()

return Math.PI * radius * radius;

@Override
CSE, MMEC, Belgavi. Page 23
Name: Mabrura A Sangoli USN: 2MM23CS025

double calculatePerimeter()

return 2 * Math.PI * radius;

class Triangle extends Shape

private double side1;

private double side2;

private double side3;

public Triangle(double side1, double side2, double side3)

this.side1=side1;

this.side2=side2;

this.side3=side3;

@Override

double calculateArea()

double s = (side1 + side2 + side3) /2;

return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

CSE, MMEC, Belgavi. Page 24


Name: Mabrura A Sangoli USN: 2MM23CS025

@Override

double calculatePerimeter()

return side1 + side2 + side3;

public class ShapeDemo1

public static void main(String[] args)

Circle circle = new Circle(5.0);

Triangle triangle = new Triangle(3.0, 4.0, 5.0);

System.out.println("Circle Area: " + circle.calculateArea());

System.out.println("Circle Perimeter: " + circle.calculatePerimeter());

System.out.println("\nTriangle Area: " + triangle.calculateArea());

System.out.println("\nTriangle Perimeter: " + triangle.calculatePerimeter());

CSE, MMEC, Belgavi. Page 25


Name: Mabrura A Sangoli USN: 2MM23CS025

Output:
C:\Users\LENOVO\Documents\mabrura025>javac ShapeDemo1.java

C:\Users\LENOVO\Documents\mabrura025>java ShapeDemo1

Circle Area: 78.53981633974483

Circle Perimeter: 31.41592653589793

Triangle Area: 6.0

Triangle Perimeter: 12.0

CSE, MMEC, Belgavi. Page 26


Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 07

Develop a JAVA program to create an interface Reziable with methods


resizeWidth(int width) and resizeHeight(int height) that allow on object to
be resized. Create a class Rectangle that implements the Resizable interface
and implements the resize methods.

Rectangle.java
interface Resizable

void resizeWidth(int width);

void resizeHeight(int height);

class Rectangle implements Resizable

private int width;

private int height;

public Rectangle(int width, int height)

this.width = width;

this.height = height;

public void resizeWidth(int newWidth)

this.width = newWidth;

CSE, MMEC, Belgavi. Page 27


Name: Mabrura A Sangoli USN: 2MM23CS025

public void resizeHeight(int newHeight)

this.height = newHeight;

public void display()

System.out.println("Rectangle width: " + width + " , height : " + height);

public static void main(String[] args)

Rectangle rectangle = new Rectangle(5, 10);

rectangle.display();

rectangle.resizeWidth(8);

rectangle.resizeHeight(15);

rectangle.display();

CSE, MMEC, Belgavi. Page 28


Name: Mabrura A Sangoli USN: 2MM23CS025

Output:
C:\Users\LENOVO\Documents\mabrura025>javac Rectangle.java

C:\Users\LENOVO\Documents\mabrura025>java Rectangle

Rectangle width: 5 , height : 10

Rectangle width: 8 , height : 15

CSE, MMEC, Belgavi. Page 29


Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 08

Developing 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 function in the main class.

Outerclass.java
class Outerclass

void display()

System.out.println("Outer Class display() method. ");

class Innerclass

void display()

System.out.println("Inner class display() method. ");

public static void main(String[] args)

Outerclass outer = new Outerclass();

outer.display();

Outerclass.Innerclass inner = outer.new Innerclass();

inner.display();

CSE, MMEC, Belgavi. Page 30


Name: Mabrura A Sangoli USN: 2MM23CS025

Output:
C:\Users\LENOVO\Documents\mabrura025>javac Outerclass.java

C:\Users\LENOVO\Documents\mabrura025>java Outerclass

Outer Class display() method.

Inner class display() method.

CSE, MMEC, Belgavi. Page 31


Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 09

Develop a JAVA program to raise a custom Exception(user defined


exception for DivisionByZero using try, catch, throw and finally.

CustomDivision.java
public class CustomDivision

public static void main(String[] args)

int numerator = 10;

int denominator = 2;

try

if(denominator == 0)

throw new DivisionByZeroException("Division by zero is not allowed!");

int result = numerator / denominator;

System.out.println("Result:" +result);

catch(DivisionByZeroException e)

System.out.println("Error:" +e.getMessage());

finally

System.out.println("This block always executes, regardless of exceptions. ");

CSE, MMEC, Belgavi. Page 32


Name: Mabrura A Sangoli USN: 2MM23CS025

class DivisionByZeroException extends Exception

public DivisionByZeroException(String message)

super(message);

CSE, MMEC, Belgavi. Page 33


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025>javac CustomDivision.java

C:\Users\LENOVO\Documents\mabrura025>java CustomDivision

Error:Division by zero is not allowed!

This block always executes, regardless of exceptions.

C:\Users\LENOVO\Documents\mabrura025>javac CustomDivision.java

C:\Users\LENOVO\Documents\mabrura025>java CustomDivision

Result:5

This block always executes, regardless of exceptions.

CSE, MMEC, Belgavi. Page 34


Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 10

Develop a JAVA program to create a package named mypack and import


and implement it in a suitable class.

Package : mypack

Class: MyClass
package mypack;

public class MyClass

public void display()

System.out.println("This is a method from the mypack package!");

Example.java
import mypack.MyClass;

public class Example

public static void main(String[] args)

MyClass obj = new MyClass();

obj.display();

CSE, MMEC, Belgavi. Page 35


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025\mypack>javac MyClass.java

C:\Users\LENOVO\Documents\mabrura025\mypack>cd ..

C:\Users\LENOVO\Documents\mabrura025>javac Example.java

C:\Users\LENOVO\Documents\mabrura025>java Example

This is a method from the mypack package!

CSE, MMEC, Belgavi. Page 36


Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 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).

Mythread.java
public class Mythread implements Runnable

public void run()

for(int i=1;i<=5;i++)

System.out.println(Thread.currentThread().getName() + " i is " + i);

try

Thread.sleep(500);

catch(InterruptedException e)

System.out.println(e.getMessage());

public static void main(String[] args)

Mythread myThread = new Mythread();

Thread t1 = new Thread(myThread);

Thread t2 = new Thread(myThread);

CSE, MMEC, Belgavi. Page 37


Name: Mabrura A Sangoli USN: 2MM23CS025

Thread t3 = new Thread(myThread);

t1.start();

t2.start();

t3.start();

CSE, MMEC, Belgavi. Page 38


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025>javac Mythread.java

C:\Users\LENOVO\Documents\mabrura025>java Mythread

Thread-2 i is 1

Thread-0 i is 1

Thread-1 i is 1

Thread-0 i is 2

Thread-1 i is 2

Thread-2 i is 2

Thread-2 i is 3

Thread-1 i is 3

Thread-0 i is 3

Thread-1 i is 4

Thread-0 i is 4

Thread-2 i is 4

Thread-1 i is 5

Thread-2 i is 5

Thread-0 i is 5

CSE, MMEC, Belgavi. Page 39


Name: Mabrura A Sangoli USN: 2MM23CS025

Program No: 12

Develop a program to create a class MyThread in the class a constructor,


call the base class constructor, using super and start the thread. The run
method of the class start after this. It can be observed that both main
thread and created child thread are executed concurrently.

TestMyThread.java
public class TestMyThread

public static void main(String args[])

new MyThread();

try

for(int k=5;k>0;k--)

System.out.println("Running main thread: " +k);

Thread.sleep(1000);

catch(InterruptedException e)

System.out.println("Exiting main thread. . .");

class MyThread extends Thread

{
CSE, MMEC, Belgavi. Page 40
Name: Mabrura A Sangoli USN: 2MM23CS025

MyThread()

super("Using Thread class");

System.out.println("child Thread: " +this);

start();

public void run()

try

for(int i=5;i>0;i--)

System.out.println("Child thread" +i);

Thread.sleep(500);

catch(InterruptedException e)

System.out.println("Exiting child thread. . .");

CSE, MMEC, Belgavi. Page 41


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025>javac TestMyThread.java

C:\Users\LENOVO\Documents\mabrura025>java TestMyThread

child Thread: Thread[#21,Using Thread class,5,main]

Child thread5

Running main thread: 5

Child thread4

Running main thread: 4

Child thread3

Child thread2

Running main thread: 3

Child thread1

Exiting child thread. . .

Running main thread: 2

Running main thread: 1

Exiting main thread. . .

CSE, MMEC, Belgavi. Page 42

You might also like