Challenges
Remember the rules for this are
- Try to use only the information given up to this point in this book.
- Try not to give up until you've given it a solid attempt
Challenge 1.
The following classes are named "wrong." Name them correctly.
class gonzo {}
class fozzie_the_bear {}
class MSPIGGY {}
void main() {
IO.println(new gonzo());
IO.println(new fozzie_the_bear());
IO.println(new MSPIGGY());
}
Challenge 2.
Make a variable named movie which is an instance of the Movie class.
Set the value of its title field to Muppets in Space.
class Movie {
String title;
}
void main() {
// ---------------
// CODE HERE
// ---------------
IO.println(
movie.title
);
}
Challenge 3.
Alter the ThemePark class so that the default value
for its entranceFee field is 35.24.
class ThemePark {
double entranceFee;
}
void main() {
ThemePark themePark = new ThemePark();
IO.println(
themePark.entranceFee
);
}
Challenge 4.
Changing only the indicated line,
make it so the word Kermit only appears twice in the
program.
Hint: Remember that inferred types exist.
class Kermit {
boolean angry = true;
}
void main() {
// ------------------------
// CHANGE ONLY THIS LINE v
Kermit kermit = new Kermit();
// ------------------------
IO.println(kermit.angry);
}
Challenge 5.
Make a method named squareRoot which returns an
instance of the SquareRoot class containing both
the positiveRoot and negativeRoot of the given double.
So if you are given 4 you should return a positive root
of 2 and a negative root of -2.
You do not have to account for the possibility of being given a negative
number. You should use Math.sqrt to find the positive root and common
sense to find the negative root.
class SquareRoot {
double positiveRoot;
double negativeRoot;
}
SquareRoot squareRoot(double value) {
// -----------
// CODE HERE
// -----------
}
void main() {
SquareRoot sqrtOfFour = squareRoot(4);
// 2
IO.println(sqrtOfFour.positiveRoot);
// -2
IO.println(sqrtOfFour.negativeRoot);
SquareRoot sqrtOfFifteen = squareRoot(15);
// 3.872983346207417
IO.println(sqrtOfFifteen.positiveRoot);
// -3.872983346207417
IO.println(sqrtOfFifteen.negativeRoot);
}
Challenge 6.
Only writing code between the lines and without directly accessing any fields on or reassigning the actor variable, make the program output Tim Curry.
Hint: The key word is "directly."
class Actor {
String name;
}
void main() {
Actor actor = new Actor();
actor.name = "Frog, Kermit the";
// --------------------------
// CODE HERE
// --------------------------
IO.println(actor.name);
}