ComeçarComece de graça

Defining a POJO

Building a POJO is about creating a straightforward Java class following a few rules. POJO classes are simple and independent; they don't extend other classes or implement interfaces. The main task is to define the fields and create getter and setter methods to access them. Here, you create a POJO for transporting savings account data in a Java application.

Este exercício faz parte do curso

Data Types and Exceptions in Java

Ver curso

Instruções do exercício

  • Add a field balance of type double to hold the account balance value, making sure to choose the correct access.
  • Define a getter method to return the balance field value.
  • Define a setter method to allow the balance field to a new value.
  • In the setter method, set the balance field to the setter's argument.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

public class Test {
  public static void main (String[] args){
  	SavingsAccount account = new SavingsAccount();
    account.setAccountNo("12345");
    account.setBalance(50000.00);
    System.out.println("Account " + account.getAccountNo() + " has balance of: " + account.getBalance());
  }
}

public class SavingsAccount {
	private String accountNo;
    // Add a balance field
	____ ____ ____;

	public String getAccountNo() {
		return accountNo;
	}

	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}

    // Define a balance getter method
	____ ____ ____() {
		return balance;
	}

    // Define a balance setter method
	public ____ setBalance(double balance) {
    	// Set the balance field
		____.____ = ____;
	}
}
Editar e executar o código