forked from Together-Java/ModernJava
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,138 @@ | ||
# 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. | ||
|
||
Make a class named `PirateShip` which has one `int` field | ||
named `crewSize`. | ||
|
||
Add an instance method to that class named `sail` which outputs | ||
|
||
``` | ||
N sailors, ready to sail! | ||
``` | ||
|
||
Where *N* is the size of the crew. | ||
|
||
```java,editable | ||
// ---------------- | ||
// CODE HERE | ||
// ---------------- | ||
void main() { | ||
PirateShip ship = new PirateShip(); | ||
ship.crewSize = 25; | ||
ship.sail(); | ||
} | ||
``` | ||
|
||
## Challenge 2. | ||
|
||
Make a class named `StringArrayView` which has | ||
one `String[]` field named `value` | ||
and two methods named `get` and `length`. | ||
|
||
`get` should take in an index and return the matching element | ||
of the array. | ||
|
||
`length` should take no arguments and give the length of the array. | ||
|
||
```java,editable | ||
// ---------------- | ||
// CODE HERE | ||
// ---------------- | ||
void main() { | ||
StringArrayView view = new StringArrayView(); | ||
view.value = new String[] { "A", "B", "C" }; | ||
// 3 | ||
System.out.println(view.length()); | ||
// A | ||
System.out.println(view.get(0)); | ||
// C | ||
System.out.println(view.get(2)); | ||
} | ||
``` | ||
|
||
## Challenge 3. | ||
|
||
Alter the `VoiceActor` class so that it has a method named `fullName` | ||
that returns their `firstName` followed by their `lastName` and separated | ||
by a space. | ||
|
||
If their `lastName` is `null`, you should have no trailing space. | ||
If their `firstName` is `null`, you should have no leading space. | ||
|
||
If both their `firstName` and `lastName` are `null`, you should | ||
return `"No Name"`. | ||
|
||
```java,editable | ||
class VoiceActor { | ||
String firstName; | ||
String lastName; | ||
// ----------------- | ||
// CODE HERE | ||
// ----------------- | ||
} | ||
void main() { | ||
VoiceActor goku = new VoiceActor(); | ||
goku.firstName = "Masako"; | ||
goku.lastName = "Nozawa"; | ||
// "Masako Nozawa" | ||
String gokuFullName = goku.fullName(); | ||
System.out.println(gokuFullName); | ||
// "Nozawa" | ||
goku.firstName = null; | ||
gokuFullName = goku.fullName(); | ||
System.out.println(gokuFullName); | ||
// "No Name" | ||
goku.lastName = null; | ||
gokuFullName = goku.fullName(); | ||
System.out.println(gokuFullName); | ||
// "Horikawa" | ||
Person vegeta = new VoiceActor(); | ||
vegeta.lastName = "Horikawa"; | ||
System.out.println(vegeta.fullName()); | ||
} | ||
``` | ||
|
||
## Challenge 4. | ||
|
||
Make a `Rectange` class which has a `width` field and a `height` | ||
field. Give it an instance method named `toCharArray` which gives | ||
a `char[]` that can be printed to display a rectangle of the given | ||
width and height. | ||
|
||
```java | ||
// ------------ | ||
// CODE HERE | ||
// ------------ | ||
|
||
void main() { | ||
Rectangle rectangle = new Rectangle(); | ||
rectangle.width = 3; | ||
rectangle.height = 4; | ||
|
||
/* | ||
*** | ||
*** | ||
*** | ||
*** | ||
*/ | ||
char[] c = rectangle.toCharArray(); | ||
System.out.println(c); | ||
} | ||
``` |