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

oopsthroughjava

The document provides a comprehensive guide on using Java with Eclipse or NetBeans, covering project creation, code writing with auto-suggestions, code formatting, and refactoring. It includes examples demonstrating OOP principles such as encapsulation, inheritance, polymorphism, and abstraction, as well as exception handling with custom exceptions. Additionally, it illustrates file operations using the Random Access File class for reading and writing data.

Uploaded by

237r5a7307
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

oopsthroughjava

The document provides a comprehensive guide on using Java with Eclipse or NetBeans, covering project creation, code writing with auto-suggestions, code formatting, and refactoring. It includes examples demonstrating OOP principles such as encapsulation, inheritance, polymorphism, and abstraction, as well as exception handling with custom exceptions. Additionally, it illustrates file operations using the Random Access File class for reading and writing data.

Uploaded by

237r5a7307
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Use Eclipse or Net bean platform and acquaint yourself with the various
menus. Create a test project, add a test class, and run it. See how you can use
auto suggestions, auto fill. Try code formatter and code refactoring like
renaming variables, methods, and classes. Try debug step by step with a small
program of about 10 to 15 lines which contains at least one if else conditionand
a for loop.

1. Create a New Java Project:


 Open Eclipse IDE.
 Go to File > New > Java Project.
 Enter a project name and click Finish.
2. Add a Test Class:
 Right-click on the src folder in the project explorer.
 Select New > Class.
 Enter a class name (e.g., Test) and click Finish.
3. Write Code with Auto Suggestions and Auto Fill:
 Inside the Test class, start typing your code.
 You should see auto-suggestions as you type, helping you
complete statements or method names.
 You can use auto-fill by pressing Ctrl + Space to automatically
complete code snippets.
4. Use Code Formatter:
 You can format your code by right-clicking in the editor window
and selecting Source > Format or using the shortcut Ctrl + Shift
+ F.
5. Code Refactoring:
 Right-click on a variable, method, or class name.
 Select Refactor and choose from options like Rename to rename
the selected element.

public class Test {


public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
sum += i;
} else {
sum -= i;
}
}
System.out.println("Sum: " + sum);
}
}
Output
2. Write a Java program to demonstrate the OOP principles. [i.e., Encapsulation,
Inheritance, Polymorphism and Abstraction]
// Encapsulation: Using private access modifiers to restrict access to class
members
class EncapsulationDemo {
private int data;

// Getter method to access private data member


public int getData() {
return data;
}

// Setter method to modify private data member


public void setData(int data) {
this.data = data;
}
}

// Inheritance: Using extends keyword to create a subclass


class Vehicle {
public void method() {
System.out.println("Vehicle makes a method");
}
}

class Bike extends Vehicle {


@Override
public void method() {
System.out.println("Bike has two wheels");
}
}

// Polymorphism: Using method overriding


class Car extends Vehicle {
@Override
public void method() {
System.out.println("Car has four wheels");
}
}

// Abstraction: Using abstract classes or interfaces to provide a blueprint for other


classes
abstract class Shape {
abstract void draw();
}

class Rectangle extends Shape {


@Override
void draw() {
System.out.println("Drawing Rectangle");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing Circle");
}
}

public class OOPDemo {


public static void main(String[] args) {
// Encapsulation demo
EncapsulationDemo obj = new EncapsulationDemo();
obj.setData(10);
System.out.println("Data: " + obj.getData());

// Inheritance demo
Bike B = new Bike();
B.method(); // Output: Bike

// Polymorphism demo
Vehicle V1 = new Bike();
Vehicle V2 = new Car();
V1.method(); // Output: Bike
V2.method(); // Output: Car

// Abstraction demo
Shape shape1 = new Rectangle();
Shape shape2 = new Circle();
shape1.draw(); // Output: Drawing Rectangle
shape2.draw(); // Output: Drawing Circle
}
}
OUTPUT
3. Write a Java program to handle checked and unchecked exceptions. Also,
demonstrate the usage of custom exceptions in real time scenario.

// Checked Exception
class InsufficientBalanceException extends Exception {
public InsufficientBalanceException(String message) {
super(message);
}
}
// Unchecked Exception
class InvalidInputException extends RuntimeException {
public InvalidInputException(String message) {
super(message);
}
}
// Custom Exception for Real-Time Scenario
class AuthenticationException extends Exception {
public AuthenticationException(String message) {
super(message);
}
}
class BankAccount {
private double balance;
public BankAccount(double balance) {
this.balance = balance;
}
// Method to withdraw money
public void withdraw(double amount) throws InsufficientBalanceException {
if (amount > balance) {
throw new InsufficientBalanceException("Insufficient balance in the
account");
}
balance -= amount;
System.out.println("Withdrawal successful. Remaining balance: " +
balance);
}
}

public class ExceptionHandlingDemo {


// Method to validate user login
public static void login(String username, String password) throws
AuthenticationException {
if (!username.equals("admin") || !password.equals("password")) {
throw new AuthenticationException("Invalid username or password");
}
System.out.println("Login successful");
}

public static void main(String[] args) {


try {
// Handling checked exception
BankAccount account = new BankAccount(1000);
account.withdraw(1500); // Throws InsufficientBalanceException
// Handling unchecked exception
int number = Integer.parseInt("abc"); // Throws NumberFormatException
} catch (InsufficientBalanceException e) {
System.out.println("Error: " + e.getMessage());
} catch (NumberFormatException e) {
System.out.println("Error: Invalid input. Please enter a valid number.");
}

try {
// Handling custom exception in a real-time scenario
login("admin", "admin@123"); // Throws AuthenticationException
} catch (AuthenticationException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output
4. Write a Java program on Random Access File class to perform different read
and write operations.
import java.io.File;
import java.io.RandomAccessFile;
public class RandomAccessFileDemo {
public static void main(String[] args) {
try {
// Creating a RandomAccessFile object with read-write mode
RandomAccessFile file = new RandomAccessFile("data.txt", "rw");
// Writing data to the file
file.writeUTF("Hello, World!");
// Moving the file pointer to the beginning of the file
file.seek(0);
// Reading data from the file
String data = file.readUTF();
System.out.println("Data read from file: " + data);
// Appending data to the file
file.seek(file.length());
file.writeUTF("\nThis is a new line.");
// Moving the file pointer to the beginning of the file
file.seek(0);
// Reading data from the file after appending
data = file.readUTF();
System.out.println("Data read from file after appending: " + data);

// Moving the file pointer to a specific position


file.seek(7);
// Updating data at the specific position
file.writeUTF("Java");

// Moving the file pointer to the beginning of the file


file.seek(0);

// Reading data from the file after updating


data = file.readUTF();
System.out.println("Data read from file after updating: " + data);
// Closing the RandomAccessFile object
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output

You might also like