count = 0
count += 1Computer Laboratory 06
Deadline: Mon, Oct 20, 2025 5pm
Before you start
- This assignment is due Mon, Oct 20, 2025 5pm and will be turned in on gradescope
- The assignment is intended to be done in groups of up to 3 people, although you’re allowed to work on your own if you want.
- You can start the assignment before your lab period, although I recommend not getting too far – the lab TAs often have useful hints, and I don’t want you wasting too much time struggling with something that the TAs might announce during lab.
- For more rules see the lab rules document on canvas.
- It will be VERY HELPFUL to find your
Lab02submission and have that ready to reference.
Introduction
In this lab we will get our first taste of Java. Rather than picking a simple task, we will be making a relatively large program. Fear not, however, because we will not be programming something new. Instead, we will be rebuilding our NIM game, translating the design directly from the python version to Java. The end result will be a collection of static functions that will allow us to play Nim.
In particular, this lab will:
- Give you a chance to make sure you have a setup for Java programming (with Java 17).
- Practice translating a program you’ve already written from one language to another.
- Give you practical experience with Java codding.
I feel compelled to note that simply translating code from python to java like this is not necessarily a common task.
While this will lead to working code – it will not be very java style code – that is, it will not make use of organizational techniques common in java code. That said, this will serve as GREAT practice for where our skills are NOW – there will be time in future labs to see the principals of a more object-oriented design. For now, focus only on gaining comfort with java’s syntax through simple practice, and on how two different programming languages can be used to encode the same computation.
Software environment setup
NOTE – there is every chance this guide is slightly out-of-date. IntelliJ seems to update their UI once every few years.
Your TAs should be able to give you a better step by step tutorial in lecture.
From this lab on we will be using the IntelliJ Idea IDE (integrated development environment). This section describes how to set up a new project for this lab and the Java files you are expected to download or create.
- Apply for a free student account
- Once you go through the application process (use your @umn.edu address) you will get a link to your JetBrains Educational Pack webpage. On that page there’s a
Downloadoption. SelectDownload IntelliJ IDEA Ultimateto dowload IntelliJ
Alternatively, you can download IntelliJ at this link:
Once you have IntelliJ open go to Help \(\rightarrow\) Manage Subscriptions... and then click on Activate Another Subscription.... IntelliJ will prompt you to log onto your jetbrains student account.
Start IntelliJ and create a new Java Project
Begin by loading IntelliJ. Depending on your past use of intelliJ you may need to enter some one-time settings such as color theme and default set of keyboard shortcuts for IntelliJ.
You can set these options however you want, while I prefer default key-bindings and a light theme, many people may disagree, and it doesn’t effect your coding output one bit.
If you are not prompted for these settings, that too is fine. You can change these in the settings at your leisure.
Once open, depending on settings and past use IntelliJ will either open to it’s splash-screen or a previously opened project. Either way you want to make a new project.
If you are in a previous project go to File \(\rightarrow\) New \(\rightarrow\) Project.... Otherwise you can simply click the new project option.
The new project window should look something like this:
You want a Java project, with no additional library or framework support.
Make sure your project has a name (“lab6” would be appropriate, but so would “NIM”). Make sure to check the project location. If necessary, update where the project is stored to keep your files well organized.
If you are working on your own computer don’t forget to check the Project SDK settings. You shouldn’t need to be using an SDK version 23, like shown in the screenshot, so long as you are using 9 or above, you should be fine. (We will be grading on version 17)
If no SDK is listed, click new and setup your JDK. Typically IntelliJ will automatically open to your JDK (Java Development Kit) location.
- Click
create
If not shown, you will likely want to open the project view. The keyboard command for that is alt-1 on windows (and I believe this is true for Linux as well). This keyboard command is also shown in the background of the file viewer if no files are open.
Setup for Java files
Now that you have a project set up, click the tiny triangles in the project view to open the project itself and the source folder (src).
Download the template java files from canvas and move them to the src folder. You can do this by clicking and dragging the file from your file browser to IntelliJ’s window. Make sure the files are in src, if they are not IntelliJ will not let you run these files.
Note: Java, as a compiled language, has no patience for code it doesn’t understand. Therefore, you may need to comment out parts of the testing code relating to functions you haven’t written yet. So long as you have function calls to functions you haven’t written Java wont run ANY of your code (even in unrelated files. Compiled languages tend to be all-or-nothing that way.)The template files provided should prevent this from being an issue this time but please remember this fact for future labs.
Files
This lab will involve the following files:
NIM.java- A template of this file is provided. You will be completing this file to build the code for managing the NIM game.NimTester.java– This file is provided. This file contains tests for the Nim functions. This should be your primary debugging tool for this lab, as the java autograder is typically less clear, and much more fussy than the python autograder.PlayNIM.java– This file is provided. When your NIM file is complete this should be able to play a 2-player game of nim!
Your intelij project directory should look something like the following (intellij should generate the “out” directory and all the files in it automatically. You should only have to worry about src)
.
|-- Lab 06
|-- out
| `-- production
| `-- student
| |-- Main.class
| |-- NimTester.class
| |-- PlayNIM.class
| `-- student
| `-- NIM.class
`-- src
|-- Main.java
|-- NimTester.java
|-- PlayNIM.java
`-- student
`-- NIM.java
Instructions
Before beginning you should:
- Setup an IntelliJ project
- Download the 3 provided files and place them in the src folder or src/student folder (you may need to create a student folder).
- Ensure that NimTester can be run – it will not produce the correct outputs, but it should be runable without modifications.
- Get a copy of your
Lab02. If you do not have a copy of your originalLab02– you can see your submission on gradescope. If you have not submitted aLab02solution, you may request a copy from another group, but if you do this you must add a comment to the top of the Nim.java file to make this clear. - Update the header comment in the provided NIM file with your name, the name of any lab partners, and if you are not working from your
lab02, the name of who you gotlab02from. - Review Lab02 instructions to make sure you remember the basic structure of the Nim game – this will not be restated here.
- Skim the formal requirements section and the java syntax sections. You will need information from both sections to complete this task.
- Only after skimming the rest of this document fully should you start programming.
Formal Requirements
The five required functions are:
public static int[] createGameState(int size, int tokenMax)public static boolean isValidMove(int[] gameState, String row, String takes)public static void drawGameState(int[] gameState)public static int[] update(int[] gameState, int row, int takes)public static boolean isOver(int[] gameState)
createGameState(int size, int tokenMax)
This function takes two parameters, size, and tokenMax. It should return a newly created array of ints representing the number of tokens in each row. The size parameter sets the length of the newly created list, and the tokenMax parameter controls the values. In particular, the first row (position 0 in the array) should have one in it, the second row should have 2 and so-forth up to the max.
Notes:
- You can assume that both size and tokenMax are positive.
- if a size value of 0 is given, you should return a size 0 array.
- You cannot assume that size \(<\) tokenMax. In a case like this the (size - tokenMax + 1) last positions should all be equal to token Max.
Hints:
- Unlike a list – when you make an array you give it a size – the array will then be full of 0s.
- Since the array is made full-length, you will need to assign to the array using indexing syntax
myarray[i] = value;. This is different from the python approach where you make the list through repeated append. - you can return an array in Java, simply use a return statement and the name of the variable pointing to your array.
isValidMove(int[] gameState, String row, String takes)
This function takes three parameters, gameState is an array representing the state of the game (similar to one that createGameState might return)
rowis a string representing which row the user has requested to take tokens from, andtakesis a string representing how many tokens the user wishes to take.
The function should return a boolean value – true to indicate that user has chosen a valid move, or false to indicate that the move would be invalid.
Notes:
This function must not modify the gameState array parameter – the given array should have the same values before and after the function.
rowandtakesare strings, not integers. You CANNOT assume that they will only have integers in them or be in any particularly valid form. You can assume the string is non-empty (has at least one letter in it)A string that contains anything OTHER than digits (1,2,3,4,5,6,7,8,9 or 0) is invalid.
- A function
isDigit(someString)is provided to help check this. You should use this function. The function calling syntax remains essentially unchanged from python in this case – simply name the function and then put parameters in the parenthesis. - You can convert a string to an integer with the following code:
lstinline{int intTakes = Integer.parseInt(takes);
- A function
If the row number is out-of-range then the row is invalid. The valid row numbers are between 1 and the length of the gameState array (inclusive)
- you can get the length of an array with
gameStates.length
- you can get the length of an array with
To be valid the takes number must be between 1 and 3 (inclusive)
To be valid, the takes number must be less than or equal the number of tokens in the selected row (I.E. if a row only has 2 tokens, it is not valid to take 3 tokens)
Hints:
- This is probably one of the more complicated functions in this lab – fortunately, you already resolved these complexities in Lab02. Make sure you understand how you approached this program in Lab02, then translate that into code suitable for Lab06.
- rows will be entered by the user 1-indexed (meaning “1” represents the first row) this is different from how rows are handled in java arrays – where the gameState array is 0-indexed (0 represents the first row.) Your code will need to account for this.
update(int[] gameState, int row, int takes)
The update method takes three parameters gameState, row, and takes. While these have the same name as the parameters to the isValidMove function, there are important differences.
Like before, gameState is an array representing the state of the game (similar to one that createGameState might return)
rowis an int representing which row the user has requested to take tokens from – this will be 0-indexed (not 1-indexed like the user input) and therefore will represent an array index directly.takesis an int representing how many tokens the user wishes to take. The function should return a array that represents the board after the listed number of tokens have been taken from the listed row.
Notes:
- You can assume that
rowis a valid index for thegameStatearray, and thattakesis between 1 and 3, and not greater thangameStates[i]. This is to say, you can assume that these parameters indicate a valid move as previously defined. - Remember that the row is a 0-indexed integer in this function, not a 1-indexed string like in
isValidMove. You can use it directly like an array index. - The
gameStatearray input to this function should be unmodified by this function (I.E. it should have the same value before and after this function runs) - The returned array should be a copy of the input array except for the index indicated by
row– which should have fewer tokens as indicated by the takes variable.
Hints:
- Like with the earlier function you should remember that java arrays, unlike python lists, are created full-length and filled with 0. Therefore, you should plan on using repeated assignments:
newTokens[i] = something;instead of appending when creating your return array.
drawGameState(gameState)
The drawGameState function takes one parameter, gameState is an array representing the state of the game (similar to one that createGameState might return). You can assume that the gameState array contains only non-negative numbers. The drawGameState function has no return value, and should instead use print statements to produce an output directly to the terminal.
The output of the function is a depiction of the state of the game, for instance, if given the array [5,2,3,1] The following should be printed:
====================
1 #####
2 ##
3 ###
4 #
====================
Formally:
- The first line should contain 20 equal-signs.
- for an array length \(n\), the next \(n\) lines should be the row number, followed by a single space, and then a number of pound-signs
#as indicated by the gameState array. - The final line should contain 20 equal-signs. (there should be a final end-line after the 20 equals as well)
- No line starts-with or ends-with any spaces (with the technical exception of a line representing a row with no tokens left, which would be a number followed by a space.)
- Your output must match letter to letter – including spaces, newlines, capitalization, symbols etc.
Hints:
- A common task for this process is to repeat a string a fixed number of times. There is not an easy way to do this in Java. It would be easy enough, however, to make a helper function to make such strings, or to print the characters one-by-one in a loop.
- You will likely need to use both
System.out.println}(print with a final new-line character) andSystem.out.print(print but do not end the line) to make this function work.
isOver(gameState)
The isOver function takes one parameter, gameState is an array representing the state of the game (similar to one that createGameState might return).
This function returns a boolean value to indicate if the game is over.
- If every value in the input array is 0 – the game is over.
- If any value is not 0 – the game is not over.
(You can assume the array is not empty – I.E. it has length greater than 0)
Java Syntax Guide
Some java has been provided in the provided Nim template file. This mostly includes:
- Function declarations
- proper javadocs
- A mostly-complete header-comment
- A single provided function which may serve as a useful example of some common syntax.
The syntax for these topics will not be explored here.
Math differences and other minor notes
Most mathematical computation does not need to be translated. The only differences:
Logical operators are back to their C-style versions:
||(or),&&(and),!(not)We no longer have exponentiation operator
**or int-division operator//The standard division operator now has type-dependent behavior. If you give it two integers then it will perform an integer division. Otherwise we get float-division.
Boolean values are now lower-case
trueandfalseAll statements should end with a semicolon. (Not needed with flow-of-control, but needed for everything else)
The return statement is unchanged.
printing is done with
System.out.println(),System.out.println(something),System.out.print(something):System.out.println()with 0 parameters, this prints the newline character onlySystem.out.println(something)only 1 parameter is allowed at max. This will printsomethingthen print the newline characterSystem.out.print(something)only 1 parameter is allowed at max. This printsomethingbut NOT print the newline character, similar toprint(something, end='')in python
Strings must have double-quotes and only double-quotes. (single quotes are used for
chartype)
Variables
Variables must have a type in java. For most variables in this lab the type will be sufficient. Once created, variable assignments require no real modification.
Task: create new int variable nad update it
Python:
Java:
int count = 0
count += 1;
count++Basic flow of control
conditionals
Python:
if condition:
some code
elif another condition:
some code
else:
some codeJava:
if (condition) {
some code;
} else if (condition) {
some code;
} else {
some code;
}while loop
Python:
while condition:
some codeJava:
while (condition) {
some code;
}for loops
Python:
for i in range(5):
do something with i
for i in range(len(lst)):
do something with lst[i]
for elem in lst:
do something with elemJava:
for (int i = 0; i < 5; i++) {
do something with i;
}
for (int i = 0; i < arr.length; i++) {
do something with arr[i];
}Java has for each loops for collections (note the specification of the type of value):
for (int number : arr) {
do something with number;
}
for (String word : arr) {
do something with word;
}arrays and lists
Arrays and lists are the biggest difference in this code.
- Python lists are created empty, and brought up to length by filling them with the append method.
- Java arrays are created at their final (and only) size, and cannot be resized once made. Therefore code for building and returning a list is quite different.
Python:
# create new list with 10 zeros
new_list = [0] * 10
# change second value
new_list[1] = 99
# retrieve value and assign it to a variable
x = new_list[2]
# get list size
size = len(new_list)Java:
# create new list with 10 zeros
int[] new_arr = new int[10];
# change second value
new_arr[1] = 99;
# retrieve value and assign it to a variable
int x = new_arr[2];
# get list size
size = new_arr.length;Java Autograder Notes
The java autograder is much fussier than the python autograder. This is mostly due to Java’s nature as a compiled language.
While we could often partially test python files that had errors in some functions – for your code to be testable your code must compile, have all the correct function definitions, and all the correct names.
Even once you get the tests running you should also be aware that the autograder for java does not give as clear feedback as the python autograder when things go wrong.
As such you are expected to be testing your code on your own. Do not use the autograder as a debugging tool.
Due to some strange things in the autograder, we have a few specific requirements for your code:
- The code you submit must be valid and compileable java code. (We cannot test code with syntax errors. If we can’t test it, we can’t give you autograder credit for it)
- The code you submit must NOT have a package statement in it. Your IDE may have put one in without you noticing. Please make sure to delete the package statement at the top of your code, if there’s one, before submitting to gradescope, otherwise your code will not compile. If you see code like
package com.whatever;at the top of your file, delete it. - The code you submit must match the provided function signature (name, parameter types, and modifiers) EXACTLY. Any mis-match, no matter how minor, may cause the test code to not compile, which would prevent testing.
As a general rule, if you started with the template file on canvas and didn’t modify anything other than the function bodies you should be fine this time. Likewise, if your code can be run with the provided testing code (with no modifications to this testing code) you should be fine. If you do run into testing issues, do not hesitate to reach out to course staff to help with debugging.
Submission
For this lab you only need to turn in NIM.java. You can submit other files if you wish, but we do not promise to look at them during grading.
- Grading on this assignment will be 50% autograded and 50% manually graded.
- Like past labs, 10% will be based on code style, and 40% on manual review.
Of particular note for code style – while java does not require proper indentation for functional behavior – this is always understood to be a core part of code style.
Failure to properly indent your code will be seen as a code style issues. Beyond that – see past labs for general guidelines on code style – your code should be easily readable, your variables should be clear, etc.
For this lab, proper documentation (javadoc format) have been provided. So long as you don’t mess with the formal documentation you should be fine.