我有一部带4名私人成员的电影。我想使用Movie Class对象数组访问main()中的这个私有成员。我在电影课中有4名成员,分别是电影名称,电影导演,电影演员,电影收入。我创建了一个参数化的构造函数,该构造函数具有4个参数和一个to-String方法来显示此类成员的详细信息。现在,我想将电影收入从高到低排序,并根据该收入显示电影名称。
class Movie {
private String movieName;
private String movieDirector;
private String movieActor;
private double movieEarning;
Movie(String movieName, String movieDirector, String movieActor, double movieEarning) {
this.movieName = movieName;
this.movieDirector = movieDirector;
this.movieActor = movieActor;
this.movieEarning = movieEarning;
}
public String toString() {
return "\n" + "Movie Name = " + movieName + "\n" + "Movie Director =" + movieDirector + "\n" + "Movie Actor = " + movieActor + "\n" + "Movie Earning = " + movieEarning;
}
public void movieData() {
//Print movie name where director = Rohit Shetty
if (movieDirector.equals("Rohit Shetty") || movieDirector.equals("rohit shetty"))
System.out.println("Movie Name=" + movieName);
//Print director name who earned between 300 - 500
if (movieEarning > 300 && movieEarning < 500)
System.out.println("Print Director Names who earns more than 300 crores= " + movieDirector);
//print moive details where actor = salman khan && director=kabir khan
if (movieActor.equals("Salman Khan") && movieDirector.equals("kabir khan")) {
System.out.println("Print Movie Names = " + movieName);
System.out.println("Print Movie Earning = " + movieEarning);
}
//print movie name whose actor name starts with s
if (movieActor.startsWith("s"))
System.out.println("Print Actor Name where movie name starts with 's' = " + movieName);
//print movie name with order of highest earning to lowest earning
}
}
public class MovieDemo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String movieName;
String movieDirector;
String movieActor;
double movieEarning;
Movie[] movieObj;
movieObj = new Movie[6];
for (int i = 1; i < movieObj.length; i++) {
System.out.print("Enter " + i + " movie name :");
movieName = sc.next();
sc.nextLine();
System.out.print("Enter " + i + " movie director name :");
movieDirector = sc.next();
sc.nextLine();
System.out.print("Enter " + i + " movie actor: ");
movieActor = sc.next();
sc.nextLine();
System.out.print("Enter " + i + " movie earning: ");
movieEarning = sc.nextDouble();
sc.nextLine();
movieObj[i] = new Movie(movieName, movieDirector, movieActor, movieEarning);
System.out.println(movieObj[i]);
}
for (int i = 1; i < movieObj.length; i++) {
movieObj[i].movieData();
}
}
}
我希望输出为当我六次输入电影收益时,然后我将按照电影最高收益到最低最低收益的顺序显示电影名称。例如,我输入的值为400,500,200,800,100,那么这给了我一份从高到低赚钱的电影名称的列表。
答案 0 :(得分:2)
在这里,我试图演示如何对数组进行排序。我为此使用了Arrays.sort()
。
请注意,Movie
类实现了java.lang.Comparable
接口。因此,Movie
类必须实现我在这里完成的Comparable
的方法compareTo()
。
import java.util.Arrays;
public class SortMovies
{
public static void main(String[] args)
{
Movie[] movies = new Movie[6];
movies[0] = new Movie("Movie 1", "Director 1", "Actor 1", 100.5);
movies[1] = new Movie("Movie 2", "Director 2", "Actor 2", 300.5);
movies[2] = new Movie("Movie 3", "Director 3", "Actor 3", 200.5);
movies[3] = new Movie("Movie 4", "Director 4", "Actor 4", 700.5);
movies[4] = new Movie("Movie 5", "Director 5", "Actor 5", 500.5);
movies[5] = new Movie("Movie 6", "Director 6", "Actor 6", 400.5);
// Assuming that you have built this movies array somehow
// Sort
Arrays.sort(movies);
// Print sorted array
for (Movie m : movies)
{
System.out.println(m);
}
}
}
class Movie implements Comparable<Movie>
{
private String movieName;
private String movieDirector;
private String movieActor;
private double movieEarning;
Movie(String movieName, String movieDirector, String movieActor, double movieEarning)
{
this.movieName = movieName;
this.movieDirector = movieDirector;
this.movieActor = movieActor;
this.movieEarning = movieEarning;
}
@Override
public String toString()
{
return "\n" + "Movie Name = " + movieName + "\n" + "Movie Director =" + movieDirector + "\n" + "Movie Actor = " +
movieActor + "\n" + "Movie Earning = " + movieEarning;
}
@Override
public int compareTo(Movie otherMovie)
{
if (this.movieEarning > otherMovie.movieEarning)
{
return -1;
}
else if (this.movieEarning < otherMovie.movieEarning)
{
return 1;
}
else
{
return 0;
}
}
}