Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Programming Fundamentals (31267) & Object Oriented Programming (48023) - Assignment 1
- Due date: Monday, 3rd October 2011, 11:00 am.
- Value: 20%
- Topics: Data flow, OO programming basics, Control flow.
- Objectives: This assignment supports objectives 1-5.
- Introduction
- In Assignment 1 you will write a simple game in which the player must move left and right to collect stones and place them on a treasure chest in the right combination to unlock a treasure.
- Read all of the following sections carefully before starting the assignment.
- Program description
- In this game, there are 4 stones represented by 4 symbols (#, @, %, $) which have numeric values (1, 2, 3, 4) respectively.
- The current state of the game is displayed as a string:
- Jack(0) #(1) @(2) %(3) $(4) Treasure/6\(5)
- The numbers in parentheses indicate positions on the ground. Here, the player named “Jack” is at position 0, the # stone is at position 1, the @ stone is at position 2, the % stone is at position 3, the $ stone is at position 4 and the treasure chest is at position 5. The number /6\ for the treasure chest indicates the combination required to unlock the treasure.
- The player must move left and right, pick up stones one at a time, and place them on the treasure chest in the right order.
- If any 4 stones A,B,C,D are placed on the treasure chest in that order, this will trigger the treasure chest to calculate (A+B)*C (and ignoring D). If the numeric value calculated by this formula matches the required combination (which is “6” in the example above), then the treasure will unlock and the player wins.
- The player also has a maximum number of moves allowed to unlock the treasure. If the maximum available moves are used up without unlocking the treasure, then the player loses the game.
- Sample output #1
- In the following sample output, the player gets the combination correct and wins the treasure. Note that anything shown in bold font indicates what the user has been asked to type in (using a Scanner). The player can choose from the following actions: move left (l), move right (r), pick up a stone (p) and drop a stone (d).
- Enter combination for the treasure chest (5-10): 6
- Enter maximum allowed moves: 100
- Enter player name: Jack
- Jack(0) #(1) @(2) %(3) $(4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack(1) #(1) @(2) %(3) $(4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack(2) #(1) @(2) %(3) $(4) Treasure/6\(5)
- Move (l/r/p/d): p
- Jack@(2) #(1) (2) %(3) $(4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack@(3) #(1) (2) %(3) $(4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack@(4) #(1) (2) %(3) $(4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack@(5) #(1) (2) %(3) $(4) Treasure/6\(5)
- Move (l/r/p/d): d
- Jack(5) #(1) (2) %(3) $(4) Treasure/6\(5)
- Move (l/r/p/d): l
- Jack(4) #(1) (2) %(3) $(4) Treasure/6\(5)
- Move (l/r/p/d): p
- Jack$(4) #(1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack$(5) #(1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): d
- Jack(5) #(1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): l
- Jack(4) #(1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): l
- Jack(3) #(1) (2) %(3) (4) Treasure/6\(5) (Continued...)
- Move (l/r/p/d): l
- Jack(2) #(1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): l
- Jack(1) #(1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): p
- Jack#(1) (1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack#(2) (1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack#(3) (1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack#(4) (1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack#(5) (1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): d
- Jack(5) (1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): l
- Jack(4) (1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): l
- Jack(3) (1) (2) %(3) (4) Treasure/6\(5)
- Move (l/r/p/d): p
- Jack%(3) (1) (2) (3) (4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack%(4) (1) (2) (3) (4) Treasure/6\(5)
- Move (l/r/p/d): r
- Jack%(5) (1) (2) (3) (4) Treasure/6\(5)
- Move (l/r/p/d): d
- Jack(5) (1) (2) (3) (4) Treasure\6/(5)
- You unlocked the treasure!
- Notice that when the player picks up a stone such as #, the # symbol disappears from its original position in the display string, and reappears next to the player's name.
- Sample output #2
- In the second sample output, the player fails to unlock the treasure within the required number of moves, and loses the game.
- Enter combination for the treasure chest (5-10): 8
- Enter maximum allowed moves: 3
- Enter player name: Jill
- Jill(0) #(1) @(2) %(3) $(4) Treasure/8\(5)
- Move (l/r/p/d): r
- Jill(1) #(1) @(2) %(3) $(4) Treasure/8\(5)
- Move (l/r/p/d): p
- Jill#(1) (1) @(2) %(3) $(4) Treasure/8\(5)
- Move (l/r/p/d): r
- Jill#(2) (1) @(2) %(3) $(4) Treasure/8\(5)
- You lose.
- Do not start the assignment yet! Keep reading!
- Solution requirements
- To receive any marks, your solution must meet the following minimum requirements:
- 1. The tasks described below must be implemented in order. That is, task 1 must be done first, followed by task 2, and so on. You will not be able to receive marks for later tasks unless you have completed the earlier tasks.
- 2. Your solution must use only the features of Java that are taught in this subject before the due date. For example, it must not use arrays (or equivalent), inheritance, exceptions, varargs, interfaces, or generics. This restriction is necessary so that we are able to assess your expertise in using fundamental programming techniques. Assignment 2 will allow you more freedom to use more advanced features of Java.
- 3. Your program's output must exactly match the output given by PLATE (see “Assignment Submission and Return”).
- 4. Your solution must not circumvent or trick PLATE's scoring system.
- Technical points
- 1. Your program should create ONLY ONE Scanner object, otherwise it may behave incorrectly when you upload your solution to PLATE.
- 2. Sometimes you will want to read a single character from the keyboard. To do this, you must first read the entire line as a String using Scanner.nextLine() and then use the String.charAt() method to get the first character from the string. The first character of a string str is obtained via the expression str.charAt(0) .
- 3. There is a well-known problem with using the Scanner.nextInt() method in combination with Scanner.nextLine(). To make sure you don't encounter any problems, you must clear the line buffer with a call to nextLine() after each call to nextInt(). Below is the correct way to read an integer from the keyboard:
- System.out.print("Please enter a number: ");
- int number = keyboard.nextInt();
- keyboard.nextLine();
- System.out.println("The number is " + number);
- Tasks
- This section describes each piece of functionality you must implement, and the order in which they must be implemented. Note that the functionality constitutes only half of your overall mark while design and coding style makes up the rest. Please read Section “Marking Scheme” for more details.
- You should submit your project to PLATE after completing each task in order to receive feedback about which parts of your program are correct/incorrect.
- Task 1: Initialisation
- The simplest solution requires at least 4 classes: Game, Player, Stone and TreasureChest.
- Before you write any code, think about what fields and methods (including get/set methods) are needed for each class and write them down in the space below:
- class Game
- - fields: e.g. player, treasureChest, ...
- - methods: e.g. turn(), ...
- class Player
- - fields:
- - methods:
- class Stone
- - fields:
- - methods:
- class TreasureChest
- - fields:
- - methods:
- (The above list is not worth marks, but it will help guide you to write code).
- You may add additional classes to your project if you can justify doing so. However, the simplest solution needs only 4 classes.
- Now you are ready to write your code. At each stage, click the compile button and fix any errors before continuing.
- 1. Create a BlueJ project.
- 2. Create your 4 classes and remove BlueJ's template code.
- 3. Declare all of the fields in each class.
- 4. Declare all of your methods. But leave the code empty for now, except for get/set methods. Write all of the code for all of your get/set methods now.
- Finally, declare a constructor for the Game class. This constructor should ask the user to enter a “combination” for the treasure (this is simply a number between 5-10), for the initial positions and information about all of the player, item and exit objects, and to then create these new objects and store them in the 5 fields of the game.
- Your constructor should be responsible for running only the initial “setup” part of the game:
- Enter combination for the treasure chest (5-10): 6
- Enter maximum allowed moves: 100
- Enter player name: Jack
- Submit this now to PLATE to receive marks for this task, or to receive feedback on mistakes in your output.
- Task 2: toString
- Define a method in class Game called toString() which is public, takes no parameters and returns a string of the form:
- Jack(0) #(1) @(2) %(3) $(4) Treasure/6\(5)
- The format of this string was explained earlier.
- This method should not print the string, only return it as the method's result.
- Hint: You can construct the string using string concatenations of the form:
- return "string1” + “string2” + variable + “another string” + …
- You can also use the StringBuffer class described in the text book. You must not, however, use the sprintf method (since it uses the untaught varargs feature).
- Tip: it is recommended from a design point of view that you create a separate toString() method in every one of your classes. For example, you can also define a toString() method in your Player class as well, which only produces the part of the string that looks like this: “Jack(0)”. And then your main Game.toString() method can invoke all of the other toString() methods to build each part of the whole string.
- Task 3: Player's turn
- Define a method in class Game called turn() which is public, takes no parameters and returns nothing. This method allows the player to have his/her turn by asking the user to type in some command, either 'l' to move the player left, 'r' to move the player right, 'p' to pick up an item at the current position or 'd' to drop an item at the current position.
- Your method should follow exactly the following output format:
- Move (l/r/p/d): r
- Your program must print the prompt ”Move (l/r/p/d): ” and then use a scanner to read the user's input. (Read the Technical Points section to learn how to read a single character)
- After reading the user's input, your program should execute the appropriate command. In this task, you can ignore the 'p' and 'd' commands and just support the 'l' and 'r' commands.
- If the user types 'l' or 'r', your program should send a message to the player object to move one position to the left or right respectively.
- You do not need to try this entire task at once. Complete a little bit at a time, and submit to PLATE to see your progress and receive PLATE's feedback.
- Task 4: Picking up items
- When a player picks up a stone, it is removed from the ground, and held by the player. There should be three elements to your solution:
- 1. You will need a field in your player object to store the stone that the player is currently holding. You can set this to null whenever the player is not holding a stone.
- 2. Update the toString() method to correctly display whenever the player is holding a stone (according to the sample output above).
- 3. Update the turn() method to recognise the 'p' command to pick up a stone at the current position. This command should do nothing if the player is already holding a stone.
- Submit to PLATE to receive your marks and feedback for this task.
- Task 5: Dropping items
- Extend your turn() method to recognise the 'd' command. If the player moves to the position of the treasure chest and inputs the 'd' command, the currently held stone (if any) should be placed into the treasure chest.
- The treasure chest is capable of storing up to 4 stones (i.e. 4 fields). If all of the fields are empty, the stone should be placed into the first field. If the first field already contains a stone, then the stone should be placed into the second field. If that is also already filled, try the 3rd field. And if that too is filled, then finally try to store it into the 4th field.
- When a stone is placed onto the treasure chest, it becomes invisible to the user.
- Implement this feature according to the sample output at the top of this document, and submit to PLATE to receive your mark and feedback for this task.
- Task 6: Game over
- Define a method in class Game called isOver() which is public, takes no parameters and returns a boolean. This method should return true in either of the following two situations (and in all other situations should return false):
- 1. All 4 stones have been placed on the treasure chest in the correct order (as explained in the “Program Description” section).
- 2. The player has used up all of the maximum allowed turns/moves and has not yet placed all 4 stones correctly.
- Submit to PLATE to receive your marks and feedback for this task.
- Task 7: Loops
- You will now implement some loops in your program. After completing each loop, submit it to PLATE to receive marks and feedback for that loop.
- • Loop 1: Define a method in class Game called play() which is public, takes no parameters, and returns nothing. This method repeatedly invokes the turn() method until the game is over (see the previous task). When the loop stops, the program should print either “You unlocked the treasure!” or “You lose.” depending on whether the player won or lost. You should also print out the toString() string before/after each turn. You can test this method by right-clicking on the new object in BlueJ and selecting the play() method.
- • Loop 2: When the user is asked to specify the treasure chest combination, this number must be within the range 5-10. Write a loop which repeatedly asks the user to enter a number until the user enters a valid number within the range 5-10. Your output should follow the following format exactly:
- Enter combination for the treasure chest (5-10): 4
- Invalid combination.
- Enter combination for the treasure chest (5-10): 2
- Invalid combination.
- Enter combination for the treasure chest (5-10): 12
- Invalid combination.
- Enter combination for the treasure chest (5-10): 5
- Enter maximum allowed moves:
- After you have done this, define a class called Main, which has a correctly defined main() method. This method should create a new Game object, and then invoke its play() method.
- Task 8: Picking up and dropping stones (advanced)
- Modify your program so that it is possible for the player to pick up stones that have already been placed on the treasure chest, and to place stones back onto the ground. This behaviour should be supported via the 'd' and 'p' commands.
- Because a treasure chest can store multiple stones at the same position, the player should always pick up the most recently placed stone first. For example, if 4 stones have already been placed on the treasure chest, then the 'p' command should pick up the 4th stone. The 'p' command has no effect if the treasure chest has no stones.
- The 'd' command can be used to drop a stone back on the ground at the current position, if the player is standing at a position between 1-4 and there is currently no stone already sitting at that position.
- Note: Unless you use code-reuse principles from the Week 7 lecture to implement this task, you may lose a significant amount of marks for “code reuse” (see the Marking Scheme section below).
- Submit to PLATE to receive your marks and feedback for this task.
- Bonus task - 3 marks
- If you have completed all of the above tasks, you may receive up to 3 bonus marks for completing this task (refer to the section "Marking Scheme" for details of how the bonus marks are awarded).
- It is good design to separate all user interface code from the rest of your program - this allows you to easily replace your user interface with a different one without affecting the rest of your program.
- Create a class called UI containing exactly all of the user interface code for your program. That is, any code that mentions Scanner, System.in or System.out should be moved into this class. To be eligible to receive the bonus marks, any given change to the user interface should be possible by making changes only to your UI class. The toString() methods themselves do not need to be moved into the UI class, but the methods that print these strings do.
- Marking scheme
- Your solution will be marked according to the following scheme:
- Correctness (100)
- Initialisation
- toString
- Player's turn
- Picking up items
- Dropping items
- Game over
- Loops
- Picking up/dropping stones 5 marks
- 15 marks
- 10 marks
- 15 marks
- 15 marks
- 15 marks
- 15 marks
- 10 marks
- Coding style/Design (50)
- Style
- Object-oriented design
- Code reuse 10 marks
- 10 marks
- 30 marks
- Bonus task (3)
- Your coding style/design marks will be awarded as follows:
- • Style will be marked based on the use of correct indentation.
- • Object-oriented design will be marked based on whether you have appropriately moved code into your separate classes. Penalties will apply for placing code into the Game class that is more relevant to one of the other classes.
- • Code reuse will be marked based on how well you have used refactoring techniques to eliminate repeated code (see week 7). If you structure your program well, there should be very little to no repeated code.
- Your overall mark will be calculated using the formula:
- • your mark = correctness * (50 + design) / 100 + bonus
- If your mark is calculated to be greater than 100 due to the bonus, it will be reduced to 100.
- Individual assessment
- This assignment must be done by yourself and not with anyone else. Group work is not allowed and will be considered as academic misconduct.
- Academic misconduct
- Working with another person on this assignment is not allowed and is considered as academic misconduct. Do not show other people your code, or look at other people's code, or discuss assignment code with other people; it is an offence to have a copy of someone else's assignment (before the submission date). Do not post your assignment on the web. Posting your assignment on the web and getting help through blogs, forums or other websites is considered to be an academic misconduct.
- To detect plagiarism, the subject uses an online system called PLATE available at http://plate.it.uts.edu.au
- Students may find it useful to consult The UTS Coursework Assessment Policy & Procedure Manual, at http://www.gsu.uts.edu.au/policies/assessment-coursework.html
- Expected work load
- It is expected that this assignment will take about 15 to 20 hours of work. A well-designed solution is expected to use approximately 150-200 lines of code, while a badly-designed solution may reach up to 300 lines of code. Some people may complete the task in 5 hours, and some may need 30 hours or more; there is a huge variation in students' experience and abilities.
- Frequently asked questions
- A FAQ (Frequently Asked Questions) will be maintained at UTSOnline/Assignments/Assignment 1/FAQ. FAQs and their answers will be posted there as they arrive. You should read the FAQ at least once before you hand in your solution, but to be safe check it every couple of days. Anything posted on the FAQ will be considered as part of the assignment specification. The FAQ will be frozen (no new entries) two days before the due date.
- Discussion board
- A discussion board has been set up on UTSOnline to discuss the assignment. It is important to read the rules below before using the discussion board. Posts that do not follow the rules will be ignored.
- You may discuss any task of the assignment; however you must not share actual code from the assignment on the board. The FAQ should always be checked before asking questions on the discussion board.
- Discussion board rules:
- • You must not share assignment code on the discussion board.
- • You must always set a relevant and descriptive subject line for your message.
- • Do not click the "reply" button to reply to another message if you are not intending to respond to that message. New questions should always be posted in reply to the root message of the thread.
- • Additional rules may be posted on the discussion board itself.
- Assignment submission and return
- Your assignment must be submitted as a JAR file through the PLATE system, online, at http://plate.it.uts.edu.au/. You may submit and resubmit as many times as you wish, before the due date, and PLATE will update your mark.
- Instructions for submitting to PLATE are displayed online at the PLATE website.
- WARNING! PLATE may become overloaded on or near the due date when many students load and test their solution at the last minute. This will not be considered as a valid reason for an extension. To be safe, you should aim to submit your solution well before the due date.
- Your marks for Assignment 1 will be available through PLATE within 3 weeks after the due date.
- Late submission
- The assignment is due at 11:00 am on Monday, 3rd October 2011. Each 24 hour period after this time is considered to be an extra day late. For example, a submission at 11:01 am on Monday, 3rd October is considered 1 day late, while a submission at 11:01 am on Tuesday, 4th October 2011 is considered 2 days late.
- Your assignment will lose 20% of its original mark per day late, unless permission has been given by the subject co-ordinator before the due date.
- If your performance in an assessment item or items has been affected by extenuating or special circumstances beyond your control you may apply for Special Consideration. Information on how to apply can be found at http://www.sau.uts.edu.au/assessment/consideration.html
- Model solution
- A model solution can be copied from UTSOnline a couple of weeks after the due date at Assignments/Assignment 1/Solution.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement