• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

I started to learn to program 4 days ago. I made a few programs. What do you think?

Status
Not open for further replies.

Cse

Banned
I started to teach myself how to program about 4 days ago. While I consider myself more technologically literate than the average individual, I still didn't know how to write even one line of code just 4 days ago when I started. So, it was a completely fresh start. I'm also not a CS major or anything like that, so I really had no foundation at all.

I started with some Youtube videos to learn the basic programming motifs - if/then conditionals, loops, functions/methods, variables, objects and classes, etc. I then jumped right into Java, which was probably a mistake, as apparently it's not the most welcoming language for a complete beginner.

Today I decided not to learn anymore new information, and to instead actually build something. So I did. Each of these programs are incredibly basic, and probably suck, and many of you who know how to code could probably write each of these in your sleep in about 5 minutes, but again, I'm only on day 4 here. Each of these took me about 30-45 minutes to write, and then about another hour of working out the bugs.

The first program is a simple questionnaire - it asks the user for their name, and then poses two questions to them. The program will tell them whether their responses to each of the questions are correct, and will then tell them how many questions of the 2 they answered correctly.

hIrfckJ.png



The second program generates a random number which ranges from 0-10, concealing it from the user, and then it asks the user to guess the random number. If the first guess is correct, they are congratulated and the program ends. If they are incorrect, the program provides a qualitative assessment of how far off they are (basically a hint), which is actually based on how numerically far away their guess was from the randomly generated number. It then asks for their guess again, and will continue doing so until the user provides the correct answer, after which the program ends.

msw2H9j.png








So, that's about the limit of what I can do so far after 4 days. Input? Suggestions? I certainly don't want to pick up any bad habits.

Does the style suck or am I making errors anywhere? Should I be able to do more than this after 4 days?
 
Change the return type on the function to void (doesn't seem like you're returning anything) and later on you'll want exception handling on scanner when it gathers input.

edit: To clarify, you are returning a value but it's not being assigned to anything or used for anything. Also check out camelCasing for when naming functions and variables.
 

RedShift

Member
Might post a bit more feedback later but the first thing that springs to mind is that you can convert their answer to all caps using "theirAnimal.toUpperCase()" (or some other method that sounds similar, I think toUpperCase is right though). That way you can replace the switch statement with something like.

if (theirAnimal.toUpperCase().equals("CAT")) {
//correct answer
} else {
//incorrect answer
}

It's probably best to avoid switch statements if possible, for now at least.

EDIT:Oh, beaten.
 

Lumination

'enry 'ollins
That's very impressive for 4 days! As a general tip, use camelCase to name variables (but not classes!) in Java. Camel case is when your first word is lower case, and every other word's first letter is capitalized.

I see you used equalsIgnoreCase() in the second half of your first function? Any reason you didn't just use that instead of the switch?

In the second function, you could use some else if's instead of just ifs. For instance:
Code:
if(theirNumber == bucket) {
    System.out.println("Correct");
}
else if(theirNumber - bucket >= 4) {
    System.out.println("Too high!");
}
...

Else ifs only execute if the if or else if before it is not satisfied. That way, if the guess was correct, you will not have to make every if check like you're doing now.

There are a few mini-optimizations you could do, but for the sake of not bombarding you with info, I'll stop here. If you're curious, I could go more in-depth.

Overall, good job!
 

injurai

Banned
I started to teach myself how to program about 4 days ago. While I consider myself more technologically literate than the average individual, I still didn't know how to write even one line of code just 4 days ago when I started. So, it was a completely fresh start. I'm also not a CS major or anything like that, so I really had no foundation at all.

Cse
Member
(Today, 03:45 PM)


537.gif
 

RobertM

Member
You could have used else if instead of repeating if statement over and over, and you didn't need to use switch case for the "cat" checking.
 

Fireblend

Banned
Awesome! Keep it up man :)

Instead of the last if sequence in the first code blurb, how about this?
Code:
int correctAnswers = 0

if(theirAnimal.equalsIgnoreCase("cat"))
    correctAnswers++;

if(theirNumber == 144)
    correctAnswers++;

if(correctAnswers > 0){
    System.out.println("You got "+correctAnswers+" out of 2 correct!" );
} else {
    System.out.println("Sorry, you missed both questions.");
}

This way, you minimize duplicate code (all those print commands)

Also, instead of testing for CAT, cat, Cat, etc... you can just take the user's response (theiranimal), and apply a trim() and toLower() to it, which will convert all of those options (and others) to "cat" :)

Code:
String theiranimal = " CAT";
theiranimal = theiranimal.trim().toLower();
if(theiranimal.equals("cat")){
    System.out.println("success!");
}

trim() removes all spaces before and after the string, and toLower() converts all its characters to lowercase. So, "CaT", " cat", "cat ", "caT", etc will all trigger the condition to print success :)

Back to the if block, if you want to be extreme:

Code:
int correctAnswers = (theirNumber == 144)? 1 : 0 + (theirAnimal.equalsIgnoreCase("cat"))? 1 : 0;
System.out.println(correctAnswers > 0? ("You got "+correctAnswers+" out of 2 correct!") : "Sorry, you missed both questions.");

Never do stuff that looks like this though. Legible code is always the better option.
 

Cse

Banned
Change the return type on the function to void (doesn't seem like you're returning anything) and later on you'll want exception handling on scanner when it gathers input.

edit: To clarify, you are returning a value but it's not being assigned to anything or used for anything. Also check out camelCasing for when naming functions and variables.

I actually struggled through that a little bit. When I used "void" to describe the method in the second program, it gave me an error, saying it was "expecting a volatile".

When I removed void from the method declaration, it just asked for a return value, and since the program was effectively done at that point, I just decided to return the integer "theirnumber", even though it's not being returned to anything.
 
You could have used else if instead of repeating if statement over and over, and you didn't need to use switch case for the "cat" checking.

A good tip when programming is to attempt to repeat yourself as little as possible (DRY). Any time where you re just copy/pasting the same code again you should think about finding a better way.

E.g
Code:
answers = ['cat', 'Cat', 'CAT', 'a cat', 'kitty cat'];
score = 0;
if(answers.indexOf(theiranimal) >= 0){
score++
}
 
Ah I see why your function returns an int now. Ignore my first post in that case, I should have read the function, not used to that sort of recursive calling. Look into do...while loops next, and rewrite the function as a challenge.

Right now it just keeps calling a new function and that causes the run time stack to keep going back to the calling functions return address if the user keeps getting it wrong and then finally gets it right.

You can fold the two last if conditions into one, so long as they are enclosed and each statement is seperated by a semi colon.
 

Cse

Banned
That's very impressive for 4 days! As a general tip, use camelCase to name variables (but not classes!) in Java. Camel case is when your first word is lower case, and every other word's first letter is capitalized.

I see you used equalsIgnoreCase() in the second half of your first function? Any reason you didn't just use that instead of the switch?

When I first started writing that program, I simply used a switch statement to cover most case variants of "cat".

When I started writing my if statements, though, I used the "==" operator to compare each case variant, which gave me problems, as I eventually figured out that the "==" operator in Java tests for reference equality, while the .equals() method for string objects tests for value equality. Then I eventually discovered a few other methods of the String class, such as the equalsIgnonreCase().

I suppose I simply got so excited when I ran the program and it worked perfectly that I forgot to go back to the first half and make it a bit more efficient.
 
Ah I see why your function returns an int now. Ignore my first post in that case, I should have read the function, not used to that sort of recursive calling. Look into do...while loops next, and rewrite the function as a challenge.

Right now it just keeps calling a new function and that causes the run time stack to keep going back to the calling functions return address if the user keeps getting it wrong and then finally gets it right.

You can fold the two last if conditions into one, so long as they are enclosed and each statement is seperated by a semi colon.

I actually struggled through that a little bit. When I used "void" to describe the method in the second program, it gave me an error, saying it was "expecting a volatile".

When I removed void from the method declaration, it just asked for a return value, and since the program was effectively done at that point, I just decided to return the integer "theirnumber", even though it's not being returned to anything.

Nvm it's working correctly, you just need to wrap a do while loop between 19-20 (on the second program) and before the function ends. Declare a boolean variable within the function (but before the while loop), and make it loop until that boolean variable is true (by default it is false). It will be set to true when they answer correctly. Then you can get rid of the return statement and put a void type.
 

Lumination

'enry 'ollins
When I first started writing that program, I simply used a switch statement to cover most case variants of "cat".

When I started writing my if statements, though, I used the "==" operator to compare each case variant, which gave me problems, as I eventually figured out that the "==" operator in Java tests for reference equality, while the .equals() method for string objects tests for value equality. Then I eventually discovered a few other methods of the String class, such as the equalsIgnonreCase().

I suppose I simply got so excited when I ran the program and it worked perfectly that I forgot to go back to the first half and make it a bit more efficient.
Totally understand that feeling, haha. Remember to always double check your work! You'll oftentimes find things you could improve on simply because you put more thought into your function as you wrote. Have fun bugtesting when the time comes. =)

Edit: ^^ case in point. Bugtesting.
 

collige

Banned
There's nothing really to bad about either of these and you're far beyond where someone taking an introductory CS course would be. If I were code reviewing this I'd have the following comments:

-There's a bunch of extraneous whitespace in both files. Whitespace should be used to separate logical blocks of code and make it more readable.
-converting the user's input to lowercase and testing against "cat" will let you avoid that switch statement
-asktest() should return void since the return value isn't actually used
-However, an even better design would be to abstract the UI elements of asktest() to main and have it only process the user's input. Have it take in two ints and return a String with the response then have a loop in main that contuinially calls asktest() until it returns a positive response. It's generally good design to segregate interface elements from code that does actual processing.
-Score tabluation for AskProgram should probably be done in real time and maintained so that your final output only has to print a single string with the number correct and total number passed in. Each question should probably have its own method too.

Most of what I just posted is either nitpicky stuff or design concepts that are beyond your current level, but from what you posted so far it's nothing you shouldn't be able to pick up. The main thing you should take away from this is to always keep in mind ways to make your code as modular and extendable as possible.
 

panzone

Member
Not so bad, especially if I think you started only 4 days ago.

A suggestion: not "hard-code" things like the questions and the correct answers. Read them from somewhere like a file, so you can reuse the same program for different games without change everything every time. Doing this you'll learn:

how to write functions
how to read from a file (hint: you can use Scanner)

4 days in I was still fucking up on '=' vs. '==', lol.

7 years in and I often forget the ";" or that in the for statement the second part is the termination and not the incrementation.

Don't worry about these things. Programming is not syntax, is about the methods for solving problems.
 
Not so bad, especially if I think you started only 4 days ago.

A suggestion: not "hard-code" things like the questions and the correct answers. Read them from somewhere like a file, so you can reuse the same program for different games without change everything every time. Doing this you'll learn:

how to write functions
how to read from a file (hint: you can use Scanner)

OP this. Will save you a lot of debug time in the future,
 

Lautaro

Member
This is so fascinating.

I really admire people who do this.

The best way to learn how to program is to not put the code in a pedestal.

Anyone can learn how to program. To be a good programmer is up to you though.

Regarding OP, wait till you hear about object oriented programming, your mind will be blown with the possibilities.
 

Cse

Banned
Awesome! Keep it up man :)

Instead of the last if sequence in the first code blurb, how about this?
Code:
int correctAnswers = 0

if(theirAnimal.equalsIgnoreCase("cat"))
    correctAnswers++;

if(theirNumber == 144)
    correctAnswers++;

if(correctAnswers > 0){
    System.out.println("You got "+correctAnswers+" out of 2 correct!" );
} else {
    System.out.println("Sorry, you missed both questions.");
}

This way, you minimize duplicate code (all those print commands)

Wow... I'm now disappointed that I didn't think of something this simple and efficient, especially since I already know how to do everything here. I didn't even think of using a loop counter as a gauge to measure the number of correct questions.

Very cool.
 
I've been programming for 3 years now and even now I miss a goddamn ';' every once in a while.

Story of most programmers' lives.

Or my favorite when you compile/run/refresh the page and you get an error. You pull your hair out looking over the code and everything is write then you realize...you forgot something simple as a letter i somewhere, and you never pick it up despite reading the mentioned line over and over again because your mind autocorrects the spelling error you're seeing.

Fun times.
 
Story of most programmers' lives.

Or my favorite when you compile/run/refresh the page and you get an error. You pull your hair out looking over the code and everything is write then you realize...you forgot something simple as a letter i somewhere, and you never pick it up despite reading the mentioned line over and over again because your mind autocorrects the spelling error you're seeing.

Fun times.

Missing/extra brackets, especially when a function I am working on has lots of nested logic are my pet peeves. OP, if you use visual studio control-] is your friend for finding missing brackets/ ensuring you terminate blocks correctly.
 

Cse

Banned
-asktest() should return void since the return value isn't actually used
.

Yeah, this was a problem I ran into.

I knew when I was creating the method that it was going to be "void" since I wasn't planning on returning any value, yet when I declared the method as "void", Eclipse gave me an error message, telling me "Syntax error on token 'Void', volatile expected".

So, since the end of the method was effectively the end of this program, I simply removed void from the method declaration and return the integer "theirnumber". Even though it wasn't really returning to anything, the program was basically over anyway.
 

Relix

he's Virgin Tight™
Missing/extra brackets, especially when a function I am working on has lots of nested logic are my pet peeves. OP, if you use visual studio control-] is your friend for finding missing brackets/ ensuring you terminate blocks correctly.

Visual Studio 2013 does this automatically now. Its a godsend.
 

collige

Banned
Yeah, this was a problem I ran into.

I knew when I was creating the method that it was going to be "void" since I wasn't planning on returning any value, yet when I declared the method as "void", Eclipse gave me an error message, telling me "Syntax error on token 'Void', volatile expected".

So, since the end of the method was effectively the end of this program, I simply removed void from the method declaration and return the integer "theirnumber". Even though it wasn't really returning to anything, the program was basically over anyway.

I don't recall ever running into that specific error message, but it sounds like a syntax error. Are you sure you don't have "static void int asktest"? Void is a return type so it has to replace int in your method declaration.
 

Fireblend

Banned
Wow... I'm now disappointed that I didn't think of something this simple and efficient, especially since I already know how to do everything here. I didn't even think of using a loop counter as a gauge to measure the number of correct questions.

Very cool.

It becomes second nature eventually :)

Here's some advice not many people will give you: try to do a bit of research on recursion, not because you'll use it very often, but because it should help you understand the concept of the call stack and what you're doing when managing nested calls or loops a little bit more.

The classic example is calculating fibonacci numbers. If you can wrap your head around it, you'll be able to come up with a ton of "clever" solutions both in terms of how much code you actually need, and efficiency. The wikipedia article might be a good starting point. In fact, when teaching I usually start with functional languages instead of an object-oriented one like Java despite its popularity, simply because I find those more effective at making people confront the kind of "mindset" they have to put themselves into to come up with effective code logic. Recursion is also a lot more natural in those.
 

Cse

Banned
. Are you sure you don't have "static void int asktest"? Void is a return type so it has to replace int in your method declaration.

Ha! That was it. Fixed it to "static void asktest() {", and now I don't have to include a meaningless return value.

Thanks.
 

mclem

Member
Awesome! Keep it up man :)

Instead of the last if sequence in the first code blurb, how about this?
Code:
int correctAnswers = 0

if(theirAnimal.equalsIgnoreCase("cat"))
    correctAnswers++;

if(theirNumber == 144)
    correctAnswers++;

if(correctAnswers > 0){
    System.out.println("You got "+correctAnswers+" out of 2 correct!" );
} else {
    System.out.println("Sorry, you missed both questions.");
}

This way, you minimize duplicate code (all those print commands)

Better still, build the score increment into the if that tests the response to the question. No need to do multiple IFs!


Regarding asktest, it's probably not a good idea to call asktest recursively if the user gives an incorrect answer; it'll work, and in this context that's fine (and there's some smart coding around that; it'd be very easy to make a rookie error that would generate a new random number each time!), but it's using much more RAM than is necessary. Instead, set up a loop that continues until the answer is correct.


It becomes second nature eventually :)

Here's some advice not many people will give you: try to do a bit of research on recursion, not because you'll use it very often, but because it should help you understand the concept of the call stack and what you're doing when managing nested calls or loops a little bit more.

The classic example is calculating fibonacci numbers. If you can wrap your head around it, you'll be able to come up with a ton of "clever" solutions both in terms of how much code you actually need, and efficiency. The wikipedia article might be a good starting point. In fact, when teaching I usually start with functional languages instead of an object-oriented one like Java despite its popularity, simply because I find those more effective at making people confront the kind of "mindset" they have to put themselves into to come up with effective code logic. Recursion is also a lot more natural in those.

When I was at university the first language we worked with was Haskell, precisely because as a functional language it was really good at getting us to understand recursion and the programming skills and risks associated with it.
 

JohnsonUT

Member
Good job OP. Why there are many valid and important suggestions in this thread, don't worry too much about them right now. Just keep coding programs that interest you. Most of the lessons in this thread are learned simply by programming over and over. Any programmer, no matter how experienced, will look at code from his or her past and think "Why the heck did I do it this way"?
 

Cse

Banned
It becomes second nature eventually :)

Here's some advice not many people will give you: try to do a bit of research on recursion, not because you'll use it very often, but because it should help you understand the concept of the call stack and what you're doing when managing nested calls or loops a little bit more.

The classic example is calculating fibonacci numbers. If you can wrap your head around it, you'll be able to come up with a ton of "clever" solutions both in terms of how much code you actually need, and efficiency. The wikipedia article might be a good starting point. In fact, when teaching I usually start with functional languages instead of an object-oriented one like Java despite its popularity, simply because I find those more effective at making people confront the kind of "mindset" they have to put themselves into to come up with effective code logic. Recursion is also a lot more natural in those.


During my first 2 days when I was looking into general programming motifs, I encountered the concept of recursion a few times, but ignored it as it seemed to be a more in depth concept that wasn't relevant to my goals at the time.

I'll definitely review it soon though.
 

mclem

Member
Good job OP. Why there are many valid and important suggestions in this thread, don't worry too much about them right now. Just keep coding programs that interest you. Most of the lessons in this thread are learned simply by programming over and over. Any programmer, no matter how experienced, will look at code from his or her past and think "Why the heck did I do it this way"?

Oh, yes, more than anything else, this. Practice and experience is the best teacher.
 

Fireblend

Banned
When I was at university the first language we worked with was Haskell, precisely because as a functional language it was really good at getting us to understand recursion and the programming skills and risks associated with it.

Haskell is the most beautiful programming language there is. I usually use Scheme (now Racket) or Python for first-year students, though. Haskell has some really powerful features like currying and lazy evaluation that are so damn useful it's incredible how very few people actually know about them. The abstraction model of object-oriented languages is nice and all, but achieving in Haskell with less than half the effort (and lines) of what it would take me to do something on Java makes me feel really powerful :)

OP, I used this question earlier today during a job interview, it might entertain you :) to solve it you'll probably need a good grasp on lists and recursion.

Code:
Given a String of arbitrary length, write a method that prints all possible permutations of the String's characters. For example, given the String "ABC" as an input, the method should output:

"ABC"
"ACB"
"BAC"
"BCA"
"CAB"
"CBA"

For 4-letter words, there are 24 permutations, and so on.

A simple (but not very elegant) solution can be found if you limit yourself to non-arbitrarily sized strings (making a method that works only on 3-letter-long strings)
 

collige

Banned
During my first 2 days when I was looking into general programming motifs, I encountered the concept of recursion a few times, but ignored it as it seemed to be a more in depth concept that wasn't relevant to my goals at the time.

I'll definitely review it soon though.

Others might disagree with me here, but I'd say you were correct in that first judgment. Its usefulness in the real world is very dependent on what you're working on but it's very important to understand as a concept. You actually ended up using recursion in your second example by accident.
 

rpmurphy

Member
Programming is a lot about logic and reuse. You're learning about functions, it looks like, so perhaps in the next step, you might want to go back to your first program to see what you can do differently.

For example, instead of having all of your logic in the main function, take that stuff out to their own functions, and leaving one call to a function like runQuiz which in turn will call functions for each question and keep tabs on the score. For example:
Code:
public static void main(String[] args) {
    runQuiz();
}

public void runQuiz() {
    int correctAnswers = 0;
    if (animalQuestion()) { correctAnswers++; }
    if (mathQuestion()) { correctAnswers++; }
...
}

public boolean animalQuestion() {
...
}

public boolean mathQuestion() {
...
}

Also think about what if you have N arbitrary questions? Instead of having separate, hard-coded logic for each of these questions, can you think of a way to make it generic?

One answer:
Have a data structure that stores question-answer pairs and have the ask question logic iterate over them.
 

Fireblend

Banned
Others might disagree with me here, but I'd say you were correct in that first judgment. Its usefulness in the real world is very dependent on what you're working on but it's very important to understand as a concept. You actually ended up using recursion in your second example by accident.

As I said (and I think you're agreeing with me on that), it's not something you end up using everyday, or at least not consciously so, but managing to understand and use recursion reliably is the best proof I know of successfully understanding how programming logic works. Whenever I teach, it is what tells me someone has successfully "rewired" their brain for programming, and it shows on their performance. As soon as someone masters it, there are few things blocking their way to understanding most other concepts and techniques that are crucial to at least the first 2 years of a CS program.
 

Cse

Banned
Better still, build the score increment into the if that tests the response to the question. No need to do multiple IFs!


Regarding asktest, it's probably not a good idea to call asktest recursively if the user gives an incorrect answer; it'll work, and in this context that's fine (and there's some smart coding around that; it'd be very easy to make a rookie error that would generate a new random number each time!), but it's using much more RAM than is necessary. Instead, set up a loop that continues until the answer is correct.




When I was at university the first language we worked with was Haskell, precisely because as a functional language it was really good at getting us to understand recursion and the programming skills and risks associated with it.


When I was writing the second program, I wasn't immediately aware of how I was going to repeatedly ask the user for an answer if they didn't answer correctly. About halfway in, I realized that the obvious solution was to continuously invoke the asktest() method if their answer didn't match, meaning I would have to build everything into the function - except for the bucket variable holding randomnumber1.nextInt(10), as I didn't want to generate a new number each time.

In the first iteration of the program, I actually did use a while loop, as shown below -

OVd9Bxb.png




Now, even though I've only been coding for 4 days, I recognized that what I was doing in the above image was terrible. The problem was that when the method obtaintheirnumber(keyboardinput); ran within the while loop, it wouldn't change int x, because in order for it to do so, it would have to jump out of the loop, which it can only do once int x = firstrandom. As a side note, the system.out.println was supposed to be the checktheirnumber() method, but once I wasn't getting the results I wanted, I installed the print out in the while loop so I could find out what the value of x was - and I realized it wasn't changing despite the fact that I was running the obtaintheirnumber method again (because, as I figured out, it was trapped in the while loop).

So, I simply realized it would be much easier to just bundle everything aside from the bucket variable holding the random number into the method, as I didn't want that to change each time I recalled the method.
 
Status
Not open for further replies.
Top Bottom