0% found this document useful (0 votes)
4 views

JAVA

The document contains various Java code snippets demonstrating file operations, graphical drawing, inheritance types, constructors, exception handling, and a word count program. Each section includes a brief example of the respective concept, such as copying file content, drawing shapes, and implementing different inheritance models. Additionally, it showcases exception handling with a custom exception and a word counting method using BufferedReader.

Uploaded by

vawagav635
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)
4 views

JAVA

The document contains various Java code snippets demonstrating file operations, graphical drawing, inheritance types, constructors, exception handling, and a word count program. Each section includes a brief example of the respective concept, such as copying file content, drawing shapes, and implementing different inheritance models. Additionally, it showcases exception handling with a custom exception and a word counting method using BufferedReader.

Uploaded by

vawagav635
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/ 6

program to copy the content of one file into another.

import java.io.*;
public class Copy {
public static void main(String[] a) throws Exception {
FileInputStream in = new FileInputStream("input.txt");
FileOutputStream out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1) out.write(c);
in.close(); out.close();
}
}

1. drawLine()

import javax.swing.*; import java.awt.*;


public class A extends JPanel {
public void paint(Graphics g) { g.drawLine(10, 10, 100, 10); }
public static void main(String[] a) {
JFrame f = new JFrame(); f.add(new A()); f.setSize(200,100);
f.setDefaultCloseOperation(3); f.setVisible(true);
}
}

2. drawOval()

import javax.swing.*; import java.awt.*;


public class B extends JPanel {
public void paint(Graphics g) { g.drawOval(30, 30, 80, 40); }
public static void main(String[] a) {
JFrame f = new JFrame(); f.add(new B()); f.setSize(200,150);
f.setDefaultCloseOperation(3); f.setVisible(true);
}
}

3. drawArc()

import javax.swing.*; import java.awt.*;


public class C extends JPanel {
public void paint(Graphics g) { g.drawArc(30, 30, 80, 40, 0, 180); }
public static void main(String[] a) {
JFrame f = new JFrame(); f.add(new C()); f.setSize(200,150);
f.setDefaultCloseOperation(3); f.setVisible(true);
}
}

4. drawString()
import javax.swing.*; import java.awt.*;
public class D extends JPanel {
public void paint(Graphics g) { g.drawString("Hello", 50, 50); }
public static void main(String[] a) {
JFrame f = new JFrame(); f.add(new D()); f.setSize(200,100);
f.setDefaultCloseOperation(3); f.setVisible(true);
}
}

file input/output stream methods


import java.io.*;
public class A {
public static void main(String[] a) throws Exception {
FileInputStream in = new FileInputStream("in.txt");
FileOutputStream out = new FileOutputStream("out.txt");
int c; while ((c = in.read()) != -1) out.write(c);
in.close(); out.close();
}
}
Thread life cycle
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running.");
}
}
public class ThreadLifeCycle {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start(); // Starts the thread, calling its run() method
}
}

Single Inheritance
class A {
void showA() { System.out.println("Class A"); }
}
class B extends A {
void showB() { System.out.println("Class B"); }
}
public class Test {
public static void main(String[] args) {
B obj = new B();
obj.showA();
obj.showB();
}
}
Multilevel Inheritance
class A {
void showA() { System.out.println("Class A"); }
}
class B extends A {
void showB() { System.out.println("Class B"); }
}
class C extends B {
void showC() { System.out.println("Class C"); }
}
public class Test {
public static void main(String[] args) {
C obj = new C();
obj.showA();
obj.showB();
obj.showC();
}
}

Multiple Inheritance using Interfaces


interface A {
void showA();
}
interface B {
void showB();
}
class C implements A, B {
public void showA() { System.out.println("A"); }
public void showB() { System.out.println("B"); }
}
public class Test {
public static void main(String[] args) {
C obj = new C();
obj.showA(); obj.showB();
}
}
Hierarchical Inheritance
class A {
void showA() { System.out.println("Class A"); }
}
class B extends A {
void showB() { System.out.println("Class B"); }
}
class C extends A {
void showC() { System.out.println("Class C"); }
}
public class Test {
public static void main(String[] args) {
B b = new B();
C c = new C();
b.showA(); b.showB();
c.showA(); c.showC();
}
}

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";
System.out.println("Number is " + result);

// Switch case example


switch (num) {
case 1: System.out.println("One"); break;
case 5: System.out.println("Five"); break;
default: System.out.println("Other");
}
}
}

1. Default Constructor

class Demo {
Demo() {
System.out.println("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() {
System.out.println("Name: " + name);
}
}
public class Test {
public static void main(String[] args) {
Student s = new Student("Alice");
s.show();
}}
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);
System.out.println("Copy constructor: " + d2.x);
}
}

4. No-Argument Constructor (Non-Parameterized)

class Demo {
Demo() {
System.out.println("No-Arg constructor called");
}

public static void main(String[] args) {


Demo d = new Demo();
}
}

NoMatchException

import java.util.Scanner;

class NoMatchException extends Exception {


NoMatchException(String message) {
super(message);
}
}
public class PasswordCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter password: ");
String password = sc.nextLine();

try {
if (!password.equals("MSBTE")) {
throw new NoMatchException("Password does not match");
} else {
System.out.println("Password accepted.");
}
} catch (NoMatchException e) {
System.out.println(e.getMessage());
}
}
}

import java.io.*;
import java.util.*;

public class WordCount {


public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("example.txt"));
String line;
int wordCount = 0;

while ((line = br.readLine()) != null) {


wordCount += line.split("\\s+").length;
}

System.out.println("Word count: " + wordCount);


br.close();
}
}

You might also like