GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Wrong coding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrong coding" Watch "Wrong coding" New topic
Author

Wrong coding

Craig E. Lewis
Greenhorn

Joined: Jan 27, 2012
Posts: 29

can anybody tell me whats wrong with this as well as what is?
boolean
=false
=true
mean

Added Code tags, please read UseCodeTags
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

What is the issue you are facing?

And

can be written as


Mohamed Sanaulla | My Blog
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Craig E. Lewis wrote:can anybody tell me whats wrong with this as well as what is?

Well if you told us what the matter is it would help.

Second (and I'll say this before Campbell does)
if (d.snare == true) {
is redundant, and prone to error (although not incorrect logically). All you need is
if (d.snare) {

Winston

[Edit] Too slow


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Craig E. Lewis
Greenhorn

Joined: Jan 27, 2012
Posts: 29
This is the problem

C:\Users\Craig\java\src>javac Drumkit.java
.\Drumkit.java:1: duplicate class: DrumKit
class DrumKit {
^
.\Drumkit.java:14: duplicate class: DrumKitTestDrive
class DrumKitTestDrive {
^
Drumkit.java:17: cannot access Drumkit
bad class file: .\Drumkit.java
file does not contain class Drumkit
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
Drumkit d = new Drumkit();
^
3 errors

C:\Users\Craig\java\src>
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Craig E. Lewis wrote:This is the problem

C:\Users\Craig\java\src>javac Drumkit.java
.\Drumkit.java:1: duplicate class: DrumKit
class DrumKit {
^
.\Drumkit.java:14: duplicate class: DrumKitTestDrive

Well the message is quite explicit; although it may have something to do with the fact that your DrumKit class isn't public.

My suggestion: Add public to the definition, and remove the DrumKitTestDrive class altogether. It doesn't add anything, and since it's not public, the main() method will never be run anyway.

Winston
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14190
    
  20

Also note that DrumKit is not the same as Drumkit. Java is case-sensitive.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Jesper de Jong wrote:Also note that DrumKit is not the same as Drumkit. Java is case-sensitive.

Well spotted that man. Totally missed it.

Winston
Craig E. Lewis
Greenhorn

Joined: Jan 27, 2012
Posts: 29
Hey Guys I have a new problem thanks for solving the last
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.


DrumKit.java:18: cannot find symbol
symbol : method playsnare()
location: class DrumKit
d.playsnare();
^
DrumKit.java:23: cannot find symbol
symbol : method playsnare()
location: class DrumKit
d.playsnare();
^

I also changed the coding
public class DrumKit {

boolean tophat = true;
boolean snare = true;

void playTopHat() {
System.out.println( "ding ding da-ding");
}
void playSnare() {
System.out.println("bang bang ba-bang");
}
}

class DrumKitTestDrive {
public static void main(String [] args) {

2 errors

Craig E. Lewis
Greenhorn

Joined: Jan 27, 2012
Posts: 29
Sorry guys ignore the last thing i said i solved it and have compiled it but i don't understand this

C:\Users\Craig\java>java DrumKit
Exception in thread "main" java.lang.NoSuchMethodError: main

This is what i did to the code on your suggestion i took drumkit testdrive



public class DrumKit {

boolean tophat = true;
boolean snare = true;

void playTopHat() {
System.out.println( "ding ding da-ding");
}
void playSnare() {
System.out.println("bang bang ba-bang");
}
}

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Craig E. Lewis wrote:Hey Guys I have a new problem thanks for solving the last
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.


DrumKit.java:18: cannot find symbol
symbol : method playsnare()
location: class DrumKit
d.playsnare();
^
DrumKit.java:23: cannot find symbol
symbol : method playsnare()
location: class DrumKit
d.playsnare();
^

I also changed the coding

2 errors



As already mentioned, Java is case-sensitive. This is also true for the method names.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Craig E. Lewis wrote:Sorry guys ignore the last thing i said i solved it and have compiled it but i don't understand this

C:\Users\Craig\java>java DrumKit
Exception in thread "main" java.lang.NoSuchMethodError: main

This is what i did to the code on your suggestion i took drumkit testdrive






The compiler is complaining that the DrumKit class doesn't have a main() method. Question. Does your DrumKit class have a main method?

Henry
Craig E. Lewis
Greenhorn

Joined: Jan 27, 2012
Posts: 29
I have just started and i am not sure what you mean
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14190
    
  20

Do you know what the main() method is? When you run a Java program, the Java runtime environment looks for a method public static void main(String[] args) in the class that you are trying to run.

You are trying to run class DrumKit, but it does not have a main() method. In fact, the main() method is in one of the other classes you wrote. You have to run that other class, not the DrumKit class.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Jesper de Jong wrote:You are trying to run class DrumKit, but it does not have a main() method. In fact, the main() method is in one of the other classes you wrote. You have to run that other class, not the DrumKit class.

Which is why I suggested that you remove the 'testDrive' class. It's not doing you any good. Just put the main() method in the DrumKit class, as Jesper says. In fact, while you're still in the early stages, my advice would be to stick to general rule of 1 public class per .java file (with the same name as the file too).

Winston
Craig E. Lewis
Greenhorn

Joined: Jan 27, 2012
Posts: 29
All right I changed my code to this



but now I have a new problem

C:\Users\Craig\java>java DrumKit
Exception in thread "main" java.lang.NoSuchMethodError: main



[Edit - added code tags - see UseCodeTags for details]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4396
    
    8

That code won't compile - you've declared the playTopHat and playSnare methods inside the main method.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Matthew Brown wrote:That code won't compile - you've declared the playTopHat and playSnare methods inside the main method.



And looking at the error message, it looks like the new code changes wasn't compiled -- it is the same error from previous.

Henry
Craig E. Lewis
Greenhorn

Joined: Jan 27, 2012
Posts: 29
So what would I have to do to get out of main if you don't mind
Craig E. Lewis
Greenhorn

Joined: Jan 27, 2012
Posts: 29
So what would I have to do if you don't mind ?
 
 
subject: Wrong coding