Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By Dark

Thread: New to Java...naturally got problems!

  1. #1
    Saracen is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Question New to Java...naturally got problems!

    Hello Java Forumers...
    I'm an absolute beginner at JAVA and also new to this site so hello! I've attempted to write some code to allow me to output the name of a cat ("Felix") with an accompanying message ("I'm playing!"). Inevitably with Java, I got errors and that's pasted at the bottom.

    So here's the Main file:

    package animals;

    /**
    *
    * @author MAM
    */
    public class Main {

    private String name;

    /**
    * @param args the command line arguments
    */
    //public static void main(String[] args) {
    // TODO code application logic here
    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;

    }

    }

    And here's the Cat Java file:

    package animals;

    /**
    *
    * @author MAM
    */
    public class Cat extends Main {

    private static class cat {
    private boolean exp;

    public cat() {
    }

    private String name;

    public static void main(String[] args) {

    //public void testNewCat(){
    cat cat = new cat();
    cat.setName ("Felix");
    System.out.println( "name" + "I'm playing!");

    }

    private void setName(String string) {
    throw new UnsupportedOperationException("Not yet implemented");
    }
    }
    }

    Output i get:

    run:
    Exception in thread "main" java.lang.UnsupportedOperationException: Not yet implemented
    at animals.Cat$cat.setName(Cat.java:33)
    at animals.Cat$cat.main(Cat.java:26)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    any tips on using this site and of course getting the Java code to work would be greatly appreciated.
    Thanks in advance!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    It's throwing that exception because that's what the code is telling it to do when setName is called:
    Java Code:
    private void setName(String string) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

  3. #3
    Saracen is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Yes, the exception statement was inserted automatically by NetBeans because of another error I received. What do you suggest I do to fix this?

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Fulfill the code block. Make it set the name of the cat to the string that it is passed.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    Saracen is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Dark, I'm not sure what you mean? How do I do that?

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    In your Animal class you have the setName method, in your Cat class you have the setName method. setName method in your cat class is overriding your setName method in your Animal class.

    The code in your Animal class will work, the one in your Cat class will throw that exception every time. What happens when you delete that setName method in the Cat class? What happens when you change the code in the Cat class to something else?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,565
    Rep Power
    23

    Default

    set the name of the cat to the string that it is passed
    Look at using an assignment statement for "setting" the value of a variable.

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Oh hey look at that, I guess I wasn't paying very close attention to your code. Yes, Sunde is right. You do not define methods in your main class, only call them. So what is your Cat class supposed to do? Extend Animal and test itself?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    Saracen is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    My god it works! (well getting there...).
    I commented out the cat.setName ("Felix"); and it gave me:

    nameI'm playing!

    Now I just need to figure out how to put name of the cat (Felix) into the code and make it work.

  10. #10
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Saracen View Post
    My god it works! (well getting there...).
    I commented out the cat.setName ("Felix"); and it gave me:

    nameI'm playing!

    Now I just need to figure out how to put name of the cat (Felix) into the code and make it work.
    Java Code:
    System.out.println( "name" + "I'm playing!");
    You have name in quotes which means it's going to print name out (literally). Since you want name to be the name of the animal you must take it out of quotes.

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    @Logik22 That is true, however he's not setting his name variable to anything. So it will just be " I'm playing!"

    You need to fix your methods. Take all your method declarations out of your main method and place them as there own individual methods. Then make your setName method in your cat class actually set the name of your cat, instead of throwing an exception. Do that, and we'll see how you're doing.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    I don't think the OP wrote the code him/herself otherwise s/he would know that this would throw an Exception:

    Java Code:
    private void setName(String string) {
       throw new UnsupportedOperationException("Not yet implemented");
    }
    Browing and copy/pasting from the internet is not an intelligent thing to do ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Saracen is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Jos, none of the code you see on this post is copied from the "Internet". The code:
    private void setName(String string) {
    throw new UnsupportedOperationException("Not yet implemented");

    ...is what NetBeans suggested because of a previous error. If you've used NetBeans before you'd know, as the little light bulb appears in the code-line column.

    Dark, I understand more about the code now than I did before,Thanks.
    I'm going to make the changes and report back.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Saracen View Post
    Jos, none of the code you see on this post is copied from the "Internet". The code:
    private void setName(String string) {
    throw new UnsupportedOperationException("Not yet implemented");

    ...is what NetBeans suggested because of a previous error. If you've used NetBeans before you'd know, as the little light bulb appears in the code-line column.

    Dark, I understand more about the code now than I did before,Thanks.
    I'm going to make the changes and report back.
    Copying anything from NetBeans is just as bad; if you don't know what NetBeans suggests, don't copy it but first think a bit.

    kind regards,

    ps. I use Eclipse
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    @JosAH, I think he just hit the button to add it. Obvious novice mistake, but in partial due to using too powerful of a IDE to learn on.
    Saracen likes this.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. java problems
    By swrta in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2011, 01:32 PM
  2. Some problems with java
    By JeanNoel 53 in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 11:24 PM
  3. java problems
    By p595285902 in forum New To Java
    Replies: 6
    Last Post: 11-28-2010, 10:55 PM
  4. Replies: 2
    Last Post: 01-16-2010, 04:50 PM
  5. Java Problems
    By xonkie in forum New To Java
    Replies: 6
    Last Post: 12-03-2008, 07:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •