Lab: Guessing Game, minimumInScanner,howSwedish

Collaboration: Complete this solo or with 1 person from your section during your scheduled lab.

Grading: For 100%, don't be late to lab and stay to end of lab –or– complete all sections of this lab.

Recommended Activities:

  1. Review the Java while loop – as necessary
  2. Review and go more into depth on Scanner ‘has’ methods: hasNext() hasNextInt()
  3. Describe Random objects and the nextInt(int) message

This lab requires the use of random numbers. Java has a class named Random that makes it easy to get seemingly random numbers into your program. After you import the Random class.

import java.util.Random;

you can construct a Random object like this

Random randomNumberGenerator = new Random();

and then use Java's nextInt method:

// From the Random class

// Return a seemingly random integer in the range of 0 through n-1.

public int nextInt(int n)

For example, you can get a number from 0 to 5 with this message:

// The message returns 1, 2, 3, 4, 5, or 6. It appears to be random.

int randomNumber = randomNumberGenerator.nextInt(6) + 1;

1) GuessingGame

In teams of two, write a Java class that implements a guessing game. You should “secretly” generate a number from 1 to 100. Then ask the user for a guess. If the guess is larger than the random number, inform the user that the guess was too high. If the guess is too low, tell that to the user. When the guess matches the random number, tell that to the user (Note: Generate a random number only once).

I am thinking of a number from 1..100. Guess what it is.

Pick a number from 1..100: 50

50 is too high

Pick a number from 1..100: 25

25 is too high

Pick a number from 1..100: 12

12 is too low

Pick a number from 1..100: 18

18 is just right

Congrats, you needed 4 guesses

When done, raise your hand to get your section leader to give you credit.

2) Two WhileLoopMethods

Write class WhileLoopMethodsTest with assertions to test two methods that you will write in a java class WhileLoopMethods. The two methods, howSwedish and minimumInScanner are both specified in comments below. When completed turn in to WebCat.

// YOUR NAME and your PARTNERS NAME

importstatic org.junit.Assert.assertEquals;

import java.util.Scanner;

import org.junit.Test;

publicclass WhileLoopMethodsTest {

@Test

publicvoid testMinInScanner() {

WhileLoopMethods wm = new WhileLoopMethods();

assertEquals(1, wm.minimumInScanner(new Scanner("1 3 5 7 9")));

// Add more assertions. At least four more.

}

@Test

publicvoid testhowSwedishWithOverlap() {

WhileLoopMethods wm = new WhileLoopMethods();

assertEquals(2, wm.howSwedish("abbabba"));

// Add more assertions. At least six more.

}

}

// YOUR NAME and your PARTNERS NAME

import java.util.Scanner;

publicclass WhileLoopMethods {

/**

* This method takes in a Scanner object as its only argument and after

* processing the contents of the scanner returns the minimum number contained

* in the Scanner. We will only test with integer inputs and will not test

* with any String, or decimal inputs. Inputs may be positive, negative or

* zero. There will be at least one integer in the Scanner.

*/

publicint minimumInScanner(Scanner scan) {

return-1;// TODO: Complete this method. You MUST use a while loop!

}

/**

* ABBA is a band, they have many songs including Dancing Queen, and Fernando.

* ABBA is actually a Swedish band, so if we wanted to find out howSwedish a

* String is, we could simply find out how many times the String contains the

* substring "abba". We want to look for this substring in a case sensitive

* manner. We also want to check for overlapping ABBA's such as in the String

* "abbabba", which contains "abba" twice.

*/

publicint howSwedish(String str) {

return-1;// TODO: Complete this method. You MUST use a while loop!

}

}