Skip to content

Commit

Permalink
Add challenges for instance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bowbahdoe committed Sep 22, 2024
1 parent c3faaf5 commit 679e02c
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions src/instance_methods/challenges.md
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);
}
```

0 comments on commit 679e02c

Please sign in to comment.