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

Thread: Implementing Methods

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Implementing Methods

    Hi everyone,

    I am taking my first Java course and I'm finding there is very little direction in it, and I'm already becoming frustrated.

    I have a question as follows:


    For a class representing a light switch (bodies of methods omitted):
    public class Switch {
      public enum Position { Off, On };
      private Position position;
      public Switch() { ... }
      public Switch (Position pos) { ... }
      public void on() { ... }
      public void off() { ... }
      public Position getPosition() { ... }
      public String toString() { return “Switch is “+position; }
      public static void main(String[] args) { // Sample usage
        Switch s1 = new Switch();
        System.out.println(s1);
        s1.on();
        System.out.println(s1);
        s1.off();
        System.out.println(s1);
      }
    }

    Implement three (3) methods: One (of the two) constructor, the method on, and the method getPosition.


    We don't have a textbook, and given that I'm a visual person, I need to see example code because I can't come up with something off the top of my head having never seen or written code before.

    I tried to look for examples online, and all I could come up with so far is this:

    public Switch (Position pos) { 
    position = pos; 
    } 
     
     
    public void on() { 
    position = Position.On; 
    } 
     
     
    public Position getPosition() { 
    return Position; 
    }


    I'm not even sure that's right. I'm 100%

    Thanks.


  2. #2
    Junior Member
    Join Date
    May 2014
    Location
    Chennai
    Posts
    9
    My Mood
    Cool
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Implementing Methods

    Hi, kindly find whether the below code snippet helps you and is that what you are looking for.
     
    Code removed.
    [COLOR="Silver"]
    Last edited by GregBrannon; May 27th, 2014 at 04:48 AM. Reason: Spoon feeding.

  3. #3
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Implementing Methods

    Please do not provide complete solutions to a problem - give helpful hints and advice.
    Giving a more or less practical solution removes the fun of learning for themselves.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  4. #4
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Implementing Methods

    BharathA: Thank you, this was GREATLY appreciated. I am a visual learner, and learning by means of verbal or written directions simply doesn't help.


    Ada: As much as I'm sure in most cases you are correct, I may be one of the few exceptions to the rule. Unfortunately, my 10-week course in java, which is thankfully coming to an end, has been anything but fun. Sometimes, people just need to see it done for the first time to understand it, rather than working off of hint-dropping and suggestions. It's nothing personal -- there are just some folks out there, like myself, that are right-brained as opposed to left. It would be helpful if college instructors could/would understand that, but that has not been the case for me.

  5. #5
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Implementing Methods

    Yes, I completely understand where you are coming from - I was simply stating that learning anything - be it programming or otherwise,
    should be a process of knowledge that is gradually formed over X amount of weeks/months/years etc. When you say "Visualise" I am guessing
    you're instructor neglected to mention that there are a fair few examples of Java program in the Internet, be it through video or tutorials written
    by other people. I can also see why people find it difficult, especially when you are given so little help from the one place in which you would expect to
    be given that help right from the off. Poorly educated instructors can lead to struggling students - and it is the people who struggle though, who sometimes
    come out on top. I think you are a strong and willing person, and whatever grade you get from this course - build on it. Whatever knowledge you got from
    the course - don;t lose it - take notes on it. Make programs in your own time - learn about the language. Java is beautiful. and is well worth the effort.
    Do not let bad teaching and poor resources stop you from becoming a programmer.
    Good luck!

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

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

    Default Re: Implementing Methods

    Right brained, left brained, it doesn't matter which half you use to learn. Being given the solution will prevent you from using either half, and you will learn nothing.

    @BharathA: Please read The Problem With Spoon-feeding.

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

    copeg (May 27th, 2014)

  8. #7
    Junior Member
    Join Date
    May 2014
    Location
    Chennai
    Posts
    9
    My Mood
    Cool
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Implementing Methods

    @Greg and Ada - Thanks for sharing me the link and the problems with spoon-feeding. Apologies from my end for sharing the entire code as I thought doswald had initially given a try with few coding lines and couple of methods as well. That's why I have shared the complete coding on the basis that atleast the person had given a try. But I now realize that it could not be a good learning process for others too. So, apologies for not letting doswald to learn.

    @doswald - I hope you got the answer. Going forward, try splitting the question into small pieces. In this example, I have tried to print the first array and then looked for printing the 2nd and so on. So, break it up into smaller junks and you could go through them.

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

    copeg (May 27th, 2014)

  10. #8
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Implementing Methods

    Quote Originally Posted by GregBrannon View Post
    Right brained, left brained, it doesn't matter which half you use to learn. Being given the solution will prevent you from using either half, and you will learn nothing.

    @BharathA: Please read The Problem With Spoon-feeding.
    Everyone -- he didn't give me the answer. He filled in the parts of the code that my (useless) instructor omitted, making it easier for me to figure out the rest of the problems. Rather than telling someone what they SHOULD'NT post to someone else, why not jump in with your own advice and tidbits? Seems a little silly to me, given all the other forums on the internet that exist to help students with java. Just saying.

  11. #9
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Implementing Methods

    I see your point, and apologies if it did seem a bit harsh on him/yourself in general. It did seem like the entire solution had been given,
    thus why the problem arose.
    People on this forum will always try to help others - and that is totally the point of a forum. Refer to my other post where I gave you a detailed explanation
    and a big thumbs up about your courageous learning. I do see where you come from with the lousy instructor, if a person cannot be bothered to teach a student
    programming - he should not be teaching it.
    To me you seem like a person who wants to succeed - and you will. Everyone here will - and people are always willing to aid others in that venture.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  12. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Implementing Methods

    Quote Originally Posted by doswald1 View Post
    Everyone -- he didn't give me the answer. He filled in the parts of the code that my (useless) instructor omitted, making it easier for me to figure out the rest of the problems. Rather than telling someone what they SHOULD'NT post to someone else, why not jump in with your own advice and tidbits? Seems a little silly to me, given all the other forums on the internet that exist to help students with java. Just saying.
    While I didn't edit the post above I stand behind the member of the moderator staff who did. The forums have guidelines, and while some may not agree with every decision made to enforce those guidelines, please understand they are meant to be in the best interest of everyone. If you disagree, I recommend starting a new thread (in the appropriate) to discuss. But let me add that learning to program is as much learning to problem solve as it is writing code. Its about trying things and seeing what happens. Its about writing code, compiling, running, then continually repeating. When a member hands over code that contains no explanation, it can rob you of the opportunity to improve these skills. In some extreme cases, it can be considered academic dishonesty.

    On a side note...
    He filled in the parts of the code that my (useless) instructor omitted
    ...many of us here are self taught - and many consider placing any type of blame on an instructor a (dare I say) useless (and often self-defeating) exercise.

    Now...back onto the initial topic at hand (presuming you are still having difficulty)

    I'm not even sure that's right
    We're you able to compile the code you posted? If not, what warnings/errors did you get? If so, did you run the code? What happened?

  13. The Following User Says Thank You to copeg For This Useful Post:

    GregBrannon (May 28th, 2014)

  14. #11
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Implementing Methods

    ,) i'm a diesel engine too, passed the first 2 tests with flunks, now i need 80% on my last one, just made it and spent probably 40-50 hours trying and failing and trying something else and moving on and starting again, but it works now, so perhaps I'l salvage this year.
    But I did learn tons
    The instructor can feed you tidbits of information each lesson, but in the end it's up to you to try them out in different ways and get accomodated to how they work.
    If you just look up complete pieces of code you won't have the slightest idea how to reproduce a likewise situation with your own code,
    + half the fun is errorhunting or atleast when you find the errors
    English is not my native language (Typo alert).

  15. The Following User Says Thank You to Time4Java For This Useful Post:

    GregBrannon (May 28th, 2014)

Similar Threads

  1. Implementing a webproxy
    By arsenewenger in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 12th, 2013, 11:19 AM
  2. implementing KeyListener
    By FARGORE in forum AWT / Java Swing
    Replies: 1
    Last Post: October 18th, 2012, 08:32 AM
  3. Implementing an algorithm
    By me_newbie in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 16th, 2012, 08:50 PM
  4. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  5. Implementing Semaphores
    By bananasplitkids in forum Threads
    Replies: 1
    Last Post: March 27th, 2010, 04:35 AM