Just going to start JAVA today

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 55
Reputation: sfar_furqan is an unknown quantity at this point 
Solved Threads: 1
sfar_furqan's Avatar
sfar_furqan sfar_furqan is offline Offline
Junior Poster in Training

Just going to start JAVA today

 
0
  #1
Dec 11th, 2008
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:
  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???
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 15
Reputation: Fuze is an unknown quantity at this point 
Solved Threads: 3
Fuze Fuze is offline Offline
Newbie Poster

Re: Just going to start JAVA today

 
0
  #2
Dec 11th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,259
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 541
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Just going to start JAVA today

 
0
  #3
Dec 11th, 2008
beware that Java is also case sensitive
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: danielernesto is an unknown quantity at this point 
Solved Threads: 1
danielernesto danielernesto is offline Offline
Newbie Poster

Re: Just going to start JAVA today

 
0
  #4
Dec 11th, 2008
Be careful whith the package .. couse you dont declare one.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 55
Reputation: sfar_furqan is an unknown quantity at this point 
Solved Threads: 1
sfar_furqan's Avatar
sfar_furqan sfar_furqan is offline Offline
Junior Poster in Training

Re: Just going to start JAVA today

 
0
  #5
Dec 11th, 2008
yeah this is the source code and its actual name is HighLow.java and its actually a tutorial downloaded from a site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Just going to start JAVA today

 
0
  #6
Dec 12th, 2008
Originally Posted by sfar_furqan View Post
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

Originally Posted by danielernesto View Post
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.

Originally Posted by sfar_furqan View Post
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 55
Reputation: sfar_furqan is an unknown quantity at this point 
Solved Threads: 1
sfar_furqan's Avatar
sfar_furqan sfar_furqan is offline Offline
Junior Poster in Training

Re: Just going to start JAVA today

 
0
  #7
Dec 12th, 2008
Originally Posted by verruckt24 View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Just going to start JAVA today

 
0
  #8
Dec 12th, 2008
yes what you need to do is perform the javac command from the directory where the tutorial files are.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 55
Reputation: sfar_furqan is an unknown quantity at this point 
Solved Threads: 1
sfar_furqan's Avatar
sfar_furqan sfar_furqan is offline Offline
Junior Poster in Training

Re: Just going to start JAVA today

 
0
  #9
Dec 12th, 2008
I got it, actually i forgot to change the value of environment variable PATH which was causing problem now my program is working fine.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,259
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 541
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Just going to start JAVA today

 
0
  #10
Dec 12th, 2008
glad you got it sorted out, please mark as solved
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC