Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Java Class and Object Concepts with File I/O Implementation

Tech May 16 4

Core Concepts of Classes and Objects A class serves as a blueprint for creating objects, defining their structure and behavior. Objects represent specific instances with unique:

Behavior: Actions an object can perform State: Current properties or attributes Idetnity: Unique distinguishnig characteristics

Class-Object Relationship Classes are abstract templates that exist only in code, while objects are runtime instances with actual memory allocation. Key differences:

Classes contain no data until instantiated Each object maintains independent state Object properties change through method interactions

Object Initialization Java requires explicit object construction before use:

Employee developer = new Employee("John", 85000);

Predefined Class Utilization Essential Java API classes:

Math: Mathematical operations String: Text manipulation Scanner: Input processing LocalDate: Date handling

Custom Class Implementation Key components in user-defined classes:

Field Declarations

private String employeeName;  // Instance field
private static int employeeCount;  // Static field

Method Types

Constructor: Initializes new instances Accessor: Retrieves object state Mutator: Modifies object state Static: Class-level operations

Method Overloading Multiple methods sharing a name but differing in parameters:

public Employee(String name) { ... }
public Employee(String name, int id) { ... }

File Input/Output Implementation

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

public class FileProcessor {
    public static void main(String[] args) throws IOException {
        // Write to file
        PrintWriter fileWriter = new PrintWriter("data.txt");
        fileWriter.println("Name,Department,Salary");
        fileWriter.println("Alice,Engineering,95000");
        fileWriter.close();
        
        // Read from file
        Scanner fileScanner = new Scanner(new File("data.txt"));
        while(fileScanner.hasNextLine()) {
            String record = fileScanner.nextLine();
            Scanner recordParser = new Scanner(record).useDelimiter(",");
            String name = recordParser.next();
            String department = recordParser.next();
            String salary = recordParser.next();
            System.out.printf("Name: %s | Department: %s | Salary: %s%n", 
                             name, department, salary);
        }
        fileScanner.close();
    }
}

Custom Class Example

public class Employee {
    private String fullName;
    private double annualCompensation;
    private LocalDate startDate;

    public Employee(String name, double salary, int year, int month, int day) {
        fullName = name;
        annualCompensation = salary;
        startDate = LocalDate.of(year, month, day);
    }

    public void adjustCompensation(double percentage) {
        double adjustment = annualCompensation * percentage / 100;
        annualCompensation += adjustment;
    }
}

Static Members Implementation

public class Employee {
    private static int nextIdentifier = 1;
    private int currentIdentifier;

    public void assignIdentifier() {
        currentIdentifier = nextIdentifier;
        nextIdentifier++;
    }

    public static int getNextIdentifier() {
        return nextIdentifier;
    }
}

Geometric Class Implementation

public class Rectangle {
    private double width;
    private double height;

    public Rectangle(double w, double h) {
        width = w;
        height = h;
    }

    public double calculatePerimeter() {
        return 2 * (width + height);
    }

    public double calculateArea() {
        return width * height;
    }
}

public class Circle {
    private double radius;
    private static final double PI = 3.14159;

    public Circle(double r) {
        radius = r;
    }

    public double calculateCircumference() {
        return 2 * PI * radius;
    }

    public double calculateArea() {
        return PI * radius * radius;
    }
}

Package Organization

package com.company.employees;

public class Manager {
    private String name;
    private String department;

    public Manager(String n, String d) {
        name = n;
        department = d;
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.