官术网_书友最值得收藏!

Exercises

Now that you understand an object's life cycle, it is time to spend some time in JShell creating new classes and instances.

Exercise 1

  1. Create a new Student class with a constructor that requires two String arguments: firstName and lastName. Use the arguments to initialize fields with the same names as the arguments. Display a message with the values for firstName and lastName when an instance of the class is created.
  2. Create an instance of the Student class and assign it to a variable. Check the messages printed in JShell.
  3. Create an instance of the Student class and assign it to a variable. Check the messages printed in JShell.

Exercise 2

  1. Create a function that receives two String arguments: firstName and lastName. Create an instance of the previously defined Student class with the received arguments as parameters for the creation of the instance. Use the instance properties to print a message with the first name followed by a space and the last name. You will be able to create a method and add it to the Student class later to perform the same task. However, we will learn more about this in the forthcoming chapters.
  2. Call the previously created function with the necessary arguments. Check the message printed in JShell.
主站蜘蛛池模板: 缙云县| 安龙县| 沾益县| 永丰县| 枝江市| 木兰县| 新乐市| 临洮县| 邵阳县| 仪陇县| 汽车| 昌宁县| 苏尼特右旗| 繁昌县| 东丽区| 临湘市| 金塔县| 遂昌县| 华阴市| 县级市| 博兴县| 百色市| 兰坪| 安陆市| 宜州市| 宜宾市| 扎兰屯市| 镇原县| 如皋市| 石河子市| 繁峙县| 兰州市| 房产| 黄大仙区| 萍乡市| 巨野县| 石狮市| 错那县| 灵石县| 临江市| 珲春市|