Consider the following recursive method:
public static void mystery(String str, char a, char b) { if (str == null || str.equals("")) { System.out.println(); return; } char first = str.charAt(0); if (first == a) { System.out.print(b); } else { System.out.print(first); } mystery(str.substring(1), a, b); }
mystery("snip", 'i', 'a')
and determine the
output. 1As you’ve seen in lecture, a blueprint class is a class that defines a new type of object – it specifies the fields and methods that each object of that class will possess. An object built from a blueprint class is known as an instance of that class.
In this exercise, we will complete the implementation of a class for an object that represents a student at a university. Start with this code template.
Fields
Each Student
object already has fields that capture the following state:
Note that these fields are made private.
Methods
Now add the following methods to the Student
class.
A constructor that takes as parameters the student’s first name, last name, and year of graduation.
Accessor methods for retrieving the first name, last name, and year of graduation for each student.
An accessor method for the student’s full name (i.e., the first name, followed by a space, followed by the last name).
A mutator method to change the student’s year of graduation, which should not allow the year of graduation to be moved earlier.
Write these methods so that they prevent “bad” values from getting into the
fields. In particular, the constructor should not allow null
values for the
names, and the mutator should not allow the user to insert an earlier year of
graduation. 3
In this exercise, we will implement a class for an object that represents an
airplane. We have started the Airplane.java
file for you; your
task is to fill out the rest of the class. The blueprint class should have the
following functionality:
Fields
Each Airplane
should have the following fields:
String
)int
)boolean
)int
)Methods
Each Airplane
should have the following methods:
A constructor that takes as parameters the plane’s manufacturer, model number, and the number of miles flown. This constructor should assume that the plane is not currently flying.
An accessor method named getID
that returns the plane’s full
identification (i.e., the manufacturer, followed by a space, followed
by the model number).
An accessor method named isFlying
that returns whether the plane is
currently flying.
An accessor method named getMilesFlown
that returns the number of
miles flown.
A mutator method named changeFlightStatus
that toggles the plane’s flying
status. In other words, if this method is called when the plane is not
flying, it should be set to flying. If this method is called when the plane
is flying, it should be set to not flying.
A mutator method named addMilesFlown
that accepts a single int
parameter and increases the number of miles flown by the given amount.
However, you should not allow the number of miles flown to be
decreased. If the user tries to
call addMilesFlown
with a negative number, an IllegalArgumentException
should be thrown.
Once your Airplane
class is implemented, download
AirportSimulator.java
and
place it in the same folder as Airplane.java
. Compile
AirportSimulator.java
and run it to verify that you correctly
implemented Airplane.java
. 4
Here’s the trace, using indented style:
mystery("snip", 'i', 'a') prints "s" mystery("nip", 'i', 'a') prints "n" mystery("ip", 'i', 'a') prints "a" mystery("p", 'i', 'a') prints "p" mystery("", 'i', 'a') reaches base case, prints newline returns returns returns returns
This method prints the specified string, with all occurences of the
character parameter a
replaced with the parameter b
. ↩
Last updated on July 10, 2025.