I'm doing small exercise on Java. I need to enter three details of 3 movies(title, genre and rating) and then get them to display.
So basically I have to write appropriate getter and setter methods for each of the private data members and should also write an appropriate constructor the application that include a tester/driver class, which creates three instances of the Movie class and stores them in an array. The tester class should use a for loop to set the title, genre and rating of the Movie objects. Should also use a for loop that should traverse the array and display the title, genre and rating for each element of the array and call the playIt() method for each object.
so far here's my code for movie.class and movieTester.class
public class Movie {
// Declare Private Variables
private String title;
private String genre;
private int rating;
// Default constructor
public Movie() {
}
/// The constructors ///
public Movie(String title,String genre, int rating) {
this.title = title;
this.genre = genre;
this.rating = rating;
}
// Getter and setter methods
public String getTitle() { // getTitle
return title;
}
public void setTitle(String title) { // SetTitle
this.title = title;
}
public String getGenre() { // GetGenre
return genre;
}
public void setGenre(String genre) { // SetGenre
this.genre = genre;
}
public int getRating() { // GetRating
return rating;
}
public void setRating(int rating) { // SetRating
this.rating = rating;
}
public void playIt(){
System.out.println("Playing the Movie");
}
}
And this is for MovieTester.class
import java.util.Scanner;
public class MovieTester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //Create Scanner input
// Populate using an array of Movie's
Movie mymovie[] = new Movie[3];
// Using for loop
for (int i = 0; i < 3; i++) {
Movie movie = new Movie();
// Enter Details For Movie
System.out.println("Please Enter The Title Of Movie: ");
String title = input.nextLine();
movie.setTitle(title);
System.out.println("Please Enter The Genre Of Movie: ");
String genre = input.nextLine();
movie.setGenre(genre);
System.out.println("Please Enter The Rating Of Movie: ");
int ret = Integer.parseInt(input.next());
movie.setRating(ret);
System.out.println();
mymovie[i] = movie;
}
// Using for loop o traverse the.
// array and display the title, genre and rating,
// for each element of the array
for (int i = 0; i < mymovie.length; i++) {
Movie movie = mymovie[i];
System.out.println("Title: " + movie.getTitle());
System.out.println("Genre: " + movie.getGenre());
System.out.println("Rating: " + movie.getRating());
mymovie[i].playIt();
System.out.println();
}
}
}
When I run it this is what I get:
Please Enter The Title Of Movie:
Harold and Kumar
Please Enter The Genre Of Movie:
Comedy
Please Enter The Rating Of Movie:
5
Please Enter The Title Of Movie:
Please Enter The Genre Of Movie:
Comedy
Please Enter The Rating Of Movie:
4
Please Enter The Title Of Movie:
Please Enter The Genre Of Movie:
Action
Please Enter The Rating Of Movie:
3
Title: Harold and Kumar
Genre: Comedy
Rating: 5
Playing the Movie
Title:
Genre: Comedy
Rating: 4
Playing the Movie
Title:
Genre: Action
Rating: 3
Playing the Movie
The issue with the output is that after entering the first full Movie details (title, genre and rating). It skipped entering movie 'title' (Second and Third) and only can enter genre and rating.
Its probably some issue with the 'for loop'. But can't figure it out. Can someone tell me what is wrong with the code?