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

JAVA 23343

The document contains a series of Java programming exercises, each demonstrating different concepts such as multiple constructors, method overloading, method overriding, arrays, string manipulation, interfaces, packages, multithreading, and exception handling. Each exercise includes the aim, coding examples, outputs, and results confirming successful execution. The exercises serve as practical applications of Java programming principles for educational purposes.

Uploaded by

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

JAVA 23343

The document contains a series of Java programming exercises, each demonstrating different concepts such as multiple constructors, method overloading, method overriding, arrays, string manipulation, interfaces, packages, multithreading, and exception handling. Each exercise includes the aim, coding examples, outputs, and results confirming successful execution. The exercises serve as practical applications of Java programming principles for educational purposes.

Uploaded by

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

EX.

NO:1 MULTIPLE CONSTRUCTOR

DATE:20/12/2024

AIM:

To Write a JAVA program using Multiple Constructors.

CODING:

import java.util.Scanner;
class Circle
{
static int a,s,r;
static double c;
Circle()
{
Scanner y=new Scanner(System.in);
System.out.println("Enter a Value:");
a=y.nextInt();
s=a*a;
System.out.println("Area of square:"+s);
}
Circle(int r)
{
c=3.14*r*r;
System.out.println("Area of Circle:"+c);
}
public static void main(String[] args)
{
Scanner z=new Scanner(System.in);
System.out.println("Area Calculation:");
System.out.println("1.Square");
System.out.println("2.Circle");
int ch;
System.out.println("Enter a Choice:");
ch=z.nextInt();
switch(ch)
{
case 1:
new Circle();
break;
case 2:
System.out.println("Enter r Value:");
r=z.nextInt();
new Circle(r);
break;
default:
System.out.println("Invalid:");
break;
}
}
}
OUTPUT :

D:\23343>javac Circle.java
D:\23343>java Circle

Area Calculation:
1.Square
2.Circle
Enter a Choice:
1
Enter a Value:
2
Area of square:4

Area Calculation:
1.Square
2.Circle
Enter a Choice:
2
Enter r Value:
5
Area of Circle:78.5

RESULT:

Thus, the above program was executed successfully and output was verified.
EX.NO:2 OVERLOADING METHOD

DATE:28/12/2024

AIM :

To Write a JAVA program using overloading method.

CODING:

import java.util.Scanner;
public class mo
{
static void per(int a,int b,int c)
{
int tri;
tri=a+b+c;
System.out.println("Perimeter Of Triangle:"+tri);
}
static void per(double r)
{
double cir;
cir=2*3.14*r;
System.out.println("Perimeter Of Circle:"+cir);
}
public static void main(String[] args)
{
Scanner z=new Scanner(System.in);
System.out.println("Perimeter Calculation:");
System.out.println("1.Triangle");
System.out.println("2.Circle");
int ch;
System.out.println("Enter a Choice:");
ch=z.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter a Value:");
int a=z.nextInt();
System.out.println("Enter b Value:");
int b=z.nextInt();
System.out.println("Enter c Value:");
int c=z.nextInt();
per(a,b,c);
break;
case 2:
System.out.println("Enter r Value:");
double r=z.nextDouble();
per(r);
break;
default:
System.out.println("Invalid:");
break;
}
}}
OUTPUT:

D:\23343>javac mo.java
D:\23343>java mo
Perimeter Calculation:
1.Triangle
2.Circle
Enter a Choice:
1
Enter a Value:
3
Enter b Value:
7
Enter c Value:
8
Perimeter Of Triangle:18
Perimeter Calculation:
1.Triangle
2.Circle
Enter a Choice:
2
Enter r Value:
4
Perimeter Of Circle:25.12

RESULT:
Thus, the above program was executed successfully and output was verified
EX.NO:3 OVERRIDING METHOD

DATE:06/01/2025

AIM:

To Write a JAVA program using Overriding Method.

CODING:

import java.util.Scanner;
class A
{
String co;
Scanner x=new Scanner(System.in);
void get()
{
System.out.println("Enter Country Name:");
co=x.nextLine();
}
}

class B extends A
{
String pl;
int jer;
void get()
{
super.get();
System.out.println("Enter Player Name:");
pl=x.nextLine();
System.out.println("Enter Jersey Number:");
jer=x.nextInt();
}
void display()
{
System.out.println("Country Name:"+ co);
System.out.println("Player Name:"+pl);
System.out.println("Jersey Number:"+ jer);
}
}

public class mor


{
public static void main(String[] args)
{
B r=new B();
r.get();
r.display();
}
}
OUTPUT:

D:\23343>javac mor.java

D:\23343>java mor

Enter Country Name:

INDIA

Enter Player Name:

DHONI

Enter Jersey Number:

Country Name: INDIA

Player Name: DHONI

Jersey Number:7

RESULT:

Thus, the above program was executed successfully and output was verified
EX.NO:4 ONE-DIMENSIONAL ARRAY

DATE:13/01/2025

AIM:

To Write a JAVA program using one-dimensional arrays.

CODING:

import java.util.Scanner;
public class array
{
public static int linearsearch(int[] arr,int target)
{
for(int i=0;i<arr.length;i++)
{
if(arr[i]==target)
{
return i;
}
}
return -1;
}

public static void main(String[] args)


{
Scanner z=new Scanner(System.in);
System.out.println("Enter the No. of elements:");
int n=z.nextInt();
int [] arr=new int[n];
System.out.println("Enter the elements of the array:");
for(int i=0;i<n;i++)
{
arr[i]=z.nextInt();
}
System.out.println("Enter the search element:");
int target=z.nextInt();
int r=linearsearch(arr,target);
if(r==-1)
{
System.out.println("Element "+target+ " is not found.");
}
else
{
System.out.println("Element "+target+ " is found at index " + r + ".");
}

}
}
OUTPUT:

D:\23343>javac array.java

D:\23343>java array

Enter the No. of elements:

Enter the elements of the array:

Enter the search element:

Element 2 is found at index 3.


Enter the No. of elements:

Enter the elements of the array:

Enter the search element:

Element 8 is not found.

RESULT:

Thus, the above program was executed successfully and output was verified
EX NO:5 TWO-DIMENSIONAL ARRAY

DATE:23/01/2025

AIM:

To Write a JAVA program using Two-dimensional array.

CODING:

import java.util.Scanner;
public class tarray
{
public static void main(String[] args)
{
Scanner z=new Scanner(System.in);
System.out.println("Enter the number of rows:");
int r=z.nextInt();
System.out.println("Enter the number of columns:");
int c=z.nextInt();
int a[][]=new int[r][c];
int b[][]=new int[r][c];
int re[][]=new int[r][c];
System.out.println("Enter First Matrix Elements:");
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
a[i][j]=z.nextInt();}}
System.out.println("Enter Second matrix Elements:");
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
b[i][j]=z.nextInt();
}
}
System.out.println("Addition Of Matrix:");
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
re[i][j]=a[i][j]+b[i][j];
}
}
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
System.out.print(re[i][j]+" ");
}
System.out.println();
}
}
}
OUTPUT:

D:\23343>javac tarray.java

D:\23343>java tarray

Enter the number of rows:


3
Enter the number of columns:
3
Enter First Matrix Elements:
132
411
710
Enter Second matrix Elements:
345
180
126
Addition Of Matrix:
477
591
836

RESULT:

Thus, the above program was executed successfully and output was verified
EX.NO:6 STRING MANIPULATION

DATE:30/01/2025

AIM:

To Write a program to do String Manipulation using Character Array and


perform the following string operations: String length, Finding a character at a
particular position, Concatenating two strings.

CODING:

import java.util.Scanner;
public class str
{
public static void main(String[] args)
{
String a,b;
Scanner z=new Scanner(System.in);
System.out.println("Enter first string:");
a=z.nextLine();
System.out.println("Enter Second string:");
b=z.nextLine();
char[] A= a.toCharArray();
char[] B= b.toCharArray();
System.out.println("Length of First String:"+A.length);
System.out.println("Length of Second String:"+B.length);
char[] C=new char[A.length+B.length];
for(int i=0;i<A.length;i++)
{
C[i]=A[i];
}
for(int i=0;i<B.length;i++)
{
C[A.length+i]=B[i];
}
String c=new String(C);
System.out.println("Concatenation of Strings: "+c);

System.out.print("Enter a position to find character string: ");


int position = z.nextInt();
if (position >= 0 && position < C.length)
{
System.out.println("Character at position " + position + ": " +
C[position]);
}
else
{
System.out.println("Invalid position!");
}
}
}
OUTPUT:

D:\23343>javac str.java

D:\23343>java str

Enter first string:

Raj

Enter Second string:

kumar

Length of First String:3

Length of Second String:5

Concatenation of Strings: Rajkumar

Enter a position to find character string: 2

Character at position 2: j

RESULT:

Thus, the above program was executed successfully and output was verified
EX.NO:7 INTERFACE

DATE:06/02/2025

AIM:

To Write a JAVA program implementing interface(s).

CODING:

import java.util.Scanner;
interface A
{
void t1 ();

}
interface B extends A
{
void t2();
}
class cri implements B
{
public void t1()
{
Scanner z=new Scanner(System.in);
System.out.println("Team 1:");
System.out.println("Enter a Team Name:");
String a=z.nextLine();
System.out.println("Enter a Captain Name:");
String b=z.nextLine();
System.out.println("Team Name:"+a);
System.out.println("Captain Name:"+b);
}
public void t2()
{
Scanner z=new Scanner(System.in);
System.out.println("Team 2:");
System.out.println("Enter a Team Name:");
String a=z.nextLine();
System.out.println("Enter a Captain Name:");
String b=z.nextLine();
System.out.println("Team Name:"+a);
System.out.println("Captain Name:"+b);
}

}
class in
{
public static void main(String[] args)
{
cri r=new cri();
r.t1();
r.t2();

}
}
OUTPUT:

D:\23343>javac in.java
D:\23343>java in
Team 1:
Enter a Team Name:
CSK
Enter a Captain Name:
DHONI
Team Name: CSK
Captain Name: DHONI

Team 2:
Enter a Team Name:
MI
Enter a Captain Name:
ROHIT
Team Name: MI
Captain Name: ROHIT

RESULT:

Thus, the above program was executed successfully and output was verified
EX.NO:8 CREATE AND IMPORT PACKAGE

DATE:13/02/2025

AIM:

To Write a JAVA program to create and import package.

CODING:

Package Program:

package raj;

import java.util.Scanner;

public class A

public void pn()

Scanner z=new Scanner(System.in);

System.out.println("Enter a Value:");

int a=z.nextInt();

if(a>=0)

System.out.println(a+" is Positive Number.");

}
else

System.out.println(a+" is Negative Number");

Main Program:

import raj.A;

public class pack

public static void main(String[] args)

A r=new A();

r.pn();

}
OUTPUT:

D:\23343\raj>javac A.java

D:\23343\raj>cd D:\23343

D:\23343>javac pack.java

D:\23343>java pack

Enter a Value:

5 is Positive Number.

Enter a Value:

-8

-8 is Negative Number

RESULT:

Thus, the above program was executed successfully and output was verified.
EX.NO:9 MULTIPLE THREADS

DATE:20/02/2025

AIM:

To Write a JAVA program to create and deal multiple threads.

CODING:

import java.util.*;
class table2 extends Thread
{
private int limit=10;
public void run()
{
System.out.println("Multiplication Table of 2:");
for(int i=1;i<=limit;i++)
{
System.out.println("2 x" +i+ "=" + (2*i));
}
}
}

class table3 extends Thread


{
private int limit=10;
public void run()
{
System.out.println("Multiplication Table of 3:");
for(int i=1;i<=limit;i++)
{
System.out.println("3 x" +i+ "=" + (3*i));
}
}
}
public class thread
{
public static void main(String[] args)
{
table2 t2=new table2();
table3 t3=new table3();
t2.start();
t3.start();
}
}
OUTPUT:

D:\23343>javac thread.java

D:\23343>java thread

Multiplication Table of 2:

Multiplication Table of 3:

2 x1=2

2 x2=4

3 x1=3

3 x2=6

2 x3=6

2 x4=8

3 x3=9

3 x4=12

3 x5=15

3 x6=18

3 x7=21

3 x8=24

3 x9=27

2 x5=10

2 x6=12
3 x10=30

2 x7=14

2 x8=16

2 x9=18

2 x10=20

RESULT:

Thus, the above program was executed successfully and output was verified
EX.NO:10 EXCEPTION HANDLING

DATE:27/02/2025

AIM:

To Write a JAVA program with throwing your own exception.

CODING:

import java.util.Scanner;
class Exp extends Exception
{
public Exp(String message)
{
super(message);
}
}
public class EH
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
try
{
System.out.println("Enter your Mark:");
int m = obj.nextInt();
if (m < 35)
{
throw new Exp("Fail");
}
System.out.println("Pass");
}
catch (Exp e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println("Invalid input");
}
finally
{
System.out.println("Student Mark List");
}
obj.close();
}
}
OUTPUT:

D:\23343>javac EH.java
D:\23343>java EH
Enter your Mark:
80
Pass
Student Mark List

Enter your Mark:


32
Fail
Student Mark List

Enter your Mark:


ab
Invalid input
Student Mark List

RESULT:

Thus, the above program was executed successfully and output was verified
EX.NO:11 DESIGN A WEB PAGE

DATE:28/02/2025

AIM:

To Write a JAVA program using Applet to Design a Web Page.

CODING:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class web extends Applet implements ActionListener
{
Button home,c1,c2,contact;
String content="Welcome";
Image photo,b,s;
public void init()
{
setSize(600,400);
setBackground(Color.YELLOW);
b=getImage(getDocumentBase(),"mb.jpg");
s=getImage(getDocumentBase(),"sh.jpg");
home=new Button("HOME");
c1=new Button("BHEEM");
c2=new Button("SHINCHAN");
contact=new Button("CONTACT US");
home.addActionListener(this);
c1.addActionListener(this);
c2.addActionListener(this);
contact.addActionListener(this);
add(home);
add(c1);
add(c2);
add(contact);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==home)
{
content="WELCOME TO CARTOON NETWORK";

}
else if(e.getSource()==c1)
{
content="Mighty Little Bheem is a Netflix animated series for children
that is a spin-off of the popular Indian show Chhota Bheem.";
photo=getImage(getDocumentBase(),"mb.jpg");
}

else if(e.getSource()==c2)
{
content="Crayon Shin-chan is a Japanese manga series written and
illustrated by Yoshito Usui.";
photo=getImage(getDocumentBase(),"sh.jpg");
}
else if(e.getSource()==contact)
{
content="Contact us: Email - CN@gmail.com phone - 123-456-7890";
}
repaint();
}
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.BOLD,16));
g.drawString("Cartoon Network", 200,50);
g.setFont(new Font("Arial", Font.PLAIN, 14));
g.drawString(content, 50,100);
if(photo!=null)
{
g.drawImage(photo,250,200,850,300,this);
}
else
{
g.drawString("",50,150);
}
}
}
/*<applet code="web.class" width="600" height="400"></applet>*/
OUTPUT:

D:\23343>set path="C:\Program Files\Java\jdk-1.8\bin"


D:\23343>javac web.java
D:\23343>appletviewer web.java
RESULT:

Thus, the above program was executed successfully and output was verified
EX.NO:12 MOUSE EVENT

DATE:06/03/2025

AIM:

To Write a JAVA program for handling mouse events.

CODING:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class mouse extends Applet implements MouseListener
{
private Color currentColor = Color.BLACK;
public void init()
{
setBackground(currentColor);
addMouseListener(this);
}
public void paint(Graphics g)
{

g.drawString("PRESS LEFT CLICK TO CHANGE COLOR!", 150,150);


}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
currentColor = Color.BLUE;
setBackground(currentColor);
repaint();
}
public void mouseReleased(MouseEvent e)
{
currentColor = Color.PINK;
setBackground(currentColor);
repaint();
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
/*<applet code="mouse.class" width="600" height="600"></applet>*/
OUTPUT:

D:\23343>set path="C:\Program Files\Java\jdk-1.8\bin"

D:\23343>javac mouse.java

D:\23343>appletviewer mouse.java

RESULT:

Thus, the above program was executed successfully and output was verified
EX.NO:13 KEYBOARD EVENT

DATE:13/03/2025

AIM:

To Write a JAVA program for handling keyboard events.

CODING:

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class keybo extends Applet implements KeyListener

private Color bgColor=Color.BLACK;

private String Text="";

public void init()

setSize(400, 400);

setBackground(bgColor);

addKeyListener(this);

setFocusable(true);

}
public void paint(Graphics g)

setBackground(bgColor);

g.setColor(Color.WHITE);

g.drawString("Press Red",20,50);

g.drawString("Type 'yellow' YELLOW",20,20);

public void keyPressed(KeyEvent e)

if(e.getKeyCode() == KeyEvent.VK_SHIFT)

bgColor = Color.RED;

repaint();

public void keyReleased(KeyEvent e)

if(e.getKeyCode() == KeyEvent.VK_SHIFT)

bgColor =Color.GREEN;
repaint();

public void keyTyped(KeyEvent e)

Text += e.getKeyChar();

if(Text.endsWith("yellow"))

bgColor=Color.YELLOW;

repaint();

/*<applet code="keybo.class" width="300" height="200">

</applet>*/
OUTPUT:

D:\23343>set path="C:\Program Files\Java\jdk-1.8\bin"

D:\23343>javac keybo.java

D:\23343>appletviewer keybo.java
RESULT:

Thus, the above program was executed successfully and output was verified

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy