Java
Java
import java.util.Scanner;
int x, y;
x = sc.nextInt();
y = sc.nextInt();
if (y != 0) {
} else {
}
Positive or negative using if
import java.util.Scanner;
int a;
a = sc.nextInt();
if (a > 0)
else if (a < 0)
else
System.out.println("Zero");
}
Greatest of three numbers
import java.util.Scanner;
int x, y, z;
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();
if (x >= y) {
if (x >= z) {
} else {
} else {
if (y >= z) {
} else {
}
Reverse given number
import java.util.Scanner;
int n, a, m = 0;
n = sc.nextInt();
do {
a = n % 10;
m = m * 10 + a;
n = n / 10;
}
Factorial of a given num
import java.util.Scanner;
int fact = 1;
int i = 1;
fact *= i;
i++;
}
Sorting arrays
import java.util.Scanner;
int n, temp;
n = sc.nextInt();
a[i] = sc.nextInt();
temp = a[i];
a[i] = a[j];
a[j] = temp;
System.out.println("Ascending order:");
System.out.println(a[i]);
}
Finding the grade
import java.util.Scanner;
// for >= 90
case 10:
case 9:
grade = "A";
break;
// for 80-89
case 8:
grade = "B";
break;
// for 70-79
case 7:
grade = "C";
break;
// for 60-69
case 6:
grade = "D";
break;
// for 40-59
case 5:
grade = "E";
break;
// for < 40
default:
grade = "F";
break;
package Java;
import java.util.Scanner;
class Laptop {
Brand = b;
RAM = r;
HDD = h;
Processor = p;
Generation = g;
void display() {
String a = sc.nextLine();
String b = sc.nextLine();
String c = sc.nextLine();
String d = sc.nextLine();
String e = sc.nextLine();
x.display();
sc.close();
}
Constructor overloading
package sakthi;
import java.util.Scanner;
class Rectangle {
int length;
int width;
int height;
width = wid;
height = hei;
length = 22;
length = l;
width = w;
height = h;
void display() {
}
public class ConstructorOverloading {
System.out.println("Constructor 1");
a.display();
System.out.println("\nConstructor 2");
b.display();
System.out.println("Enter width:");
int w =scan.nextInt();
System.out.println("Enter height:");
int h =scan.nextInt();
System.out.println("Enter length:");
int l =scan.nextInt();
c.dispaly();
scan.close();
Method overloading
class Addition {
return a + b;
}
double Add(double a, double b, double c) {
return a + b + c;
return a + b;
return a + b;
Abstract class
int amount;
super(amount);
void taxCalculation() {
bill.display();
bill.taxCalculation();
}
Runtime polymorphism
import java.util.Scanner;
class Product {
void disp() {
void disp() {
void disp() {
}
}
void disp() {
croma.disp();
single inhertitacnce
class Bank {
int AccNo;
String Name;
double Balance;
AccNo = a;
Name = n;
Balance = b;
}
void display() {
String Debit;
String Credit;
super(a, n, b);
Debit = d;
Credit = c;
void displayCards() {
x.display();
x.displayCards();
}
Multiple inheritance
class Product {
int pid;
String pname;
Product(int a, String b) {
pid = a;
pname = b;
void display() {
double cost;
String category;
super(a, b);
cost = c;
category = d;
void displayType() {
}
class Popularity extends Type {
String brand;
int ratings;
super(a, b, c, d);
brand = e;
ratings = f;
void displayPopularity() {
display();
displayType();
x.displayPopularity();
Hierarchical inheritance
class Banks {
class ICICI {
class Canara {
class Yes {
// Example:
b.display();
// Similar instantiation and method calls for ICICI, Canara, and Yes
Implementation of packages
package Java;
System.out.println("private:" + password);
System.out.println("protected:" + mailId);
void displayDefault() {
System.out.println("Default:" + contactNo);
x.displayPrivate();
x.displayProtected();
x.displayPublic();
x.displayDefault();
package Java;
code2
y.displayPublic();
y.displayProtected();
y.displayDefault();
Code3
package Java;
y.displayPublic();
y.displayProtected();
y.displayDefault();
Implementation of interface
interface shapes {
double volume();
}
void draw();
int r = 15;
return (PI * r * r); // Corrected the formula to calculate the area instead of volume
System.out.println("Drawing a Circle");
x.draw();
}
Exception handling 1,2,3
} else {
try {
validate(25);
System.out.println("Eligible To Vote");
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
Code2
class ExceptionDemo {
int div;
try {
div = 5 / 2;
div = 5 / 0;
System.out.println("The Result of Division is:" + div);
} catch (ArithmeticException e) {
System.out.println("Division by zero.");
Code 3
package sakthi;
int div;
try {
div = 5 / 2;
} catch (ArithmeticException e) {
} finally {
Implementation of threads
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
class RecThread2 {
x.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Code 2
this.name = name;
this.wait = wait;
try {
Thread.sleep(wait);
System.out.println(this.name);
} catch (InterruptedException e) {
counterA.start();
counterB.start();
System.out.println("Main Thread");
Thread synchronization
class Table {
try {
Thread.sleep(500);
} catch (Exception e) {
System.out.println("Thread interrupted.");
int n;
Table t;
MyThread(Table x, int y) {
this.t = x;
this.n = y;
t.printTable(this.n);
x.start();
y.start();
}
String manipulation
System.out.println(msg.toUpperCase());
System.out.println(msg.toLowerCase());
System.out.println(msg.length());
System.out.println(msg.equals(str));
System.out.println(msg.equals(s));
System.out.println(s.trim());
System.out.println(msg.concat(" M.O.P."));
System.out.println(msg.charAt(5));
System.out.println(msg.replace("Our", "the"));
System.out.println(msg.substring(7));
System.out.println(msg.contains("Our"));
System.out.println(ni[i]);
}
String buffer
package sakthi;
sb.append("college");
System.out.println(sb.capacity()); // Output: 24
System.out.println(sb.charAt(5)); // Output: m
sb.delete(3, 7);
sb.reverse();
import java.io.*;
try {
while (!str.equals("stop")) {
fop.write(line);
str = br.readLine();
fop.close();
} catch (Exception e) {
System.out.println(e);
try {
int i;
System.out.print((char) i);
fin.close();
} catch (Exception e) {
System.out.println(e);
Code2
import java.io.*;
public class FileCopy {
try {
int i;
fop.write(i);
fin.close();
fop.close();
System.out.println("File Copied");
} catch (Exception e) {
System.out.println(e);
Stack
import java.io.*;
import java.util.*;
stack.push(20);
stack.push(30);
stack.push(50);
Vector
Code 1
import java.io.*;
import java.net.*;
try {
System.out.println("Client Started....");
System.out.println("Sending msg");
dout.flush();
dout.close();
s.close();
} catch (Exception e) {
System.out.println(e);
}
Code 2
import java.io.*;
import java.util.*;
stack.push(20);
stack.push(30);
stack.push(50);
Network programming
import java.io.*;
import java.net.*;
try {
System.out.println("Server Started...");
Socket s = ss.accept(); // Accepts a connection
System.out.println("message=" + str);
} catch (Exception e) {
System.out.println(e);
url class
import java.net.*;
try {
} catch (Exception e) {
System.out.println(e);
Awt controls
import java.awt.*;
class JobApplication extends Frame {
JobApplication() {
setLayout(null);
add(l1);
add(l2);
add(t1);
add(l3);
add(c1);
add(c2);
Panel implementation
import java.awt.*;
import java.awt.event.*;
public PanelDemo() {
p1 = new Panel();
p2 = new Panel();
b1 = new Button("One");
b2 = new Button("Two");
b3 = new Button("Three");
b4 = new Button("Four");
p1.add(l1);
p1.add(b1);
p1.add(b2);
p2.add(l2);
p2.add(b3);
p2.add(b4);
p1.setBackground(Color.GREEN);
p2.setBackground(Color.YELLOW);
f.add(p1);
f.add(p2);
f.setLayout(new FlowLayout());
f.setSize(300, 300);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
System.exit(0);
});
}
new PanelDemo();
Panel code 2
import java.awt.*;
import java.awt.event.*;
public PanelDemo() {
p1 = new Panel();
p2 = new Panel();
b1 = new Button("One");
b2 = new Button("Two");
b3 = new Button("Three");
b4 = new Button("Four");
p1.add(l1);
p1.add(b1);
p1.add(b2);
p2.add(l2);
p2.add(b3);
p2.add(b4);
p1.setBackground(Color.GREEN);
p2.setBackground(Color.YELLOW);
f.add(p1);
f.add(p2);
f.setLayout(new FlowLayout());
f.setSize(300, 300);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
System.exit(0);
});
Simple calculator:
import java.awt.*;
import java.awt.event.*;
Calculator() {
// Giving Coordinates
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p1.add(l3);
p1.add(t3);
add(p1);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.setLayout(new FlowLayout());
add(p2);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
setLayout(new FlowLayout());
setVisible(true);
setSize(300, 300);
addWindowListener(new WindowAdapter() {
System.exit(0);
});
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
if (e.getSource() == b1) {
t3.setText(String.valueOf(n1 + n2));
t3.setText(String.valueOf(n1 - n2));
t3.setText(String.valueOf(n1 * n2));
} else if (e.getSource() == b4) {
t3.setText(String.valueOf(n1 / n2));
System.exit(0);
new Calculator();
Implementing Menu
import java.awt.*;
import java.awt.event.*;
// Create menus
public MenubarDemo() {
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(closeItem);
fileMenu.add(exitItem);
saveMenu.add(pdfItem);
saveMenu.add(jpegItem);
editMenu.add(copyItem);
editMenu.add(cutItem);
editMenu.add(findItem);
mb.add(fileMenu);
mb.add(editMenu);
mb.add(saveMenu);
f.setMenuBar(mb);
newItem.addActionListener(this);
openItem.addActionListener(this);
closeItem.addActionListener(this);
exitItem.addActionListener(this);
copyItem.addActionListener(this);
f.add(label);
f.setVisible(true);
f.setSize(400, 400);
@Override
// ...
new MenubarDemo();
Code 2
import java.awt.*;
import java.awt.event.*;
f.add(label);
f.setSize(300, 300);
f.setLayout(new FlowLayout());
f.setVisible(true);
label.addMouseListener(this);
@Override
label.setText("Mouse Clicked");
@Override
label.setText("Mouse Entered");
@Override
label.setText("Mouse Exited");
@Override
label.setText("Mouse Pressed");
}
@Override
label.setText("Mouse Released");
new MouseListenerDemo();
Dialog box
import java.awt.*;
import java.awt.event.*;
public DialogDemo() {
d.setLayout(new FlowLayout());
button.addActionListener(new ActionListener() {
d.setVisible(false);
}
});
d.add(label);
d.add(button);
d.setSize(200, 100);
d.setVisible(true);
new DialogDemo();
import java.awt.*;
import java.applet.*;
private Thread t;
t.start();
while (true) {
x += 10;
if (x > 300)
x = 50;
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
setBackground(Color.yellow);
setForeground(Color.red);
g.setFont(f);
g.drawString(display, x, y);
Event handling
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
setBackground(Color.white);
addKeyListener(this);
}
@Override
setBackground(Mycolor);
// Empty methods
@Override
@Override
Graphics class
import java.applet.Applet;
import java.awt.*;
g.setColor(Color.red);
g.setColor(Color.pink);
Layout
Border layout:
import java.awt.*;
import javax.swing.*;
BorderLayoutDemo() {
panel.setLayout(new BorderLayout());
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutDemo();
Grid layout:
import java.awt.*;
import java.awt.event.*;
fl.add(b1);
fl.add(b2);
fl.add(b3);
fl.add(b4);
fl.add(b5);
fl.add(b6);
fl.add(b7);
fl.add(b8);
fl.add(b9);
fl.add(b10);
fl.setSize(500, 500);
fl.setVisible(true);
fl.addWindowListener(new WindowAdapter() {
System.exit(0);
});
Flow layout
import java.awt.*;
import java.awt.event.*;
fl.setLayout(new FlowLayout());
fl.add(b1);
fl.add(b2);
fl.add(b3);
fl.add(b4);
fl.add(b5);
fl.add(b6);
fl.add(b7);
fl.add(b8);
fl.add(b9);
fl.add(b10);
fl.setSize(300, 300);
fl.setVisible(true);
fl.addWindowListener(new WindowAdapter() {
System.exit(0);
});
Jdbc
Code 1
import java.sql.*;
import java.util.Scanner;
try {
int n, i, t;
String query;
Class.forName("com.mysql.cj.jdbc.Driver");
Statement st = con.createStatement();
st.execute(query);
System.out.println("Table Created...");
n = sc.nextInt();
int id = sc.nextInt();
query = "insert into cus_tomer values (" + id + ", '" + name + "', " + age + ")";
t = st.executeUpdate(query);
System.out.println("Record Inserted");
}
con.close();
} catch (Exception e) {
e.printStackTrace();
Code 2
import java.sql.*;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish connection
// Execute queries
Statement st = con.createStatement();
while (rs.next()) {
// Close connection
con.close();
} catch (Exception e) {
e.printStackTrace();
Code 3
package JDBC;
import java.sql.*;
import java.util.*;
int rno;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Statement st = con.createStatement();
rno = sc.nextInt();
name = sc.next();
query = "update cus_tomer set custname = '" + name + "' where custid = " + rno;
st.execute(query);
System.out.println("Record Updated");
System.out.println("Enter id to be deleted:");
rno = sc.nextInt();
st.execute(query);
System.out.println("Record Deleted");
} catch (Exception e) {
System.out.println(e);
Applet image:
import java.applet.Applet;
import java.awt.*;
Image pic;