Need help in Java assignment
Java Help/a4.pdf
ASSIGNMENT 4 : A game and an email system
COMP-202, Winter 2014, All Sections
Due: Tuesday, 25th of March, 2014, 23:59
Please read the entire pdf before starting.
You must do this assignment individually and, unless otherwise specified, you must follow all the general instructions and regulations for assignments. Graders have the discretion to deduct up to 10% of the value of this assignment for deviations from the general instructions and regulations.
Part 1: 0 points Part 2a: Question 1: 25 points Part 2b, Question 1: 10 points Part 2b, Question 2: 25 points Part 2b, Question 3: 5 points Part 2b, Question 4: 10 points Part 2b, Question 5: 15 points Part 2c, Question 1: 10 points
100 points total
It is very important that you follow the directions as closely as possible. The directions, while perhaps tedious, are designed to make it as easy as possible for the TAs to mark the assignments by letting them run your assignment through automated tests. While these tests will not determine your entire grade, it will speed up the process significantly, which will allow the TAs to provide better feedback and not waste time on administrative details. Plus, if the TA is in a good mood while he or she is grading, then that increases the chance of them giving out partial marks. Marks can be removed if comments are missing, if the code is not well structured, and if the problems your solution does not respect the assignment requirement.
Part 1 (0 points): Warm-up
Do NOT submit this part, as it will not be graded. However, doing these exercises might help you to do the second part of the assignment, which will be graded. If you have difficulties with the questions of Part 1, then we suggest that you consult the TAs during their office hours; they can help you and work with you through the warm-up questions.
Warm-up Question 1 (0 points) Write a class Vector. A Vector should consist of three private properties of type double: x,y, and z. You should add to your class a constructor which takes as input 3 doubles. These doubles should be assigned to x,y, and z. You should then write methods getX(), getY(), getZ(), setX(), setY(), and setZ() which allow you to get and set the values of the vector.
Warm-up Question 2 (0 points) Add to your Vector class a method calculateMagnitude() which returns a double representing the magnitude of the vector. The magnitude can be computed by taking√
x2 + y2 + z2
1
Warm-up Question 3 (0 points) Write a method scalarMultiply which takes as input a double[], and a double scale, and returns void. The method should modify the input array by multiplying each value in the array by scale. Question to consider: Would this approach work if we had a double as input instead of a double[]?
Warm-up Question 4 (0 points) Write a method deleteElement which takes as input an int[] and an int target and deletes all occurrences of target from the array. The method should return the new int[]. Question to consider: Why is it that we have to return an array and can’t simply change the input parameter array?
Warm-up Question 5 (0 points) Write the same method, except this time it should take as input a String[] and a String. What is different about this than the previous method? (Hint: Remember that String is a reference type.)
Part 2
The questions in this part of the assignment will be graded.
There are two parts to this assignment. In part one, you will write a little two-player game. In the second part, you will write a very simple email system.
The focus of the questions is on how to structure a more complex model, how to represent complex data, and how the different classes you are going to create interact with each other. There will not be any complex algorithm that you have to design and implement, except of some array manipulation in the second questions.
The descriptions are not very detailed, and this is on purpose. We want to leave some of the design choices to you.
Part 2a
Assume the following 2-player game which is played in rounds and has some similarity to a card game where two players each pick a card and the player with the larger card wins.
In each round, each player picks two numbers: a base number and a tie number. Each of the numbers must be between 0 and a given max. (e.g., the base number can be a 0, 1, 2 or 3, and the tie number can be 0 or 1). The player who has chosen the bigger base number wins the round. In case of a tie, i.e. both players have chosen the same base number, the one who has the bigger tie number wins the round. If this one is also tied, there is no winner in this round.
At the start of the game (before the first round), each player has a fixed budget for the game numbers and a fixed budget for the tie numbers. In each round, the budgets are reduced according to the numbers the player has chosen. In any round, a player can choose only numbers that are smaller or equal to the budget the player has left. The game finishes, once both players have used up the budgets for both their base number and their tie number. The player who has won the most rounds has won.
Let’s look at an example. Assume the budget for base numbers is 5, and the budget for tie numbers is 2 at the beginning of the game. Assume players can choose as base numbers 0,1,2,3 and as tie numbers 0 or 1.
• Round 1
– p1 chooses base number b = 2 and tie number t = 0. p2 chooses b = 1 and t = 1.
– p1 wins the round because its base number is higher than p2’s base number
– p1’s base budget after the round is 3, and the tie budget is 2
Page 2
– p2’s base budget after the round is 4, and the tie budget is 1
• Round 2
– p1 chooses b = 2 and t = 1, p2 chooses b = 3 and t = 0.
– p2 wins the round because its base number is higher than p1’s base number
– p1’s base budget after the round is 1, and the tie budget is 1
– p2’s base budget after the round is 1, and the tie budget is 1
• Round 3
– p1 chooses b = 2 and t = 0, p2 chooses b = 1 and t = 1.
– p1’s base budget is only 1, so the system will automatically reduce the bid from b = 2 to b = 1 (the maximum that is still available).
– p2 wins the round. Both have the same base number, but p2’s tie number is higher than p1’s tie number.
– p1’s base budget after the round is 0, and the tie budget is 1
– p2’s base budget after the round is 0, and the tie budget is 0
• Round 4: Although p2 has no budget left, the game still continues as p1 still has some budget left
– p1 chooses b = 0 and t = 1, p2 can choose whatever it likes, the system will automatically set it to b = 0 and t = 0 because p2 has used up all the available budget.
– p1 wins. Both base numbers are 0, but p1 has a higher tie number than p2.
– All budgets are now 0.
• The game ends because all budgets are depleted. Overall the game is a tie, because both have won 2 rounds.
In this assignment, you have to implement a class Player that maintains most of the state needed to play the game as well as some of the game semantics.
Question 1: Player Class (30 points) The Player Class has the following private properties:
• baseBudget: the budget to be used for base numbers
• tieBudget: the budget used for tie numbers
• lastBase: the base number that the player chose last
• lastTie: the tie number that the player chose last
• wins: the number of rounds the player has won
The Player Class has the following public methods:
• A constructor Player (int b, int t) initializes the baseBudget and the tieBudget according to the values of the input parameters, and the other properties to 0.
• void playRound(int base, int tie): base and tie are the numbers the player has chosen for that round. Thus, the method should keep track of these values by assigning them to lastBase and lastTie, and also accordingly reduce the baseBudget and tieBudget. However, both baseBudget
and tieBudget may not go below 0. Thus, if base or tie is bigger than the corresponding budget, we have to adjust it so that it is equal to the budget before we modify the object properties.
• boolean canStillPlay() returns true if at least one of the budgets is bigger than zero.
Page 3
• int compare(Player p) checks who has won the last round. For instance, assume player variables p1 and p2. If we make a call p1.compare(p2), the compare method should do the following. It compares the properties lastBase and lastTie of both players. The one with the bigger lastBase wins. If they are equal, the one with the bigger lastTie wins. If both values are equal, nobody wins. The method returns 1 if player p1, that is, the player on which the method is called, wins. It returns -1 if p2, that is, the player that was given as input parameter, wins. It returns 0 in the case that nobody wins.
• void win() increases the wins property by one.
• int getWins() returns the number of wins.
• String toString returns the properties of the object in a nice String format. That is useful for debugging. Using that method, you can print the object at any time to make sure the properties are always set correctly.
On myCourses, together with the assignment description you can find a Game.java. This file implements the game using the player data type to maintain and manipulate most of the data. This file will help you see how the methods of Player are used. It will also help you debug your system. Game.java
automatically maintains one of the players. The other player is you running the program.
Part 2b: An email system
You will implement a simplified email system. The email system has users. A registered user can send an email to another registered user (or to somebody else, but then the email will disappear to nowhere...). The system keeps track of all messages sent among users.
For this assignment, you will write several classes and use object oriented programming to build up the email system. In general, you may NOT add extra public methods or properties to any of the classes. If you want to add extra methods or properties, you should make them private.
Please note that for this assignment you MAY NOT use ArrayList, or any other form of sets or lists that you might be familiar with. As described below, you always have to use arrays whenever you have to keep track of multiple elements.
Question 1: Message class (10 points) In this question, you will define a type Message that represents an email message.
A Message object should have the following properties (attributes): sender, receiver, subject and body. Furthermore, it should have at least the following public methods:
• A constructor that has as four input arguments, one for each of the properties, and which sets the properties to the given input values.
• toString returns the properties of the message in a proper format suitable for printing.
• A set of getter (observer) methods (getSender, getReceiver, ...) to get the value of the individual attributes.
• Once an attribute is set through the constructor, it should not be changed anymore by the calling class. This can be achieved by having no setter methods, and by making the properties private. (Encapsulation).
Question 2: Mailbox class (30 points) In this question, you will write a class Mailbox. The point of this class is to keep track of a set of messages. Internally, a mailbox object will maintain an array that holds all these messages. As the mailbox system allows each user only to have a maximum number of messages in a mailbox, the array
Page 4
will have a fixed size. However, once the mailbox is full, instead of disallowing any further operations, the mailbox will discard old messages in order to make space for new ones.
A Mailbox needs to have at least the following property:
• private Message[] mb : This is an array with a certain size dictated by the class that creates the mailbox.
Furthermore, the system has to keep track of how large the array is and how many messages are currently stored in the array (or in which slot of the array you would put the next message). Think about which properties you might need to store such information.
You should have the following public methods.
• public Mailbox(int size) that takes as the size of the message array and creates an array of the corresponding size.
• A method void addMsg(Message m). This method takes a message as input and adds it to the next free element in the array. If the array is full, the oldest message should be removed. In this case, one possibility to maintain the array in proper order is to shift all elements in the array appropriately (message in slot 1 is moved to slot 0, message in slot 2 is moved to slot 1, etc. and the new message is added to the last slot). You can do such a shift one by one or by using the System.arraycopy method. Alternatively, you simply put the new message into the slot currently occupied by the oldest message. The latter case avoids a lot of shifting around in the array. On the other hand, you have to make sure that you keep track of where the oldest message is.
• A method viewMailbox that does a pretty print of the messages in the mailbox indicated in the example below:
Viewing mailbox with 3 messages:
No Sender Receiver Subject
1 Alice Xi great weather
2 Alice Sandra nothing new
3 Alice Peter my third message
In particular, messages are shown in the order they were added to the mailbox (oldest first). In each line, the first column provides a consecutive number. The second column presents the sender, the third the receiver, and the last column the subject.
The Sender, Receiver and Subject columns should all always be of a specific width. For this assignment, sender and receiver columns should have a max width of 20 and the subject of 30. Keep track of these values in variables that are final. If the sender/receiver/subject values of a message are longer than these predefined values, they should be cut accordingly to fit in the predefined space. (You might want to delegate the padding (in case a value is shorter than the predefined 20/30), or the cutting (in case it is longer) to some helper method.
• A method printMailboxMsg has as input the number of a message as shown by the viewMailbox
method (described above) and prints the corresponding message. If there is no such message a corresponding note should be printed.
• A method removeMailboxMsg takes as input the number of a message as shown by the viewMailbox method and removes the corresponding message, shifting all other messages accordingly in the array. If there is no such message a corresponding note should be printed.
If you create any ”helper” methods to further split up your code and avoid redundant code, then these messages should not be callable from other classes.
Question 3: User Class (5 points) In this question, you will define a type User that represents a user of the mailbox system.
Page 5
The user class has properties username, and two mailboxes, one is an inbox for messages to be received by the user, and an outbox that keeps the last messages the user has sent.
The class has only a single constructor, that takes the user name and the mailbox maximum size as input and initializes all properties accordingly. Otherwise, this class should make the properties directly accessible from other classes (by making them public). No getter or setter methods are thus needed.
Note that making attributes directly accessible from other classes (by making them public) is only recommended for very simple classes like this one, where no additional methods are associated with the class but the class really only represents a new data type.
Question 4: UserList Class (10 points) This class should maintain an array of users as property. In principle, the system wants to allow arbitrary many users. Thus, choosing the right array size is a challenge. Therefore, the idea is to create an array with an initial, fairly small size. Then, if there are more users than fit in the array, we create a new, larger array on the fly. Note that as with the mailboxes, at the beginning not all elements of the arrays are filled with users. Thus, at any time, we have to keep track of the size of the current array as well as the number of users in that array.
Methods:
• The Constructor initializes the array with a size of 2. (No users yet in the array).
• A method findUser takes as input a string username and looks whether the user array contains a user that has this name. If yes, the user is returned, otherwise null is returned.
• A method addUser takes as input a user. If a user with that name already exists in the array, then an according message should be printed and the user not be added. Otherwise, the user should be added to the array (first free element). If the array is full, the array size should be doubled. For that, a new array has to be created of the according size, the users from the old array copied and array variables accordingly reassigned so that they refer to the bigger array. Note that we always have to keep track of how many users are currently in the array.
Question 5: MailboxSystem Class (15 points) You have already a rudimentary class MailboxSystem given on MyCourses. This class provides a (very simple) interface to the user. It maintains a list of users as static global variable in which all users are maintained. It has a main method that offers the main menu of the system, a method handleUser that handles the menu once a user has logged in, and an auxiliary method transform that makes sure that input from the keyboard does not mess up the system.
Within this code, you have to add functionality. Whenever you find a comment that starts with
/* TODO .... */
you have to add code that does what the TODO line indicates.
Note that at the end of this specification, you have the output of some execution of the system. This might give you a better idea of how this should work.
Part 2c
Question 1: Variable and Method Declarations (10 points) Below find 3 class definitions whose variable declarations, property declarations and method declarations are incomplete. You have to correct the lines that are marked with a comment (// line x). Write each of the lines into a file Part2c.txt, one line for each line to be corrected (and in the proper order). Things to consider are the date type, whether public or private, whether static or not static, return type, data type of the input parameters, etc.
Page 6
public class ASimpleType
{
int att1; // line 1
String att2; // line 2
}
public AMoreComplexType
{
int att1; // line 3
int att2; // line 4
AMoreComplexType (... a1, ... a2) // line 5
{
att1 = a1;
att2 = a2;
}
getAtt1() // line 6
{
return att1;
}
getAtt2() // line 7
{
return att2;
}
increaseCalculation() // line 8
{
att1 = att1 * 2;
att2 = att2 + att1;
}
}
public UseMyObjects
{
public static main void (String[] args)
{
x = 1; // line 9
ASimpleType o1 = new ASimpleType();
AMoreComplexType o2 = new AMoreComplexType(2, x);
o1.att1 = x;
o1.att2 = "A String";
o2.increaseCalculation();
x = o2.getatt1;
System.out.println(aFunManipulation(o1, x));
}
aFunManipulation( ...a, .... b ) // line 10
{
int x = a.att1 + b;
String s = "The result is " + x;
return s;
}
}
Page 7
What To Submit
You should submit your assignment on MyCourses. In order to do this, you will need to make a zip of the file. You can do this on windows by following the instructions at this link: http://condor.depaul.edu/
slytinen/instructions/zip.html. On a mac or linux, you can find instructions at http://osxdaily.
com/2012/01/10/how-to-zip-files-in-mac-os-x/
You should submit a zip file called Assignment4.zip with the following files inside of it.
Player.java
Message.java
Mailbox.java
User.java
UserList.java
MailboxSystem.java
Part2c.txt
Confession.txt (optional but strongly recommended) In this file, you can tell the TA about any issues you ran into doing this assignment. If you point out an error that you know occurs in your problem, it may lead the TA to give you more partial credit. On the other hand, it also may lead the TA to notice something that otherwise he or she would not. You should include a description of what you tried to fix the problem, what part of code you think it occurs in, etc.
Marking Scheme
Up to 30% can be removed for bad indentation of your code as well as omitting comments, coding struc- ture, or missing files. Marks will be removed as well if the class names and other requirements are not respected.
Part 2a: Question 1
each method except of win and getWins 4 (20) points win and getWins each 2.5 (5) points
25 points
Part 2b: Question 1
properties/constructor and toString each 3 (6) points getter (observer) and setter (modifier) methods 4 points
10 points
Part 2b: Question 2
properties and constructor 3 points addMsg 5 points
viewMailbox 10 points printMailboxMsg 2 points removeMailboxMsg 5 points
25 points
Part 2b: Question 3
all 5 points 5 points
Part 2b: Question 4
properties and constructor 3 points findUser 3 points addUser 4 points
10 points
Page 8
Part 2b: Question 5
all together 15 points 15 points
Part 2c: Question 1
each line 1 point 10 points
Page 9
Example Execution Mailbox System
The following is a run with having mailboxes of size 3 and the initial user array of size 2.
> run MailboxSystem
Main Menu Options:
-------------
1: Create a user
2: Login
3: Exit
> 1
Name of the User:
>Alice
User Created
Main Menu Options:
-------------
1: Create a user
2: Login
3: Exit
> 2
Name of the User:
> Bob
User can’t be found.
Main Menu Options:
-------------
1: Create a user
2: Login
3: Exit
> 1
Name of the User:
> Bob
User Created
Main Menu Options:
-------------
1: Create a user
2: Login
3: Exit
> 1
Name of the User:
> Xue
User Created
Page 10
Main Menu Options:
-------------
1: Create a user
2: Login
3: Exit
> 2
Name of the User:
Alice
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 3
Receiver:
> Bob
Subject:
> first message
One line msg body:
> blub
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 3
Receiver:
> Bob
Subject:
> second message
One line msg body:
...
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
Page 11
7: Logout:
> 3
Receiver:
> Bob
Subject:
> 3rd message
One line msg body:
> here it goes
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 3
Receiver:
> another one
Subject:
> s1
One line msg body:
> m1
Mailbox full; remove oldest message
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 2
Viewing mailbox with 3 msg
No Sender Receiver Subject
1 Alice Bob second message
2 Alice Bob 3rd message
3 Alice another one s1
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 7
Page 12
Main Menu Options:
-------------
1: Create a user
2: Login
3: Exit
> 2
Name of the User:
> Bob
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 1
Viewing mailbox with 3 msg
No Sender Receiver Subject
1 Alice Bob first message
2 Alice Bob second message
3 Alice Bob 3rd message
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 6
Message Number in Inbox to be removed
> 2
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 1
Viewing mailbox with 2 msg
No Sender Receiver Subject
1 Alice Bob first message
2 Alice Bob 3rd message
Page 13
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 7
Main Menu Options:
-------------
1: Create a user
2: Login
3: Exit
> 2
Name of the User:
> Xue
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 3
Receiver:
> Bob
Subject:
> different senders
One line msg body:
> ...
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 7
Main Menu Options:
-------------
1: Create a user
2: Login
3: Exit
> 2
Page 14
Name of the User:
> Bob
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 1
Viewing mailbox with 3 msg
No Sender Receiver Subject
1 Alice Bob first message
2 Alice Bob 3rd message
3 Xue Bob different senders
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 7
Main Menu Options:
-------------
1: Create a user
2: Login
3: Exit
> 2
Name of the User:
> Xue
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 3
Receiver:
> Bob
Subject:
> last one
One line msg body:
Page 15
> ...
Mailbox full; remove oldest message
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 7
Main Menu Options:
-------------
1: Create a user
2: Login
3: Exit
> 2
Name of the User:
> Bob
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 1
Viewing mailbox with 3 msg
No Sender Receiver Subject
1 Alice Bob 3rd message
2 Xue Bob different senders
3 Xue Bob last one
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 4
Message Number in Inbox:
> 2
Sender: Xue
Receiver: Bob
Subject: different senders
Page 16
Message: ...
User Menu Options:
------------------
1: View your inbox:
2: View your outbox:
3: Send a message:
4: View specific inbox message
5: View specific output message
6: Remove specific inbox message
7: Logout:
> 7
Main Menu Options:
-------------
1: Create a user
2: Login
3: Exit
> 3
Exit
>
Page 17
Java Help/Game.java
Java Help/Game.java
import
java
.
util
.
*
;
public
class
Game
{
// needed to get input from the player
static
Scanner
sc
=
new
Scanner
(
System
.
in
);
// game specific values
static
final
int
baseBudget
=
10
;
static
final
int
tieBudget
=
3
;
static
final
int
baseMax
=
3
;
static
final
int
tieMax
=
1
;
public
static
void
main
(
String
args
[])
{
// p1 and p2 will play against each other
Player
p1
=
new
Player
(
baseBudget
,
tieBudget
);
Player
p2
=
new
Player
(
baseBudget
,
tieBudget
);
int
base
,
tie
;
// numbers played in this round
int
playResult
;
Random
r
=
new
Random
();
// for the computer generated player
System
.
out
.
println
(
"Welcome: you have a base budget of "
+
baseBudget
+
" and a tie budget of "
+
tieBudget
);
while
(
p1
.
canStillPlay
()
||
p2
.
canStillPlay
())
{
System
.
out
.
println
(
"Indicate your base number between 0 and "
+
baseMax
);
base
=
sc
.
nextInt
();
sc
.
nextLine
();
System
.
out
.
println
(
"Indicate your tie number between 0 and "
+
tieMax
);
tie
=
sc
.
nextInt
();
sc
.
nextLine
();
if
((
base
<
0
||
base
>
baseMax
||
tie
<
0
||
tie
>
tieMax
))
{
System
.
out
.
println
(
"Wrong input"
);
}
else
{
// play for the user
p1
.
playRound
(
base
,
tie
);
// now play for the computer
base
=
r
.
nextInt
(
baseMax
+
1
);
tie
=
r
.
nextInt
(
tieMax
+
1
);
p2
.
playRound
(
base
,
tie
);
System
.
out
.
println
(
"You: "
+
p1
.
toString
());
System
.
out
.
println
(
"Computer: "
+
p2
.
toString
());
// check the win
playResult
=
p1
.
compare
(
p2
);
if
(
playResult
==
1
)
{
p1
.
win
();
System
.
out
.
println
(
"You won this round"
);
}
else
if
(
playResult
==
-
1
)
{
p2
.
win
();
System
.
out
.
println
(
"You loose this round"
);
}
else
{
System
.
out
.
println
(
"It's a tie"
);
}
}
}
System
.
out
.
println
(
"Game over"
);
System
.
out
.
println
(
"You have numer of wins: "
+
p1
.
getWins
());
System
.
out
.
println
(
"Your opponent has number of wins: "
+
p2
.
getWins
());
if
(
p1
.
getWins
()
>
p2
.
getWins
())
System
.
out
.
println
(
"You are the winner"
);
else
if
(
p1
.
getWins
()
<
p2
.
getWins
())
System
.
out
.
println
(
"You are the looser"
);
else
System
.
out
.
println
(
"It's a tied game"
);
}
}
Java Help/MailboxSystem.java
Java Help/MailboxSystem.java
public
class
MailboxSystem
{
public
static
final
int
MailboxSize
=
3
;
// for easier debugging, this system supports mailboxes with up to 3 messages
public
static
Userlist
emailUsers
=
new
Userlist
();
// the list of registered users
// we need that one all the time...
public
static
java
.
util
.
Scanner
sc
=
new
java
.
util
.
Scanner
(
System
.
in
);
public
static
void
main
(
String
args
[])
{
while
(
true
)
{
System
.
out
.
println
(
"\nMain Menu Options:"
);
System
.
out
.
println
(
"-------------"
);
System
.
out
.
println
(
"1: Create a user"
);
System
.
out
.
println
(
"2: Login"
);
System
.
out
.
println
(
"3: Exit"
);
String
inputString
=
sc
.
nextLine
();
int
input
=
transform
(
inputString
,
4
);
// make sure input is readable
if
(
input
==
1
)
{
// read name of user to be created
System
.
out
.
println
(
"Name of the User:"
);
String
username
=
sc
.
nextLine
();
/* TODO: check whether user exists, if not create user and add to the emailUser List */
}
else
if
(
input
==
2
)
{
// read name of user to be logged in
System
.
out
.
println
(
"Name of the User:"
);
String
username
=
sc
.
nextLine
();
User
u
=
emailUsers
.
findUser
(
username
);
if
(
u
==
null
)
System
.
out
.
println
(
"User can't be found."
);
else
handleUser
(
u
);
}
else
if
(
input
==
3
)
{
System
.
out
.
println
(
"Exit"
);
return
;
}
}
}
// User menu
public
static
void
handleUser
(
User
u
)
{
while
(
true
)
{
System
.
out
.
println
(
"\n User Menu Options:"
);
System
.
out
.
println
(
"------------------"
);
System
.
out
.
println
(
"1: View your inbox:"
);
System
.
out
.
println
(
"2: View your outbox:"
);
System
.
out
.
println
(
"3: Send a message:"
);
System
.
out
.
println
(
"4: View specific inbox message"
);
System
.
out
.
println
(
"5: View specific output message"
);
System
.
out
.
println
(
"6: Remove specific inbox message"
);
System
.
out
.
println
(
"7: logout:"
);
String
inputString
=
sc
.
nextLine
();
int
input
=
transform
(
inputString
,
8
);
int
msgn
;
switch
(
input
)
{
case
1
:
u
.
inbox
.
viewMailbox
();
break
;
case
2
:
/* TODO: make according call to view outbox mailbox */
break
;
case
3
:
System
.
out
.
println
(
"Receiver: "
);
String
receiver
=
sc
.
nextLine
();
System
.
out
.
println
(
"Subject: "
);
String
subject
=
sc
.
nextLine
();
System
.
out
.
println
(
"One line msg body: "
);
String
body
=
sc
.
nextLine
();
/* TODO: create message, add it to user's outbox, and if the receiver is also a user with the
* very same email system, then add the messages to the receiver's inbox (if the receiver is not
* a registered user, then the email should still be added to the sender's outbox */
break
;
case
4
:
System
.
out
.
println
(
"Message Number in Inbox:"
);
msgn
=
sc
.
nextInt
();
sc
.
nextLine
();
/* TODO: make the proper call to print the message */
break
;
case
5
:
System
.
out
.
println
(
"Message Number in Outbox"
);
msgn
=
sc
.
nextInt
();
sc
.
nextLine
();
/* TODO: make the proper call to print the message */
break
;
case
6
:
System
.
out
.
println
(
"Message Number in Inbox to be deleted"
);
msgn
=
sc
.
nextInt
();
sc
.
nextLine
();
/* TODO: make the proper call to remove the message from the mailbox */
break
;
case
7
:
return
;
default
:
}
}
}
// just make sure that input has correct format; if not, transform it into the exist number
public
static
int
transform
(
String
inputstring
,
int
other
)
{
if
(
inputstring
.
length
()
!=
1
)
return
other
;
char
c
=
inputstring
.
charAt
(
0
);
if
(
c
>
48
&&
c
<=
48
+
other
)
return
Character
.
getNumericValue
(
c
);
return
other
;
}
}
Java Help/Player.java
Java Help/Player.java
import
java
.
util
.
Scanner
;
public
class
Player
{
private
int
baseBudget
;
private
int
tieBudget
;
private
int
lastBase
=
0
;
private
int
lastTie
=
0
;
private
int
wins
=
0
;
//constructor (always public)
public
Player
(
int
b
,
int
t
)
{
baseBudget
=
b
;
tieBudget
=
t
;
}
//keep track of base and tie values and update base and tie budgets
//make sure that base and tie budgets do not go below zero
// Need help ................................................................................
public
void
playRound
(
int
base
,
int
tie
)
//is at least one of the budgets greater than zero?
public
boolean
canStillPlay
(){
if
(
baseBudget
>
0
||
tieBudget
>
0
)
return
true
;
return
false
;
}
//getter method for base
public
int
getLastBase
()
{
return
lastBase
;
}
//getter method for tie
public
int
getLastTie
()
{
return
lastTie
;
}
//check who has won the last round.... Is this correct?????
public
int
compare
(
Player
P
){
if
(
getLastBase
()
>
P
.
getLastBase
())
System
.
out
.
println
(
"Player 1 wins!"
);
else
if
(
P
.
getLastBase
()
>
getLastBase
())
System
.
out
.
println
(
"Player 2 wins!"
);
else
if
(
getLastBase
()
==
P
.
getLastBase
()){
if
(
getLastTie
>
P
.
getLastTie
)
System
.
out
.
println
(
"Player 1 wins!"
);
else
System
.
out
.
println
(
"Player 2 wins!"
);
}
}
//increase wins property by one
public
void
win
()
{
wins
++
;
}
//return the number of wins (getter method)
public
int
getWins
()
{
return
wins
;
}
//toString method
public
String
toString
()
{
return
"Base budget is "
+
baseBudget
+
"/n Tie Budget is "
+
tieBudget
+
"/n lastBase is "
+
lastBase
+
"/n Last tie is "
+
lastTie
+
"/n Number of wins is "
+
wins
;
}