Forum Stats

  • 3,827,529 Users
  • 2,260,790 Discussions


I am really confused on how to identify the solution to Lab 2. Can you help?

user7813571 Member Posts: 2
edited May 16, 2018 5:47PM in Java Puzzle Ball

I am really confused on how to identify the solution to  Lab 2.  Can you help?



  • NickR2600-Oracle
    NickR2600-Oracle Member Posts: 530 Employee
    edited May 14, 2018 12:37PM

    Of course.  That's exactly the kind of thing the forum is for.  Where is it you're having trouble?

  • NickR2600-Oracle
    NickR2600-Oracle Member Posts: 530 Employee
    edited May 16, 2018 5:44PM

    Someone else sent an email about Lab 2.  I'll post a general summary of what I sent so everyone can benefit.

    t, o, b, and r are created within the Constructor and don't exist outside the scope of that constructor.  The official name for them are "method parameters".  Private class fields like accountType can't be accessed outside the scope of the SavingsAccount class.  But what we can do is supply values to t, o, b, and r by calling the constructor.  This is done in the main method where savings account instances are created.  To do any sort of calculations with these values later, we need to somehow get these values out of the constructor and store them somewhere a little more permanent.  That's why the constructor takes these values and uses them to set the initial values of each class field.  A class can access and change its field values whenever it wants.

    You may also see people who name their constructor variables the same as their fields.  When this happens, they use the keyword "this" to differentiate the field from the temporary constructor variable:

    public class SavingsAccount {    //Fields    private String accountType;    private String accountOwner;    private double balance;    private double interestRate;    private int accountNum;    private static int nextAccountNum = 1;  //Constructor    public SavingsAccount(String accountType, String accountOwner, double balance, double interestRate){        this.accountType = accountype;        this.accountOwner = accountOwner;        this.balance = balance;        this.interestRate = interestRate;        setAccountNumber();
  • NickR2600-Oracle
    NickR2600-Oracle Member Posts: 530 Employee
    edited May 16, 2018 5:47PM

    And here's part 2:

    If you look at the SavingsAccount fields from Lab 1, you'll see that we're setting the initial field values as soon as the field variables are declared:

        private String accountType = "Savings Account";    private String accountOwner = "Duke";    private double balance = 0.0;    private double interestRate = 0.02;

    The problem with this is that it doesn't provide much customization.  For example, all of the accountOwners are going to be called Duke!  It's possible to write methods to change these values one-by-one, but that would lead to really verbose looking code, which is harder to maintain and a pain to write for.  A constructor is a convenient and elegant way to set many initial field values within a single method call.

    If you choose not to set a variable's value when it's declared or through a method, the variable keeps what's considered it's"default value".  Default values are sometimes useful.  They depend on the variable's data type.  A String will have a default value of 'null'.  A double or integer will have a default value of zero.  But sometimes they're not useful.  If you try to use null or 0 for calculations or whatever, you run the risk of getting "null pointer" errors or "cannot divide by 0" errors.