JAVA PROGRAMS
1) Conditional Operator
java
public class ConditionalOperator {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
[Link]("Maximum value is: " +
max);
}
}
2) Single Inheritance
```java
class Parent {
void display() {
[Link]("Parent class method");
}
}
class Child extends Parent {
public static void main(String[] args) {
Child obj = new Child();
[Link]();
}
}
```
3) Multilevel Inheritance
```java
class Grandparent {
void grandparentMethod() {
[Link]("Grandparent's method");
}
}
class Parent extends Grandparent {
void parentMethod() {
[Link]("Parent's method");
}
}
class Child extends Parent {
public static void main(String[] args) {
Child obj = new Child();
[Link]();
[Link]();
}
}
```
4) Hierarchical Inheritance
```java
class Parent {
void display() {
[Link]("Parent class method");
}
}
class Child1 extends Parent {
public static void main(String[] args) {
Child1 obj = new Child1();
[Link]();
}
}
class Child2 extends Parent {
public static void main(String[] args) {
Child2 obj = new Child2();
[Link]();
}
}
```
5) Four String Functions
```java
public class StringFunctions {
public static void main(String[] args) {
String str = "Hello World";
[Link]("Length: " + [Link]());
[Link]("Uppercase: " +
[Link]());
[Link]("Substring: " +
[Link](6));
[Link]("Replace: " +
[Link]('o', 'x'));
}
}
```
## 6) Thread Priority Methods
```java
class MyThread extends Thread {
public void run() {
[Link]([Link]().getNam
e() + " running with priority " +
[Link]().getPriority());
}
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MAX_PRIORITY);
[Link]();
[Link]();
}
}
```
## 7) JPasswordField
```java
import [Link].*;
import [Link].*;
public class PasswordFieldExample {
public static void main(String[] args) {
JFrame f = new JFrame("Password Field
Example");
JPasswordField pass = new JPasswordField();
JLabel l = new JLabel("Password:");
[Link](100, 100, 100, 30);
[Link](20, 100, 80, 30);
[Link](pass);
[Link](l);
[Link](300, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE
);
}
}
```
## 8) Color Menu with Disabled Black Option
```java
import [Link].*;
import [Link].*;
public class ColorMenu {
public static void main(String[] args) {
JFrame f = new JFrame("Color Menu");
JMenuBar mb = new JMenuBar();
JMenu colorMenu = new JMenu("Colors");
JMenuItem red = new JMenuItem("Red");
JMenuItem green = new JMenuItem("Green");
JMenuItem blue = new JMenuItem("Blue");
JMenuItem black = new JMenuItem("Black");
[Link](false);
[Link](red);
[Link](green);
[Link](blue);
[Link](black);
[Link](colorMenu);
[Link](mb);
[Link](400, 400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE
);
}
}
```
## 9) TextField and TextArea
```java
import [Link].*;
import [Link].*;
public class TextFieldTextArea {
public static void main(String[] args) {
JFrame f = new JFrame("TextField and
TextArea");
JTextField tf = new JTextField("This is a text
field");
[Link](50, 50, 200, 30);
JTextArea ta = new JTextArea("This is a text
area");
[Link](50, 100, 200, 100);
[Link](tf);
[Link](ta);
[Link](400, 400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE
);
}
}
```
## 10) Radiobutton in AWT
```java
import [Link].*;
import [Link].*;
public class RadioButtonExample {
public static void main(String[] args) {
Frame f = new Frame("RadioButton
Example");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox cb1 = new Checkbox("Male", cbg,
false);
Checkbox cb2 = new Checkbox("Female",
cbg, false);
[Link](100, 100, 50, 50);
[Link](100, 150, 70, 50);
[Link](cb1);
[Link](cb2);
[Link](400, 400);
[Link](null);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
[Link]();
}
});
}
}
```
## 11) Multiple Newspaper Selection using Choice
```java
import [Link].*;
import [Link].*;
public class NewspaperChoice {
public static void main(String[] args) {
Frame f = new Frame("Newspaper
Selection");
Label l = new Label("Select Newspapers:");
[Link](50, 50, 150, 30);
Choice c = new Choice();
[Link](50, 100, 150, 30);
[Link]("Times of India");
[Link]("The Hindu");
[Link]("Indian Express");
[Link]("Hindustan Times");
[Link]("Economic Times");
[Link](l);
[Link](c);
[Link](300, 300);
[Link](null);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
[Link]();
}
});
}
}
```
## 12) Border Layout
```java
import [Link].*;
public class BorderLayoutExample {
public static void main(String[] args) {
Frame f = new Frame("BorderLayout
Example");
Button b1 = new Button("NORTH");
Button b2 = new Button("SOUTH");
Button b3 = new Button("EAST");
Button b4 = new Button("WEST");
Button b5 = new Button("CENTER");
[Link](b1, [Link]);
[Link](b2, [Link]);
[Link](b3, [Link]);
[Link](b4, [Link]);
[Link](b5, [Link]);
[Link](300, 300);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
[Link]();
}
});
}
}
```
## 13) Grid Layout
```java
import [Link].*;
public class GridLayoutExample {
public static void main(String[] args) {
Frame f = new Frame("GridLayout Example");
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
[Link](new GridLayout(2, 3));
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](300, 300);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
[Link]();
}
});
}
}
```
## 14) Flow Layout
```java
import [Link].*;
public class FlowLayoutExample {
public static void main(String[] args) {
Frame f = new Frame("FlowLayout Example");
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
[Link](new FlowLayout());
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
[Link](300, 300);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
[Link]();
}
});
}
}
```
## 15) Image Icon in Swing
```java
import [Link].*;
public class ImageIconExample {
public static void main(String[] args) {
JFrame f = new JFrame("ImageIcon Example");
ImageIcon icon = new
ImageIcon("[Link]"); // Replace with actual
image path
JLabel label = new JLabel(icon);
[Link](label);
[Link](300, 300);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE
);
}
}
```
## 16) JTable for Student Names and Percentages
```java
import [Link].*;
public class JTableExample {
public static void main(String[] args) {
JFrame f = new JFrame("JTable Example");
String[][] data = {
{"John", "85.5"},
{"Mary", "92.3"}
};
String[] column = {"Name", "Percentage"};
JTable jt = new JTable(data, column);
[Link](30, 40, 200, 300);
JScrollPane sp = new JScrollPane(jt);
[Link](sp);
[Link](300, 300);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE
);
}
}
```
## 17) JTabbedPane
```java
import [Link].*;
public class JTabbedPaneExample {
public static void main(String[] args) {
JFrame f = new JFrame("JTabbedPane
Example");
JTabbedPane tp = new JTabbedPane();
JPanel p1 = new JPanel();
[Link](new JLabel("First Panel"));
JPanel p2 = new JPanel();
[Link](new JLabel("Second Panel"));
JPanel p3 = new JPanel();
[Link](new JLabel("Third Panel"));
[Link]("First", p1);
[Link]("Second", p2);
[Link]("Third", p3);
[Link](tp);
[Link](400, 400);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE
);
}
}
```
## 18) Parameterized Constructor
```java
class Student {
int id;
String name;
Student(int i, String n) {
id = i;
name = n;
}
void display() {
[Link](id + " " + name);
}
public static void main(String[] args) {
Student s1 = new Student(1, "John");
Student s2 = new Student(2, "Mary");
[Link]();
[Link]();
}
}
```
## 19) Method Overriding
```java
class Parent {
void display() {
[Link]("Parent's display");
}
}
class Child extends Parent {
@Override
void display() {
[Link]("Child's display");
}
public static void main(String[] args) {
Parent p = new Parent();
Parent c = new Child();
[Link]();
[Link]();
}
}
```
## 20) URL Class
```java
import [Link].*;
import [Link].*;
public class URLExample {
public static void main(String[] args) throws
Exception {
URL url = new
URL("[Link]
[Link]("Protocol: " +
[Link]());
[Link]("Host: " + [Link]());
[Link]("Port: " + [Link]());
[Link]("File: " + [Link]());
URLConnection conn = [Link]();
InputStream is = [Link]();
BufferedReader br = new BufferedReader(new
InputStreamReader(is));
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
}
}
```
## 21) JTree
```java
import [Link].*;
import [Link].*;
public class JTreeExample {
public static void main(String[] args) {
JFrame f = new JFrame("JTree Example");
DefaultMutableTreeNode root = new
DefaultMutableTreeNode("Root");
DefaultMutableTreeNode node1 = new
DefaultMutableTreeNode("Node 1");
DefaultMutableTreeNode node2 = new
DefaultMutableTreeNode("Node 2");
[Link](node1);
[Link](node2);
[Link](new
DefaultMutableTreeNode("Leaf 1"));
[Link](new
DefaultMutableTreeNode("Leaf 2"));
[Link](new
DefaultMutableTreeNode("Leaf 3"));
JTree tree = new JTree(root);
[Link](new JScrollPane(tree));
[Link](300, 300);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE
);
}
}
```
## 22) Method Overloading
```java
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]([Link](5, 10));
[Link]([Link](5.5, 10.5));
[Link]([Link](5, 10, 15));
}
}
```
## 24) Multiple Inheritance (Interface)
```java
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
[Link]("Method A");
}
public void methodB() {
[Link]("Method B");
}
public static void main(String[] args) {
C obj = new C();
[Link]();
[Link]();
}
}
```
## 25) Try, Catch, Finally
```java
public class TryCatchFinally {
public static void main(String[] args) {
try {
int result = 10 / 0;
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("Exception caught: " +
[Link]());
} finally {
[Link]("This block always
executes");
}
}
}
```
## 26) JRadioButton
```java
import [Link].*;
import [Link].*;
import [Link].*;
public class JRadioButtonExample {
public static void main(String[] args) {
JFrame f = new JFrame("JRadioButton
Example");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new
JRadioButton("Female");
[Link](50, 100, 100, 30);
[Link](50, 150, 100, 30);
ButtonGroup bg = new ButtonGroup();
[Link](rb1);
[Link](rb2);
[Link](rb1);
[Link](rb2);
[Link](300, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE
);
}
}
```
## 27) Menu Bar
```java
import [Link].*;
import [Link].*;
public class MenuBarExample {
public static void main(String[] args) {
JFrame f = new JFrame("Menu Bar Example");
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem exit = new JMenuItem("Exit");
[Link](open);
[Link](save);
[Link]();
[Link](exit);
[Link](fileMenu);
[Link](mb);
[Link](400, 400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE
);
}
}
```
## 28) Command Line Argument
```java
public class CommandLineArgument {
public static void main(String[] args) {
if ([Link] > 0) {
[Link]("Command line
arguments:");
for (String arg : args) {
[Link](arg);
}
} else {
[Link]("No command line
arguments provided.");
}
}
}
```
## 29) Print Even/Odd Numbers Using Two
Threads
```java
class EvenThread extends Thread {
public void run() {
for (int i = 0; i <= 10; i += 2) {
[Link]("Even: " + i);
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]();
}
}
}
}
class OddThread extends Thread {
public void run() {
for (int i = 1; i <= 10; i += 2) {
[Link]("Odd: " + i);
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]();
}
}
}
}
public class EvenOddThreads {
public static void main(String[] args) {
EvenThread t1 = new EvenThread();
OddThread t2 = new OddThread();
[Link]();
[Link]();
}
}
```
## 30) Wrapper Class
```java
public class WrapperClassExample {
public static void main(String[] args) {
// Autoboxing
Integer intObj = 10;
Double doubleObj = 10.5;
Character charObj = 'A';
Boolean boolObj = true;
// Unboxing
int i = intObj;
double d = doubleObj;
char c = charObj;
boolean b = boolObj;
[Link]("Integer object: " + intObj);
[Link]("Double object: " +
doubleObj);
[Link]("Character object: " +
charObj);
[Link]("Boolean object: " +
boolObj);
[Link]("int value: " + i);
[Link]("double value: " + d);
[Link]("char value: " + c);
[Link]("boolean value: " + b);
}
}
```
## 31) Four Methods of StringBuffer Class
```java
public class StringBufferMethods {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Hello");
// append()
[Link](" World");
[Link]("After append: " + sb);
// insert()
[Link](5, " Java");
[Link]("After insert: " + sb);
// reverse()
[Link]();
[Link]("After reverse: " + sb);
// delete()
[Link](0, 5);
[Link]("After delete: " + sb);
}
}
```
## 32) Four Methods of Vector Class
```java
import [Link];
public class VectorMethods {
public static void main(String[] args) {
Vector<String> vec = new Vector<>();
// add()
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
[Link]("Vector: " + vec);
// size()
[Link]("Size: " + [Link]());
// get()
[Link]("Element at index 1: " +
[Link](1));
// remove()
[Link](0);
[Link]("After removal: " + vec);
}
}
```
## 33) Package
```java
// File: mypackage/[Link]
package mypackage;
public class MyClass {
public void display() {
[Link]("This is MyClass from
mypackage");
}
}
// File: [Link]
import [Link];
public class PackageExample {
public static void main(String[] args) {
MyClass obj = new MyClass();
[Link]();
}
}
```
Note: For the package example, you need to:
1. Create a directory named "mypackage"
2. Place [Link] inside it
3. Compile both files
4. Run PackageExample