0% found this document useful (0 votes)
14 views42 pages

Javajournal

Uploaded by

Sahim Tambadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views42 pages

Javajournal

Uploaded by

Sahim Tambadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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).

[Link]
import [Link];

public class Add

public static void main(String args[])

int m,n;

Scanner scan = new Scanner([Link]);

[Link] ("Enter the numbers of rows in matrix:");

m = [Link]();

[Link]("Enter the numbers of columns in matrix:");

n = [Link]();

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

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

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

[Link]("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] = [Link]();

CSE, MMEC, Belgavi. Page 1


Name: Mabrura A Sangoli USN: 2MM23CS025

[Link]("");

[Link]("Enter all the elements of second matrix:");

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

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

b[i][j] = [Link]();

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

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

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

[Link]("Matrix after addition:");

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

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

[Link](c[i][j]+" ");

[Link]("");

CSE, MMEC, Belgavi. Page 2


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025>javac [Link]

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.

[Link]
import [Link].*;

public class Stack

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

int size = 3;

void push(int i)

if(top==size-1)

[Link]("Stack Overflow");

else

s[++top]=i;

void pop()

if(top==-1)

[Link]("Stack Underflow");

else

CSE, MMEC, Belgavi. Page 4


Name: Mabrura A Sangoli USN: 2MM23CS025

[Link]("Popped Element="+ s[top]);

top--;

void display()

if(top==-1)

[Link]("Stack is empty\n");

else

[Link]("Stack Elements are:\n");

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

[Link](s[i]);

public static void main(String args[])

Scanner scan = new Scanner([Link]);

Stack stk = new Stack();

for(;;)

[Link]("\n--Stack Operation---");

[Link]("[Link]");

CSE, MMEC, Belgavi. Page 5


Name: Mabrura A Sangoli USN: 2MM23CS025

[Link]("[Link]");

[Link]("[Link]");

[Link]("[Link]");

[Link]("Enter your choice:\n");

int choice = [Link]();

switch(choice)

case 1:

[Link]("Enter the element to push");

[Link]([Link]());

break;

case 2:

[Link]();

break;

case 3:

[Link]();

break;

case 4:

[Link](0);

default :

[Link]("Invalid choice\n");

break;

CSE, MMEC, Belgavi. Page 6


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025>javac [Link]

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

--Stack Operation---

[Link]

[Link]

[Link]

[Link]

Enter your choice:1

Enter the element to push

11

--Stack Operation---

[Link]

[Link]

[Link]

[Link]

Enter your choice:1

Enter the element to push

12

--Stack Operation---

[Link]

[Link]

[Link]

[Link]

Enter your choice:1

Enter the element to push

CSE, MMEC, Belgavi. Page 7


Name: Mabrura A Sangoli USN: 2MM23CS025

13

--Stack Operation---

[Link]

[Link]

[Link]

[Link]

Enter your choice:1

Enter the element to push

14

Stack Overflow

--Stack Operation---

[Link]

[Link]

[Link]

[Link]

Enter your choice:2

Popped Element=13

--Stack Operation---

[Link]

[Link]

[Link]

[Link]

Enter your choice:3

Stack Elements are:

12

11

--Stack Operation---

CSE, MMEC, Belgavi. Page 8


Name: Mabrura A Sangoli USN: 2MM23CS025

[Link]

[Link]

[Link]

[Link]

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.

[Link]
import [Link];

public class Employee

int id;

String name;

double salary;

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

[Link] = name;

[Link] = id;

[Link] = salary;

public void display()

[Link]("Id: " + id);

[Link]("Name: " + name);

[Link]("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);

[Link]();

Scanner scan = new Scanner([Link]);

[Link]("\nEnter the percentage to raise the salary");

p=[Link]();

[Link](p);

[Link]("\nAfter raising salary");

[Link]();

CSE, MMEC, Belgavi. Page 11


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025>javac [Link]

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.

[Link]
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 [Link](xDiff*xDiff + yDiff*yDiff);

public double distance(MyPoint another)

int xDiff = this.x-another.x;

int yDiff = this.y-another.y;

return [Link](xDiff*xDiff + yDiff*yDiff);

public double distance()

return [Link](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);

[Link]("Point 1 coordinates: " +[Link]());

[Link]("Point 2 coordinates: " +[Link]());

[Link]("Distance from Point 1 to (6,8) : " +[Link](6,8));

[Link]("Distance from Point 1 to point 2 : " +[Link](point2));

[Link]("Distance from Point 1 to origin : " +[Link]());

CSE, MMEC, Belgavi. Page 16


Name: Mabrura A Sangoli USN: 2MM23CS025

Output:
C:\Users\LENOVO\Documents\mabrura025>javac [Link]

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.

[Link]
class Shape

protected String name;

public Shape(String name)

[Link]=name;

public void draw()

[Link]("Drawing a" +name);

public void erase()

[Link]("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);

[Link] = radius;

@Override

public void draw()

[Link]("Drawing a circle with radius" +radius);

@Override

public void erase()

[Link]("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);

[Link]=base;

[Link]=height;

@Override

public void draw()

[Link]("Drawing a triangle with base" +base+ "andheight" +height);

@Override

public void erase()

[Link]("Erasing a triangle with base" +base+ "andheight" +height);

class Square extends Shape

private double side;

public Square(String name,double side)

super(name);

[Link]=side;

@Override

public void draw()

CSE, MMEC, Belgavi. Page 20


Name: Mabrura A Sangoli USN: 2MM23CS025

[Link]("Drawing a square with side length" +side);

@Override

public void erase()

[Link]("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)

[Link]();

[Link]();

[Link]();

CSE, MMEC, Belgavi. Page 21


Name: Mabrura A Sangoli USN: 2MM23CS025

Output:
C:\Users\LENOVO\Documents\mabrura025>javac [Link]

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.

[Link]
abstract class Shape

abstract double calculateArea();

abstract double calculatePerimeter();

class Circle extends Shape

private double radius;

public Circle(double radius)

[Link]=radius;

@Override

double calculateArea()

return [Link] * radius * radius;

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

double calculatePerimeter()

return 2 * [Link] * 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 [Link](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);

[Link]("Circle Area: " + [Link]());

[Link]("Circle Perimeter: " + [Link]());

[Link]("\nTriangle Area: " + [Link]());

[Link]("\nTriangle Perimeter: " + [Link]());

CSE, MMEC, Belgavi. Page 25


Name: Mabrura A Sangoli USN: 2MM23CS025

Output:
C:\Users\LENOVO\Documents\mabrura025>javac [Link]

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.

[Link]
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)

[Link] = width;

[Link] = height;

public void resizeWidth(int newWidth)

[Link] = newWidth;

CSE, MMEC, Belgavi. Page 27


Name: Mabrura A Sangoli USN: 2MM23CS025

public void resizeHeight(int newHeight)

[Link] = newHeight;

public void display()

[Link]("Rectangle width: " + width + " , height : " + height);

public static void main(String[] args)

Rectangle rectangle = new Rectangle(5, 10);

[Link]();

[Link](8);

[Link](15);

[Link]();

CSE, MMEC, Belgavi. Page 28


Name: Mabrura A Sangoli USN: 2MM23CS025

Output:
C:\Users\LENOVO\Documents\mabrura025>javac [Link]

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.

[Link]
class Outerclass

void display()

[Link]("Outer Class display() method. ");

class Innerclass

void display()

[Link]("Inner class display() method. ");

public static void main(String[] args)

Outerclass outer = new Outerclass();

[Link]();

[Link] inner = [Link] Innerclass();

[Link]();

CSE, MMEC, Belgavi. Page 30


Name: Mabrura A Sangoli USN: 2MM23CS025

Output:
C:\Users\LENOVO\Documents\mabrura025>javac [Link]

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.

[Link]
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;

[Link]("Result:" +result);

catch(DivisionByZeroException e)

[Link]("Error:" +[Link]());

finally

[Link]("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 [Link]

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 [Link]

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()

[Link]("This is a method from the mypack package!");

[Link]
import [Link];

public class Example

public static void main(String[] args)

MyClass obj = new MyClass();

[Link]();

CSE, MMEC, Belgavi. Page 35


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025\mypack>javac [Link]

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

C:\Users\LENOVO\Documents\mabrura025>javac [Link]

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).

[Link]
public class Mythread implements Runnable

public void run()

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

[Link]([Link]().getName() + " i is " + i);

try

[Link](500);

catch(InterruptedException e)

[Link]([Link]());

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);

[Link]();

[Link]();

[Link]();

CSE, MMEC, Belgavi. Page 38


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025>javac [Link]

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.

[Link]
public class TestMyThread

public static void main(String args[])

new MyThread();

try

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

[Link]("Running main thread: " +k);

[Link](1000);

catch(InterruptedException e)

[Link]("Exiting main thread. . .");

class MyThread extends Thread

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

MyThread()

super("Using Thread class");

[Link]("child Thread: " +this);

start();

public void run()

try

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

[Link]("Child thread" +i);

[Link](500);

catch(InterruptedException e)

[Link]("Exiting child thread. . .");

CSE, MMEC, Belgavi. Page 41


Name: Mabrura A Sangoli USN: 2MM23CS025

Output :
C:\Users\LENOVO\Documents\mabrura025>javac [Link]

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