Java File Uday
Java File Uday
class Addition1
{
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
int a = s1.nextInt();
int b = s1.nextInt();
int add = a + b;
System.out.println("Addition of two numbers is " + add);
s1.close();
}
}
OUTPUT:
Page |2
class circle
{
public static void main(String[] args)
{
Scanner S = new Scanner(System.in);
Area A = new Area();
System.out.println("Enter Radius of Circle");
int b = S.nextInt();
double AREA = A.circle(b);
System.out.print("Area Of The Circle Is : "+AREA+" sq units.");
S.close();
}
}
OUTPUT:
Page |3
OUTPUT:
Page |4
OUTPUT:
Page |5
int b = obj1.nextInt();
int c = obj1.nextInt();
int BIGGER = obj.bigger(a,b,c);
System.out.print("\nThe Largest Number is "+BIGGER);
obj1.close();
}
}
OUTPUT:
Page |7
double result = 0;
switch (operator) {
case '+':
result = firstNumber + secondNumber;
break;
case '-':
result = firstNumber - secondNumber;
break;
case '*':
result = firstNumber * secondNumber;
break;
case '/':
result = firstNumber / secondNumber;
break;
default:
System.out.println("Invalid operator.");
break;
}
OUTPUT:
Page |9
OUTPUT:
P a g e | 10
class rect2
{
public static void main(String[] args)
{
Scanner S = new Scanner(System.in);
rect r1 = new rect();
int a,b;
a = S.nextInt();
b = S.nextInt();
int Area = r1.area(a,b);
System.out.println("The area of rectangle is");
System.out.println(Area);
S.close();
}
}
OUTPUT:
P a g e | 11
OUTPUT:
P a g e | 12
OUTPUT:
P a g e | 14
// Subclass
class Dog01 extends Animal01 {
void bark() {
System.out.println("The dog barks.");
}
}
Output:
P a g e | 15
// Child class 1
class Dog11 extends Animal11 {
void bark() {
System.out.println("The dog barks.");
}
}
// Child class 2
class Cat11 extends Animal11 {
void meow() {
System.out.println("The cat meows.");
}
}
myDog.eat(); // Call the eat() method from the Parent class (Animal) via
Dog
myDog.bark(); // Call the bark() method from the Dog class
System.out.println();
myCat.eat(); // Call the eat() method from the Parent class (Animal) via Cat
myCat.meow(); // Call the meow() method from the Cat class
}
}
P a g e | 17
OUTPUT:
P a g e | 18
// Getter methods
public String getName() {
return name;
}
OUTPUT:
P a g e | 20
// Concrete method
public void display() {
System.out.println("This is a shape.");
}
}
// Main class
class absclass
{
public static void main(String[] args) {
// Creating objects of concrete classes
Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(4, 6);
// Calling methods
circle.display();
circle.calculateArea();
rectangle.display();
rectangle.calculateArea();
}
}
OUTPUT:
P a g e | 22
}
}
OUTPUT:
P a g e | 24
interface Mammal {
void eat();
}
class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
dog.eat();
}
}
OUTPUT:
P a g e | 25
// Using StringBuilder
StringBuilder sb = new StringBuilder();
sb.append(str1);
sb.append(str2);
String result3 = sb.toString();
System.out.println("Using StringBuilder: " + result3);
// Using StringBuffer
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(str1);
stringBuffer.append(str2);
String result4 = stringBuffer.toString();
System.out.println("Using StringBuffer: " + result4);
}
}
OUTPUT:
P a g e | 26
OUTPUT:
P a g e | 27
OUTPUT:
P a g e | 28
package myPackage;
import myPackage.MyClass;
OUTPUT:
P a g e | 29
try {
int result = dividend / divisor;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
scanner.close();
}
}
OUTPUT:
P a g e | 30
OUTPUT:
P a g e | 31
OUTPUT:
P a g e | 32
Animal(String name) {
this.name = name;
}
void sound() {
System.out.println("Animal makes a sound");
}
}
void sound() {
super.sound(); // calling the sound() method of the parent class
System.out.println("Dog barks");
}
void display() {
System.out.println("Name: " + super.name); // accessing the name variable
of the parent class
System.out.println("Breed: " + breed);
}
}
OUTPUT:
P a g e | 34
class Main {
public static void main(String[] args) {
Thread t1 = new Thread(new Task1());
Thread t2 = new Thread(new Task2());
t1.start();
t2.start();
}
}
OUTPUT:
P a g e | 35
class Main {
public static void main(String[] args) {
Task1 t1 = new Task1();
Task2 t2 = new Task2();
t1.start();
t2.start();
}
}
OUTPUT:
P a g e | 36
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("Connecting to the database...");
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
// Create a table
String createTableSQL = "CREATE TABLE IF NOT EXISTS employees (" +
"id INT AUTO_INCREMENT PRIMARY KEY, " +
P a g e | 37
// Clean up
resultSet.close();
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
P a g e | 38
}
}
}
}
Output:
P a g e | 39
import java.io.*;
try {
// Create an instance of FileWriter
FileWriter fileWrite = new FileWriter("charfile.txt");
fileWrite.write(str); // write the string to the file
fileWrite.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
// Create an instance of FileReader
FileReader fileRead = new FileReader("charfile.txt");
Output:
P a g e | 40
import java.io.*;
try {
// Create a file input stream
FileInputStream fin = new FileInputStream("mydata.dat");
}
}
Output: