Question One

advertisement
Tutorial 8 - Answers
Question One
Look at the program below.
a) First, just look at the “mysteryMethod” and try to determine what the mysteryMethod does.
b) Using the array given in the main method, trace each variable of the method to track what the method
actually does. The first iteration through the for loop is done for you. After each iteration through the
loop, show what the array looks like.
c) Show the output that would be seen on the screen and give a description of the method.
b)
Array
12 85 62 3 9 45 32 21 myNums
0
public class Trace {
public static int[] mysteryMethod (int[] myNums) {
int temp = 0;
for (int i = 0; i < myNums.length/2; i++)
{
1
2
3
4
5
6
7
(index)
Do For Loop for myNums.length/2 (8/2 = 4)
i temp myNums[i] myNums[myNums.length-1-i]
0
12
21
12
Array after change
21 85 62 3 9 45 32 12 myNums
temp = myNums[i];
myNums[i] = myNums[myNums.length-1-i];
0
1
2
3
4
5
6
7
(index)
i temp myNums[i] myNums[myNums.length-1-i]
1
85
32
85
myNums[myNums.length-1-i] = temp;
}
21 32 62 3 9 45 85 12 myNums
0
1
2
3
4
5
6
7
(index)
}//end method
i temp myNums[i] myNums[myNums.length-1-i]
2 62
45
62
21 32 45 3 9 62 85 12 myNums
0
1
2
3
4
5
6
7
(index)
i temp myNums[i] myNums[myNums.length-1-i]
3
3
9
3
Array after change
21 32 45 9
0
public static void printArray (int [] myNums)
{
System.out.print (“The array is: “);
for (int i=0; i<myNums.length;i++)
System.out.print(myNums[i] + “ “);
System.out.println();
}//end method
public static void main (String args[])
{
int [] myArray = {12, 85, 62, 3, 9, 45, 32, 21};
printArray(myArray);
int [] newArray = mysteryMethod(myArray);
printArray(newArray);
}//end main
}//end class
1
2
3
3 62 85 12 myNums
4
5
6
7
(index)
c)
Output:
The array is: 12 85 62 3 9 45 32 21
The array is: 21 32 45 9 3 62 85 12
Description of the “mysteryMethod”:
This method takes in an int array as a parameter and
then reverses the order of the array. It returns the
reverse order array.
Question Two
Write programs that have the following static methods. Call these methods in main () method.
a) A method that accepts an integer parameter and returns the sum of the integers from 1 to this
integer.
public class SumNumbers
{
//sum method that returns a int
public static int sum (int num)
{
int sum = 0;
for (int count=1; count <= n; count++)
sum += count;
return sum;
}
//the main method – calls the method sum
public static void main (String args[])
{
int n = 100;
//print out the number returned by the method sum
System.out.println("Sum of 1 to " + n + " is " + sum(n));
}//end main
}//end class
Output:
Sum of 1 to 100 is 5050
b) A method that accepts two floating point parameters (of type double), returns true if the first
parameter is greater than the second, and false otherwise.
public class CheckNumbers
{
//method to check if first double is greater than second double
public static boolean firstLarger (double num1, double num2)
{
return (num1 > num2);
}
//main method
public static void main (String args[])
{
double num1 = 7.1, num2 = 8.24;
System.out.println(num1 + “ is larger than “ + num2 + “? “ + firstLarger (num1, num2) + “.”);
}//end main
}//end class
Output:
7.1 is larger than 8.24? false.
c) A method that accepts a string parameter and a character parameter, and returns the number of
times the character is found in the string. Eg, if the string is "hello" and character is '0', return
value is 1.
public class CharCount
{
//method to printout the number of times a char is found in a given string
public static int countChar (String str, char ch)
{
int count = 0;
//go through each char in the string and count the number of times “ch” exists
for (int index=0; index < str.length(); index++)
if (str.charAt(index) == ch)
count++;
return count;
}
//main method
public static void main (String args[])
{
String myStr = "JAVA"; char myChar = 'A';
System.out.println("There are " + countChar (myStr, myChar) + " '" + myChar + "'s in string \""
+ myStr + "\".");
}//end main
}//end class
Output:
There are 2 'A's in string "JAVA".
d) A method that takes a String and an integer as parameters, and returns a string that is the
parameter string concatenated with itself n number of times (where n is the second parameter).
Eg, if the parameters are "hi" and 4, the return value is "hihihihi".
public class String Output
{
//method to print out a string repeatedly by a given number
public static String multiConcat (String str, int max)
{
String result = "";
for (int count=1; count <= max; count++)
result += str;
return result;
}
OR – use the String concat method:
public static String multiConcat (String str, int max)
{
String result = "";
for (int count=1; count <= max; count++)
result = result.concat(str);
return result;
}
//main method
public static void main (String args[])
{
int myCount = 6;
System.out.println("After " + myCount + " times of concatenation, " + myStr + " becomes "
+ multiConcat(myStr, myCount));
}//end main
}//end class
Output
After 6 times of concatenation, JAVA becomes JAVAJAVAJAVAJAVAJAVAJAVA
Download