Workbook 7.1
Inside the Java Bootcamp Resources
, launch Workbook 7.1
.
Task 1
Create a new file named Person.java
. Inside the file, create the Person
class
.
public class Person {
}
The Person
class
will define the following fields:
String name;
String nationality;
String dateOfBirth;
String[] passport;
int seatNumber;
The Person
class is a blueprint from which you can create Person
objects.
Task 2
Create one object of the Person
class. Then print every field in the object.
>>: null >>: null >>: null >>: null >>: 0
Task 3
Update each field in the object before printing them.
person.name = // a String person.nationality // a String person.dateOfBirth = // a String person.passport = // Array that stores: {person.name, person.nationality, person.dateOfBirth} person.seatNumber = // an Integer
Hint for passport
You can only use shorthand initialization in the same line as the variable declaration.
That's because the compiler is able to infer the array type.
You cannot use shorthand initialization on another line because the compiler can't infer the type.
You need to explicitly define a new
array that can store String
elements.
Task 4
Print the fields of your updated object.
System.out.println(person.name); System.out.println(person.nationality); System.out.println(person.dateOfBirth); System.out.println(Arrays.toString(person.passport)); System.out.println(person.seatNumber);
Result
>>: Rayan Slim
>>: Canadian
>>: 01/01/1111
>>: [Rayan Slim, Canadian, 01/01/1111]
>>: 5
Visualizing the Runtime
After you solve this workbook, I still recommend watching the video solution on Udemy.
It will show you how to visualize the runtime using Visual Studio Code.