Assignments Java Chapter 6A

  • Noteson sections 6.0, 6.1, 6.2, 6.3
  • Multiple Choice p. 372-374 #1, 2, 3, 4, 8
  • True/False p. 375 #1 - 7
  • Short Answer p. 375-376 #1, 3, 5, 6 (assume both arrays are the same length)
  • Key Concepts p. 370 (all except last 4)
  • Self Review Questions p. 371 #1-9 (answers on p. 379-380)

#1 – Make a class called Searches that has both linear searching and binary searching on an array of integers and on an array of Comparable objects. Make a class called Sorts that has both selection sorting and insertion sorting on an array of integers and on an array of Comparable objects. Make a new class called Contact which like a contact in a address book, keeps track of a person’s first name, last name, and phone number. Make this class implement the Comparable interface and have a 3 parameter constructor and a toString method. Make another new class called SortSearch that runs both the linear and binary searches on an array of ints, on an array of Strings, and on an array of Contacts. Then make this class run both the selection and insertion sorts on an array of ints, on an array of Strings, and on an array of Contacts.

#2 – Name your class GuessingGame. User tries to guess a number between two boundaries. Make an array to store the user’s guesses. Make a method that is in charge of getting the user’s guess. That method should error trap each guess and only return a valid guess that is within the boundaries. After each valid guess is stored in the array, the user is told whether his guess was too high or too low. The user keeps guessing until he guesses the number correctly or the array is filled with guesses. If the user was successful, congratulate him and print the number of guesses followed by the list of guesses. If the user was not successful, state what the target number was followed bythe list of guesses.

#3 – Name your class Stats. Ask the user how many numbers there will be. Make an error trapping loop to be sure that answer to that question is not a negative value. Create an array of that size. Ask for each entry by number (Enter number 1: Enter number 2: ), don’t error trap, and store each entry in the array. After all numbers are entered, print all the numbers and give statistics. When there is at least one number, give the average, minimum, and maximum. When there are at least three numbers, give those 3 statistics along with an adjusted average. The adjusted average is an average done after dropping the highest and lowest numbers. If no numbers were entered, print a statement to that effect. Use JOptionPane dialog boxes with just one message box at the end that has all information. Round both averages to the nearest hundredth. Make 5 private static methods to calculate the minimum, maximum, sum, average, and adjusted average. No sorting!

#4 – Name your class Graphing. Design and implement an application that reads a set of values in the range of 1 to 100 from the user and creates a chart showing how often the values appeared. The user keeps entering values until 999 is entered to quit the data entries. Error trap their entries to make sure each number is either from 1 to 100 or 999 (great constant!). The chart should look like the one shown below. It shows how many values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered. Use \t to line up the vertical bars. You probably will initially write this program with a lot of ifs to determine the appropriate range. That’s fine – get it working but then see if you can eliminate all the ifs by using mathematics.

1-10|*****

11-20|**

21-30|****************************

31-40|

41-50|***

51-60|********

61-70|**

71-80|*****

81-90|******

91-100|****************

Assignments Java Chapter 6B

  • Notes on sections 6.4, 6.5, 6.6, 6.7
  • Multiple choice p. 372-374 #5, 6, 7, 9, 10
  • True/False p. 375 #8, 9, 10
  • Key Concepts p. 370 (last 4)
  • Self Review Questions p. 371 #10-12 (answers on p. 380)

#1 – Name your class HighScores1. You will be keeping track of the five highest scorers from a computer game. A constant for 5 would be great. Program this using 3 parallel arrays. Make one array an array of Strings for the names. Make one array an array of integers for the scores. Make one array an array of Strings for the dates. Start each array with dummy data that is all the same for all 5 entries. Create an additional insertion sort method in your Sorts class to sort 3 arrays based on the score array. Evaluate whether the most recent player deserves to be in the array. If he does belong, place him in the 3 arrays and then sort.

Now you are going to revise this class. Copy/paste your previous work into a new class called HighScores2. Make a new class called Person that contains the name, score, and date for a single player. Make the default constructor fill the Person with dummy data using the constructor. Make a three parameter constructor, a toString method, a getScore method, and a compareTo method. The HighScores2 class should just have one array of Person objects and function just like the prior lab. No need to touch your Sorts class.

*Homework comparing these 2 approaches

#2 – You need the Card class from chapter 4. Modify the toString method of the Card class to just display the suit and the face. Add 3 public get methods to the Card class.

Make a new class called Player. That classes object’s will each have an integer for the player’s total, an integer for the player’s number of cards, and an array of 5 Card objects. Make a default constructor, a getTotal method, a getNumcards method, a drawNewCard method that will be called each time the player gets another card (card put in array, total added on to, and numcards incremented). Also write a toString method that concatenates a String with the cards and the total.

Make a new class called BlackJack. Test the Player class that you just wrote, by making a Player, printing that Player, getting that Player a new card, and printing the Player again. Now erase the testing code.

The BlackJack class will have an array of Player objects. The dealer is just another Player object. Each of several players and dealer gets 2 unseen cards. Have a loop in the main to do this repeatedly. Then make a method in which a player (refer to player by number) is asked if he wants more cards (as long as he doesn’t have 5 cards yet, doesn’t have 21 points yet, and hasn’t already said ‘stay’). Call this method repeatedly from the main once for each player. Then make a method in which the dealer gets more cards as long as he doesn’t have 5 cards or 17 points yet. Finally make another method that decides if a player (refer to player by number) won or lost. Call this method repeatedly from the main once for each player. For a player to win at BlackJack, he has to beat the dealer. Use JOptionPane dialog boxes. Use constants for 5, 17, and 21. Be sure to just send the minimum information needed to each method.

#3 – Make a class called Student. A student object should store a name, a license plate, the year of graduation, and the assigned parking place. The class needs a constructor, a toString method, and a compareTo method which compares based on the Student’s name.

Make a class called ParkingLot that uses an ArrayList of these students. Present a numerical or character menu in which the user can choose to add a student, see all students, find a student, delete a student, or quit. You should have static methods for the menu, adding, seeing, finding, and deleting. The menu method should error trap so that user can’t enter anything but the 5 options. Make your delete function reuse the find function.

#4 – Make a class called Life that uses a two-dimensional array of chars. A certain char means that someone is alive at that location; a different char means that no-one is alive there. Have constants for these characters and for the array size.

The rules of life are that if you are alive, you stay alive if you have 2 or 3 neighbors. If you have fewer than that many neighbors, you die from loneliness. If you have more than that many neighbors, you die from overcrowding. If you are dead, you get to come to life when you have exactly 3 neighbors. There is a link on the THS website for an excellent Java applet and game rules.

Make a static method that counts the neighbors for a single cell. Make a static method that prints the current grid. Put it in a loop that continues as long as the user wants to continue. Compare your results with the Internet results. The secret of this program is that you have to have two 2-dimensional arrays, one to store the current situation that will be looked at to determine the other 2-dimensional array that has the next generation.

#5 – Copy your code from Buffoxchapter 2. Revise your code and make one method called makeLetter that is passed the appropriate String and returns the chosen character. This method will be used repeatedly to generate each of the letters. Make an array that will hold 100 (constant!) Strings. After the array of Strings is filled with Buffox-like words, sort it and print the list. Limit the number of variables.

Bonus: As each String is made, check to see if it is already in the array. Only put it in if it is unique, so that there will not be any duplication with the 100 Strings.

Double Bonus: Write the list to a text file so that I can be read using Microsoft Word.

Triple Bonus: Randomly generating unique words become more difficult as the number of words grows. So instead, write totally new code (new class) to generate all possible words without doing it randomly.