Logan17
Logan17
JCHJava Community | Help. Code. Learn.
Created by Abdullah on 9/22/2024 in #java-help
GETTERS AND SETTERS
Ex.2
public class BankAccount {
private double balance;

public BankAccount(double initialBalance) {
this.balance = initialBalance;
}

// Getter for balance
public double getBalance() {
return balance;
}

// Setter for balance (with validation)
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}

public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
}

// Example usage
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(100.0);

// Depositing money
account.deposit(50.0);
System.out.println("Balance after deposit: " + account.getBalance());

// Withdrawing money
account.withdraw(30.0);
System.out.println("Balance after withdrawal: " + account.getBalance());
}
}
public class BankAccount {
private double balance;

public BankAccount(double initialBalance) {
this.balance = initialBalance;
}

// Getter for balance
public double getBalance() {
return balance;
}

// Setter for balance (with validation)
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}

public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
}

// Example usage
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(100.0);

// Depositing money
account.deposit(50.0);
System.out.println("Balance after deposit: " + account.getBalance());

// Withdrawing money
account.withdraw(30.0);
System.out.println("Balance after withdrawal: " + account.getBalance());
}
}
7 replies
JCHJava Community | Help. Code. Learn.
Created by Abdullah on 9/22/2024 in #java-help
GETTERS AND SETTERS
Ex.
public class Employee {
private String name;
private double salary;

public Employee(String name, double salary) {
this.name = name;
setSalary(salary); // Using the setter for validation
}

// Getter for name
public String getName() {
return name;
}

// Getter for salary
public double getSalary() {
return salary;
}

// Setter for salary with validation
public void setSalary(double salary) {
if (salary >= 0) {
this.salary = salary;
}
}

// Private method to calculate tax (example)
private double calculateTax() {
return salary * 0.2; // 20% tax
}

// Public method to get tax amount
public double getTax() {
return calculateTax();
}
}

// Example usage
public class Main {
public static void main(String[] args) {
Employee emp = new Employee("Alice", 5000.0);

System.out.println("Name: " + emp.getName());
System.out.println("Salary: " + emp.getSalary());
System.out.println("Tax: " + emp.getTax());

emp.setSalary(6000.0); // Updating salary
System.out.println("Updated Salary: " + emp.getSalary());
System.out.println("Updated Tax: " + emp.getTax());
}
}
public class Employee {
private String name;
private double salary;

public Employee(String name, double salary) {
this.name = name;
setSalary(salary); // Using the setter for validation
}

// Getter for name
public String getName() {
return name;
}

// Getter for salary
public double getSalary() {
return salary;
}

// Setter for salary with validation
public void setSalary(double salary) {
if (salary >= 0) {
this.salary = salary;
}
}

// Private method to calculate tax (example)
private double calculateTax() {
return salary * 0.2; // 20% tax
}

// Public method to get tax amount
public double getTax() {
return calculateTax();
}
}

// Example usage
public class Main {
public static void main(String[] args) {
Employee emp = new Employee("Alice", 5000.0);

System.out.println("Name: " + emp.getName());
System.out.println("Salary: " + emp.getSalary());
System.out.println("Tax: " + emp.getTax());

emp.setSalary(6000.0); // Updating salary
System.out.println("Updated Salary: " + emp.getSalary());
System.out.println("Updated Tax: " + emp.getTax());
}
}
7 replies
JCHJava Community | Help. Code. Learn.
Created by Abdullah on 9/22/2024 in #java-help
GETTERS AND SETTERS
It's literally what their names say. Getters and setters are methods that allow controlled access to a class's attributes. A getter retrieves the value of an attribute, while a setter modifies it. For example, if you have a private attribute that you don't want to expose directly to other classes, you can create a public getter that allows access to that value. This way, other classes can retrieve the value without directly accessing the attribute. Similarly, if you need to change the value of that private attribute, you can use a setter, which allows modification without exposing the attribute directly. This approach helps maintain encapsulation and data integrity within your class.
7 replies