Need help in Activity, Java Only Please! Create a class to hold and manipulate d
ID: 3866193 • Letter: N
Question
Need help in Activity, Java Only Please!
Create a class to hold and manipulate data for a Movie. The class will have these fields:
name of movie rating (G, PG, R, not rated) year made gross sales
Write the following methods for the Movie class:
A default (no-args) constructor A constructor with parameter values for all the field A toString( ) method that uses all the fields. An equals( ) method that uses the movie name and the year made. A compareTo( ) method that is based on the gross sales.
Once the Movie class compiles write a Driver class with the main( ) method.
The main( ) method will create an array of four Movie object references. It will then create the Movie objects themselves. Read the data from a text file named “movies.txt”. The file is comma delimited. Use a loop to do this.
After the array has been created use a loop to print the data of each object referred to by the array.
Test the equals( ) method using the second Movie object and the third Movie object. Test the compareTo( ) method by using the first Movie object and the last Movie object. Print the results of these two tests.
____movie.txt file____
Home,PG,2015,173343675
Goodfellas,R,1980,46836214
Inside Out,PG,2015,90401232
Waking Ned Devine,PG,1998,55213567
Explanation / Answer
import java.io.*;
class Movie {
private String name;
private String rating;
private int year;
private long gross_sales;
public Movie(){
name = "";
rating = "";
year=0;
gross_sales = 0;
}
public Movie(String nm, String rat, int yr, long sal){
name = nm;
rating = rat;
year= yr;
gross_sales = sal;
}
public int getYear(){
return year;
}
public long getGrossSales(){
return gross_sales;
}
public String getName(){
return name;
}
public String toString(){
return "The movie name is " + name + " with rating " + rating +
" made in " + String.valueOf(year) + " have gross sales " + String.valueOf(gross_sales);
}
public boolean equals(Movie m){
if (name == m.getName() && year == m.getYear())
return true;
else
return false;
}
public String compareTo(Movie m){
String res="";
if (gross_sales == m.getGrossSales())
res = "Equal";
if (gross_sales > m.getGrossSales())
res = "More";
if (gross_sales < m.getGrossSales())
res = "Less";
return res;
}
}
public class DemoMovie {
public static void main(String[] args){
Movie[] list = new Movie[4];
int count;
String line;
try {
InputStream fis = new FileInputStream("movies.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
count = 0;
while ((line = br.readLine()) != null) {
String[] words = line.split(",");
list[count] = new Movie(words[0], words[1], Integer.parseInt(words[2]), Long.parseLong(words[3]));
count++;
}
if (list[1].equals(list[2]))
System.out.println("They are equal");
else
System.out.println("They are not equal");
if (list[0].compareTo(list[3]) == "More")
System.out.println("It is more");
if (list[0].compareTo(list[3]) == "Equal")
System.out.println("It is equal");
if (list[0].compareTo(list[3]) == "Less")
System.out.println("It is less");
} catch (Exception e){
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.