首页 > > 详细

ELEC 279程序辅导、辅导java程序、java编程调试解析C/C++编程|辅导R语言编程

ELEC 279 - Winter 2021
Introduction to Object Oriented Programming
Assignment III
Due Date: Friday April 9th, 2021 at 23:59 EST.
Submission Guide
• Add your 8 classes (GuessMaster3.java, Date.java, Person.java, Singer.java, and
Politician.java, Guessable.java, GuessablePerson.java, IncorrectDateException.java)
to a folder named ”FirstName LastName StudentNumber Assignment3” where you
place your first name, last name and student number
• Zip up your folder and submit it under the “Assignment 3” submission on onQ.
• Late submission: If you submit the assignment late, for every day late (to a
maximum of 2 days), you will lose 15% of your assignment marks.
• Add a comment at the beginning of your class (.java) files to include your name and
student number as a proof of authorship.
• All instance variables should be private unless stated otherwise and all
method MUST avoid privacy leaks (unless it is impossible to avoid (i.e.
in generic classes))
• Ensure that your Java source code has good indentation, proper white spaces, and
clear variable names
• Add comments as necessary for important parts of your code to explain what the
code is doing. Commenting is necessary for this assignment as the TAs and
I need to understand what you are doing in your code in order to assign marks as
the assignment can be completed in any number of different ways.
• This is an individual assignment. You are allowed to discuss the assignment
but any form of plagiarism will lead to a score of 0 for your
assignment, in addition to other academic sanctions
Grading
• 90% - Solve the problem as expected
– 35% - Correctly define GuessMaster3.java
– 25% - Correctly define GuessiblePerson.java
– 10% - Correctly define Date.java
– 10% - Correctly define Person.java , Singer.java, Politician.java
– 10% - Correctly define Guessible.java, IncorrectDateException.java
• 10% - Programming style and documentation (indentation/naming/comments/clarity)
Page 1 of 8 – ELEC 279 - Assignment 2
Problem: GuessMaster Version 3.0
Assignment 3 is based on Assignment 2. I am working with the teaching assistant’s
to get your assignments graded as fast as possible so that you can either have it at the
start of this assignment to continue using your code, or you can start with your code and
then implement any relevant feedback from assignment 2 once the grades are released.If
you are not confident in your solution to Assignment 2 you can use the starting code I
have provided on OnQ.
This new version of the game, GuessMaster 3.0, is played with the same rules as in
assignment I and II. However, you will need to restructure the classes you wrote in assignment
II to utilize sets, nested classes, generic classes, interfaces, iterators and exception
handling. These concepts will help clean up and shorten some parts of your code, while
allowing for better encapsulation within your program. However, the addition of some
of these concepts will make the program less efficient and reduce functionality so do not
consider this assignment the most optimal or efficient way to solve the problem, just the
one that includes the most course concepts.
A Recap of Assignment II
In assignment II, you designed a simple game that allows users to guess the birthday of
a person. It requires a player to guess the correct birthday stored secretly in the computer
program (e.g. Justin Trudeau was born on December 25, 1971). To start, the program
welcomes the player and informs them whose birthday to guess. More specifically, from
an array of instance variables representing a person, the program randomly selected one
of those people and prints the name on the screen and ask the user to guess the birth
date. After reading the inputted guess from the user from the console, the program responds
with useful information if the guess was incorrect such as: “Incorrect. Try an
earlier day.” or “Incorrect. Try a later month.”. If the guess was correct the system
displayed “CORRECT. You got it!” and then awarded the user a number of points based
on the difficulty of the person whose birthday was guessed.. The user can type “quit”
to exit the game. We will be expanding (and reducing) this functionality in Assignment 3.
Assignment III Instructions
• Let’s start off by modifying the Date class to use the better clone method, and
implement the Comparable and Cloneable interfaces.
(i) Remove the copy constructor. We will be modifying every clone method in this
project to use the better clone method rendering the copy constructor obsolete.
Let’s remove it to reduce a couple lines of code and be more efficient.
(ii) Add to your class declaration that this class now implements the Comparable
interface for Date type objects, and the Cloneable interface.
(iii) replace the clone method in the class which uses the now removed copy constructor
with the methodology we discussed in lecture where we use the parent
classes clone method.
Page 2 of 8 – ELEC 279 - Assignment 2
(iv) Around the code in your clone method put a try block.
(v) After the try block include the following:
catch(CloneNotSupportedException e)
return null;
This will catch the CloneNotSupportedExceptions and avoid needing to include
throws clauses throughout your project. As discussed in lecture it is
always good practice to catch or declare an exception so we are just getting it
out of the way here.
(vi) Now finish implementing the Comparable interface by include the full definition
for the compareTo method. This method will replace most of the code/
functionality of guessing the birthday in the GuessMaster class.
(vii) The comparable method should be implemented such that -1 is returned if the
calling object is earlier than the parameter passed to the function, and 1 if it
is later. A value of 0 should be returned if the dates are equal.
(viii) With this change fresh in your mind go into your GuessMaster3 class (previously
GuessMaster2) and change your code where you compare the user’s
guess to the birthday of the person to utilize the compareTo method you just
created. This will greatly reduce the length of your code. As we are no longer
checking the day, month, and year individually for this assignment you only
need to output to the user ”Incorrect. Try a earlier date.” or ”Incorrect. Try
a later date.” if their guess is incorrect. The functionality for a correct guess
will not change.
(ix) Define a private static nested class within the Date class called Months
(x) Create private static method called getDays which returns an int type value,
and takes in an int parameter called month. Inside this method, utilize a
switch statement to return a value equal to the number of days in numeric
value of the month that was passed in as a parameter (ex. 1 is January which
has 31 days). As you go to make this switch case statement think about how
you can efficiently return the number of days using a default case.
(xi) Now we are going to modify the constructor for the Date class that takes in a
string parameter (the input from the user) to throw exceptions if the string is
in an incorrect format, or the values of the day, month and year are impossible.
(xii) More specifically, create code to check if the string being entered is too long, or
too short, or if the day, month, or year is less than 1, or if the month is greater
than 12, or the day is not possible based on the given month. This method
should utilize the getDays method you created in the nested Month class.
(xiii) If the string is incorrect, throw a new exception of the type IncorrectDateException,
that takes in a single parameter, the string passed into the constructor.
If the string is correct, then initialize the instance variables of the object as
was done in assignment’s 1 and 2.
• Now we need to create the IncorrectDateException class, and define it to give us an
informative message when the exception is caught.
Page 3 of 8 – ELEC 279 - Assignment 2
(i) Define a new exception called IncorrectDateException which is derived from
the Exception class.
(ii) This class needs be able to create an object with the message ”The date you
have entered, INSERTSTRINGHERE is not a valid input”, where you insert
the parameter dateString.
• Modify the Person abstract class to remove most of the methods and functionality
used for the game by guessing the person birthday. This will allow for the Person
abstract class to be used to create people that are not used for the guessing game.
This better encapsulates our guessing functionality while allowing for our code to be
reused and re-purposed for other needs by only giving the programmer/ user access
to the relevant methods (which is also part of the concept of abstraction).
(i) Remove the difficulty instance variable, and any parts of any method that refer
to it.
(ii) Remove the getAwardedPointNumber, startMessage, and closingMessage methods.
(iii) Have the person class implement the Cloneable interface.
(iv) Define (or redefine) the clone method in this class using the ”better” clone
method discussed in lecture (and implemented earlier in this assignment).
(v) Around the code in your clone method put a try block.
(vi) After the try block include the following:
catch(CloneNotSupportedException e)
return null;
This will catch all of the CloneNotSupportedExceptions that you will be throwing
later in this assignment.
(vii) Remove the copy constructor from this class.
(viii) Search your Person class for anytime you utilize the Date copy constructor and
replace its use with the clone method of the Date class we defined earlier. (You
will likely need to add in some throws declarations)
• Create a Guessable interface. This interface will contain the method headings we
will implement in any class derived from the Person abstract class that we want to
be able to use in our guessing game (which in this assignment is all of them).
(i) Use your IDE to create a new interface called Guessable or created a new class
and change the class definition to be that of an interface.
(ii) Have the Guessable interface be derived from the Comparable interface such
that we can compare Person type objects.
(iii) Define a method called startMessage that takes in no parameters and returns
a String.
(iv) Define a method called closingMessage that takes in no parameters and returns
a String.
Page 4 of 8 – ELEC 279 - Assignment 2
(v) Define a method called getAwardedPointNumber that takes in no parameters
and returns an int.
• Now let’s bring the Person class and the Guessable interface together to create a
new class to use for classes we want to use in our guessing game.
(i) Create new generic class called GuessablePerson.
(ii) Put a bound on the type parameter accepted by this class such that only classes
which are derived from the Person class will be accepted.
(iii) Have this class implement the Guessable and Cloneable interfaces. (Note that
since the Guessable interface is derived from the Comparable interface this
class also essentially implements the Comparable interface).
(iv) Define a type parameter instance variable called person and a double called
difficulty.
(v) Create a constructor for the class that takes in a type parameter variable and a
double variable and initialize the instance variables to those parameters values.
Notice here you are unable to avoid a privacy leak.
(vi) Define a compareTo method such that the calling GuessablePerson object
comes ”before” the parameter if they are younger. If the two are the same age,
then the one that comes before is the one whose name comes first alphabetically.
Think critically on how to do this the most efficiently.
(vii) Include the following methods from Assignment II in this class to allow for this
class to be used with our game:
public String startMessage()
return ”Guess the birthday of the ”+person.personType()+” ”+person.getName();
public String closingMessage()
return ”Congratulations! You were correct, ”+person.toString();
public int getAwardedPointNumber()
double points = this.difficulty*100;
return (int) points;
(viii) Create a clone method for this class using the ”better” clone method.
Page 5 of 8 – ELEC 279 - Assignment 2
• Modify the Singer and Politician Classes to fit with the new changes to the code.
(i) Remove any reference to the difficulty variable that used to be in the Person
class.
(ii) Remove any use of copy constructors, and the use of copy constructors in the
classes to instead use that class’ clone method.
(iii) Redefine the clone methods to use the ”better” clone methods. Be sure to
avoid privacy leaks.
• Time to bring all this work together, with a few more course concepts in the GuessMaster3
class. Remember you will either need to rename the class and file from
GuessMaster2 to GuessMaster3, or create a new class and copy over the contents.
The latter option may be more beneficial to you in completing the lab as you can
drag over parts of the code one after another to slowly add in the new functionality.
(i) For our guessing game we now want to use a collection class to store the people
we are guessing. As we do not want the elements in the collection to occur
more than once, we will change the people instance variable from an Array,
to an HashSet. The HashSet should be of GuessablePerson objects where the
type parameter can be anything.
(ii) Create/ edit the default (no-argument) constructor for the GuessMaster3 class
such that it initializes the HashSet to a size of 0, and initializes the variable
numPoints to 0.
(iii) Modify the addPerson method to be a generic method that has a type parameter
M. This type parameter should be bound such that only reference type
objects derived from the Person class can be used.
(iv) The method will be used to add a Person type object to the game with a given
difficulty. The method returns nothing, and takes in 2 parameters, an M type
variable called person, and a double called difficulty.
(v) The method should create a new GuessablePerson object from the person parameter
while avoiding a privacy leak. This object should then be added
to the HashSet called people.
(vi) Create a method called getPeople that takes in no parameters and returns a
HashSet of GuessablePerson objects with any type parameter. The method
should return the HashSet called people.
(vii) Download the ”test.txt” file from OnQ and add it to the src folder of your
project.
(viii) In this version of the game, to demonstrate another method for system input
and to allow you to better test your assignment the Scanner object should be
changed to utilize a FileInputStream instead of System.in. For this file you
need to input the full path (location on your computer) to the ”test.txt” file.
(ix) to avoid having the startGame method throw an error you will need to put a
try block around your FileInput code to catch FileNotFoundExceptions.
Page 6 of 8 – ELEC 279 - Assignment 2
(x) Remove the random number generation functionality from your code. The
person to guess will no longer be selected based on a random number.
(xi) Instead, create an Iterator of GuessablePerson objects, with any type parameter
that is derived from person, from the people Hashset. Call this iterator
peopleIterator.
(xii) Modify your code now to accommodate the current person being guessed being
selected by using this iterator.
(xiii) You will need to adjust your while loops that end the program based on whether
there is more input to be read from the txt file, and whether there are more
people to iterate through. If there is no more input in the file, or if every
person has been guessed the game should end (in addition to the functionality
to end the game if the user types ”quit”).
(xiv) Where you get the user’s input and convert it to a Date object and then
compare the date to see if the guess is correct you will now need to utilize
exception handling of the IncorrectDateException we defined earlier to check
if the user has given an invalid input.
(xv) Think critically on where the try block should be placed so that the game
continues to run (and allow more guesses/input) after an incorrect date string
is given.
(xvi) The IncorrectDateException should be caught and utilized to display the
exception message to the user.
(xvii) Whether or not an error is thrown or caught your code should display ”If
you would like to stop, type ”quit” to exit”.
Page 7 of 8 – ELEC 279 - Assignment 2
Test Code
We will use the following code to test assignment III in combination using the
”test.txt” file for the system input that has been posted alongside this assignment
on OnQ:
Politician trudeau = new Politician(”Justin Trudeau”, new Date(25, 12, 1971), ”Liberal”);
Singer dion = new Singer(”Celine Dion”, new Date(30, 3, 1961), ”La voix du bon
Dieu”, new Date(6, 11, 1981));
Singer robertson = new Singer(”Ed Robertson”, new Date(25, 10, 1970), ” Gordon”,
new Date(7, 28, 1992));
GuessMaster3 gm = new GuessMaster3();
gm.addPerson(trudeau, 0.25);
gm.addPerson(dion, 0.5);
gm.addPerson(robertson, 0.75);
System.out.println(”There are ”+ gm.getPeople().size()+” people in the game”);
GuessablePerson guessableTrudeau = new GuessablePerson<>(trudeau,
0.25);
System.out.println(”The next output should be -1”);
System.out.println(guessableTrudeau.compareTo(dion));
gm.startGame();
With that, you are now done with the GuessMaster game! Congratulations on all your
hard work on these assignments. I hope you enjoyed getting to see how the concepts we
have learned in the course can be used together, and how each concept built up to the
next allowing us to make more efficient code, with more functionality.
Page 8 of 8 – ELEC 279 - Assignment 2

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!