943,708 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 690
  • Java RSS
Dec 11th, 2008
0

Just going to start JAVA today

Expand Post »
I just started java installed everything that I was required. I just completed my python class.
for just trying a java code I tried this code.
code:
java Syntax (Toggle Plain Text)
  1. /**
  2.  * This program lets the user play HighLow, a simple card game
  3.  * that is described in the output statements at the beginning of
  4.  * the main() routine. After the user plays several games,
  5.  * the user's average score is reported.
  6.  */
  7.  
  8. public class HighLow {
  9.  
  10.  
  11. public static void main(String[] args) {
  12.  
  13. System.out.println("This program lets you play the simple card game,");
  14. System.out.println("HighLow. A card is dealt from a deck of cards.");
  15. System.out.println("You have to predict whether the next card will be");
  16. System.out.println("higher or lower. Your score in the game is the");
  17. System.out.println("number of correct predictions you make before");
  18. System.out.println("you guess wrong.");
  19. System.out.println();
  20.  
  21. int gamesPlayed = 0; // Number of games user has played.
  22. int sumOfScores = 0; // The sum of all the scores from
  23. // all the games played.
  24. double averageScore; // Average score, computed by dividing
  25. // sumOfScores by gamesPlayed.
  26. boolean playAgain; // Record user's response when user is
  27. // asked whether he wants to play
  28. // another game.
  29.  
  30. do {
  31. int scoreThisGame; // Score for one game.
  32. scoreThisGame = play(); // Play the game and get the score.
  33. sumOfScores += scoreThisGame;
  34. gamesPlayed++;
  35. TextIO.put("Play again? ");
  36. playAgain = TextIO.getlnBoolean();
  37. } while (playAgain);
  38.  
  39. averageScore = ((double)sumOfScores) / gamesPlayed;
  40.  
  41. System.out.println();
  42. System.out.println("You played " + gamesPlayed + " games.");
  43. System.out.printf("Your average score was %1.3f.\n", averageScore);
  44.  
  45. } // end main()
  46.  
  47.  
  48. /**
  49.   * Lets the user play one game of HighLow, and returns the
  50.   * user's score on that game. The score is the number of
  51.   * correct guesses that the user makes.
  52.   */
  53. private static int play() {
  54.  
  55. Deck deck = new Deck(); // Get a new deck of cards, and
  56. // store a reference to it in
  57. // the variable, deck.
  58.  
  59. Card currentCard; // The current card, which the user sees.
  60.  
  61. Card nextCard; // The next card in the deck. The user tries
  62. // to predict whether this is higher or lower
  63. // than the current card.
  64.  
  65. int correctGuesses ; // The number of correct predictions the
  66. // user has made. At the end of the game,
  67. // this will be the user's score.
  68.  
  69. char guess; // The user's guess. 'H' if the user predicts that
  70. // the next card will be higher, 'L' if the user
  71. // predicts that it will be lower.
  72.  
  73. deck.shuffle(); // Shuffle the deck into a random order before
  74. // starting the game.
  75.  
  76. correctGuesses = 0;
  77. currentCard = deck.dealCard();
  78. TextIO.putln("The first card is the " + currentCard);
  79.  
  80. while (true) { // Loop ends when user's prediction is wrong.
  81.  
  82. /* Get the user's prediction, 'H' or 'L' (or 'h' or 'l'). */
  83.  
  84. TextIO.put("Will the next card be higher (H) or lower (L)? ");
  85. do {
  86. guess = TextIO.getlnChar();
  87. guess = Character.toUpperCase(guess);
  88. if (guess != 'H' && guess != 'L')
  89. TextIO.put("Please respond with H or L: ");
  90. } while (guess != 'H' && guess != 'L');
  91.  
  92. /* Get the next card and show it to the user. */
  93.  
  94. nextCard = deck.dealCard();
  95. TextIO.putln("The next card is " + nextCard);
  96.  
  97. /* Check the user's prediction. */
  98.  
  99. if (nextCard.getValue() == currentCard.getValue()) {
  100. TextIO.putln("The value is the same as the previous card.");
  101. TextIO.putln("You lose on ties. Sorry!");
  102. break; // End the game.
  103. }
  104. else if (nextCard.getValue() > currentCard.getValue()) {
  105. if (guess == 'H') {
  106. TextIO.putln("Your prediction was correct.");
  107. correctGuesses++;
  108. }
  109. else {
  110. TextIO.putln("Your prediction was incorrect.");
  111. break; // End the game.
  112. }
  113. }
  114. else { // nextCard is lower
  115. if (guess == 'L') {
  116. TextIO.putln("Your prediction was correct.");
  117. correctGuesses++;
  118. }
  119. else {
  120. TextIO.putln("Your prediction was incorrect.");
  121. break; // End the game.
  122. }
  123. }
  124.  
  125. /* To set up for the next iteration of the loop, the nextCard
  126.   becomes the currentCard, since the currentCard has to be
  127.   the card that the user sees, and the nextCard will be
  128.   set to the next card in the deck after the user makes
  129.   his prediction. */
  130.  
  131. currentCard = nextCard;
  132. TextIO.putln();
  133. TextIO.putln("The card is " + currentCard);
  134.  
  135. } // end of while loop
  136.  
  137. TextIO.putln();
  138. TextIO.putln("The game is over.");
  139. TextIO.putln("You made " + correctGuesses
  140. + " correct predictions.");
  141. TextIO.putln();
  142.  
  143. return correctGuesses;
  144.  
  145. } // end play()
  146.  
  147.  
  148. } // end class HighLow

I just tried to compile this program that gave me an error : >javac HighLow.java
>The system cannot find the file specified.

i dont know what is the problem i searched online and thinks that there is something to do with classpath my question is what is a class path and how can I correct this problem. I read couple articles but i just couldnt pick anything can anyone help me???
Similar Threads
Reputation Points: 7
Solved Threads: 2
Junior Poster in Training
sfar_furqan is offline Offline
61 posts
since Jan 2008
Dec 11th, 2008
0

Re: Just going to start JAVA today

is your source code file named HighLow.java? Your files always have to be named the exact same thing as the class. Also, by look at the code, im predicting a few more errors after you get passed that initial point. Do you have anymore class files along with that to support all the methods you're calling from deck?
Reputation Points: 10
Solved Threads: 3
Newbie Poster
Fuze is offline Offline
15 posts
since Mar 2008
Dec 11th, 2008
0

Re: Just going to start JAVA today

beware that Java is also case sensitive
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is online now Online
16,505 posts
since Apr 2005
Dec 11th, 2008
0

Re: Just going to start JAVA today

Be careful whith the package .. couse you dont declare one.
Reputation Points: 9
Solved Threads: 2
Newbie Poster
danielernesto is offline Offline
16 posts
since Nov 2007
Dec 11th, 2008
0

Re: Just going to start JAVA today

yeah this is the source code and its actual name is HighLow.java and its actually a tutorial downloaded from a site.
Reputation Points: 7
Solved Threads: 2
Junior Poster in Training
sfar_furqan is offline Offline
61 posts
since Jan 2008
Dec 12th, 2008
0

Re: Just going to start JAVA today

what is a class path and how can I correct this problem. I read couple articles but i just couldnt pick anything can anyone help me???
I am surprised that no one has made any effort to answer his primary question.
You can get detailed knowledge about classpath - > here

Be careful whith the package .. couse you dont declare one.
It's absolutely not necessary to declare a package, your program can work as well without a package declaration.

I just tried to compile this program that gave me an error : >javac HighLow.java
>The system cannot find the file specified.
As far this problem goes I think the java compiler is not able to find the .java file, are you running the javac command from the same directory in which your program file rests ? If yes provide us with a full error trace so that we can guide you better.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Dec 12th, 2008
0

Re: Just going to start JAVA today

Click to Expand / Collapse  Quote originally posted by verruckt24 ...
I am surprised that no one has made any effort to answer his primary question.
You can get detailed knowledge about classpath - > here



It's absolutely not necessary to declare a package, your program can work as well without a package declaration.



As far this problem goes I think the java compiler is not able to find the .java file, are you running the javac command from the same directory in which your program file rests ? If yes provide us with a full error trace so that we can guide you better.

I think not because all tutorials are on my desktop folder while Java is installed in my C: volume while sciTE is also on my desktop. and I also do think that it cant find .java file how can I do that?
Reputation Points: 7
Solved Threads: 2
Junior Poster in Training
sfar_furqan is offline Offline
61 posts
since Jan 2008
Dec 12th, 2008
0

Re: Just going to start JAVA today

yes what you need to do is perform the javac command from the directory where the tutorial files are.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Dec 12th, 2008
0

Re: Just going to start JAVA today

I got it, actually i forgot to change the value of environment variable PATH which was causing problem now my program is working fine.
Reputation Points: 7
Solved Threads: 2
Junior Poster in Training
sfar_furqan is offline Offline
61 posts
since Jan 2008
Dec 12th, 2008
0

Re: Just going to start JAVA today

glad you got it sorted out, please mark as solved
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is online now Online
16,505 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: SCJP 6.0 Mock Practice test 1000+ questions
Next Thread in Java Forum Timeline: Need some help with my Final please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC