Lab session 5

To start on this assignment, please download the file lab5_skel.java, rename it to lab5.java, and write all your solutions in this file. You don’t have to check the input to the methods, you may assume that it will always satisfies the preconditions.

The main method in this file contains some tests for you to check your solution. It is not enough to write code that only passes these tests, so it is a good idea to add some more tests of your own.

Tip: To enable assert statements in IntelliJ, go to Run > Edit configurations… and add -ea or -enableassertions to the VM options field.

Part 1: string manipulation and command-line interfaces

Assignment 1

Write a method public static String toRobber(String text) that translates a string (a Swedish or English sentence) into Robber Speak (Swedish: rövarspråket).

Examples.

assert toRobber("java") == "jojavova";
assert toRobber("stubborn") == "sostotubobboborornon";
assert toRobber("Hej på dig") == "Hohejoj popå dodigog";

Tip: You can use methods from the Character and String classes.

Tip: The vowels are the characters in the string “aeiouyåäö”.

Tip: If the word starts with a capital letter, only its first copy should be capitalized!

Assignment 2

Write a method public static String toPigLatin(String text) that translates a string to Pig latin.

Examples.

  assert toPigLatin("pig") == "igpay";
  assert toPigLatin("string") == "ingstray";
  assert toPigLatin("omelet") == "omeletay";
  assert toPigLatin("i") == "iway";
  assert toPigLatin("Hej på dig") == "Ejhay åpay igday";

Tip: You can get an individual character from a string using the method char charAt(int pos), or take part of a string using the method String substring(int from, int to).

Tip: You can split up a sentence into words using the method split as follows: String[] words = text.split(" ");.

Tip: Use a boolean variable to remember whether a word starts with a capital letter.

Assignment 3

Create a command line menu with choices r, p, and q

The program should keep asking for inputs until the q option is chosen.

Example.

Choose a translation method (r=robber, p=pig latin, or q=quit to quit).
> r
Type a sentence to translate into Robber speak:
>> java
Translation: jojavova
Choose a translation method (r=robber, p=pig latin, or q=quit to quit).
> q

(the program closes)

Tip: To read a whole sentence instead of a single word, you can use the Scanner method String nextLine() (instead of next()).

Part 2: classes

Assignment 4

Create a new class Die (‘tärning’ in Swedish, plural is ‘dice’) for simulating a die with a given number of sides. The number of sides should be an argument to the constructor.

Tip: To create a new class in IntelliJ, right click on the src folder and go to new > Java class.

Add a method int roll() which generates a random number between 1 and the number of sides and returns the result.

In the method rollADie(), create a new die with six sides and roll it 100 times. Print out the result for each roll, for example:

3 2 6 3 2 5 2 1 5 5 5 1 5 6 6 2 1 3 3 1 1 2 4 1 2 4 4 5 6 5 5 5 5 2 3 6 1 ...

Assignment 5

Create a new class Player1. Each player should have a name and a reference to a die. Both should be arguments to the constructor.

Add a method public String getName() for getting the name of the player. Also add a method public int rollTheDie() which rolls the player’s die and returns the result.

In the method letPlayerRoll(), create a new player and let them roll the die five times. Print out the result, for example:

Player is Otto
6 3 5 2 3

Assignment 6

In the method letTwoPlayersRoll(), create two players that share the same die. Let each player roll 5 times and write out the results. For example:

Player is Otto
3 4 1 5 3
Player is Fia
1 3 5 5 1

Assignment 7

Create a new class DiceCup. The dice cup contains 5 different 6-sided dice. Add a constructor that takes no arguments but creates 5 new dice and adds them to the cup.

Add a method public int rollAllDice() that rolls all the dice and returns the total.

Then, create another class Player2 which uses a dice cup (and has a name). Add a method int rollDice() that rolls alls dice in the player’s dice cup and returns the result.

Add an instance variable to keep track of the total accumulated result of all dice rolls made by the player. Add a method int totalResult() that returns this number.

In the method letPlayerUseDice(), create a new player, let them roll the dice in the dice cup twice, and print out the accumulated result after each roll. For example:

Arne 20
Arne 31

Assignment 8

Override the toString() method in both the Die and DiceCup class to show the latest result of each die in the following format:

DiceCup{dice=[Die{result=4}, Die{result=3}, Die{result=1}, Die{result=6}, Die{result=6}]}

Tip: Add an extra (private) instance variable to the Die class to remember the result of the last roll.

In the method rollDice() in the Player2 class, print out the cup before returning the result. The output of letPlayerUseDiceCup should look like this:

DiceCup{dice=[Die{result=4}, Die{result=3}, Die{result=1}, Die{result=6}, Die{result=6}]}
arne 20
DiceCup{dice=[Die{result=2}, Die{result=1}, Die{result=4}, Die{result=1}, Die{result=3}]}
arne 31

Assignment 9

In the method findPlayerWithMax(), create 5 players (of the Player2 class) that share the same dice cup. Let each player roll the dice once. Find the player with the highest result and print out their name and the result.

DiceCup{dice=[Die{result=2}, Die{result=2}, Die{result=2}, Die{result=6}, Die{result=1}]}
Alice:13
DiceCup{dice=[Die{result=5}, Die{result=4}, Die{result=4}, Die{result=1}, Die{result=4}]}
Benjamin:18
DiceCup{dice=[Die{result=3}, Die{result=4}, Die{result=2}, Die{result=3}, Die{result=1}]}
Charlie:13
DiceCup{dice=[Die{result=2}, Die{result=1}, Die{result=1}, Die{result=6}, Die{result=2}]}
Daniel:12
DiceCup{dice=[Die{result=4}, Die{result=5}, Die{result=6}, Die{result=3}, Die{result=3}]}
Emelie:21
Emelie won with 21 points
Menu