Java Assignment
Shivika Mittal A2305222388
Basic
1) Method Overloading
public class MethodOverload {
public int add(int a, int b) { return a + b; }
public double add(double a, double b) { return a + b; }
public int add(int a, int b, int c) { return a + b + c; }
public static void main(String[] args) {
MethodOverload m = new MethodOverload();
[Link]([Link](2,3));
[Link]([Link](2.5,3.1));
[Link]([Link](1,2,3));
}
}
2) String Handling Methods
public class StringDemo {
public static void main(String[] args) {
String s = " Hello, Java! ";
[Link]("length: " + [Link]());
[Link]("trim: '" + [Link]() + "'");
[Link]("upper: " + [Link]());
[Link]("contains 'Java': " + [Link]("Java"));
[Link]("replace: " + [Link]("Java","World"));
[Link]("charAt(1): " + [Link](1));
for (String part : [Link](",")) [Link]("-> '"+part+"'");
}
}
3) Implement Interface
interface Shape { double area(); }
class Circle implements Shape {
double r;
Circle(double r) { this.r = r; }
public double area() { return [Link] * r * r; }
}
public class InterfaceDemo {
public static void main(String[] args) {
Shape c = new Circle(2.0);
[Link]("Circle area: " + [Link]());
}
}
4) Single Inheritance
class Animal {
void speak() { [Link]("Animal sound"); }
}
class Dog extends Animal {
void speak() { [Link]("Woof"); }
}
public class InheritanceDemo {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
}
5) Method Overriding
class Parent {
void greet(){ [Link]("Hello from Parent"); }
}
class Child extends Parent {
@Override void greet(){ [Link]("Hello from Child"); }
}
public class OverrideDemo {
public static void main(String[] args) {
Parent p = new Child();
[Link]();
}
}
6) Multiple Inheritance using Interface
interface A { void a(); }
interface B { void b(); }
class Multi implements A, B {
public void a(){ [Link]("a"); }
public void b(){ [Link]("b"); }
}
public class MultipleInterfaceDemo {
public static void main(String[] args) {
Multi m = new Multi();
m.a();
m.b();
}
}
7) Command Line Argument
public class CmdArgs {
public static void main(String[] args) {
[Link]("Got " + [Link] + " args.");
for (int i=0; i<[Link]; i++)
[Link](i + ": " + args[i]);
}
}
8) Static Class, Method, Block, Variable
public class StaticDemo {
static int count = 0;
static {
[Link]("Static block executed");
count = 5;
}
static void show() {
[Link]("count = " + count);
}
public static void main(String[] args) {
[Link]();
}
}
9) Abstract Class and Method
abstract class Vehicle {
abstract void start();
void info(){ [Link]("I am a vehicle"); }
}
class Car extends Vehicle {
void start(){ [Link]("Car started"); }
}
public class AbstractDemo {
public static void main(String[] args) {
Vehicle v = new Car();
[Link]();
[Link]();
}
}
10) Wrapper Class, Autoboxing, Unboxing
public class WrapperDemo {
public static void main(String[] args) {
Integer boxed = 10; // autoboxing
int primitive = boxed; // unboxing
[Link]("boxed: " + boxed + ", primitive: " + primitive);
}
}
11) throw and throws
class ThrowDemo {
static void check(int age) throws Exception {
if (age < 18) throw new Exception("Too young");
}
public static void main(String[] args) {
try {
check(16);
} catch (Exception e) {
[Link]("Caught: " + [Link]());
}
}
}
12) Checked & Unchecked Exceptions
public class ExTypes {
public static void main(String[] args) {
try {
[Link](1/0);
} catch (ArithmeticException e) {
[Link]("Divide by zero");
}
try {
[Link](1);
} catch (InterruptedException e) {
[Link]("Interrupted");
}
}
}
13) Threads
class MyRunnable implements Runnable {
public void run() { [Link]("Runnable running: " +
[Link]().getName()); }
}
class MyThread extends Thread {
public void run() { [Link]("Thread running: " + getName()); }
}
public class ThreadDemo {
public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable());
MyThread t2 = new MyThread();
[Link]();
[Link]();
}
}
14) JDBC Application (SQLite Example)
import [Link].*;
public class JdbcDemo {
public static void main(String[] args) {
String url = "jdbc:sqlite:[Link]"; // Example SQLite DB
try (Connection conn = [Link](url);
Statement st = [Link]()) {
[Link]("CREATE TABLE IF NOT EXISTS person(id INTEGER PRIMARY
KEY, name TEXT)");
[Link]("INSERT INTO person(name) VALUES('Alice')");
ResultSet rs = [Link]("SELECT * FROM person");
while ([Link]())
[Link]([Link]("id") + ": " + [Link]("name"));
} catch (SQLException e) {
[Link]();
}
}
}
Advance
15) Thread Synchronization
class Counter {
private int c = 0;
public synchronized void inc() { c++; }
public int get() { return c; }
}
public class SyncDemo {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
Runnable r = () -> { for(int i=0;i<1000;i++) [Link](); };
Thread t1 = new Thread(r), t2 = new Thread(r);
[Link](); [Link]();
[Link](); [Link]();
[Link]("Count: " + [Link]());
}
}
16) Define and Implement a Package
mypkg/[Link]
package mypkg;
public class Utils {
public static void hi(){
[Link]("Hello from mypkg");
}
}
[Link]
import [Link];
public class Main {
public static void main(String[] args){
[Link]();
}
}
17) Lambda Expressions & Method References
import [Link].*;
public class LambdaDemo {
public static void main(String[] args) {
List<Integer> nums = [Link](1,2,3,4);
[Link](n -> [Link](n)); // Lambda
[Link]([Link]::println); // Method reference
}
}
18)Java Database Connectivity (JDBC) using Applet
import [Link].*;
import [Link].*;
import [Link].*;
public class JdbcApplet extends Applet {
String msg = "";
public void init() {
try {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/testdb", "root", "pass");
ResultSet rs = [Link]().executeQuery("SELECT name FROM students");
while ([Link]()) msg += [Link](1) + " ";
[Link]();
} catch (Exception e) { msg = [Link](); }
}
public void paint(Graphics g) { [Link](msg, 20, 20); }
}
Run using HTML:
<html>
<body>
<applet code="[Link]" width="300" height="200"></applet>
</body>
</html>
19) Layouts using Applet
import [Link].*;
import [Link].*;
public class LayoutApplet extends Applet {
public void init() {
setLayout(new BorderLayout());
Panel p = new Panel(new FlowLayout());
[Link](new Button("One")); [Link](new Button("Two"));
add(p, [Link]);
Panel g = new Panel(new GridLayout(2,2));
for(int i=1;i<=4;i++) [Link](new Button(""+i));
add(g, [Link]);
}
}
<html>
<body>
<applet code="[Link]" width="300" height="200"></applet>
</body>
</html>
20) File Handling
import [Link].*;
public class FileDemo {
public static void main(String[] args) throws Exception {
Path p = [Link]("[Link]");
[Link](p, "Hello\n");
[Link]([Link](p));
}
}
21) Event Handling
import [Link].*;
import [Link].*;
public class AwtEventShort extends Frame {
public AwtEventShort() {
Button b = new Button("Click");
[Link](e -> [Link]("Clicked!"));
add(b);
setSize(200,100);
setLayout(new FlowLayout());
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we){ dispose(); }
});
}
public static void main(String[] a){ new AwtEventShort(); }
}
22) Networking
import [Link].*;
import [Link].*;
public class SimpleServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(5000);
Socket s = [Link]();
BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));
[Link]("Client: " + [Link]());
[Link]();
}
}
23) Implement AWT
java
Copy code
import [Link].*;
public class AwtDemo extends Frame {
public AwtDemo() {
add(new Button("Click"));
setSize(200,100);
setLayout(new FlowLayout());
setVisible(true);
}
public static void main(String[] a){ new AwtDemo(); }
}
24) Implement Swing
java
Copy code
import [Link].*;
public class SwingDemo {
public static void main(String[] a) {
JFrame f = new JFrame("Swing");
[Link](new JButton("Click"));
[Link](200,100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
25) Implement Servlet
java
Copy code
import [Link].*;
import [Link].*;
import [Link].*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
[Link]("text/html");
[Link]().println("<h1>Hello Servlet</h1>");
}
}
(Run in a servlet container like Tomcat.)
26) Implement Beans
java
Copy code
import [Link];
public class MyBean implements Serializable {
private String name;
public String getName(){ return name; }
public void setName(String n){ name = n; }
}
27) NIO File Copy
import [Link].*;
public class NioDemo {
public static void main(String[] args) throws Exception {
Path src = [Link]("[Link]"), dst = [Link]("[Link]");
[Link](src, dst, StandardCopyOption.REPLACE_EXISTING);
[Link]("Copied");
}
}
28) Concurrency
import [Link].*;
public class ConcurrencyDemo {
public static void main(String[] args) throws Exception {
ExecutorService ex = [Link](2);
[Link](() -> [Link]("Task1 by " + [Link]().getName()));
[Link](() -> [Link]("Task2 by " + [Link]().getName()));
[Link]();
[Link](1, [Link]);
}
}