I know my code works as i have recieved screenshots of others running it, but i
ID: 3607149 • Letter: I
Question
I know my code works as i have recieved screenshots of others running it, but i am unable to compile it through command prompt for some reason. I am required to use command prompt.
Lab7:
import java.util.*;
import java.io.*;
public class Lab7
{
public static void main(String [] args) throws IOException
{
MovieDB movies = new MovieDB(10); // Create MovieDB object. The
// size is set at 10, meaning it can hold up to 10
// movies. If we wanted (as discussed in lecture) we
// could allow for it to be resized so it could hold
// an arbitrary number of movies.
loadData(movies); // input movie data from file
getCommands(movies); // interact with user
saveData(movies); // save movie data back to file
}
public static void loadData(MovieDB movies) throws IOException
{
// Note the syntax below for creating a Scanner to a file
//Please change path to movieFile
Scanner S = new Scanner(new FileInputStream("movieFile.txt"));
// *** CODE SEGMENT 1 *** //
// Complete this method in the following way:
// Read in the number of movies from the file
// For each movie read the data from the file and create a Movie
// object
// Add the Movie to the MovieDB object (movies) using the appropriate
// method (see MovieDB class)
int numMovies=Integer.parseInt(S.nextLine());
for(int i=0; i<numMovies; i++){
String movieName=S.nextLine();
String director=S.nextLine();
String studio=S.nextLine();
Double gross=Double.parseDouble(S.nextLine());
Movie movie=new Movie(movieName, director, studio, gross);
//created the movie object, now add it to MovieDB object (movies)
movies.addMovie(movie);
}
}
public static void getCommands(MovieDB movies)
{
Scanner inScan = new Scanner(System.in);
System.out.println("Enter your choice:");
System.out.println("1. List movies");
System.out.println("2. Add new movie");
System.out.println("3. Find movie");
System.out.println("4. Quit");
String choice = inScan.nextLine();
while (true)
{
Movie temp;
if (choice.equals("1"))
{
System.out.println(movies.toString());
}
else if (choice.equals("2"))
{
// *** CODE SEGMENT 2 *** //
// Complete this choice in the following way:
// Prompt for and read in each of the values needed
// for the new Movie object (3 strings, 1 double)
// Create a new Movie object and then add it to the
// MovieDB object (movies) using the correct method.
System.out.println("Enter movie name"); //prompting the user to enter value
String movieName=inScan.nextLine();
System.out.println("Enter director name");
String director=inScan.nextLine();
System.out.println("Enter studio name");
String studio=inScan.nextLine();
System.out.println("Enter gross amount for movie");
Double gross=Double.parseDouble(inScan.nextLine());
Movie newMovie=new Movie(movieName, director, studio, gross);
//created the movie object, now add it to MovieDB object (movies)
movies.addMovie(newMovie);
}
else if (choice.equals("3"))
{
// *** CODE SEGMENT 3 *** //
// Complete this choice in the following way:
// Ask the user for the movie name and read it in
// Call the appropriate method in the MovieDB object
// (movies) to find the Movie and return it
// Show the movie's info (or indicate it is not found)
System.out.println("Enter movie name");
String title=inScan.nextLine();
Movie movieFound=movies.findMovie(title);
if(movieFound==null) //movie not found
System.out.println("Movie with this title not found");
else{
System.out.println("Movie information: "+movieFound.toString());
} // print formatted information of movie
}
else
{
System.out.println("Good-bye");
break; // any other value -- quit
}
System.out.println("Enter your choice:");
System.out.println("1. List movies");
System.out.println("2. Add new movie");
System.out.println("3. Find movie");
System.out.println("4. Quit");
choice = inScan.nextLine();
}
}
public static void saveData(MovieDB movies) throws IOException
{
PrintWriter P = new PrintWriter("movieFile.txt");
// Note that we are printing the entire DB to the file with
// one statement. This is because the toStringFile() method
// of the MovieDB class calls the toStringFile() method of
// each Movie within the DB.
P.print(movies.toStringFile());
P.close();
}
}
Movie:
import java.text.*;
import java.util.*;
public class Movie
{
private String title;
private String director;
private String studio;
private double gross;
// Constructor -- take 4 arguments and make a new Movie
public Movie(String t, String d, String s, double g)
{
title = new String(t);
director = new String(d);
studio = new String(s);
gross = g;
}
// Return a formatted string version of this Movie
public String toString()
{
StringBuffer B = new StringBuffer();
B.append("Title: " + title + " ");
B.append("Director: " + director + " ");
B.append("Studio: " + studio + " ");
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);
B.append("Gross: " + formatter.format(gross) + " ");
return B.toString();
}
// Return an unformatted string version of this Movie
public String toStringFile()
{
StringBuffer B = new StringBuffer();
B.append(title + " ");
B.append(director + " ");
B.append(studio + " ");
B.append(gross + " ");
return B.toString();
}
// Accessor to return title of this Movie
public String getTitle()
{
return title;
}
}
MovieDB:
import java.text.*;
import java.util.*;
public class Movie
{
private String title;
private String director;
private String studio;
private double gross;
// Constructor -- take 4 arguments and make a new Movie
public Movie(String t, String d, String s, double g)
{
title = new String(t);
director = new String(d);
studio = new String(s);
gross = g;
}
// Return a formatted string version of this Movie
public String toString()
{
StringBuffer B = new StringBuffer();
B.append("Title: " + title + " ");
B.append("Director: " + director + " ");
B.append("Studio: " + studio + " ");
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);
B.append("Gross: " + formatter.format(gross) + " ");
return B.toString();
}
// Return an unformatted string version of this Movie
public String toStringFile()
{
StringBuffer B = new StringBuffer();
B.append(title + " ");
B.append(director + " ");
B.append(studio + " ");
B.append(gross + " ");
return B.toString();
}
// Accessor to return title of this Movie
public String getTitle()
{
return title;
}
}
movieFile:
3
Star Wars
George Lucas
20th Century Fox
7.98E8
Jaws
Steven Spielberg
Universal
4.706E8
The Return of the King
Peter Jackson
New Line
1.1292E9
My errors:
Explanation / Answer
Hello,
The program is not complete which you have uploaded here.
There are numerous mistakes in the code, so you are getting these errors.
1. The program doesn't have MovieDB class with MovieDB(int) constructor.
MovieDB movies = new MovieDB(10); //No MovieDB class with this constructor type
2. In the method, loadData(MovieDB movies),
movies.addMovie(movie); //where is addMovie(Movie) method defined in MovieDB class, because movies is an object of MovieDB.
3. In the method, getCommands(MovieDB movies),
Movie movieFound=movies.findMovie(title); // where is findMovie() method defined which returns Movie Object.
So, All the errors which are displaying is because of MovieDB.java file.
Please check the code of MovieDB class or upload proper code of MovieDB class to debug it.
Please upvote the answer if you are satisfied. Please upload code of MovieDB class in comment section or edit the question for help. Thanks.