ITI0011-2015:harjutus 04
Harjutuse tähtaeg on 6. praktikum (19.-20. veebruar).
Üldine informatsioon harjutuste kohta: ITI0011:harjutused.
Kirjeldus
Teil on vaja implementeerida arvu äraarvamise mäng. Mäng käib nii, et arvuti "mõtleb" suvalise numbri vahemikus [10..99] (9 ja 100 enam ei ole lubatud). Inimene hakkab seda arvu ära arvama. Selleks küsib programm kasutajalt sisendit. Sisend peab olema number lubatud vahemikus. Korrektsed arvamised (korrektse sisendiga) loetakse kokku. Programm ütleb kasutajale, kas tema arvatud number oli liiga väike või liiga suur. Kui kasutaja arvab numbri ära, lõpetab programm oma töö.
Teil on vaja seekord realiseerida päris mitu meetodit. Järgenvalt on lühidalt kirjas, mida te kuskil tegema peate:
generateRandomNumber()
See meetod peab genereerima lubatud vahemikus [10..99] ja salvestab selle klassi muutujasse (et hiljem mõnes teises meetodis/funktsioonis seda kasutada saaks).
Mall
Tähelepanu, seekord on teil main
meetod ette antud. Te võite seda muuta, aga oluline on, et kõik muud meetodid teeksid täpselt seda, mis on nõutud. Etteantud main
meetod peaks ilusti töötama, kui ülejäänud programmi osad on implementeeritud.
<source lang="java"> /**
* Home assignment 04.
*
* Guessing game. *
* Computer thinks of a number. Human has to guess the number. * Every time the human guesses a number, computer lets her know * whether the actual numbers is smaller, greater or equal to the * guessed number. The program counts valid guesses made by the human. *
* Read more: https://courses.cs.ttu.ee/pages/ITI0011:harjutus_04 */ public class Task04 { /** * The main method, which is the entry point of the program. * !!IMPORTANT!! You have to keep the main method in order * to get your solution tested. *
* For this assignment, the main method implementation is * provided. If you want, you can modify it. But remember * that your code will be tested automatically. All the other * methods should do exactly what is required. Otherwise, if you * will have the main loop inside another method, the tester * might break. * @param args Arguments from the command line */ public static void main(String[] args) { // First, the computer "thinks" of a number generateRandomNumber(); // if you want, you can print out the generated number here while (true) { // what? we have an endless loop here... // this loop would never stop as the condition is always true // ... // not to worry, we can break out from this loop // let the user guess a number boolean result = guess(); if (result) { // if the result was true, we can end the "game". System.out.println("Correct! Number of guesses:" + getCount()); // breaks out from the while(true) loop break; } // if result is false, then the user did not guess the actual number // therefore we will just continue with our endless loop. } } /** * Generates a random number and stores it internally. */ public static void generateRandomNumber() { // generate a random number and store it in the class variable } /** * This method should ask user the the input (guess a number), * read one line and return. If the guessed number is correct, * the method should return true, otherwise return false. *
* Implement the following steps: *
-
*
- ask user to enter a number *
- validate the input *
- if the input is not correct, return false * (we will ask again next time we come to this method from main loop) *
- if the input is correct, evaluate the guessed number * using evaluate(int) method. *
- print out the hint for the user depending on the evaluate result * (actual number is bigger or smaller). *
- you should count the correct guesses. So, if the input * was correct, you should increment the counter. *
*
* Remember: *
-
*
- read only one line within this method *
- validate the input *
- return true only if the guessed value was correct, * otherwise print out the hint and return false. *
- count correct guesses *
* @return True, if the guessed value was correct, false otherwise. */ public static boolean guess() { return false; } /** * Compares guessed number with the actual number. * @param guess Guessed number * @return 0 if the guessed number and the actual number are equal, * 1 if the actual number is greater than the guessed number, * -1 if the actual number is smaller than the guessed number. */ public static int evaluate(int guess) { return -100; }
/** * Returns the number of guesses made. * You have to store internally the number * of guesses made. Note, that only correct * guesses (check the requirements from the web) * are counted. * @return Number of guesses. */ public static int getCount() { return 0; }
} </source>