From the course: Java Essential Training: Syntax and Structure
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Code Solution: Create a car object - Java Tutorial
From the course: Java Essential Training: Syntax and Structure
Code Solution: Create a car object
(upbeat music) - [Instructor] In this challenge, your task is to build a car. You were provided with the car class, which serves as a blueprint containing the attributes of a car. In the buildsCar method, this method accepts details about a specific car, allowing you to construct an instance of the car class. In fact, let's go ahead and do that now. So we create an object of type car, and we can name it Car as well. And to create an instance of the car class, we'll use the new keyword, and we'll call the car constructor, which accepts the make and the model. So now we have a car, and we have the make and the model specified. However, we also need to include the year and the color. We can do that by calling the car setter methods. So we can say car.setYear, and we'll pass in the year, and then car.setColor, and we'll pass in the color. Finally, we'll go ahead and return the car object. And let's go ahead and test this. Great, it worked.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
(Locked)
Defining classes for objects5m 22s
-
(Locked)
Constructors3m 7s
-
(Locked)
Instantiating objects4m
-
(Locked)
Objects as method parameters2m 9s
-
(Locked)
Objects as method return types1m 55s
-
(Locked)
Wrapper classes1m 54s
-
(Locked)
Records1m 51s
-
(Locked)
Code Solution: Create a car object1m 12s
-
(Locked)
-
-
-