• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Errors

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a Java program for a 2-dice game. This game will roll the two dice together for 100 times. In each time, it will display the face values from both dice. The program will count how many times (out of these 100 attempts) did the two dice generate a total point of 10 or above, and display the result at the end.
Requirements:
a) In this question, use the Random class and its object to simulate dice rolling. Make sure your dice will generate integer values in the range of 1 - 6.
b) You will need two counter variables. One is to count for how many attempts you have rolled the two dice – to control the loop. The other is to count how many times that your two dices gave a total point of 10 or above.
c) Use a WHILE loops in this program. DO NOT USE THE OTHER LOOP METHODS in this question.
d) Your class should be named TwoDiceGame, and your source code file should be named TwoDiceGame.java.
e) Programming styles are always required.




I am getting this errors: ----jGRASP exec: javac -g TwoDiceGame.java
TwoDiceGame.java:9: error: '{' expected
public class Random.java
                  ^
1 error


I tried putting in a bracket where it says but it is still coming out as an error.
 
Marshal
Posts: 68066
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sarah Harvey wrote:. . . use the Random class and its object . . . Your class should be named TwoDiceGame . . .

You have more errors than your compiler error. One is that you have called your class Random, which you were told not to.
I got a different compiler error from yours: what does like 9 mean? What do lines 22‑22 mean?
Why have you put so much code in the main() method? Where is the method you appear to be calling from line 24?

I think you will get more attention if I move you to the beginning forum.
 
Sarah Harvey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the instructions it says use the Random class right? I am very new to Java so please do move me to the beginner forum. Thanks so much.
 
Sheriff
Posts: 15038
251
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch! As Campbell pointed out, you are not following instructions (d) in naming your class Random.

I think you've tried to write too much code at one go. Now you have a LOT of code that's messed up. If you're going to mess up, it's best done in small installments. That is, start with a minimal program that works. Have you written a Hello World program yet? Use that as your starting point. Then just change small parts, a little bit at a time, making sure each change you make doesn't blow up the program.

So from saying "Hello, World!", you'd change the program to say something like "There were 0 times the total was 10 or more" -- that's one single statement you have to change. If you get that to work, then add a little bit more so that it displays the count variable instead. Like so:

NEVER write more than 3 or 4 lines of code without running your program to check if you've messed anything up. Even good developers (and especially great developers) follow this practice. It's totally a beginner move to write more than 10 lines of code only to find out none of it works.
 
Sarah Harvey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just confused because in A) it says to use the random class and object but then D) says your class should be named TwoDiceGame. Can you explain what I am to do? Thanks so much.
 
Junilu Lacar
Sheriff
Posts: 15038
251
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be wondering from the example I gave, why set count to 8? Well, I know it's a mistake but I just want to see if I can make the program display that value and 8 is something I randomly chose, not something the program will provide by default or be assigned by accident without my knowledge. If I'm going to make a mistake, I want to do it deliberately at least. However if the printf() message works, it should display "There were 8 rolls that were 10 or more" so that tells me the program is picking up the value I assigned to the variable count. If I'm still not sure after running the program like that, I might double check and change it to something like 42, for example. Then run the program again. Then I'd move on a fix the program by assigning 0 to count, because that's really what I want it to be at the start.

After that, I just keep making small tweaks until I get to the program I really want to have.

Does that make sense?
 
Junilu Lacar
Sheriff
Posts: 15038
251
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"use a class" is different from "name your class"

"Use the Random class" is like saying "Use a Hammer to build your boat"

"Name your class blah" is like saying "Christen your boat 'Scary Starts'"

 
Sarah Harvey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would the code for that be public class TwoDiceGame {    ? Or would I include anything about random?
 
Junilu Lacar
Sheriff
Posts: 15038
251
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sarah Harvey wrote:Would the code for that be public class TwoDiceGame {    ? Or would I include anything about random?



Yes, but you could have answered that yourself by writing out that little bit of code and trying to compile it.

Here's a minimal class named HelloWorld:

Compiles but does nothing when I run it.

Now, here's a small change to that class that makes it do something:

Now it compiles and does something. From here I can make it do a little bit more, then a little bit more. See how that works?
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!