Join Peggy Fisher for an in-depth discussion in this video Solution: Movie list, part of Learning Java 8.
- Let's talk about one possible solution to this challenge. This challenge was designed to provide you with the opportunity to start object oriented programming. You needed to create a class, instantiate an object of that class, and then print the results. Let's take a look at my solution, but remember that your solution might be very different. I'm gonna start with the movie class file itself. Remember, the class file does not have a main method. So, I have public class Movie. I declared four pieces of instance data.
Two are strings and two are integers. They're all four private because we want to ensure data integrity. We have movie title, rating, year, and run time. I only have one constructor. This is the constructor that we'll use when we wanna create a new movie object. Inside this method, I'm updating the instance data from movie title and setting it to the title that was in the parameter. The rating is getting set to the rating, coming in from the calling program. The year and the run time.
I went ahead and added accessor and mutator methods, just so you can get an idea of what this program would look like if you added that for every piece of data. So, for each piece of data, I have a get and a set. The getMovieTitle returns the movie title. That's why I have string as the return type. Right below it, I have the mutator, which allows me to change the value of the MovieTitle. I have setMovieTitle, which would take in a value from the calling program and allow me to change the title of my movie.
I have getRating and setRating, as well as getYear, setYear, getRuntime and setRuntime. At the very bottom, I have public string toString, which formats the output for a movie in a nice way. So, when I return this information, it'll have labels as well as the data. Now, let's go back over to our movie list and look at the main program. In the main program, I had to import java.util.Scanner on line 5.
Then, starting on line 17, I declare four variables because I need to ask the user what their favorite movie was. So, I still need a variable to temporarily hold the movie title, the rating, the year and the run time. Line 19 shows how I create an object from my movie class. Movie favoriteMovie. Notice, I don't have to instantiate it right away. I do have to instantiate it before I try and use it though. The next few lines ask the user for the information about their favorite movie.
Line 28 is where I'm instantiating my object. I have favoriteMovie = new. Remember that key word, new? = new Movie, the name of the class and in parentheses, I'm passing it the movie, the rating, the year and the runtime. Finally, I have a print line statement where I print out the information about the favorite movie. OK. Let's run this solution. Please enter the movie title. How about Marvel's The Avengers? Click enter.
Now I need the movie rating. It's rated PG-13. Next, the year that it was produced. 2012. And finally, the total runtime. 143 minutes. You can see the program created a object that has the movie title, Marvel's The Avengers. The rating, the year and the total runtime. Remember, classes and objects are the foundation of object oriented programming. So, take a few minutes to practice. Try creating a person class.
Think about what instance data you need and then add a constructor and methods to represent the behaviors. Finally, in the main program, create some person objects. OK, I hope you enjoyed the challenge.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs