• Post Reply Bookmark Topic Watch Topic
  • New Topic

Newbie having trouble with the basics  RSS feed

 
stephanie zide
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys and gals,

I am a newbie taking an Intro to Java course. My prior experience includes an Intro to Programming using Python 3 years ago. I have not done any programming in between and I have always felt confused by syntax and even logic for that matter. My professor is very nice but I am unable to meet him during office hours due to work and other classes. He has just provided us with a subscription to an online interactive textbook that I really like.
http://fscjcop2551fall13.zyante.com/ch02-variables#15 (this is the textbook).

This is part of what I have read that I feel could apply to creating the solution to his assignment:

Figure 2.15.1: Import for Java Random class.

import java.util.Random;

The programmer creates a new random number generator object using a statement like Random randGen = new Random();. After that, the programmer can generate a random number by calling the nextInt() method on that object using randGen.nextInt(). The nextInt() method returns a seemingly random integer each time the method is called. The following program illustrates.
Figure 2.15.2: Random class: Generating random integers.


5 random numbers...
1867837740
1323199502
1186748378
726302883
-1390684875

This is the assignment:
What to do?
-code a class game and declare inside this class all the necessary variables needed to play the game.
This game consist of the following:
1-the user is prompted to enter a number between 2 and 12 (reject any number that is not within this interval [2,12]): useNum
2-the program will simulate throwing two dices using the random number generator discussed in class: dice1 and dice2
3-if the user number matches the sum of the number generated by the two dices then user wins...otherwise use loses.
All processes for this game must be handled outside of main()...but called from main().
step 1, 2, and 3 must be coded in different methods.
-user should be prompted if he/she want to play the game again or quit.
-when user chooses to quit, display the number of games he/she lost and the number of games he.she won.

Help:
-declare all your variables inside the class and not in main()
-create a loop inside main that allows the user to repeat program until the user chooses to quit.
-code a methods to prompt the user to enter an integer between 2 and 12 and within a loopk that validate the input.
-code a method that randomly generate two integers between 1 and 6.
-code a method that decide if user won or lost.
-code a function that display the final report when user quit: this report shows how many games were won and how many were lost by the user.

Requirement::
You must fully document your program and add a set of comment with your full name and the date of your submission and a brief description of what the program does.
Your program MUST follow the standards used in class.
Refer to the programs we developed in class. All tools and techniques needed by this assignment were all covered in class and throughout practices.
Limit your code to these tools and techniques.

This is my code so far:



I do not have errors but the program automatically generates the random dice and does not accept user input. This assignment is tricky to me but I must figure this out. Our class started at the end of August and I feel that we have kind of blown through some things and I am just a bit lost and need some guidance and maybe a bit of encouragement. : )
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephanie,

Consider what you are required to make given the information available to you:

-Code a class game and declare inside this class all the necessary variables needed to play the game. This game consists of the following:

1 - All processes for this game must be handled outside of main()...but called from main().
-declare all your variables inside the class and not in main(). Create class variables.
-create a loop inside main that allows the user to repeat program until the user chooses to quit. (User should be prompted if he/she want to play the game again or quit.)

2 - The user is prompted to enter a number between 2 and 12 (reject any number that is not within this interval [2,12]): useNum
Code a method to prompt the user to enter a number between 2 and 12 and within a loop that validate the input.

3 - The program will simulate throwing two die using the random number generator discussed in class: dice1 and dice2
Code a method that randomly generate two integers between 1 and 6.

4 - If the user number matches the sum of the number generated by the two dices then user wins...otherwise user loses.
Code a method that decides if user won or lost.

5 - When user chooses to quit, display the number of games he/she lost and the number of games he/she won.
-Code a function that display the number of games when user quit: this report shows how many games were won and how many were lost by the user.

Based on the above, you should be able to see the structure of your program (green = class, blue = methods, red = variables). Regarding your code, think about the structure and flow of the logic...prompt the user for a number (output), have the user enter a number(input...useNum) and validate the number.
What your program is doing is prompting the user (line 13), declares variables (lines 15 - 20), displays two random numbers (lines 22 - 26), accepts two numbers from the user (lines 28 - 31) but doesn't prompt for them, and finally the loop for checking for a match won't work because num[dice1] is not within the size of the num array. num has two indices, 0 and 1, trying to use 2 won't work. The matching doesn't need to be in a loop, remember, the matching is simply what #4 above states to do.

Hope this helps.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stephanie zide wrote:This is my code so far:...

First: Welcome to JavaRanch, stephanie.

Second: I'm a bit confused by your indentation, especially as your program seems to end on a brace titled "end if"; and you have empty opening and closing braces around code which, while not illegal, is certainly odd.

I do not have errors but the program automatically generates the random dice and does not accept user input.

Well, I notice that you appear to get two values from the user when you only asked for one, and then immediately clobber the second one by using it in a loop.

My suggestion: StopCoding (←click), and write down on paper and in English (or your native language), what you need to do.

And I mean every single step. And don't write another line of Java code until you completely understand it.

You will NEVER code your way to a solution.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!