Read Sections 5.1-5.5 in your textbook and answer the following questions based on your readings and the course lectures.
1. A student wrote a method findMin that returns the minimum of an array of data. Here is the code that the student wrote:
public static double findMin() {
double min = data[0];
for (int i = 1; i <= data.length(); i++) {
if (data[i] < data[i-1]) min = data[i];
}
return min;
}
Identify and correct FOUR errors made by the student in the code above. Explain each error including whether it is a syntax, runtime, or logical error.
2. In addition to being able to write code from a description of a problem, another skill is to be able to understand other people's code. In a short sentence, for each method below, state what the method does, that is, its purpose? Your answer should NOT explain how it is doing what it does. It should state the outcome. For example, for the first question (assuming it has been corrected) your answer would be something like "the method finds the minimum of the all the elements in an array".
For this problem, it is critical that you do not try to run these methods on the computer, nor even hint to your friends what the answer might be. To gain the most benefit of the exercise, you should reason your way through the code and derive the pattern on your own and without using the computer. If after a long time of hard thinking you cannot figure out what the code is doing, as a hint, you may run the code on various input and see if you can find the pattern.
(a)
public int mystery1(String s1, char c){
int n = 0;
for (int index = 0; index < s1.length(); index++){
if (s1.charAt(index) == c) n++;
}
return n;
}
(b)
public static void mystery2(int[] data) {
if (data == null || data.length == 0) {
return;
}
double y = 0;
for (int i = 1; i < data.length; i++) {
y += data[i];
}
y = y / data.length;
for (int i = 1; i < data.length; i++) {
if (data[i] > y) {
System.out.println(data[i]);
}
}
}
(c)
public boolean mystery3(int[] nums, int n) {
boolean flag = true;
int len = nums.length;
if (if n < 0 || n > len) flag = false;
int i = 0;
while( i < n && flag) {
if (nums[i] != nums[len - n + i]) {
flag = false;
}
i++ ;
}
return flag;
}
(d)
public static String mystery4(String s1, String s2) {
String ss = "";
int len1 = s1.length() - 1;
int len2 = s2.length() - 1 ;
int index = 0;
while ( (index < Math.min(len1, len2) )
&& (s1.charAt(index) == s2.charAt(index)) ) {
ss = ss + s1.charAt(index);
index++;
}
return ss;
}
3. Some researchers have been taking daily water samples from the Monongahela River just down stream from a cement plant. They want to know which reading was the most alkaline.
(a) Complete the following static method which returns the index of the maximum pH value in an array of pH samples. If more that one index of the array equals the maximum, return the index of the oldest sample (closest to the beginning of the array).
public static int findMaxIndex(double[] pH) {
int maxIndex = ______; // start with max at the first position
for (int i = 1; i < pH.length; i++) {
if (pH[i] ____ ________________________) {
maxIndex = ___________________;
}
}
if (___________________) return -1;
else return maxIndex;
}
(b) Suppose the water sample readings for the month of June are stored in an double array referenced by variable june. Write a Java code fragment that prints the day that was the most alkaline and the alkalinity for that day.
4.
(a) Consider the method swap that is defined as follows:
public static void swap(char x, char y) {
char temp = x;
x = y;
y = temp;
}
What is the output of the following Java code fragment?
char m = 'M'; char r = 'R'; swap(m, r); System.out.println(m + " " + r);
(b) Consider a second overloaded method swap that is defined as follows:
public static void swap(char[] data) {
char temp = data[0];
data[0] = data[1];
data[1] = temp;
}
What is the output of the following Java code fragment?
char[] charArray = {'M', 'R'};
swap(charArray);
System.out.println(charArray[0] + " " + charArray[1]);
(c) Explain why the answers to parts (a) and (b) are not the same.
5. The static method areEqual returns true if
the two int array parameters have exactly the same values in the same
order, and false if the arrays are different. If the array lengths
are different then return false. For example, for the arrays
{1, 2, 3} and {1, 2, 3} it will return true
{1, 2} and {1, 2, 3} it it will return false
{2, 3, 3} and {3, 3, 3} it will return false
{3, 2, 1, 0} and {3, 4, 1, 0} it will return false
{} and {} it will return true.
Complete this method:
public static boolean areEqual( int[] array1, int[] array2)
In the Homework7 project there are two classes you are to complete. The Hangman class selects the secret word and maintains the status of the game. The HangmanGame contains the main method and interacts with the player, ensuring the player's input is valid and updating the game.
The constructor randomly selects a secret word, then creates and initializes all the arrays. the guess array should contain the characters from the randomly selected work. Both the guess and alphabet arrays should be filled with dashes. It should initialize the number of guesses remaining to 7.
The class contains five other methods you need to complete:
public String toString()
This method returns a string that summarizes the status of the game. The form of the string should be:
You have 5 guesses remaining: --ESS Letters used: A E R SThe letters used should be listed in alphabetical order.
public int getGuessesRemaining()
This method returns the number of guesses the player has remaining.
public boolean isGoodGuess(char letter)
This method returns true if the letter specified in the parameter is in the secret word and false otherwise. It should update the with this letter and as appropriate.
public boolean haWon()
This method returns true if the player has guessed all the letters in the secret word and false if some letters are missing.
public boolean hasUsedBefore(char letter)
This method returns true if the player has used the letter specified in the parameter before, and false if it is a new guess. This method should also update the state so that if the same letter is specified in another call to this method, it will return false. (Implement this method last, after you have implemented and tested the other methods.
Start by creating a Hangman object. Then show the current status of the game and ask the user for the next guess, repeatedly. The user should be able to use either upper or lower case letters. After each guess report whether the guess is good or bad. Stop the game when either the user made 7 bad guesses or guessed all the letters of the secret word. Finally report whether the user won or lose the game.
As an added feature, if the user guesses the same letter more than once, report the error and ask the user for another guess, without penalty.
WELCOME TO THE GAME OF HANGMAN! You have 7 guesses remaining: ------- Letters used: Guess a letter: e Bad guess. You have 6 guesses remaining: ------- Letters used: E Guess a letter: a Good guess! You have 6 guesses remaining: -----A- Letters used: A E Guess a letter: i Bad guess. You have 5 guesses remaining: -----A- Letters used: A E I Guess a letter: o Good guess! You have 5 guesses remaining: --O--A- Letters used: A E I O Guess a letter: r Good guess! You have 5 guesses remaining: -RO-RA- Letters used: A E I O R Guess a letter: t Bad guess. You have 4 guesses remaining: -RO-RA- Letters used: A E I O R T Guess a letter: m Good guess! You have 4 guesses remaining: -RO-RAM Letters used: A E I M O R T Guess a letter: d Bad guess. You have 3 guesses remaining: -RO-RAM Letters used: A D E I M O R T Guess a letter: t You have used T already Guess a letter: P Good guess! You have 3 guesses remaining: PRO-RAM Letters used: A D E I M O P R T Guess a letter: G Good guess! YOU WIN!
Complete and test each step before going on to the next step.
You should be able to write your program without writing statements of the of the form
if (x == true)
return true;
else
return false;
where x is some boolean expression. Simply return x.
You have 4 guesses remaining: -RO-RA- _____ | | | O | \" | | | |_______ Letters used: A E I O R T Guess a letter: m Good guess!