Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS1027

LAB 8

Computer Science Fundamentals II

Learning Outcomes

•    Implement comparison methods for ordered lists of objects of generic types

•    Translate a sequence of ordering rules into Java code

•    Read user typed input

Pre-Lab

•    Create a new Java project called Lab8

•    Download lab8Files.zip and extract the files in it.

•    Save these files into the Lab8 src folder

Exercise 1 - Ordering a Deck of Cards

1.   Open Card.java and TestCards.java in Eclipse and examine the code in both classes. 2.   Try running the TestCards class. An exception will be thrown. Read the exception

message in the console and click on the line it points to method add() in class ArrayOrderedList.java, to help you understand why this occurred.

3.   Go back into Card.java and modify the class header so that the class implements Comparable<Card>

4.   The class name will now show an error due to it missing the method that are required from the Comparable interface: compareTo()

5.   Hover over the underlined word “Card” in the class declaration to pop-up an error

window and click "Add unimplemented methods" to auto-generate the compareTo() method or just type in the method signature yourself.

6.   Before completing this method, there are a few other helper methods to implement.

7.   Create an equals(Card other) method in class Card.java. This method must return true if both the rank and suit instance variables are the same between this and

other, and false otherwise.

8.   Create two helper methods called getSuitValue() and getRankValue(). Both methods will have int return types. These methods will be used to order the cards  given the specified rules below:

•    getSuitValue() returns the value 1 if suit is “ Diamonds” , the value 2 if suit is “Clubs” , 3 for “ Hearts” , and 4 for “Spades” .

•    getRankValue() returns the value 2 if rank is “2”, 3 if rank is “3”, …, 10 if rank is “10”, 11 if rank is “Jack” , “Queen” , or “ King” and 12 for “Ace” .

9.   Go back to the compareTo() method to fill it in based on the following specifications:

•    Call the equals() method to see if the two cards are the same and return 0 if so.

•    Use the getRankValue() method on both cards and compare them. If they

have different rank values, use this comparison to determine whether the method must return 1 or - 1. If they have the same rank value, proceed to the next option.

•    Use getSuitValue() to compare two cards of the same rank value and determine whether the method must return 1 or - 1.

10. Run TestCards.java to check if your ordering system was implemented correctly. The first example, at the top, should produce the following results:

7 of Diamonds

7 of Diamonds

7 of Hearts

10 of Clubs

King of Spades

Ace of Hearts

11. The results of the second example are found in Output8.txt.

Exercise 2 - Ordering a Contact List

1.   Open Person.java and ContactList.java in Eclipse and examine the code in both classes.

2.   The Person class already implements Comparable<Person> and contains an empty compareTo() method. This program, once it is complete, will provide 3 options for  ordering the contacts: sorting by name, email, or city. We will revisit compareTo() shortly.

3.   In class Person add 3 public getter methods: getName(), getEmail(), and getCity().

4.   In class Person create 3 private helper methods called compareByName(Person

other), compareByEmail(Person other), and compareByCity(Person

other) and all should have int return types: For compareByName the method must return 1 if this person’s name is lexicographically larger than other.name, it must    return 0 if this person’s name is equal to other.name, and it must return - 1 if this person’s name is lexicographically smaller than other.name; the output is similar for  compareByEmail and compareByCity, but using instance variables email and

city, respectively. Hint. Use the String compareTo() within these methods.

5.   Now fill in the Person class compareTo(Person other) using these private helper   methods: If variable ContactList.sortBy is equal to ‘n’ then compare this Person and other by name, if ContactList.sortBy is ‘e’ then compare this Person and   other by email, and it ContactList.sortBy is ‘c’ then compare this Person and   other by city.

6.   Go into ContactList.java. There are two methods that have been left incomplete.

7.   In the main method, first create an object of the class Scanner to read user typed input: Scanner input = new Scanner (System.in);

8.  Add a loop to the main() method that will repeat forever (How is this done?). Each iteration of the loop must print the message “Type ‘n’ to sort by name, ‘e’ to sort by  email, or ‘c’ to sort by city. Type any other letter to quit.” and then it must read the character typed by the user:

char c = input.next().charAt(0);

9.   If the user does not enter ‘n’, ‘e’, or ‘c’, break out of the loop and terminate the program. Otherwise create a new ContactList object passing as parameters to the constructor contacts and c. This will sort the array contacts according to the user typed input.

10. The last part to fill in is the printContacts(ArrayOrderededList<Person> list) method. This method must print the information stored in list. Use

list.size() to get the size of the list and list.get(i) to get the i-th data item stored in list.

11. Run ContactList.java and enter each of the options 'n', 'e', and 'c' and check that

the list is printed correctly and ordered by the corresponding parameter (name, email, or city).

Submission

When you have completed the lab, navigate to the weekly module page on OWL and click the

Lab link (where you found this document). Make sure you are in the page for the correct lab.

Upload the files listed below and remember to hit Save and Submit. Check that your submission went through and look for an automatic OWL email to verify that it was submitted successfully.

Rules

•    Please only submit the files specified below. Do not attach other files even if they were part of the lab.

•    Do not ZIP or use any other form of compressed file for your files. Attach them individually.

•    Submit the lab on time.

•    Forgetting to hit "Submit" is not a valid excuse for submitting late.

•    Submitting the files in an incorrect submission page will receive a penalty.

•    You may re-submit code if your previous submission was not complete or correct, however, re-submissions after the regular lab deadline will receive a penalty.

Files to submit

•    Card.java

•    Person.java

•    ContactList.java