ITI0011-2015:harjutus 04

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

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).

guess()

Siin toimub põhiline protsess. Sammud, mis siin tuleb realiseerida:

  1. programm küsib kasutajalt sisendit
  2. programm valideerib sisendit (et oleks arv lubatud vahemikus)
  3. kui sisend pole korrektne, antakse sellest kasutajale teada
  4. korrektse sisendi korral kontrollitakse evaluate(int) funktsiooniga, kas arvatud number on õige
  5. vastavalt kontrollile, kui tulemus on õige, lõpetab funktsioon ära tagastades true: return true;.
  6. kui tulemus ei ole õige, annab programm vihje, kas "mõeldud" arv on suurem või väiksem kasutaja pakutust
  7. korrektse sisendi puhul tuleb arvamine kokku lugeda, ehk siis kõik korrektsed pakkumised loetakse kokku (kui kasutaja pakub 107, siis seda ei loeta, kuna see pole korrektne sisend - ei ole lubatud piirides)

Oluline:

  • funktsioon peab lugema täpselt ühe rea sisendist. Kui sisend on vale, lõpetab funktsioon ära ning main-meetodist kutsutakse see funktsioon uuesti välja (see osa on juba mallis olemas).
  • sisend tuleb valideerida (mitte-number jms, ebakorrektne sisend ei lähe pakkumise arvestusse)
  • funktsioon tagastab true vaid siis, kui kasutaja arvas numbri õigesti ära. Kõikidel muudel juhtudel kuvatakse kasutajale vihjet või veateadet ning tagastatakse false
  • korrektsed sisendid lähevad lugemisele

evaluate(int)

Võtab sisse kasutaja pakutud numbri ja võrdleb seda arvuti poolt "mõeldud" numbriga. Kui kasutaja pakkumine on õige (pakkumine == "mõeldud" number), tagastab funktsioon 0. Kui kasutaja pakkumine on suurem kui arvuti "mõeldud" number (pakkumine > "mõeldud" number), tagastab -1. Muul juhul tagastab 1.


getCount()

Tagastab korrektsete pakkumise arvu. Programm peab sisemiselt seda väärtust hoidma (ja suurendama õiges kohas).

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: *

    *
  1. ask user to enter a number *
  2. validate the input *
  3. if the input is not correct, return false * (we will ask again next time we come to this method from main loop) *
  4. if the input is correct, evaluate the guessed number * using evaluate(int) method. *
  5. print out the hint for the user depending on the evaluate result * (actual number is bigger or smaller). *
  6. 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>