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 4 of 4

Thread: Intro to Java problem! Please help!

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Intro to Java problem! Please help!

    So I am taking a class that just started us out in Java. We have been using Alice for most of the class which is completely different so I am extra confused when dealing with some raw code. I have an assignment question that she wants answered but I need some help. The question is.

    Code a simple class in JAVA classed “Student.” Your class must contain the followings (20 points):
    · Variables (name, age, addressStreet, city, state, zip, country)
    · Setter and Getter methods

    This is an all online college that I am taking so it is difficult to communicate with instructors and get prompt feedbacks, at least with this class. I have a basis for what I have started and it may be ok but I am still unsure if what I am doing is right. I don't know if this means I have to include fake names for the getters so I included them anyway. Someone please point me in the right direction! Here is my progress thus far.

    public class Student {
        private String name;
        private String age;
        private String addressStreet;
        private String city;
        private String state;
        private String country;
        private int zip;
     
        /**
         *  This method is demonstrating a simple class clabeled "Student"
         *
         *
         */
        public void setName(String name) { 
            this.name = "Gig Harbor";
        }
     
        public String getName(){
            return name;
        }
     
        public void setAddressStreet(String address) { 
            this.addressStreet = "555 Blah Blah Lane";
        }
     
        public String getAddressStreet(){
            return addressStreet;
        }
     
        public void setCity(String cityName) { 
            this.city = "Gig Harbor";
        }
     
        public String getCity(){
            return city;
        }
     
        public void setState(String stateName) { 
            this.state = "Washington";
        }
     
        public String getState(){
            return state;
        }
     
        public void setZip (String zipName) {
            this.zip = "98446";
        }
     
        public String getZip(){
            return zip;
        }
     
        public void setCountry (String countryName) {
          this.country = "USA";
        }
     
        public String getCountry(){
           return country;
     
        // Public void's are setters and public String are getters. 
     
     
      }
      }
    Last edited by jps; August 21st, 2013 at 05:53 PM. Reason: code tags


  2. #2
    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: Intro to Java problem! Please help!

    The purpose of a 'setter' is to provide an interface to the outside world to set something, usually a variable in the class, to the parameter passed to the method. You currently set most variables to constants. Instead, apply the values passed:
    public void setName(String name) { 
    this.name = name;
    }
    Your class could also have a constructor. It's not asked for specifically by the assignment words you've provided, but do you think it might be expected, or is it mentioned in some part of the assignment you didn't show?

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Intro to Java problem! Please help!

    Nope. The assignment just asked for that then gave a small example of a setter and getter so that is somewhat how I formatted the assignment. So you are saying if I just add that part instead of actual names that it could be complete based on the given criteria?

  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: Intro to Java problem! Please help!

    I would also be consistent in variable naming. If the variable name is 'city', the setter signature should be:

    public void setCity( String city )

    and the getter signature should be:

    public String getCity()

    Note the setter, getter, and variable names are consistent throughout. (There's a reason - perhaps more than one - for maintaining this consistency that you may learn about later.)

    You also waffled on whether 'zip' is an int or a String. For simplicity, you should probably leave it a String, but whatever you pick, be consistent throughout. Don't set an int to a String object or return an int if zip is a String. Make sense?

    Other than that, your work seems to me to answer the assignment, but I accept no liability.

Similar Threads

  1. Intro to Java
    By omen1090 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 07:37 AM
  2. intro:
    By rajeev34 in forum Member Introductions
    Replies: 2
    Last Post: December 20th, 2012, 06:18 AM
  3. Intro to java assignment help
    By Rahiant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2012, 01:05 PM
  4. intro
    By Trina in forum Member Introductions
    Replies: 0
    Last Post: March 17th, 2012, 12:03 PM
  5. Intro to java hw assignment
    By coke32 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2011, 07:45 AM