Lab Assignment #5: More if-else

Due by 11:59PM on Wednesday, March 3.


Reading Assignment

Read sections 4.1 to 4.9 of the text Computing Concepts with Java Essentials. This covers material on Java's branching statements: if and if-else.

Pay particular attention to Common Error 4.3 and Common Error 4.4 on pages 156-158.


In-Lab Work

Okay, for a change, no piglatin this week, but watch out for it coming back with a vengeance next week! For the in-lab exercise this week, we are going to write a program that plays with your if-else knowledge.

In the lab, you will perform an exercise on if-else statements. You will write a program that generates three random numbers (say, first, second and third) between 1 and 50, and answers the following questions on these numbers.

  1. Is first greater than second?
  2. Are first, second and third in ascending order or descending order?
  3. If they are not in either ascending or descending order, print the numbers out in ascending order, and then in descending order.
Save the program as Numbers.java, and submit it by sending an email to a201@cs.indiana.edu with a subject that will be given to you in class.

Lab Assignment

I heard! Since you have been talking about the labs being too long (remember for the last lab you had two weeks!), this time there will be only two problems for the lab. Read the following very carefully.

  1. For the first problem of this lab, you are going to design a guessing game. The objective of this game is to use nested if-else statements to help users guess a word. The game will go like this. The program will randomly select a number between 1 and 15, and ask the user to guess what the number was. The user will then have four chances to guess this number. After each guess, the computer should say "Nope, too big" or "Nope, two small" (or something to that effect - be imaginative) or if the user guesses correctly, should say "Yes, you got it!" If after 4 chances, the user still hasn't guessed the number, then the computer should say a message telling the user what the number was. Here is a sample run:
        Okay, I have selected a number. You have four choices to guess it...
        Enter your first guess....  10
        Nope, too big!
        Enter your second guess.... 5
        Nope, too small!
        Enter your third guess.... 7
        Nope, too small!
        Enter your fourth and last guess.... 8
        Sorry, my selected number was 9! Thanks for playing.
        

    Name this program Youguess.java

  2. The second problem is just the reverse of the first problem. This time, instead of the computer selecting the number, you think of a number between 1 and 15, and let the computer guess. This time you should tell the computer if the number was too big or small by typing b (for big) or s (for small) or c (for correct). Think of a strategy that the computer can use so that it can always guess the correct number within four tries. Here is a sample run (this should give you a clue on what the strategy is):
        Think of a number between 1 and 15. Hit return when ready
    
        My first guess is: 8
        Is this too (b)ig or too (s)mall or (c)orrect? s
        My second guess is: 12
        Is this too (b)ig or too (s)mall or (c)orrect? c
        Yay!
        

    Save this program as Iguess.java


Submitting Your Lab Assignment

For this lab, you should submit two files:

Do not submit .prj, .class or .html files. Only the files named above should be submitted.

In order to submit files, click on the following link to send mail to:

a201@cs.indiana.edu

with the following Subject:

lab5

and then Attach your program files to the e-mail message. You may attach multiple files in the same message, but you don't have to. You can submit your programs in separate e-mails if you wish. Do not type anything in the body of the message! You must use the filenames specified in this assignment and you must attach the files to the mail message.