0% found this document useful (0 votes)
3 views24 pages

Java 44

The document outlines a series of Java programming lab exercises for students at G. Narayanamma Institute of Technology and Science. It includes tasks such as solving quadratic equations, checking for palindromes, matrix multiplication, finding prime numbers, and demonstrating concepts like method overloading, overriding, abstract classes, and multiple inheritance. Each exercise is accompanied by source code and sample outputs to illustrate the expected results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views24 pages

Java 44

The document outlines a series of Java programming lab exercises for students at G. Narayanamma Institute of Technology and Science. It includes tasks such as solving quadratic equations, checking for palindromes, matrix multiplication, finding prime numbers, and demonstrating concepts like method overloading, overriding, abstract classes, and multiple inheritance. Each exercise is accompanied by source code and sample outputs to illustrate the expected results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

G.

Narayanamma Institute of Technology and Science


(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Week 1:

a)Write a Java program that prints all real solutions to the quadratic equation ax2 +bx+c=0.
Read in a, b, c and use the quadratic formula. If the discriminant b2 -4ac is negative, display
a message stating that there are no real solutions.

Source Code:
import [Link];
class quadratic
{
public static void main(String args[])
{
Scanner s=new Scanner([Link]);
double a=[Link]();
double b=[Link]();
double c=[Link]();
double d=b*b-4*a*c;
if(d>0)
{
double x=(-b/(2*a))+[Link](d)/(2*a);
double y=(-b/(2*a))-[Link](d)/(2*a);
[Link]("Real and Unequal");
[Link](x);
[Link](y);
}
else if(d==0)
{
double x1=(-b/(2*a));
double y1=(-b/(2*a));
[Link]("Real and Equal");
[Link](x1);
[Link](y1);
}
else
{
double x3=(-b/2*a);
double y3=[Link](d)/(2*a);
[Link]("Not Real");
[Link](x3+"+i"+y3);
[Link](x3+"-i"+y3);
}
}
}

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Output:
D:\22251A6728>java quadratic
10
9
1
Real and Unequal
-0.12984378812835756
-0.7701562118716425
D:\22251A6728>javac [Link]

D:\22251A6728>java quadratic
1
-8
16
Real and Equal
4.0
4.0
D:\22251A6728>javac [Link]

D:\22251A6728>java quadratic
1
-8
16
Real and Equal
4.0
4.0

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Week 2:

a)Write a Java program that checks whether the given string is a palindrome or not.

Source Code:
import [Link];
class palindrome
{
public static void main(String args[])
{
String str,rev="";
Scanner sc=new Scanner([Link]);
[Link]("Enter string: ");
str=[Link]();
int length=[Link]();
for(int i=length-1;i>=0;i--)
rev=rev+[Link](i);
if([Link](rev))
[Link](str+ "is a palindrome");
else
[Link](str+ "is not a palindrome");
}
}

Output:
D:\22251A6728>javac [Link]

D:\22251A6728>java palindrome
Enter string:
madam
madam is a palindrome

D:\22251A6728>java palindrome
Enter string:
java
java is not a palindrome

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

b) Write a Java program to multiply given 3X3 matrices.

Source Code:
import [Link];
public class matrix
{
public static void main(String args[])
{
int n;
Scanner sc = new Scanner([Link]);
[Link]("Enter base of matrices");
n = [Link]();
int[][] m1 = new int[n][n];
int[][] m2 = new int[n][n];
int[][] mat = new int[n][n];
[Link]("Enter the elements of 1st matrix row wise : ");
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
m1[i][j] = [Link]();
}
}
[Link]("Enter the elements of 2nd matrix row wise : ");
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
m2[i][j] = [Link]();
}
}
[Link]("Multiplying the matrices : ");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
for(int k = 0; k < n; k++)
{
mat[i][j] = mat[i][j] + m1[i][k] * m2[k][j];
}
}

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

}
[Link]("Product :");
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
[Link](mat[i][j] + " ");
}
[Link]();
}
}
}

Output:
D:\22251A6728>javac [Link]

D:\22251A6728>java matrix
Enter base of matrices
3
Enter the elements of 1st matrix row wise :
123
456
234
Enter the elements of 2nd matrix row wise :
123
456
234
Multiplying the matrices :
Product :
15 21 27

36 51 66

22 31 40

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Week 3:
a)Write a Java program that accepts a number from the end-user and then prints all prime
numbers up to a given number.

Source Code:
import [Link].*;
class PrimeNum
{
public static void main(String args[])
{

Scanner sc=new Scanner([Link]);


int n=[Link]();
for(int i=1;i<=n;i++){
int c=0;
for(int j=1;j<=i;j++){
if(i%j==0){
c+=1;
}
}
if(c==2)
{
[Link](i);
}
}
}
}

Output:
D:\22251A6728>javac [Link]

D:\22251A6728>java PrimeNum
10
2
3
5
7

D:\22251A6728>java PrimeNum
22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24
G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

20
2
3
5
7
11
13
17
19

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

b) Write a Java program to create a Box class with properties like length, breadth, width,
and volume. Display the volume of 2 different boxes by using objects.

Source Code:
class Box
{
int l;
int b;
int h;
void volume(int l,int b,int h)
{
[Link]("volume of box"+(l*b*h));
}
public static void main(String args[])
{
Box b1=new Box();
Box b2=new Box();
[Link](2,3,4);
[Link](1,2,3);
}
}

Output:
D:\22251A6728>javac [Link]

D:\22251A6728>java Box
volume of box24
volume of box6

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Week 4:
a. Write a Java program that demonstrates constructor overloading.

Source Code:
import [Link].*;

class Box {

int length;

int breadth;

int height;

Box(){

length=4;

breadth=5;

height=2;

Box(int x){

length=breadth=height=x;

Box(int l,int b,int h){

length=l;

breadth=b;

height=h;

void volume(){

[Link]("Volume of the box: "+(length*breadth*height));

public static void main(String[] args) {

//object creation

Box b1=new Box();

Box b2=new Box(5);

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Box b3=new Box(2,3,4);

[Link]();

[Link]();

[Link]();

Output:

D:\22251A6728>javac [Link]

D:\22251A6728>java Box
Volume of the box: 40
Volume of the box: 125
Volume of the box: 24

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

b. Write a Java program to implement the use of inner classes.

Source Code:

import [Link].*;

class Outer{

private int i=5;

class Inner

void print()

[Link]("Inner class method");

[Link]("i= "+i);

void display(){

[Link]("This is outer class method");

public static void main(String[] args){

Outer ot=new Outer();

[Link] in=[Link] Inner();

[Link]();

[Link]();

Output:
D:\22251A6728>javac [Link]
D:\22251A6728>java Outer
Inner class method
i= 5
22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24
G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

This is outer class method

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Week 5:

a. Write a Java program that demonstrates the following:


i. Method overloading

Source Code:

class Method_overload{

void sum(int a ,int b){

[Link]("sum="+(a+b));

void sum(float a,float b){

[Link]("sum="+(a+b));

public static void main(String[] args){

Method_overload obj=new Method_overload();

[Link](2,3);

[Link](4.6f,3.4f);

Output:

D:\22251A6728>javac Method_overload.java

D:\22251A6728>java Method_overload

sum=5

sum=8.0

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

ii. Method overriding.

Source Code:

class A{

void display(){

[Link]("class A display method");

class B extends A

void display(){

[Link]("class B display method");

class Method_overriding

public static void main(String[] args){

A obj=new B();

[Link]();

Output:

D:\22251A6728>javac Method_overriding.java

D:\22251A6728>java Method_overriding

class B display method

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

b. Write a Java program to create an abstract class named ‘Shape’ that contains two integers and an
empty method named printArea( ). Provide three classes named Rectangle, Triangle and Circle such
that each one of the classes extends the class Shape. Each one of the classes contains only the
method printArea( ) that prints the area of the given shape.

Source Code:

abstract class Shape{

int x,y;

abstract void printArea(int x,int y);

class Rectangle extends Shape{

public void printArea(int p,int q){

[Link]("Area of Rectangle:"+(p*q));

class Circle extends Shape{

public void printArea(int p,int q){

[Link]("Area of Circle:"+(3.14*p*p));

class Triangle extends Shape{

public void printArea(int p,int q){

[Link]("Area of Triangle:"+(0.5*p*q));

class AbstractDemo{

public static void main(String[] args){

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Rectangle r=new Rectangle();

[Link](5,6);

Circle c=new Circle();

[Link](3,3);

Triangle t=new Triangle();

[Link](3,6);

Output:

D:\22251A6728>javac [Link]

D:\22251A6728>java AbstractDemo

Area of Rectangle:30

Area of Circle:28.259999999999998

Area of Triangle:9.0

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

c. Write a Java program that implements multiple inheritance.

Source Code:

interface A{

void print();

interface B{

void display();

class MultipleInheritanceDemo implements A,B{

public void print(){

[Link]("Interface A method");

public void display(){

[Link]("Interface B method");

public static void main(String[] args){

MultipleInheritanceDemo obj=new MultipleInheritanceDemo();

[Link]();

[Link]();

Output:
D:\22251A6728>javac [Link]

D:\22251A6728>java MultipleInheritanceDemo

Interface A method
Interface B method

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Week 6:

[Link] a Java program that implements a multi-threaded application that has three threads. First
thread generates random integer every 1 second and if the value is even, second thread computes
the square of the number and prints. If the value is odd, the third thread will print the value of cube
of the number.

Source Code:

import [Link].*;
class even implements Runnable
{
public int x;
public even(int x){
this.x=x;
}
public void run(){
[Link]("The Thread "+ x +" is EVEN and Square of " + x + " is : " + x * x);
}
}
class odd implements Runnable {
public int x;
public odd(int x) {
this.x = x;
}
public void run() {
[Link]("The Thread "+ x +" is ODD and Cube of " + x + " is: " + x* x * x);
}
}
class A extends Thread {
public void run(){
int n=0;
Random r=new Random();
try{
for (int i = 0; i < 5; i++) {
n=[Link](100);
[Link]("Generated Number is " + n);
if(n%2==0){
Thread t1 = new Thread(new even(n));
[Link]();
}
else{
Thread t2 = new Thread(new odd(n));
22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24
G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

[Link]();
}
[Link](1000);
[Link]("------------------------------------");
}
}
catch(Exception e){
[Link]([Link]());
}
}
}
public class Random_Gen{
public static void main(String[] args){
A rand_num=new A();
rand_num.start();
}
}
Output:

D:\22251A6744>javac Random_Gen.java

D:\22251A6744>java Random_Gen
Generated Number is 40
The Thread 40 is EVEN and Square of 40 is : 1600
------------------------------------
Generated Number is 27
The Thread 27 is ODD and Cube of 27 is: 19683
------------------------------------
Generated Number is 27
The Thread 27 is ODD and Cube of 27 is: 19683
------------------------------------
Generated Number is 34
The Thread 34 is EVEN and Square of 34 is : 1156
------------------------------------
Generated Number is 64
The Thread 64 is EVEN and Square of 64 is : 4096
------------------------------------

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

[Link] a Java program that implements producer – consumer problem using the concept of Inter
thread communication.

Source Code

class Q {
int n;
boolean valueSet = false;

synchronized int get()

{
while (!valueSet)
try {
wait();
} catch (InterruptedException e) {
[Link]("InterruptedException caught");
}
[Link]("Got:" + n);
valueSet = false;
notify();
return n;
}

synchronized void put(int n) {


while (valueSet)
try {
wait();
} catch (InterruptedException e) {
[Link]("InterruptedException caught");
}
this.n = n;
valueSet = true;
[Link]("Put: " + n);
notify();
}
}

class Producer implements Runnable{


Q q;
Producer(Q q){
this.q=q;
new Thread(this, "Producer").start();
}
public void run() {
22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24
G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

int i = 0;
try {
while(true) {
[Link](30);
[Link](i++);
}
} catch (InterruptedException e) {}

}
}
class Consumer implements Runnable{

Q q;
Consumer(Q q){
this.q=q;
new Thread(this, "Consumer").start();
}
public void run() {
try {
while(true) {
[Link](30);
[Link]();
}
} catch (InterruptedException e) {}

}
}

class ProducerConsumer{
public static void main(String args[]) {
Q q=new Q();
new Producer(q);
new Consumer(q);
[Link]("Press Control-C to stop");

}
}

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Output:
D:\22251A6744>javac [Link]

D:\22251A6744>java ProducerConsumer
Press Control-C to stop
Put: 0
Got:0
Put: 1
Got:1
Put: 2
Got:2
Put: 3
Got:3
Put: 4
Got:4
Put: 5
Got:5

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Week 7

Write a Java program that handles all mouse events and shows the event name at the center of the
window when a mouse event is fired. (Use Adapter classes).

Source Code

import [Link].*;
import [Link].*;
public class MouseEventsAdapter extends MouseAdapter{
Frame f;
Label l;
MouseEventsAdapter(){
f=new Frame("Mouse Adapter");
[Link](this);
l=new Label();
[Link](200,200,100,100);
[Link](l);
[Link](500,500);
[Link](null);
[Link](true);
}
public void mouseClicked(MouseEvent e){
[Link]("Mouse Clicked");
}
public void mouseEntered(MouseEvent e){
[Link]("Mouse Entered");
}
public void mouseExited(MouseEvent e){
[Link]("Mouse Exited");
}
public void mousePressed(MouseEvent e){
[Link]("Mouse Pressed");
}
public void mouseReleased(MouseEvent e){
[Link]("Mouse Released");
}
public static void main(String[] args){
new MouseEventsAdapter();
}
}

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24


G. Narayanamma Institute of Technology and Science
(Autonomous) (For Women)
DEPARTMENT OF CSE (DS)
II/IV [Link]
OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB

Output:

D:\22251A6744>javac [Link]

D:\22251A6744>java MouseEventsAdapter

22251A6744 Class-II/IV CSD-II Sem Academic Year: 2023-24

You might also like