Solution: Movie list

From the course: Learning Java 8

  • Course details

    Java is one of the top-five programming languages, and is used for websites, embedded controllers, and Android app development. This is an introduction to get you started programming with Java. Peggy Fisher introduces the basics: data types, strings, arrays, expressions, loops, and functions. She'll help you control the flow and logic of your code, and create classes using the principles of object-oriented design. Then go a bit beyond the basics and learn advanced techniques for working with arrays, manipulating files, and building graphical user interfaces (GUIs) that respond to user input. This three-hour capsule course is perfect for developers who need to get up to speed with Java fast, as well as for beginning programmers who want their first taste of this popular language.


    • Click here to view Peggy Fisher’s instructor page

      Peggy Fisher

      Content Strategist, Software Development Languages at Linkedin Learning Solutions with content

      Peggy Fisher is a programmer and content manager at LinkedIn Learning.

      Peggy's main focus is application programming in Java, Arduino, and C++. She has also worked on courses in COBOL and discrete ,athematics. Previously she was a faculty member at Penn State University's College of Information Sciences and Technology. She started out as a programmer working for a large insurance company, but after 18 years she left her job as a director of information technology to pursue her true passion teaching. She earned a master's degree in math education, and went on to teach high school math and computer science in Pennsylvania. In 2012, Peggy accepted a position as an instructional designer at Penn State, and shortly thereafter began teaching Intro to Application Programming with Java. She is a strong supporter of women in STEM. As one of the few female programming teachers, she serves as a mentor to incoming female freshmen who are considering a career in programming. She was also the K–12 outreach coordinator for the college, where she scheduled, ran, and taught summer camps for middle school and high school students. In a PBS NewsHour interview, she expressed that all students should take at least one programming class either in high school or college. Peggy enjoys constantly learning and finding new and exciting ways to bring technology to life in and outside of the classroom, such as using Arduino microcontrollers or Lego Mindstorms, to help make learning hands-on and fun.

    Skills covered in this course

  • Welcome

    - 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.

  • 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.

    Download the exercise files for this course. Get started with a free trial today.

  • Download courses and learn on the go

    Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.

    Watch this course anytime, anywhere. Get started with a free trial today.