COSC 1047 – Winter 2019 – Assignment 4
See D2L For Due Date
This assignment is designed to give you some experience writing Java GUIs, recursion, collection classes and
data structures. This assignment is done using Eclipse. Import the .zip file that is provided on D2L and when it
is complete, export it again as a .zip. All submissions should be done through D2L. You can submit and
resubmit as many times as you want up to the due date and time. If you have any problems submitting, email me
– [email protected] - in a timely manner. Make sure your name, student number and a brief
description of your program appear at the top of each file in comments. Also make sure that you use
comments to clarify your code for marking. Failure to do either of these tasks may result in a deduction of
marks.
Submit solutions to questions 1 and 2 and (6 or 7). But DO ALL questions to be prepared for the tests and
exam. Solutions will be provided (via D2L) only for required questions.
If you work in partners remember to include both names in the source code. The person who is left out will
receive zero.
Note: It is imperative that you import the cosc1047-w19-a4.zip file from D2L properly or you won’t be able to
work in the proper project environment.
Grading: See rubric.
- (DieGUI.java)
screenshot to D2L. - (Recursion.java) Write a java program that has three static recursive methods and a main method. In your main method (5
marks) prompt for user input and display the results of each of your recursive methods as shown in the sample output
below.
a) Write a method that uses recursion to compute the value of , where a and n are both arguments to the method. If n =
0, the method should return 1 as 1. If n = 1, the method should return a as. If n is any other number …
that’s for you to determine but remember,
b) Write a method that uses recursion to return the reverse of a String that is passed to the method as an argument. Hint:
For base cases, consider a string that has 1 or fewer characters…how much work is there to reverse them? Otherwise a
reversed string is the last letter of the original string plus the reverse of the rest. Try it out on paper first.
c) Write a recursive method that determines the number of digits in an integer, n. Hint: If n < 10, there is one digit.
Otherwise, it has one more digit than n / 10.
For these questions and with recursion in general, the trick is to determine 1) the base case(s) that will cause the
function to simply return and end the recursion and 2) the recursive case(s) that will cause the function to call
itself with a smaller version of the same problem. All input and output should take place in your main method.
* Don’t forget to “eat the new line” if you use your scanner between numbers and strings.
java Recursion
Enter two numbers, base then exponent: 2 0
Result: 1
Enter a string to reverse: apples
Result: selppa
Enter a number: 123
Number of digits: 3
java Recursion
Enter two numbers, base then exponent: 100 1
Result: 100
Enter a string to reverse: Z
Result: Z
Enter a number: 6
Number of digits: 1
java Recursion
Enter two numbers, base then exponent: 2 12
Result: 4096
Enter a string to reverse: i luv cosc!
Result: !csoc vul i
Enter a number: 1234567890
Number of digits: 10 - Initial amount: $50.00
After year 1: $51.00
After year 2: $52.00
few considerations:
You need to change the data field in CardDeck to a LinkedList (don’t forget any necessary import
statements).
The constructor needs to be rewritten. Start with an empty LinkedList and add all 52 cards in order.
Look at how the CardDeck constructor works now to know what order they should be put in. Keep in
mind that a Card takes a single integer as an argument that determines both the suit and the rank.
Shuffle() needs to be rewritten. You’ll need to find a way to randomize the cards without losing any
of them. You could try using an iterator to grab a card from a random location then moving to a new
random location in the list and adding the card back in – but that’s just one possible approach.
Deal() needs a bit of work too – as with the current method, you need to remove the top card and return
it. The top Card must leave the list – we don’t deal cards AND keep them in the deck unless we are
playing at a crooked table.
cardsInDeck() needs to return the number of cards left in the deck. This ~could~ be used when
dealing to figure out if there are enough cards left to deal.
Empty() should return true if there are no cards left in the deck.
toString() should be rewritten. Take a look at the existing one. It prints out the cards left in the deck.
You don’t need to worry about the rank and suit, they are part of the Card class. I suggest an Iterator
here to walk through the LinkedList.
Just to be clear – ALL of your modifications should be done in the CardDeck class. Do NOT modify Card or
CardTester. The markers will run CardTester to make sure that your class works properly. Again, the idea
here is to recognize that we can change the engine of a data structure without changing its external interface (i.e., it
still works the same). As you are modifying CardDeck, comment out some of the lines in CardDeckTester
so that it’s not trying to test methods you haven’t adjusted yet.
- (ArrayBag, DiceArrayBagTester) Every ~real~ gamer carries around a bag of dice just in case of a
game-emergency. Your job in this question is to rewrite the IntArrayBag class so that it is generic and can
hold any type of object. This means you need to change the data datafield and then correct all of the
methods that use it (12 marks). Then, when this is done, write a GUI that allows you to create a bag of dice
objects. This means your GUI has the following functionality:
a. Create an empty ArrayBag. It is ok to have it pre-set to use dice as in ArrayBag.
b. The GUI should have inputs so that you can create dice and add them to the bag. You can use text fields
but this might be a good place for radio buttons or a drop down menu since there are only a few valid dice
configurations.
c. The GUI should allow you to grab a random die from the bag and roll it. This can be done with a single
click – grab-and-roll. Remember, the grab() method doesn’t actually remove the Die from the bag.
d. The GUI should allow you to remove a die from the bag. For this you’ll need to specify the configuration
of die being removed – i.e., removing a 6-sided die.
e. The GUI should allow you to print the dice that are in the bag.
f. The GUI should display (on demand) the number of occurrences of a particular Die configuration (ask the
user for the configuration of interest).
g. For good measure your GUI should also allow you to print out the number of dice in the bag and the
current capacity of the bag.
You are given the Die class. No modifications should be needed for this class. You are given the
IntArrayBag class to use as a reference. I recommend that you copy this class so you can use it as a reference.
Leave this class with an array engine, DO NOT change it to a LinkedList.
WX:codehelp