0% found this document useful (0 votes)
4 views38 pages

Java Coding Examples and Output

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)
4 views38 pages

Java Coding Examples and Output

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

CODING:

import [Link].*;
import [Link];
class list1
{
public static void main(String args[])
{
try
{
BufferedReader in =new BufferedReader(new
InputStreamReader([Link]));
int a,b;
[Link]("ENTER THE STRING:");
String s1=[Link]();
[Link]("ENTER THE STARTING POSITION VALUE:");
a=[Link]([Link]());
[Link]("ENTER THE ENDING POSITION VALUE:");
b=[Link]([Link]());
String s2=([Link](a,b));
[Link]("YOUR CHARACTER STRING IS:" +s2);
}
catch(IOException e)
{
[Link]("ERROR");
[Link](1);
}
}
}
OUTPUT:
CODING:
import [Link].*;
import [Link];
class student
{
String name;
int regno;
void getdata(String sname,int rno)
{
name=sname;
regno=rno;
}
void putdata()
{
[Link]("NAME:" +name);
[Link]("REGNO:" +regno);
}
}
class mark extends student
{
int m1,m2,m3;
void getmarks(int mark1,int mark2,int mark3)
{
m1=mark1;
m2=mark2;
m3=mark3;
}
void putmarks()
{
[Link]("MARK1:" +m1);
[Link]("MARK2:" +m2);
[Link]("MARK3:" +m3);
}
}
interface s
{
int pract_mark=60;
}
class result extends mark implements s
{
int total;
void display()
{
putdata();
putmarks();
[Link]("PRACTICAL MARK:" +pract_mark);
total=m1+m2+m3+pract_mark;
[Link]("TOTAL :" +total);
if ((m1 > 40) && (m2 > 40) && (m3 > 40) && (pract_mark > 40))
[Link]("THE STUDENT IS PASS");
else
[Link]("THE STUDENT IS FAIL");
}
}
class list2
{
public static void main(String args[])
{
result st=new result();
[Link]("Yashwanth",1001);
[Link](98,99,100);
[Link]();
}
}
OUTPUT:
CODING:
import [Link].*;
class list3
{
public static void main(String args[])throws IOException
{
BufferedReader in =new BufferedReader(new
InputStreamReader([Link]));
[Link]("ENTER A:");
int a = [Link]([Link]());
[Link]("ENTER B:");
int b = [Link]([Link]());
int c;
try
{
c = b - a;
if (c > 32768)
{
throw new ArithmeticException("PAY OUT BOUNDS");
}
[Link]("ANSWER:" + c);
}
catch (ArithmeticException e)
{
[Link](e);
}
}
}
OUTPUT:
CODING:
import [Link].*;
class A extends Thread
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
[Link](i + "*"+5+ "="+(i * 5));
}
[Link]("END OF THE 1st THREAD");
}
}
class B extends Thread
{
public void run()
{
for (int j = 1; j <= 7; j++)
{
[Link](j + "*" +7+ "=" +(j * 7));
}
[Link]("END OF THE 2st THREAD");
}
}
class C extends Thread
{
public void run()
{
for (int k = 1; k <= 13; k++)
{
[Link](k + "*" +13+ "=" +(k * 13));
}
[Link]("END OF THE 3st THREAD");
}
}
public class list4
{
public static void main(String args[])throws IOException
{
A ThreadA=new A();
B ThreadB=new B();
C ThreadC=new C();
[Link](Thread.MAX_PRIORITY);
[Link](Thread.NORM_PRIORITY);
[Link](Thread.MIN_PRIORITY);
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
CODING:
import [Link].*;
import [Link].*;
public class list5 extends JPanel {
@Override
protected void paintComponent(Graphics g) {
[Link](g);
// Draw Circle
[Link]([Link]);
[Link]("Circle", 20, 20);
[Link]([Link]);
[Link](20, 30, 40, 40);
// Draw Ellipse
[Link]([Link]);
[Link]("Ellipse", 20, 100);
[Link]([Link]);
[Link](20, 110, 50, 40);
// Draw Square
[Link]([Link]);
[Link]("Square", 90, 80);
[Link]([Link]);
[Link](90, 90, 90, 90);
// Draw Rectangle
[Link]([Link]);
[Link]("Rectangle", 80, 30);
[Link]([Link]);
[Link](80, 40, 80, 40);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
[Link](new list5());
[Link](400, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
OUTPUT:
CODING:
import [Link].*;
import [Link].*;

public class list6 extends Frame implements WindowListener, ActionListener {


TextField tname, tstreet, tcity, tpincode;
Label lname, lstreet, lcity, lpincode;
Button mydet;

public list6() {
// Set layout and frame title
setLayout(new FlowLayout([Link], 10, 10));
setTitle("Personal Details");

// Initialize text fields and labels


tname = new TextField(20);
tstreet = new TextField(20);
tcity = new TextField(20);
tpincode = new TextField(20);

lname = new Label("NAME", [Link]);


lstreet = new Label("STREET", [Link]);
lcity = new Label("CITY", [Link]);
lpincode = new Label("PINCODE", [Link]);

mydet = new Button("MY DETAIL");

// Add components to the frame


add(mydet);
add(lname);
add(tname);
add(lstreet);
add(tstreet);
add(lcity);
add(tcity);
add(lpincode);
add(tpincode);

// Set initial text for text fields


[Link]("");
[Link]("");
[Link]("");
[Link]("");

// Add action and window listeners


[Link](this);
addWindowListener(this);

// Set frame size and visibility


setSize(320, 240);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


if ([Link]() == mydet() {
[Link]("tharmar");
[Link]("B172, TNHB Colony, Elango Nagar");
[Link]("Civil Aerodrome Post, Coimbatore");
[Link]("641 014");
}
}

public void windowClosing(WindowEvent e) {


setVisible(false);
dispose();
[Link](0);
}

public void windowOpened(WindowEvent e) {}


public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}

public static void main(String[] args) {


new list6();
}
OUTPUT:
CODING:
import [Link].*;
import [Link].*;

public class list7 extends JFrame {


JList<String> colours, shapes;
DefaultListModel<String> colourModel, shapeModel;
JTextArea display;

public list7() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
[Link] = new Insets(5, 5, 5, 5);

// Initialize list models


colourModel = new DefaultListModel<>();
shapeModel = new DefaultListModel<>();

// Add items to colour model


[Link]("Blue");
[Link]("Red");
[Link]("Black");
[Link]("Cyan");

// Add items to shape model


[Link]("Circle");
[Link]("Rectangle");
[Link]("Oval");
[Link]("Square");

// Initialize lists
colours = new JList<>(colourModel);
shapes = new JList<>(shapeModel);

// Enable multiple selections for colours

[Link](ListSelectionModel.MULTIPLE_INTERVAL_SELE
CTION);

// Enable single selection for shapes


[Link](ListSelectionModel.SINGLE_SELECTION);

// Add lists to scroll panes


JScrollPane colourPane = new JScrollPane(colours);
JScrollPane shapePane = new JScrollPane(shapes);

// Initialize display area


display = new JTextArea(5, 20);
[Link](false);

// Add components to frame with GridBagLayout


[Link] = 0;
[Link] = 0;
[Link] = [Link];
add(new JLabel("Colours:"), gbc);

[Link] = 0;
[Link] = 1;
[Link] = [Link];
[Link] = [Link];
add(colourPane, gbc);

[Link] = 0;
[Link] = 2;
[Link] = [Link];
[Link] = [Link];
add(new JLabel("Shapes:"), gbc);

[Link] = 0;
[Link] = 3;
[Link] = [Link];
[Link] = [Link];
add(shapePane, gbc);

[Link] = 0;
[Link] = 4;
[Link] = [Link];
[Link] = [Link];
add(display, gbc);

// Register listeners
[Link](e -> updateDisplay());
[Link](e -> updateDisplay());

// Set frame properties


setTitle("Multiple Selection List Box");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

private void updateDisplay() {


String msg = "Colours: ";
for (String colour : [Link]()) {
msg += colour + " ";
}
msg += "\nShape: " + [Link]();
[Link](msg);
}

public static void main(String[] args) {


new list7();
}
}
OUTPUT
CODING:
import [Link].*;
import [Link].*;

public class list8 extends Frame implements WindowListener {

public list8() {
FlowLayout flow = new FlowLayout([Link], 10, 10);
setLayout(flow);

TextField tname = new TextField(15);


TextField tage = new TextField(3);
TextField tqualification = new TextField(20);
TextArea taddress = new TextArea(5, 20);

Label lname = new Label("NAME:", [Link]);


Label lage = new Label("AGE:", [Link]);
Label lqualification = new Label("QUALIFICATION:", [Link]);
Label laddress = new Label("ADDRESS:", [Link]);

add(lname); add(tname);
add(lage); add(tage);
add(lqualification); add(tqualification);
add(laddress); add(taddress);

[Link]("tharmar");
[Link]("18");
[Link]("Bsc");
[Link](" airport Coimbatore-14");

addWindowListener(this);

setSize(600, 400);
setVisible(true);
}

// Implement WindowListener methods


public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}

public static void main(String[] args) {


new list8();
}
}
OUTPUT:
CODING:
import [Link].*;
import [Link].*;

public class list9 extends Frame implements ActionListener,


WindowListener {

MenuBar a = new MenuBar();


Menu FileMenu = new Menu("FILE");
Menu EditMenu = new Menu("EDIT");
Menu SearchMenu = new Menu("SEARCH");

MenuItem NewItem = new MenuItem("NEW");


MenuItem OpenItem = new MenuItem("OPEN");
MenuItem SaveItem = new MenuItem("SAVE");
MenuItem SaveasItem = new MenuItem("SAVE AS");
MenuItem ExitItem = new MenuItem("EXIT");

MenuItem CutItem = new MenuItem("CUT");


MenuItem CopyItem = new MenuItem("COPY");
MenuItem PasteItem = new MenuItem("PASTE");

MenuItem FindItem = new MenuItem("FIND");


MenuItem ReplaceItem = new MenuItem("REPLACE");

public list9() {
setTitle("Menu Bar Example");
setMenuBar(a);

[Link](FileMenu);
[Link](EditMenu);
[Link](SearchMenu);

[Link](NewItem);
[Link](OpenItem);
[Link](SaveItem);
[Link](SaveasItem);
[Link](ExitItem);

[Link](CutItem);
[Link](CopyItem);
[Link](PasteItem);

[Link](FindItem);
[Link](ReplaceItem);

[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);

addWindowListener(this);
setSize(320, 240);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


if ([Link]() == ExitItem) {
quitApp();
} else {
[Link]([Link]() + " selected");
}
}

public void windowClosed(WindowEvent e) {}

public void windowClosing(WindowEvent e) {


quitApp();
}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void quitApp() {
setVisible(false);
dispose();
[Link](0);
}
public static void main(String[] args) {
new list9();
}
}
OUTPUT:
CODING:
import [Link].*;
import [Link].*;
public class list10 extends Frame implements MouseListener,
MouseMotionListener, WindowListener {

String s = " ";

public list10() {
addMouseListener(this);
addMouseMotionListener(this);
addWindowListener(this);
setSize(320, 240);
setVisible(true);
}

public void mouseClicked(MouseEvent e) {


s = "Mouse Clicked";
repaint();
}

public void mouseReleased(MouseEvent e) {


s = "Mouse Up";
repaint();
}

public void mouseEntered(MouseEvent e) {


s = "Mouse Entered";
repaint();
}
public void mouseDragged(MouseEvent e) {
s = "Mouse Dragged";
repaint();
}

public void mouseMoved(MouseEvent e) {


s = "Mouse Moved";
repaint();
}

public void mouseExited(MouseEvent e) {


s = "Mouse Exited";
repaint();
}

public void mousePressed(MouseEvent e) {


s = "Mouse Pressed";
repaint();
}

public void paint(Graphics g) {


[Link](s, 50, 100);
}

public void windowClosed(WindowEvent e) {


}

public void windowClosing(WindowEvent e) {


setVisible(false);
dispose();
[Link](0);
}
public void windowDeiconified(WindowEvent e) {
}

public void windowIconified(WindowEvent e) {


}

public void windowOpened(WindowEvent e) {


}

public void windowActivated(WindowEvent e) {


}

public void windowDeactivated(WindowEvent e) {


}

public static void main(String[] args) {


new list10();
}
}
OUTPUT:
CODING:
import [Link].*;

import [Link].*;

public class list11 extends Frame implements KeyListener, MouseListener,


MouseMotionListener {

int x = 1;

int x1, x2, y1, y2;

int i = 0, j = 0;

public list11() {

addMouseListener(this);

addMouseMotionListener(this);

addKeyListener(this);

setSize(400, 400);

setVisible(true);

public void keyPressed(KeyEvent ke) {

char ch = [Link]();

if (ch == 'o') {

x = 1; // Draw Oval

if (ch == 'c') {

x = 2; // Draw Circle (assuming same method as oval)

if (ch == 'r') {
x = 3; // Draw Rectangle

if (ch == 's') {

x = 4; // Draw Square (assuming same method as rectangle)

repaint();

public void keyTyped(KeyEvent ke) {}

public void keyReleased(KeyEvent ke) {}

public void mouseClicked(MouseEvent e) {}

public void mousePressed(MouseEvent e) {

x1 = [Link]();

y1 = [Link]();

public void mouseReleased(MouseEvent e) {

x2 = [Link]();

y2 = [Link]();

repaint();

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseMoved(MouseEvent e) {}

public void mouseDragged(MouseEvent e) {

x2 = [Link]();

y2 = [Link]();

repaint();
}

public void paint(Graphics g) {

[Link]("Circle (c), Oval (o), Rectangle (r), Square (s)", 50, 50)

switch (x) {

case 1:

[Link](x1, y1, x2 - x1, y2 - y1); // Draw Oval

break;

case 2:

[Link](x1, y1, x2 - x1, y2 - y1); // Draw Circle (same as oval)

break;

case 3:

[Link](x1, y1, x2 - x1, y2 - y1); // Draw Rectangle

break;

case 4:

[Link](x1, y1, x2 - x1, y2 - y1); // Draw Square (same as rectangle)

break;

public static void main(String[] args) {

list11 frame = new list11();

[Link](new WindowAdapter()

public void windowClosing(WindowEvent e) {

[Link](0);

});

}
OUTPUT:
CODING:
import [Link].*;

public class list12 {

public static void main(String args[]) {

RandomAccessFile newfile;

try {

newfile = new RandomAccessFile("[Link]", "rw");

[Link]([Link]());

BufferedReader reader = new BufferedReader(new


InputStreamReader([Link]));

[Link]("Enter the text to be updated:");

String s = [Link]();

[Link](s + "\n"); // Append text with a newline to file

[Link]();

[Link]("Text appended successfully.");

} catch (IOException e) {

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

}
OUTPUT:

You might also like