Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Java Report 2

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

Java Report 2

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

K.V.G.

COLLEGE OF ENGINEERING
SULLIA, D.K. – 574327, KARNATAKA

LABORATOTY REPORT
ON
OBJECT ORIENTED PROGRAMMING WITH JAVA (BCS306)
Submitted by
Name:Lismitha CP
USN:4KV23CI026
SEMESTER:3RD

Submitted to
PROF.SINDHU V
Professor, Dept. of CS &Engg.

Matrix multiplication

Devlop a Java program to add 2 matrices of suitable order N (The value of N


should be read from command line arguments).
package matrix;

import java.util.Scanner;

public class Matrix

public static void main(String[] args)

{
int m,n;

Scanner scan=new Scanner(System.in);

System.out.println("enter the mumber of rows in the matrix:");

m=scan.nextInt();

System.out.println("enter the mumber of columns in the 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 the all elements the matrix:");

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

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

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

System.out.println("");

System.out.println("enter 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.println(c[i][j]+"");

System.out.println("");

}
Stack demo

Develop a stack class to hold maximumof 10 integer with suitable


method. Develop a Java main method to illustrate stack operation.
package stackdemo;

import java.util.Scanner;

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

System.out.println("Popped elements="+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 class Stackdemo {

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

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

}
Employee demo

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.
import java.util.*;

class Employee{

private int id;

private String name;

private double salary;

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

this.id=id;

this.name=name;

this.salary=salary;

public void raiseSalary(double percent){

if(percent>0){

double raiseAmount=salary*(percent/100);

salary+=raiseAmount;

System.out.println(name+"s salary raised by"+percent+"%.New Salary:"+salary);

}else{

System.out.println("Invalid percentage.Salary remains unchanged");


}

public String toString(){

return "Employee ID:"+id+"\nName:"+name+"\nSalary:"+salary;

public class Employeedemo

public static void main(String[]args){

Scanner sc=new Scanner(System.in);

Employee emp=new Employee(1,"John doe",50000.0);

System.out.println("Initial employee details:");

System.out.println(emp);

System.out.println("Enter the percentage to raise the salary:");

double percent=sc.nextDouble();

emp.raiseSalary(percent);

System.out.println("\nEmployee deatails after salary raise:");

System.out.println(emp);

}
Point demo

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 overloaded constructor that constructs a point with the given x and y


coodinates.

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

· An overloaded distance(My-Point another) that returns the distance from


this point to the given MyPoint instance(called anoter).

· 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 Test
MyPoint) to test all the Methods defined in the class.
package pointdemo;

class MyPoints{

int x;

int y;

public MyPoints()

this.x=0;

this.y=0;

public MyPoints(int m,int n)

this.x=m;

this.y=n;

public void setXY(int m,int n)

{
this.x=m;

this.y=n;

public int[] getXY()

int[] coordinates={this.x,this.y};

return coordinates;

public String toString()

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

public double distance(int m,int n)

int Xdiff=this.x-m;

int Ydiff=this.y-n;

return Math.sqrt(Xdiff*Xdiff+Ydiff*Ydiff);

public double distance(MyPoints another)

return distance(another.x,another.y);

public double distance()

{
return distance(0,0);

public class PointDemo{

public static void main(String[] args) {

MyPoints point1=new MyPoints();

MyPoints point2=new MyPoints(6,8);

System.out.println("point1:"+point1);

System.out.println("point2:"+point2);

point1.setXY(3,4);

System.out.println("Point1 after setXY:" + point1);

int[] coordinates=point2.getXY();

System.out.println("coordinates of point 2:("+ coordinates[0]+"," +coordinates[1] +")");

System.out.println("Distance between "+point1+" and(0,0):"+point1.distance());

System.out.println("Distance between "+point1+" and " +point2+ ": "


+point1.distance(point2));

System.out.println("Distance between "+point1+" and another point(6,8):


"+point1.distance(6,8));

You might also like