0% found this document useful (0 votes)
12 views2 pages

Practical

The document contains a comprehensive overview of various Java programming concepts, including String and StringBuffer methods, inheritance types, multithreading, GUI components using Swing, and exception handling. It includes code examples demonstrating the implementation of these concepts, such as creating classes, handling user input, and performing mathematical operations. Additionally, it covers advanced topics like networking with InetAddress and URL class usage.

Uploaded by

aditipatwa20
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)
12 views2 pages

Practical

The document contains a comprehensive overview of various Java programming concepts, including String and StringBuffer methods, inheritance types, multithreading, GUI components using Swing, and exception handling. It includes code examples demonstrating the implementation of these concepts, such as creating classes, handling user input, and performing mathematical operations. Additionally, it covers advanced topics like networking with InetAddress and URL class usage.

Uploaded by

aditipatwa20
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
You are on page 1/ 2

1. All methods of String class sb.append(" Programming"); 4. Integer Wrapper class 6. Constructors to add complex numbers 7.

6. Constructors to add complex numbers 7. Multilevel Inheritance 8. Single Inheritance (Room Area and Volume)

public class StringExample { sb.insert(4, " Language"); public class IntegerWrapper { class Complex { class A { class Room { int length, breadth;
public static void main(String[] args) { sb.replace(4, 13, " is a"); public static void main(String[] args) { int real, imag; void displayA() {
Room(int l, int b) {
String s = " Hello Java "; sb.delete(4, 10); Integer i = 100; Complex() {} System.out.println("Class A"); length = l;
breadth = b;
System.out.println("Length: " + s.length()); sb.reverse(); System.out.println("Byte: " + i.byteValue()); Complex(int r, int i) { }
}
System.out.println("Char at 1: " + System.out.println("StringBuffer: " + System.out.println("Short: " + i.shortValue()); real = r; }
s.charAt(1)); sb.reverse()); int area() {
System.out.println("Int: " + i.intValue()); imag = i; class B extends A {
return length * breadth;
System.out.println("Substring: " + } }
System.out.println("Long: " + i.longValue()); } void displayB() {
s.substring(1, 5));
}
System.out.println("Float: " + i.floatValue()); Complex add(Complex c) { System.out.println("Class B"); }
System.out.println("Equals: " + s.equals("
Hello Java ")); System.out.println("Double: " + return new Complex(real + c.real, imag + } class RoomVolume extends Room { int height;
3. Vector methods i.doubleValue()); c.imag);
System.out.println("Ignore Case: " + }
RoomVolume(int l, int b, int h) {
s.equalsIgnoreCase(" hello java ")); import java.util.Vector; } }
class C extends B { super(l, b);
System.out.println("Index of 'J': " + public class VectorExample { } void display() { height = h;
void displayC() {
s.indexOf('J')); }
public static void main(String[] args) { System.out.println(real + " + " + imag + "i");
System.out.println("Class C");
System.out.println("Replace: " +
Vector<String> v = new Vector<>(); 5. Convert Integer object to primitive } int volume() {
s.replace("Java", "World")); }
return length * breadth * height;
v.add("Apple"); public class IntegerToPrimitive { }
System.out.println("To Upper: " + } }
s.toUpperCase()); v.add("Banana"); public static void main(String[] args) { public class ComplexDemo {
public class MultiLevel { }
System.out.println("To Lower: " + v.add("Cherry"); Integer obj = 150; public static void main(String[] args) {
public static void main(String[] args) {
s.toLowerCase()); public class InheritanceDemo {
v.add(1, "Mango"); byte b = obj.byteValue(); Complex c1 = new Complex(2, 3);
C obj = new C();
System.out.println("Trim: " + s.trim()); public static void main(String[] args) {
System.out.println(v); short s = obj.shortValue(); Complex c2 = new Complex(1, 4);
obj.displayA();
System.out.println("Starts With: " + RoomVolume obj = new RoomVolume(10, 20,
v.remove("Banana"); double d = obj.doubleValue(); Complex c3 = c1.add(c2);
s.startsWith(" ")); obj.displayB();
30);
System.out.println("Contains Apple? " + System.out.println("Byte: " + b); c3.display();
System.out.println("Ends With: " + obj.displayC();
v.contains("Apple")); System.out.println("Area: " + obj.area());
s.endsWith(" ")); System.out.println("Short: " + s); }
}
System.out.println("First: " + System.out.println("Volume: " + obj.volume());
} System.out.println("Double: " + d); }
v.firstElement()); }
} } }
System.out.println("Last: " +
2. All methods of StringBuffer v.lastElement()); } }
public class StringBufferExample { }

public static void main(String[] args) { }

StringBuffer sb = new StringBuffer("Java");

9. Multiple Inheritance with Interfaces 10. Area of rectangle and circle using interfaces 14. Multithread: print even and odd numbers 15. Three threads with setPriority 16. Password check with exception 17. NotMatchException

interface A { interface Shape { class Even extends Thread { class Task extends Thread { import java.util.Scanner; class NotMatchException extends Exception {

void methodA(); public void run() { public Task(String name) { class AuthenticationFailure extends Exception { public NotMatchException(String msg) {
void area();
} for (int i = 0; i <= 10; i += 2) super(name); public AuthenticationFailure(String msg) { super(msg);
}
interface B { System.out.println("Even: " + i); } super(msg); }
class Rectangle implements Shape {
void methodB(); } } }

} int length = 5, breadth = 4; } public void run() { }

class C implements A, B { public void area() { class Odd extends Thread { System.out.println("Running " + getName()); public class PasswordCheck { public class MatchCheck {

public void methodA() { System.out.println("Rectangle Area: " + (length * public void run() { } public static void main(String[] args) { public static void main(String[] args) {

System.out.println("Method A"); breadth)); for (int i = 1; i <= 10; i += 2) } Scanner sc = new Scanner(System.in); String country = "USA";

} } System.out.println("Odd: " + i); public class PriorityDemo { System.out.print("Enter Password: ");

public void methodB() { } } public static void main(String[] args) { String input = sc.nextLine(); try {

System.out.println("Method B"); } Task t1 = new Task("Thread1"); try { if (!country.equals("India"))


class Circle implements Shape {
} public class MultiThreadExample { Task t2 = new Task("Thread2"); if (!input.equals("admin123")) throw new NotMatchException("String
int radius = 3; public void area() {
does not match 'India'");
} public static void main(String[] args) { Task t3 = new Task("Thread3"); throw new
System.out.println("Circle Area: " + (3.14 * AuthenticationFailure("Authentication Failure"); else
public class InterfaceDemo { new Even().start();
radius * radius));
else System.out.println("String matched");
public static void main(String[] args) { new Odd().start(); t1.setPriority(Thread.MIN_PRIORITY);
} System.out.println("Login Successful"); } catch (NotMatchException e) {
C obj = new C(); } t2.setPriority(Thread.MAX_PRIORITY);
} } catch (AuthenticationFailure e) { System.out.println(e.getMessage());
obj.methodA(); } t3.setPriority(Thread.NORM_PRIORITY);
public class ShapeDemo { System.out.println(e.getMessage()); }
obj.methodB();
} }
} public static void main(String[] args) { t1.start();
} }
} Shape r = new Rectangle(); t2.start();
}
Shape c = new Circle(); t3.start();

}
r.area();
}
c.area();

}
18. RadioButton and Checkbox using Swing public class CityList { 21. Menu with disabled "Black f.add(new JButton(String.valueOf(i))); 25. JTree: Root and Subfolders f.setVisible(true);

import javax.swing.*; public static void main(String[] args) { import javax.swing.*; } import javax.swing.*; }

public class GUIRadioCheckbox { JFrame f = new JFrame("City List"); public class MenuColor { f.setSize(300, 300); import java.io.File; }

public static void main(String[] args) { String[] cities = {"Mumbai", "Delhi", "Pune", public static void main(String[] args) { f.setVisible(true); public class JTreeExample {
"Nagpur", "Chennai", "Kolkata", "Bhopal",
JFrame f = new JFrame("Radio and JFrame f = new JFrame("Color Menu"); } public static void main(String[] args) { 27. KeyEvent example
"Lucknow", "Indore", "Ahmedabad"};
Checkbox");
JMenuBar mb = new JMenuBar(); } JFrame f = new JFrame("JTree Example"); import javax.swing.*;
JList<String> list = new JList<>(cities);
f.setSize(300, 200);
JMenu menu = new JMenu("Colors"); 23. Display number on button 0–9 File root = new File("C:/"); import java.awt.event.*;
JScrollPane sp = new JScrollPane(list);
f.setLayout(null);
JMenuItem red = new JMenuItem("Red"); import javax.swing.*; JTree tree = new JTree(root.list()); public class KeyEventExample {
f.add(sp);
JRadioButton r1 = new JRadioButton("Male");
JMenuItem blue = new JMenuItem("Blue"); import java.awt.*; f.add(new JScrollPane(tree)); public static void main(String[] args) {
f.setSize(200, 200);
JRadioButton r2 = new
JMenuItem black = new JMenuItem("Black"); public class ButtonNumbers { f.setSize(300, 400); JFrame f = new JFrame("Key Event");
JRadioButton("Female"); f.setVisible(true);
black.setEnabled(false); public static void main(String[] args) { f.setVisible(true); JTextField tf = new JTextField();
r1.setBounds(50, 20, 100, 30); }
menu.add(red); JFrame f = new JFrame("Number Buttons"); } JLabel l = new JLabel();
r2.setBounds(150, 20, 100, 30); }
menu.add(blue); f.setLayout(new GridLayout(2, 5)); }
ButtonGroup bg = new ButtonGroup();
menu.add(black); for (int i = 0; i <= 9; i++) { tf.addKeyListener(new KeyAdapter() {
bg.add(r1); bg.add(r2); 20. Select multiple newspapers
mb.add(menu); f.add(new JButton(String.valueOf(i))); 26. JTable with 10 students public void keyPressed(KeyEvent e) {
JCheckBox cb = new JCheckBox("Accept import javax.swing.*;
Terms"); f.setJMenuBar(mb); } import javax.swing.*; l.setText("Key Pressed");
public class NewsPaperList {
cb.setBounds(50, 60, 150, 30); f.setSize(300, 200); f.setSize(300, 100); public class StudentTable { }

f.add(r1); f.add(r2); f.add(cb); public static void main(String[] args) { f.setVisible(true); f.setVisible(true); public static void main(String[] args) { });

f.setVisible(true); JFrame f = new JFrame("Newspapers"); } } JFrame f = new JFrame("Student Table"); f.add(tf, "North");

} String[] papers = {"TOI", "Hindustan Times", "The } } String[] columns = {"Name", "Percentage", f.add(l, "South");
Hindu", "Indian Express", "DNA"}; "Grade"};
} 22. Grid of 5x5 f.setSize(300, 100);
String[][] data = {
JList list = new JList<>(papers); import javax.swing.*; 24. Custom Output (Add specific content if f.setVisible(true);
list.setSelectionMode(ListSelectionModel.MULT required) {"A", "85", "A"}, {"B", "78", "B"}, {"C", "90",
import java.awt.*; }
IPLE_INTERVAL_SELECTION); "A"}, {"D", "56", "C"}, {"E", "88", "A"},
public class CustomOutput {
}
f.add(new JScrollPane(list)); {"F", "66", "B"}, {"G", "73", "B"}, {"H", "92",
public static void main(String[] args) {
public class Grid5x5 { "A"}, {"I", "59", "C"}, {"J", "80", "A"}
f.setSize(200, 200); System.out.println("Java Practical Output
public static void main(String[] args) { };
Example");
f.setVisible(true); JFrame f = new JFrame("5x5 Grid"); JTable table = new JTable(data, columns);
}
19. List of 10 cities } f.setLayout(new GridLayout(5, 5)); f.add(new JScrollPane(table));
}
import javax.swing.*; } for (int i = 1; i <= 25; i++) { f.setSize(400, 200);

28. MouseMotionListener (mouseDragged & System.out.println("Output Example Line 32. InetAddress - IP from hostname
mouseMoved) 2");
import java.net.*;
import javax.swing.*; }
public class IPAddress {
import java.awt.event.*; }
public static void main(String[] args) throws
public class MouseMotionDemo extends JFrame 30. Addition using JTextField Exception {
implements MouseMotionListener {
import javax.swing.*; InetAddress ip =
JLabel label; InetAddress.getByName("google.com");
import java.awt.event.*;
MouseMotionDemo() { System.out.println("IP Address: " +
public class AddNumbers {
ip.getHostAddress());
label = new JLabel();
public static void main(String[] args) {
}
add(label);
JFrame f = new JFrame("Add Two Numbers");
}
addMouseMotionListener(this);
JTextField t1 = new JTextField();
33. URL class (protocol, host, port, file)
setSize(300, 200);
JTextField t2 = new JTextField();
import java.net.*;
setVisible(true);
JTextField result = new JTextField();
public class URLInfo {
}
JButton b = new JButton("Add");
public static void main(String[] args) throws
public void mouseDragged(MouseEvent e) {
t1.setBounds(50, 20, 100, 30); Exception {
label.setText("Dragged at " + e.getX() + "," +
t2.setBounds(160, 20, 100, 30); URL url = new
e.getY());
URL("http://www.msbte.org.in");
b.setBounds(100, 60, 80, 30);
}
System.out.println("Protocol: " +
result.setBounds(100, 100, 100, 30);
public void mouseMoved(MouseEvent e) { url.getProtocol());
b.addActionListener(e -> {
label.setText("Moved at " + e.getX() + "," + System.out.println("Host: " + url.getHost());
e.getY()); int a = Integer.parseInt(t1.getText());
System.out.println("Port: " + url.getPort());
} int b1 = Integer.parseInt(t2.getText());
System.out.println("File: " + url.getFile());
public static void main(String[] args) { result.setText(String.valueOf(a + b1));
}
new MouseMotionDemo(); });
}
} f.add(t1); f.add(t2); f.add(b); f.add(result);

} f.setSize(300, 200);

29. Output Display (custom content) f.setLayout(null);

public class DisplayOutput { f.setVisible(true);

public static void main(String[] args) { }

System.out.println("Output Example Line 1"); }

You might also like