LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Data Types and Exceptions in Java

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

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
		____.____ = ____;
	}
}
Code bearbeiten und ausführen