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

Thread: Lab 14

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

    Cool Lab 14

    Here's what I need to do for Lab 14:

     
    The StreetAddress class has this constructor:
     
    StreetAddress( String street, String city,
                   String state, String zip );
     
    and the following methods:
     
    void setStreet( String street ); and String getStreet();
     
    void setCity( String city); and String getCity();
     
    void setState( String state ); and String getState();
     
    void setZIP( String zip ); and String getZIP();
     
    String mailingLabel();.
     
    The last of these returns the mailing address on two lines in the following form:
     
    street
    city, state zip
     
    Use the following code segment to experiment with the behavior of this class — click the Run button and see what happens; change the code and click the Run button again to see the effect of your changes; repeat as often as you like.
     
     
     
    Run
    Write a definition for the StreetAddress class in the code area below.

    Here is my code:

    public class StreetAddress (String street, String city,String state,String zip) 
    {
     
    public void setStreetAddress(String Street, String City, String, State, String Zip){
        street = Street;
        city = City;
        state = State;
        zip = Zip;
    } 
     
    public String getStreet(){
        return street; 
    }
     
    public String getCity(){
        return city;
    }
     
    pubilc String getState(){
        return Zip;
    }
    }

    Thx!

    I'm actually making some progress. I'm glad its happening in such a short amount of time.


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Lab 14

    Quote Originally Posted by ghostheadx View Post
    public class StreetAddress (String street, String city,String state,String zip)
    {
    This syntax is wrong.

    Quote Originally Posted by ghostheadx View Post
    public void setStreetAddress(String Street, String City, String, State, String Zip){
        street = Street;
        city = City;
        state = State;
        zip = Zip;
    }
    This is not a constructor. (a constructor has the same exact name of the class and no return type)

    And apart this ... where are the instance fields of the class?
    Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginners – My new project Java Examples on Google Code

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

    Default Re: Lab 14

    ...spoonfeeding removed
    Last edited by copeg; December 29th, 2013 at 11:19 PM. Reason: removed spoonfed code

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Lab 14

    public class StreetAddress ()
        {
            public StreetAddress(){
     
            private String number;
            private String Street;
            private String city;
            private String zip;
     
            }
            public getStreet(){
                streetName = " ";
            }
            public getNumber(){
                StreetNumber = " ";
            }
            public getCity(){
                city = " ";
            }
            public String zip(){
                zip = " ";
            }
     
     
            public void AddressGetter(String YourStreetIs) {
                StreetNumber += YourStreetIs;
            }
     
            public void findAddress(double number){
                SteetNumber -= YourStreetIs;
            }
     
            public String returnStreet(){
                return street;   
            }
        }

    How's that for a start? Haha, it's not a good start is it?

    --- Update ---

    Oh yeah, one more editation:

    public class StreetAddress ()
        {
            public StreetAddress(){
     
            private String number;
            private String Street;
            private String city;
            private String zip;
     
            }
            public getStreet(){
                streetName = " ";
            }
            public getNumber(){
                StreetNumber = " ";
            }
            public getCity(){
                city = " ";
            }
            public String zip(){
                zip = " ";
            }
     
     
            public void AddressGetter(String YourStreetIs) {
                StreetNumber += YourStreetIs;
            }
     
            public void findAddress(double number){
                SteetNumber -= YourStreetIs;
            }
     
            public String returnStreet(){
                return street;   
            }
        }


    --- Update ---

    I'll stop now, as I'm posting a little to much and it might get a little annoying. Sorry about that. Didn't mean to spam.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,073
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: Lab 14

    It's not annoying that you keep posting - that's what a forum is for. It is annoying or frustrating that we've told you repeatedly what getter/accessor and setter/mutator methods are, shown you examples, corrected your attempts, told you what's wrong and right with what you've done, and still you can't get it right. You are unable to grasp and apply the basic concept that getters/accessors return a value (hence the word "get") and setters/mutators set or change a value (hence the word "set"). In fact, as the code added to your last post shows, you can't even write a method that conforms to basic Java requirements. Why is that? You tell us what point there is to helping you when you don't apply what we tell you, and you continue to write nonsense like this (one example of several blatant syntax and concept errors in your code):
    public getNumber(){
        StreetNumber = " ";
    }
    and then ask us what we think. What I wonder is why nothing we've told you over and over again has sunk in, and then I think that helping you is a complete waste of time. Show me I'm wrong.

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Lab 14

    And if GregBrannon was not clear .....

    Quote Originally Posted by ghostheadx View Post
    public class StreetAddress ()
    {
    I repeat, this is *wrong*, it's not a valid syntax. The Java language doesn't want anything like () or (String xyz, String zyx) between the class name and '{'.

    Quote Originally Posted by ghostheadx View Post
            public StreetAddress(){
     
            private String number;
            private String Street;
            private String city;
            private String zip;
     
            }
    Yes, public StreetAddress() is a "constructor" but constructors cannot contain fields declarations.

    Quote Originally Posted by ghostheadx View Post
            public getStreet(){
                streetName = " ";
            }
            public getNumber(){
                StreetNumber = " ";
            }
            public getCity(){
                city = " ";
            }
            public String zip(){
                zip = " ";
            }
    All methods *must* have a declared return type. Here only the last has a return type in the declaration. And apart this, "getter" methods should return something .... not do a simple assignment.

    Quote Originally Posted by ghostheadx View Post
            public void AddressGetter(String YourStreetIs) {
                StreetNumber += YourStreetIs;
            }
    Again, this is not a "getter" .... even if you put in the word "Getter".

    Quote Originally Posted by ghostheadx View Post
            public void findAddress(double number){
                SteetNumber -= YourStreetIs;
            }
    This is totally meaningless.

    Quote Originally Posted by ghostheadx View Post
            public String returnStreet(){
                return street;   
            }
    This method is syntactically correct but a getter should be named (by convention) getSomething ... not returnSomething.


    And the instance fields of the class are still not present/not correct.
    Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginners – My new project Java Examples on Google Code

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Lab 14

    I knew most of it was titled wrong, because I was sort of not focused on the titles. I actually tried to copy this guy's programming:

    Accessors and mutators in Java - YouTube

    I think that was a bad idea.

    --- Update ---

    Lol. like this?

    public class StreetAddress
        {
            public StreetAddress(){
     
            private String number;
            private String Street;
            private String city;
            private String zip;
     
            }
            public String getStreet(){
                return Street;
            }
            public String getNumber(){
                return number;
            }
            public String GetCity(){
                return city;
            }
            public String GetZipCode(){
                return zip;
            } 
        }
     
    Address firstAddress = new StreetAddress ("16", " 5th Street", " New York", " 89215");

    I think everything there is a little better, except for the constructor.

    --- Update ---

    Lol. like this?

    public class StreetAddress
        {
            public StreetAddress(){
     
            private String number;
            private String Street;
            private String city;
            private String zip;
     
            }
            public String getStreet(){
                return Street;
            }
            public String getNumber(){
                return number;
            }
            public String getCity(){
                return city;
            }
            public String getZipCode(){
                return zip;
            } 
        }
     
    Address firstAddress = new StreetAddress ("16", " 5th Street", " New York", " 89215");

    I think everything there is a little better, except for the constructor.

  8. #8
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Lab 14

    Quote Originally Posted by ghostheadx View Post
            public StreetAddress(){
     
            private String number;
            private String Street;
            private String city;
            private String zip;
     
            }
    For the second time, I repeat: constructors cannot contain fields declarations.

    Fields declarations are into a class definition but outside constructors/methods.

    Quote Originally Posted by ghostheadx View Post
            public String getStreet(){
                return Street;
            }
            public String getNumber(){
                return number;
            }
            public String getCity(){
                return city;
            }
            public String getZipCode(){
                return zip;
            }
    These are syntactically correct. Note that by convention, variables should be in mixed case with the first letter in lowercase.

    Quote Originally Posted by ghostheadx View Post
    Address firstAddress = new StreetAddress ("16", " 5th Street", " New York", " 89215");
    In general, you can obviously pass arguments to a constructor ... but to do this, the constructor must have corresponding parameters.

    public StreetAddress(){      <--- you don't have parameters here


    Sorry but what's the problem/issue in finding and following a decent tutorial on basic class declarations?
    Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginners – My new project Java Examples on Google Code

  9. #9
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Lab 14

    public class StreetAddress{
     
     
     
        public String street;
     
        public String city;
     
        public String state;
     
        public String zip;
     
     
     
        public StreetAddress(){
     
            street = findStreet;
     
            city = findCity;
     
            state = findState;
     
            zip = findZip;
     
        }
     
     
     
       public int getStreet() {
     
          return street;
     
       }
     
       public void setStreet(int newStreet) {
     
          street = newStreet'
     
       }
     
     
     
       public int getcity() {
     
          return city;
     
       }
     
      public void setcity(int newCity) {
     
         city = newCity;
     
     
     
      }
     
     
     
       public int getState() {
     
          return state;
     
       }
     
      public void setState(int newState) {
     
         state = newState;
     
     
     
      }
     
     
     
      public int getZIP() {
     
          return zip;
     
       }
     
      public void setZip(int newZip) {
     
         zip = newZip;
     
     
     
      }
     
     
     
      }
     
     
     
    }

    I think I'm starting to get some of it.

    --- Update ---

    Wait, I edited an earlier post. I think I'll post it here:

       public String street;
     
        public String city;
     
        public String state;
     
        public String zip;
     
     
     
        public StreetAddress(){
     
            street = findStreet;
     
            city = findCity;
     
            state = findState;
     
            zip = findZip;
     
        }
     
     
     
       public int getStreet() {
     
          return street;
     
       }
     
       public void setStreet(int newStreet) {
     
          street = newStreet'
     
       }
     
     
     
       public int getcity() {
     
          return city;
     
       }
     
      public void setcity(int newCity) {
     
         city = newCity;
     
     
     
      }
     
     
     
       public int getState() {
     
          return state;
     
       }
     
      public void setState(int newState) {
     
         state = newState;
     
     
     
      }
     
     
     
      public int getZIP() {
     
          return zip;
     
       }
     
      public void setZip(int newZip) {
     
         zip = newZip;
     
     
     
      }
     
     
     
      }
     
     
     
    }


    --- Update ---

    I'm just saving my code:

    public class StreetAddress{
     
     
     
        public String street;
     
        public String city;
     
        public String state;
     
        public String zip;
     
     
     
        public StreetAddress(){
     
            street = findStreet;
     
            city = findCity;
     
            state = findState;
     
            zip = findZip;
     
        }
     
     
     
       public int getStreet() {
     
          return street;
     
       }
     
       public void setStreet(int newStreet) {
     
          street = newStreet;
     
       }
     
     
     
       public int getcity() {
     
          return city;
     
       }
     
      public void setcity(int newCity) {
     
         city = newCity;
     
     
     
      }
     
     
     
       public int getState() {
     
          return state;
     
       }
     
      public void setState(int newState) {
     
         state = newState;
     
     
     
      }
     
     
     
      public int getZIP() {
     
          return zip;
     
       }
     
      public void setZip(int newZip) {
     
         zip = newZip;
     
     
     
      }
     
     
     
    }
     
      public String mailinglabel(){
     
          System.out.print(street);
     
          System.out.println(city + " " + zip);
     
      }
     
    }

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,073
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: Lab 14

    You've made no progress that I can see. What are findStreet, findCity, findState, and findZip? And how will this work:
    public void setStreet(int newStreet) {
     
          street = newStreet;
     
       }
    if 'street' is a String?

  11. #11
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Lab 14

    Example Construtor:

    public class Name{
    private String fName;
    private String lname;
    public Name(String fN, String lN)
    {
    fName = fN;
    lName = lN;
    }
    }

  12. #12
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Lab 14

    Quote Originally Posted by llowe29 View Post
    Example Construtor:

    public class Name{
    private String fName;
    private String lname;
    public Name(String fN, String lN)
    {
    fName = fN;
    lName = lN;
    }
    }
    I thought that you didn't declare fields variables in a constructor.

    --- Update ---

    @GregBrannon

    I just sort of thought that a constructor looked like this per-say:

       public Bicycle(int startCadence, int startSpeed, int startGear) {
            gear = startGear;
            cadence = startCadence;
            speed = startSpeed;
        }

    I couldn't see much difference between that, which I copied from a tutorial and what I did. I made the "find" variables as sort of like the "start" variables in the constructor in the tutorial I found. I'll give you a link so you can relate.

    Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    That tutorial is where I got the idea from.

    --- Update ---

    Wait, is it because I didn't put parameter in mine like the tutorial did?

  13. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,073
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: Lab 14

    Why are you collecting programming 'ideas' from multiple sources? Some of those ideas may be good, others not so good, but you've demonstrated that you can't tell the difference or that you can't combine and apply the ideas, good or bad, into valid code. Stop jumping from video to tutorial to who-knows-what, collecting bits of code that you don't really understand and combining them without really thinking about or understanding the result. Fall back, regroup, and concentrate on one source, your textbook, if you have one, and any class notes you took or were given.

    Your last post shows that you don't know what "declare a variable" means. Look that up. The constructor copied from the Oracle tutorials in post #12 and the one you posted in post #9 are clearly different, and my questions about post #9 addresses variable in the constructor that did not exist anywhere else in the class. That won't work.

    You are nearly hopelessly confused and lost at this point, and if you don't find some way to ground yourself to truth and reality and start afresh from there, you won't survive this class.

  14. #14
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Lab 14

    I thought that declaring a variable essentially meant:

    int five = 5;

    It would store the number "5" in the data space, represented by 5, which is an integer. That's what my understanding is.

    --- Update ---

    Declaring the variable means assigning it the type "int."

    As where is I declared it as "double" that would be a different declaration.

    --- Update ---

    Not the first part of being represented by "5."

  15. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,073
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: Lab 14

    So there are two parts to the first statement you posted, declaration and initialization. Take that thought and go back to your first statement in post #12, reread, rethink.

  16. #16
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Lab 14

    Okay, so how's this for a start:

     
    public class StreetAddress{
     
     
     
       public String street;
     
       public String city;
     
       public String state;
     
       public String zip;
     
     
     
       public StreetAddress(String findStreet, String findCity,String findState,String findZIP){
     
           street = findStreet;
     
           city = findCity;
     
           state = findState;
     
           zip = findZIP;
     
       }
     
     
     
    public String getStreet() {
     
       return street;
     
    }
     
     
     
     
     
    public String getcity() {
     
       return city;
     
    }
     
     
     
     
     
    public String getState(){
     
        return state; 
     
    }
     
     
     
     
     
    public String getZip() {
     
       return zip;
     
    }
     
     
     
    }
     
    }


    --- Update ---

    I know that not everything is on there, but just to begin with. When the constructor and the getters get better, I'm going for the setters. I'm hoping that my constructors are getting just a tiny bit better because I'm going crazy doing this lol.

  17. #17
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,073
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: Lab 14

    Well, you're way beyond the starting line - that's a good thing - and you're finally showing some progress. Except for inconsistent capitalization (not wrong, just annoying), random code indentation and spacing, and some odd or non-standard variable/method names, you've pretty much correctly written a class, its constructor with parameters, and getters/accessor methods.

    Since you've been all over the map from where you started, you might want to look back at post #1 or review the assignment to make sure you're still on track.

    Whatever conventions/standards you adopt for naming, capitalizing, code indenting, etc., be consistent. If you want to show your code to others, choose conventions and standards that are generally accepted by the community in which you show your work so they don't cringe at the mere sight of your code, but consistency is mostly for your own benefit. After you've written enough code and simply "know" what correct code looks like, even subtle inconsistencies will jump out at you as being suspect and potentially incorrect. Right now, most all of your code has those inconsistencies that indicate there might be problems, but it's getting better.

  18. #18
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Lab 14

    So now, hopefully I can get some setters working. Thanks.

  19. #19
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Lab 14

    Alright so, as always, my mutators don't work. Here's my code:

    public class StreetAddress
     
    {
     
     
     
        public String street;
     
        public String city;
     
        public String state;
     
        public String zip;
     
     
     
       public StreetAddress(String findStreet, String findCity,String findState,String findZIP){
     
     
     
        street = findStreet;
     
        city = findCity;
     
        state = findState;
     
        zip = findZIP;
     
       }
     
     
     
      public String getStreet() {
     
         return street;
     
      }
     
     
     
      public void makeStreet(String street){
     
          this.street = street;
     
      }
     
     
     
      public String getcity() {
     
         return city;
     
     
     
      }
     
     
     
      public void makeCity(String city){
     
          this.city = city;
     
      }
     
     
     
     
     
      public String getState(){
     
         return state;
     
      }
     
     
     
      public void makeState(String state){
     
          this.state = state;
     
      }
     
     
     
     
     
    public String getZip() {
     
         return zip;
     
     
     
     
     
    public void makeZip(String zip){
     
          this.zip = zip;
     
    }
     
     
     
    }
     
    }
     
    }

  20. #20
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Lab 14

    Quote Originally Posted by ghostheadx View Post
    Alright so, as always, my mutators don't work.
    Some things:

    Fields:
    - Fields should be private, not public.
    Naming conventions:
    - Why makeXyz? Setters should be named setXyz .
    - For setters/constructors there is generally the common convention to name the parameter the same as the field. Eg. setCity(String city) and then use this.city = city;. This can and should be done also for constructors.
    Syntax:
    - It's still wrong (for example there is a missing } for getZip and there is a bad number of } at the end).
    Indentations/spacings:
    -Take great care of indentations and spacings that are not good in your source.
    Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginners – My new project Java Examples on Google Code

  21. #21
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,073
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: Lab 14

    I want to emphasize anbin's point: a getter method to obtain the variable this' value is called getThis and a setter method to store a value in the variable 'this' is called setThis(). The naming is not debatable or a time to be original. There's a reason (probably several) for the naming convention. Conventions are conventions for a reason.

Similar Threads

  1. is something wrong with my code? Lab 10.1
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 28th, 2013, 09:05 PM
  2. Need Help with While Loop Lab
    By LennDawg in forum Other Programming Languages
    Replies: 4
    Last Post: June 29th, 2012, 10:41 AM
  3. Replies: 2
    Last Post: February 7th, 2012, 03:26 PM
  4. Help with a lab!
    By Spicy McHaggis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2012, 07:03 AM
  5. PLEASE HELP ME WITH MY COLLEGE LAB!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 29th, 2011, 04:06 PM

Tags for this Thread