Results 1 to 15 of 15
- 07-18-2011, 04:18 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
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!
- 07-18-2011, 04:30 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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"); }
- 07-18-2011, 04:53 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
Yes, the exception statement was inserted automatically by NetBeans because of another error I received. What do you suggest I do to fix this?
- 07-18-2011, 04:56 PM #4
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.
- 07-18-2011, 04:58 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
Dark, I'm not sure what you mean? How do I do that?
- 07-18-2011, 05:01 PM #6
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.
- 07-18-2011, 05:02 PM #7
Look at using an assignment statement for "setting" the value of a variable.set the name of the cat to the string that it is passed
- 07-18-2011, 05:06 PM #8
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.
- 07-18-2011, 05:08 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
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.
- 07-18-2011, 05:13 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
- 07-18-2011, 05:17 PM #11
@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.
- 07-18-2011, 05:24 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
I don't think the OP wrote the code him/herself otherwise s/he would know that this would throw an Exception:
Browing and copy/pasting from the internet is not an intelligent thing to do ...Java Code:private void setName(String string) { throw new UnsupportedOperationException("Not yet implemented"); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-18-2011, 05:31 PM #13
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
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.
- 07-18-2011, 05:36 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 07-18-2011, 09:14 PM #15
Similar Threads
-
java problems
By swrta in forum AWT / SwingReplies: 1Last Post: 03-23-2011, 01:32 PM -
Some problems with java
By JeanNoel 53 in forum New To JavaReplies: 1Last Post: 12-30-2010, 11:24 PM -
java problems
By p595285902 in forum New To JavaReplies: 6Last Post: 11-28-2010, 10:55 PM -
Problems with Java 2D example in the book: JAVA 2D Graphics by O'Reilly
By Lil_Aziz1 in forum Java 2DReplies: 2Last Post: 01-16-2010, 04:50 PM -
Java Problems
By xonkie in forum New To JavaReplies: 6Last Post: 12-03-2008, 07:14 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks