Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: What do I need to learn to understand this code?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default What do I need to learn to understand this code?

    So I was a sick for some time and missed a few uni lectures and I was at a complete loss when we were supposed to do the mock test. It was based on these resources I found on our module webpage. Obviously, it's not just a single thing I missed out on. I know the very basics of Java, I know a little bit about creating and using multiple classes with objects and I know a little bit about constructors. What do I need to catch up on to understand any of this code?

    public class BankAccount
    {
        private double balance;
     
        public BankAccount ()
        {
    	balance = 0;
        }
     
        public BankAccount (double b)
        {
    	balance = b;
        }
     
        public void setBalance (double b)
        {
    	balance = b;
        }
     
        public double getBalance ()
        {
    	return balance;
        }
     
        public String toString()
        {
    	return "\n\tbalance: " + balance;
        }
    } //end class BankAccount

    /**
     Base class for clerk and customers: name and age.
    */
     
    public class Person
    {
        public static final String NONAME = "No name yet";
     
        private String name;
        private int age; //in years
     
        public Person ()
        {
            name = NONAME;
            age = 0;
        }
     
        public Person (String initialName, int initialAge)
        {
    /*    name = initialName;
          if (initialAge >= 0)
              age = initialAge;
    	else
    	   {
                System.out.println("Error: Negative age or weight.");
                System.exit(0);
               }
    */
          this.set(initialName);
          this.set(initialAge);
        }
     
        public void set (String newName)
        {
            name = newName; //age is unchanged.
        }
     
     
        public void set (int newAge)      //name is unchanged.
        {
            if (newAge >= 0)
            {
    	    age = newAge;  
            }
            else
    	   {    
    	    System.out.println("Error: Negative age.");
                System.exit(0);      
    	   }
     
        }
     
        public String getName ( )
        {
            return name;
        }
     
        public int getAge ( )
        {
            return age;
        }
     
        public String toString( )
        {
            return (name + " is " + age + " years old\n");
        }
    } //end class Person

    public class Clerk extends Person {
     
       private String socialSecurityNumber;
     
       // constructor
       public Clerk( String initialName, int initialAge, String ssn )
       {
          /* name = initialName;
          age = initialAge; */
          super (initialName, initialAge);
          socialSecurityNumber = ssn;
       } 
     
       public Clerk ()
       {
          super();
          socialSecurityNumber = "";
       }
     
       // sets social security number
       public void setSocialSecurityNumber( String number )
       {
          socialSecurityNumber = number;  // should validate as 
       } 
     
       // returns social security number
       public String getSocialSecurityNumber()
       {
          return socialSecurityNumber;
       } 
     
       // returns String representation of Clerk object
       public String toString()
       {
          return getName() + " (" + getAge() + " years old)" +  
             "\n\tsocial security number: " + getSocialSecurityNumber();
       } 
     
    } // end class Clerk

    public class Customer extends Person {
     
       private BankAccount ba;
     
       // constructors
     
       public Customer ()
       {
          super();
          ba = new BankAccount();
       }
     
       public Customer ( String initialName, int initialAge, double b )
       {
          super (initialName, initialAge);
          ba = new BankAccount (b);
       } 
     
       // returns String representation of Clerk object
       public String toString()
       {
          return getName() + " (" + getAge() + " years old)" +  
             "\n\tbank account balance: " + ba.getBalance();
       } 
     
    } // end class Customer


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: What do I need to learn to understand this code?

    What do you mean by "mock test"? Because if you mean creating a test class using Mocked versions of these objects (with Mockito or something similar), I'm a bit surprised that is covered in a basic java class (but kudos for your professor for including it).
    As for what you need to know about these classes, most of the stuff is pretty basic. I can't really provide a detailed list, since everything is very low-level. Perhaps a better approach would be if you ask about specific areas you are confused or unsure.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: What do I need to learn to understand this code?

    Mock Test is essentially practice before the Lab Test. They just call it that in UK universities.

    I am now slowly realizing I know nothing about setting or returning something. What's the point of doing it? Couldn't you just assign the values regulary? So the part from the first code that confuses me is:

     public void setBalance (double b)
        {
    	balance = b;
        }
     
        public double getBalance ()
        {
    	return balance;
        }
     
        public String toString()
        {
    	return "\n\tbalance: " + balance;

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What do I need to learn to understand this code?

    Look up "setter" or mutator and "getter" or accessor methods. Their purpose is to provide a "safe" access or interface to an object's attributes by other objects.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    import.Snupas (December 6th, 2013)

  6. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: What do I need to learn to understand this code?

    Oh, ok. A "Mock Test" is a java thing, which is why I was confused, lol.

    Ok, well the toString() method is actually a method inherited by the Object class. When you print out an object (with System.out.println(object) or something), it uses the toString() method to determine the object's String representation.
    Your question of "why use getters and setters when you can just use direct reference" is a hotly debated topic. I prefer using setters and getters because it allows you to modify the way in which you retrieve/set the data. For example, you can modify the data before setting or returning it. But, for that specific class, using the setter and getter is mandatory. Look at where the balance variable is declared. Its scope is set to private. This means that you cannot directly reference the variable from outside the class. So the only way to access the variable is by getters and setters.
    If balance was set to public instead, you could use a direct reference.
    Consider the following:
    public variables and methods can be directly referenced from all other classes (assuming they have an initialized object or other static-related conditions which I will not go into for now).
    private variables and methods can only be directly referenced from inside its own class. Not even subclasses can reference those variables.
    protected variables and methods can only be directly referenced from inside its own class, or from a subclass.
    (there is also default, but it is so rarely used it is no worth mentioning for now)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. The Following 2 Users Say Thank You to aussiemcgr For This Useful Post:

    GregBrannon (December 6th, 2013), import.Snupas (December 6th, 2013)

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What do I need to learn to understand this code?

    Everything aussiemcgr posted, bless his kind heart, is available in the Oracle Java Tutorials and probably your textbook, two places you should be spending all of your free time to discover the information presented during the classes you didn't attend. Read, practice, practice, practice, . . . , practice some more, read, repeat.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    import.Snupas (December 6th, 2013)

  10. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: What do I need to learn to understand this code?

    Thanks aussie!

    You're right Greg. I just wanted to write up a laundry list of stuff I needed to catch up on, but it probably was selfish for someone to figure it out for me.

    Thanks again everyone, I'll be off to study my setters, getters, constructors and super().

Similar Threads

  1. Help me understand this code?
    By Farkuldi in forum Java Theory & Questions
    Replies: 11
    Last Post: November 12th, 2013, 03:37 PM
  2. I want to learn more and code!
    By ModestMiata in forum Member Introductions
    Replies: 2
    Last Post: May 30th, 2013, 03:17 PM
  3. Hi,Can any one help me to understand this code.Am a beginner.
    By Jawad24 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 23rd, 2013, 08:47 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Grid Computing, need to understand and learn
    By madcrazyboys in forum The Cafe
    Replies: 1
    Last Post: December 21st, 2010, 11:32 PM