Hi! I'm new at Java and programming in general and have been trying to learn the language for about two and a half months now. I know that that isn't much time, but I have gotten far and I hope to go much further. I learn java in a school class, but this is the first class of it's kind at my school and the only students are me and one other kid.
While the other kid seems to understand things right away I'm having a problem with thinking out programs; I tend to think of code that is overly extravagant, incredibly redundant, way too long and just wastes memory on a whole- at least it seems that way. I first began learning programming through Alice, in the 'Computer Programming 1' course at my school, so it may be that I'm not used to having to write out everything that could possibly be considered in the program.
For instance, take this tiny game of guessing which number the die will roll on I just created:
[QUOTE]import java.util.Scanner;
import java.util.Random;
class SixSidedDie {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random generator = new Random();
System.out.println("Do you want to play some dice?");
int yes = input.nextInt();
if (yes == 1){
System.out.println("Alright then! Choose a number that the die will land on.");
int choice = input.nextInt();
int roll = generator.nextInt(6) + 1;
if (choice == roll)
System.out.println("Congratulations! You won!");
else
System.out.println("Oh, dang! Sorry, you lost. The real answer is " + roll);}
else
System.out.println("Sorry for wasting your time, then.");
}
}[/QUOTE]
Is it just me or does that seem over-done? It seems like way too much code for a simple guessing game. When I attempt to complete larger projects the whole thing seems to explode exponentially; the code can easily reach two to three pages with barely any of the actual problem being completed. Am I going about this the wrong way? And also, what other hints do you have for a newbie? Thanks for reading and helping!
That's very simple and easy to understand code. Actually, it's fairly minimal as well. I can't think of anything that doesn't have to be there.
[QUOTE=Smashmaster;34937637]That's very simple and easy to understand code. Actually, it's fairly minimal as well. I can't think of anything that doesn't have to be there.[/QUOTE]
Really? I guess I'm too used to Alice's 'drag-and-drop' approach. Suddenly programming went from simply wondering how fast to make a fat man glide out of view of the camera to trying to remember whether there's an asterisk at the end of the first line of code, then getting pissed off because that one thing will ruin the whole project.
[QUOTE=willer;34937663]Really? I guess I'm too used to Alice's 'drag-and-drop' approach. Suddenly programming went from simply wondering how fast to make a fat man glide out of view of the camera to trying to remember whether there's an asterisk at the end of the first line of code, then getting pissed off because that one thing will ruin the whole project.[/QUOTE]
But it's so [b]fun[/b] when everything works as you told it to. Stick at it, I promise you it gets more rewarding.
[sp]And more angering[/sp]
[QUOTE=willer;34937663]Really? I guess I'm too used to Alice's 'drag-and-drop' approach. Suddenly programming went from simply wondering how fast to make a fat man glide out of view of the camera to trying to remember whether there's an asterisk at the end of the first line of code, then getting pissed off because that one thing will ruin the whole project.[/QUOTE]
A lot of new programmers get hung up on how a single error will supposedly 'ruin' the whole project. A simple mistake is also simple to fix, and when you get comfortable with the language, you won't make mistakes like that.
I guess my best advice is that this is just how programming is. It takes a lot of code to do simple things, but once you're good this wont seem like a problem.
Sounds like you already know how to [i]think[/i] like a programmer, you're just getting used to [i]writing code[/i]. And it really is just a matter of getting used to it; after awhile, a program like that won't seem as complicated as it does now.
[QUOTE=willer;34937548]When I attempt to complete larger projects the whole thing seems to explode exponentially; the code can easily reach two to three pages with barely any of the actual problem being completed. Am I going about this the wrong way? And also, what other hints do you have for a newbie? Thanks for reading and helping![/QUOTE]
[i]Part[/i] of this is the fact that you're using Java. Java doesn't have a lot of nuances to the language, which makes it relatively easy to learn — I'm referring to the syntax of the language itself, not the huge class library — but it also means that Java code isn't very "dense". It [i]can[/i] take a lot of lines to express a simple idea sometimes. Big groups of trivial getter/setter methods are a prime example of this.
There are other languages with richer syntax, where you can express more meaning in less space on the screen, but conversely, that can make the code harder to read and understand.
[QUOTE=Smashmaster;34937723]I guess my best advice is that this is just how programming is. It takes a lot of code to do simple things, but once you're good this wont seem like a problem.[/QUOTE]
uhhh mistakes like these literally don't matter at all even if you've been in the industry for a decade. IDEs solve everything.
it should be thought about in the same way as writing -- minor grammatical errors don't matter, what matters is the overall structure and organization of the document.
If you have any more than one line in any if/else statement you should really put some brackets as well, like so:
[cpp]
if (yes == 1)
{
System.out.println("Alright then! Choose a number that the die will land on.");
int choice = input.nextInt();
int roll = generator.nextInt(6) + 1;
if (choice == roll)
{
System.out.println("Congratulations! You won!");
}
else
{
System.out.println("Oh, dang! Sorry, you lost. The real answer is " + roll);
}
}
else
{
System.out.println("Sorry for wasting your time, then.");
}
[/cpp]
When I read the code from the browser I could not see where the "Sorry for wasting your time" would fit in. After pasting it to Netbeans and auto formating it made the brackets and I could understand it.
Even more so I generally hate bracket less statements.
[U][B][I]Indenting![/I][/B][/U]
For the love of god, INDENTING!
Other than that, nothing really wrong with your code as it is.
Yea, I tried to indent it in the browser, but when I hit the post button again everything just defaulted to the left-most position. In Netbeans it looked much more easy to read and followed most of the guidelines.
Thanks for your advice and help!
[QUOTE=willer;34941432]Yea, I tried to indent it in the browser, but when I hit the post button again everything just defaulted to the left-most position. In Netbeans it looked much more easy to read and followed most of the guidelines.
Thanks for your advice and help![/QUOTE]
Indents aren't supported in quite tags. Use [noparse][cpp]code[/cpp][/noparse] tags instead.
Sorry, you need to Log In to post a reply to this thread.