• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object-Oriented Programming  RSS feed

 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been given the task of a project oriented program. I need to learn how to use a package in java. I am not sure how to set it up as I have tried to make a package. I need to know how to set up a package so I can recall information from my main. This way I can run muti code and have my main clean. Can you please give me ideas on how to set up the code. So I have my main and I reference my packages but is my package code just part of the main? I can not add any code to the package that I have made off of my main. This will be the first time I have used the package method of programming. I have been using my Java book as reference but still seem to be stuck. Thanks again. I will post my code once it looks better.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by package? Do you mean the package key word in Java or do you mean the way you package your classes into a runnable piece of logic? If you had meant the Java package keyword, you do not need to spend any effort to use it other than typing the keyword package followed by the package name.

Post what you have written so far, you will be much better supported.
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean Java’s packaging scheme and how to set up the logic correctly. As it is I have all code on my main. I have a package class that will change depending on user commands.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Bykenhaal wrote:I mean Java’s packaging scheme and how to set up the logic correctly. As it is I have all code on my main. I have a package class that will change depending on user commands.


Frankly speaking, I do not understand what you mean! Show us some code for us to help you better!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Bykenhaal wrote:I mean Java’s packaging scheme and how to set up the logic correctly. As it is I have all code on my main. I have a package class that will change depending on user commands.

I hate to say, Brian, but you're not giving us much to work with.

A "package" is a folder that contains compiled Java class (.class) files that relate to each other. You compile them with the javac command and include other packages with the classpath (-cp) parameter if you need to. Beyond that, there's very little else we can tell you without more information.

About the only other thing I can tell you is that putting all your code in main() is NOT a good idea. You might want to read the MainIsAPain page for more details, but even better might be to start off with the tutorials.

Winston
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So sorry for the lack of information. What I mean is I think I want to have a package and I have added the import at the start of my main. I do not know where to put the code for the package. I am using NetBeans and looking to learn the basics on how to make a program using packages.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Bykenhaal wrote:So sorry for the lack of information. What I mean is I think I want to have a package and I have added the import at the start of my main.

By 'main' I assume you mean you have a main class which you have called 'main' or do you mean the main method that is used to start applications.
This sort of confusion shows why choosing a good descriptive name for your classes is so important. And main (or Main as it should be called to conform to Java Naming Conventions) is not a descriptive name.

I do not know where to put the code for the package.

I'm not sure what you mean by this. I suggest you read the Java tutorial on packages (http://docs.oracle.com/javase/tutorial/java/package/) and ask here if you need further clarification.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, close that Netbeans program and get a decent text editor like Notepad++ or Textpad and learn from the Oracle tutorials linked above using that text editor. Only return to using Netbeans when you have understood how packages and the classpath works.
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello thanks for the help I am making a game board tester. I have a new project started and wanted to have packages that I can use. I think I can make like a class for board and main but I do not get packages. I need to test three board game boards. Here is my main question how do I add an enumeration named Outcome to the games.board package? I can not write in the package this is what I read. So how would I go about doing this?




 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code so far I am working on the board now. I just made a class called game.board and I want to have both classes work together so I have imported them.



 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does not help us. Have you read the links you have been given, particularly the link about packages in the Java Tutorials?
You do not want an enumeration; an enumerated type would probably be useful. You can read about them in the Java tutorials, too.
Brian Bykenhaal wrote: . . . I can not write in the package this is what I read. So how would I go about doing this?
Where on earth did you read that? That sounds like rubbish; I presume you have completely misunderstood what you read.
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I am just new to this IDE I need to make each source file for this project so different classes for all of them. I can not write in a package I can write in a class the package is what holds the class.
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link Winston Gutkowski I want to have more classes and only one main. I will try to follow the Main is a Pain tutorial.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Bykenhaal wrote:Thanks for the link Winston Gutkowski I want to have more classes and only one main. I will try to follow the Main is a Pain tutorial.

Glad it was of use.

Winston
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Bykenhaal wrote: . . . I can write in a class the package is what holds the class.
That is writing in the package when you use an IDE. the IDE will put the .java and .class files in the right place to match the package structure.

It goes to show why it is probably best not to use IDEs until you are more experienced.
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I have I have changed my project so that each class other then main in under my game board package. I am would like to make my main in outside the game package where I have all my classes should this be any issue? I have not been able get past these errors. I have 6 classes including my main.


run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable EMPTY
location: class games.board.Mark
at games.board.Cell.<init>(Cell.java:10)
at games.board.Board.<init>(Board.java:10)
at BoardGameTester.main(BoardGameTester.java:13)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Bykenhaal wrote:





This won't solve the problem you are having below, but I don't think you need to declare the Board variables as private static in your class and also local in your function. If you do it in one or the other, you should be able to get rid of the @SurpressWarning .
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Bykenhaal wrote:Okay I have I have changed my project so that each class other then main in under my game board package.
I would like to make my main in outside the game package where I have all my classes should this be any issue?



No, you can have the class that has the main() method in a package different from code that is is going to use. But you do have to specify to the compiler or the JVM where to start looking for your package via the CLASSPATH env variable or -classpath command line option. But I guess Netbeans has an options dialog that is used to specify that info.


I have not been able get past these errors. I have 6 classes including my main.


run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable EMPTY
location: class games.board.Mark
at games.board.Cell.<init>(Cell.java:10)
at games.board.Board.<init>(Board.java:10)
at BoardGameTester.main(BoardGameTester.java:13)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)


Can you add the code line that corresponds to Cell.java:10, Board.java:10 and BoardGameTester.java:13 ?

This error is from running your program or compiling your program?
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Campbell Ritchie for the tips and the help. I plan to get Eclipse and findbugs to help debug. I have another question in regards to packages. I have made a package game.board in Netbeans this is two packages right? I have tried to make a package named games then another package in games called board this gives me two separate packages the later inside the games package. My question is should I have two packages or use the first method to make a sub package in games?


The project Log,

I am plan to use both debuggers that Campbell Ritchie recommends to see if I can test my board. Thanks for the help.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Bykenhaal wrote: . . . I have made a package game.board in Netbeans this is two packages right? . . . My question is should I have two packages or use the first method to make a sub package in games?
I do not think we can advise you on that; you will have to design the app yourself. The package games.board is a package inside another package. Does the board package belong inside the game package? Only you can decide.
. . . I am plan to use both debuggers that Campbell Ritchie recommends to see if I can test my board. Thanks for the help.
They will help debugging and finding style errors, but not with testing. You will have to learn something like JUnit or write tests yourself.

I suspect learning Eclipse and findbugs and JUnit all together will turn out to be biting off more than you can chew.
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator










































 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted my code last post been tweaking the code looking for a way to output my boards. The first code in the last post is in it's own package. The rest is in a different package. This keeps the test package away from the game package. I have yet to see anything in my console when I run the file. I will keep working with the debugger and IDE to find the problems. Thanks for all your help with the package set up.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Bykenhaal wrote:I have yet to see anything in my console when I run the file.

Not even a stack trace from a UnsupportedOperationException ?
You have an inner class called Board inside your Board class. All your functionality seems to be inside the inner class, but your test class is creating instances of the outer class and the constructor of the outer class simply throws a UnsupportedOperationException exception.
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tried to compile the code to debug my program it gave me some error messages. I am going to go and fix any problems I after I use javac. Thanks for the tip on using the console it helped me find some problems that my IDE called something else.
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I everyone thanks for the help been working on my project and have run into a problem I would like some help on. I


Exception in thread "main" java.lang.ExceptionInInitializerError
at games.board.Cell.<init>(Cell.java:10)
at games.board.Board.<init>(Board.java:16)
at games.board.BoardGameTester.main(BoardGameTester.java:16)
Caused by: java.lang.RuntimeException: Uncompilable source code - class games.board.Mark is already defined in package games.board
at games.board.Mark.<clinit>(Mark.java:15)
... 3 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

I then tried to fix the problem by only have the for statement on one class and I got the following error.


Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at games.board.BoardGameTester.main(BoardGameTester.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Bykenhaal wrote:
I then tried to fix the problem by only have the for statement on one class and I got the following error.

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at games.board.BoardGameTester.main(BoardGameTester.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)


http://www.coderanch.com/how-to/java/FixAllCompilerErrorsBeforeRunningTheApplication
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice thanks Mr. Wong, I will go to the console and see if I can debug my code. Fix one by one and then try to compile. Sounds a lot easier then what I was thinking. Thanks for the link it was most useful.
 
Brian Bykenhaal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After research I have learned that I must use a class path for my main. I have tried and not been able to fix the error. Any one have is error and know how to solve it?

run:
Exception in thread "main" java.lang.ExceptionInInitializerError
at games.board.Cell.<init>(Cell.java:10)
at games.board.Board.<init>(Board.java:16)
at games.board.BoardGameTester.main(BoardGameTester.java:17)
Caused by: java.lang.RuntimeException: Uncompilable source code - class games.board.Mark is already defined in package games.board
at games.board.Mark.<clinit>(Mark.java:15)
... 3 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!