The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Please explain how to get the answers to the following PDF document provided
Name _______________________
CMPS 11 Quiz4
This is a closed notes, closed book exam.
1. May 22, 2013 Ok to leave out What does the following program print?
class ClassOne {
String data = "go";
public String get() {
return data;
}
public String mystery() {
return get()+" team";
}
}
class ClassTwo extends ClassOne {
String other = "home";
public String get() {
return other;
}
} class OneTwoTest {
public static void main(String args) {
ClassOne one = new ClassOne();
ClassTwo two = new ClassTwo();
fiddle(one);
fiddle(two);
}
static void fiddle(ClassOne x) {
System.out.println(x.mystery());
}
} __________________________________
__________________________________
2. Write a method, rangeBuilder(), that takes two integer parameters m and n. The method should create an array of
integers with n-m+1 elements. You may assume that n is greater than m. Fill the array with the integer values m,
m+1, m+2, … n and return the filled array. For example if called with rangeBuilder(4,6), the method would return
an array of 3 elements containing 4 at index 0, 5 at index 1, and 6 at index 2. 3. Fill in the blanks so that the method longest() returns the longest word in the array, which is then printed by main.
For example if run with “java CmdArgs one two three four” the program would print “three” which is the longest
word in the arguments passed to the program. (Caution: This is not the exact same problem as done on quiz 4 from
last year which some of you may have studied, although obviously similar.)
class CmdArgs {
public static void main(String args) {
System.out.println(longest(args));
}
static String longest(String words) {
String max = "";
for (int i = 0; i < words.length; i++) {
if (words[i].length() > __________________________) {
_____________________________________________________
}
}
return ________________________________;
}
}
THERE ARE MORE QUESTIONS ON THE BACK 4. Write the Potion class needed by the following program in order to produce the output shown. Note it must work
for other types of Potion as well, such as new Potion("poison", 5). There is no need to check that the
amount of potion is greater than zero.
class PotionTest {
public static void main(String args) {
Potion potion = new Potion("healing", 10);
System.out.println(potion); // prints healing:10
potion.drink(3); // drink the specified amount of the potion
System.out.println(potion); // prints healing:7
}
} 5. What does the following program print?
class TwoD {
public static void main(String args) {
char data = {{'t', 'e', 's', 't'},{'i', 'n', 'g', '!'}};
printMystery(data);
}
static void printMystery(char pic) {
for (int row = pic.length-1; row >= 0; row--) {
for (int col = pic[row].length-1; col >= 0; col--) {
System.out.print(pic[row][col]);
}
System.out.println();
}
}
}
-----------