program to copy the content of one file into another.
import [Link].*;
public class Copy {
public static void main(String[] a) throws Exception {
FileInputStream in = new FileInputStream("[Link]");
FileOutputStream out = new FileOutputStream("[Link]");
int c;
while ((c = [Link]()) != -1) [Link](c);
[Link](); [Link]();
}
}
1. drawLine()
import [Link].*; import [Link].*;
public class A extends JPanel {
public void paint(Graphics g) { [Link](10, 10, 100, 10); }
public static void main(String[] a) {
JFrame f = new JFrame(); [Link](new A()); [Link](200,100);
[Link](3); [Link](true);
}
}
2. drawOval()
import [Link].*; import [Link].*;
public class B extends JPanel {
public void paint(Graphics g) { [Link](30, 30, 80, 40); }
public static void main(String[] a) {
JFrame f = new JFrame(); [Link](new B()); [Link](200,150);
[Link](3); [Link](true);
}
}
3. drawArc()
import [Link].*; import [Link].*;
public class C extends JPanel {
public void paint(Graphics g) { [Link](30, 30, 80, 40, 0, 180); }
public static void main(String[] a) {
JFrame f = new JFrame(); [Link](new C()); [Link](200,150);
[Link](3); [Link](true);
}
}
4. drawString()
import [Link].*; import [Link].*;
public class D extends JPanel {
public void paint(Graphics g) { [Link]("Hello", 50, 50); }
public static void main(String[] a) {
JFrame f = new JFrame(); [Link](new D()); [Link](200,100);
[Link](3); [Link](true);
}
}
file input/output stream methods
import [Link].*;
public class A {
public static void main(String[] a) throws Exception {
FileInputStream in = new FileInputStream("[Link]");
FileOutputStream out = new FileOutputStream("[Link]");
int c; while ((c = [Link]()) != -1) [Link](c);
[Link](); [Link]();
}
}
Thread life cycle
class MyThread extends Thread {
public void run() {
[Link]("Thread is running.");
}
}
public class ThreadLifeCycle {
public static void main(String[] args) {
MyThread t = new MyThread();
[Link](); // Starts the thread, calling its run() method
}
}
Single Inheritance
class A {
void showA() { [Link]("Class A"); }
}
class B extends A {
void showB() { [Link]("Class B"); }
}
public class Test {
public static void main(String[] args) {
B obj = new B();
[Link]();
[Link]();
}
}
Multilevel Inheritance
class A {
void showA() { [Link]("Class A"); }
}
class B extends A {
void showB() { [Link]("Class B"); }
}
class C extends B {
void showC() { [Link]("Class C"); }
}
public class Test {
public static void main(String[] args) {
C obj = new C();
[Link]();
[Link]();
[Link]();
}
}
Multiple Inheritance using Interfaces
interface A {
void showA();
}
interface B {
void showB();
}
class C implements A, B {
public void showA() { [Link]("A"); }
public void showB() { [Link]("B"); }
}
public class Test {
public static void main(String[] args) {
C obj = new C();
[Link](); [Link]();
}
}
Hierarchical Inheritance
class A {
void showA() { [Link]("Class A"); }
}
class B extends A {
void showB() { [Link]("Class B"); }
}
class C extends A {
void showC() { [Link]("Class C"); }
}
public class Test {
public static void main(String[] args) {
B b = new B();
C c = new C();
[Link](); [Link]();
[Link](); [Link]();
}
}
Switch case
public class Test {
public static void main(String[] args) {
int num = 5;
switch case
// Conditional operator example
String result = (num % 2 == 0) ? "Even" : "Odd";
[Link]("Number is " + result);
// Switch case example
switch (num) {
case 1: [Link]("One"); break;
case 5: [Link]("Five"); break;
default: [Link]("Other");
}
}
}
1. Default Constructor
class Demo {
Demo() {
[Link]("Default constructor called");
}
public static void main(String[] args) {
Demo d = new Demo();
}
}
2. Parameterized Constructor
class Student {
String name;
Student(String n) {
name = n;
}
void show() {
[Link]("Name: " + name);
}
}
public class Test {
public static void main(String[] args) {
Student s = new Student("Alice");
[Link]();
}}
3. Copy Constructor
class Demo {
int x;
Demo(int a) {
x = a;
}
Demo(Demo d) {
x = d.x;
}
public static void main(String[] args) {
Demo d1 = new Demo(5);
Demo d2 = new Demo(d1);
[Link]("Copy constructor: " + d2.x);
}
}
4. No-Argument Constructor (Non-Parameterized)
class Demo {
Demo() {
[Link]("No-Arg constructor called");
}
public static void main(String[] args) {
Demo d = new Demo();
}
}
NoMatchException
import [Link];
class NoMatchException extends Exception {
NoMatchException(String message) {
super(message);
}
}
public class PasswordCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter password: ");
String password = [Link]();
try {
if () {
throw new NoMatchException("Password does not match");
} else {
[Link]("Password accepted.");
}
} catch (NoMatchException e) {
[Link]([Link]());
}
}
}
import [Link].*;
import [Link].*;
public class WordCount {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
String line;
int wordCount = 0;
while ((line = [Link]()) != null) {
wordCount += [Link]("\\s+").length;
}
[Link]("Word count: " + wordCount);
[Link]();
}
}