• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New to Java looking for guidance.

 
Steffen Hogan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone
First post ever in a Java Forum. I'm learning Java, trying to at least. I read a the Java for dummies book wrote up a blackjack game and now looking to the experts on how to make it better. I wanted to make a working program without any guidance to see if I could do it. Basically its 5000 lines of ifs and a few whiles. Anyways if I wanted to know if I should paste the code, if so where and how and how much of it. Just looking for guidance on what to do next and how to make life easier.
Thank you. -steffHogan
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffen Hogan wrote:First post ever in a Java Forum. I'm learning Java, trying to at least. I read a the Java for dummies book wrote up a blackjack game and now looking to the experts on how to make it better. I wanted to make a working program without any guidance to see if I could do it. Basically its 5000 lines of ifs and a few whiles. Anyways if I wanted to know if I should paste the code, if so where and how and how much of it.

Welcome Steffen.

First: I don't think anyone's going to look through 5,000 lines of code (or even 500, I hate to say).

Second: If it's mostly 'if's and 'when's, I suspect you have a lot of redundancy (repeated code), and that's what you're going to need to fix.

Third: How many classes does it have? If less than 5, chances are you need to break up the logic into more.

Fourth: How big is your main() method? If more than about 20 lines, again, chances are you need to break down the logic.

Fifth (and probably most important): Does it work?

Winston
 
Steffen Hogan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston for getting back to me.
Yeah I didn't think anyone wanted to look through the code. Lots and lots of redundancy. Its all in one class and all in the main. It does work though. I'm reading the headfirst java book now and learning more about classes at least getting a better understanding of them. I'm confused about it but okay with it. Its all part of the game right. -steffHogan
 
Branden Bobo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First: I don't think anyone's going to look through 5,000 lines of code (or even 500, I hate to say).


While I agree, I am also new to java, but i'm not new to object oriented programming.
If you want to post your code as an attachment, I'll take a look and see if I can help.

also by putting all your code in one method it makes it harder to make changes to, re-use parts of, or extend your code.
From my humble understanding this is one of the key benefits to OOP.

If were that's where would start, by re-factoring your to an OO approach.
 
Steffen Hogan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Branden

I think I did the upload right chose the .java file at least. Yeah I'm new to the whole programming thing in general besides a c++ class I took back in high school. I'm going through the coderanch java assignments now trying to get a better understanding of everything. I can see how creating different methods and classes can help out so you can add features with out messing with code that already works. Thanks for replying Branden. -steffHogan
 
Steffen Hogan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Server was down first time I tried to upload it and the back button took off the attachment. -steffHogan
-- Second time says .java files are now allowed so threw it in as a .txt
-- Would not let me upload a .txt
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see how creating different methods and classes can help out so you can add features with out messing with code that already works.

you are well on your way. the previous replies are helpful. it took me a long time before i got the hang of OO since i started programming before it was even an idea. i grew to love it though.

most important(well maybe not most important, but important) IMO of the earlier comments is your main method sould do very little. ideally it should look something like this(if it is not a GUI)
public static void main()
{
MyClass myClass = new MyClass();
myClass.doSomething();
}
 
Steffen Hogan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall,
So the basics that I should shoot for in the main is just bringing all the classes and methods together in a nice small package for the user, right? This is where I would also make this public while the rest are private? -steffHogan
 
Steffen Hogan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel stupid when it seems like the simple things do not work out.
I'm working on a blackjack game and it works when its all in one class. Now I'm trying to condense the 5000 lines down by putting in classes to get rid of the redundant code. Yet the first class I'm working on the cards dealt is not working I have an array for cards it goes out to 52 (for each card) and a random int to pick a card yet its not returning the card. Not looking for a response just letting out some frustration till I figure it out. - steffHogan
 
Joseph Potts
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't looked at the code, but by the information you've given, I'd suggest use the switch statement instead of all those if statements, it would considerably make it much easier to read, and (i'm not quite sure about this, but I think it will) shorten the amount of statements with the same output in a better way
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic