, fn Grades: View x e Eses County Colege x ew.phptid-1234075 , UNITED STATES (EN
ID: 3726686 • Letter: #
Question
, fn Grades: View x e Eses County Colege x ew.phptid-1234075 , UNITED STATES (EN-US) . CSC_225_001 Data Structures Home I My courses 201803 CSC 225 001/March 5-Mach 11/Class Activity 03/08/2018 A Sorted Lisit of Movie Stars Class Activity 03/08/2018 A Sorted List of Movie Stars Class Actvity 030/21B A Soned List of Move Stars wite a program that asks a user to provide his/her 5 tavonte Movie Stars and save them in a SORTED ist and display the total number of Movie Stars with the lst of thee names Display the list one more ime with a copy constructor Submssion Gude Upload a programs) and a screenshot Submission status Supmnsion rading s Tuesday, Mach 13.201 100 PM DELLExplanation / Answer
It is not clearly stated that in which language the solution is needed..
Still I think it is a Java problem and I have implemented it in java .
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class MovieStars implements Comparable<MovieStars> {
private String name;
public static void main(String[] args) {
//create a List of Movie Stars
List<MovieStars> stars=new ArrayList<>();
//read input from user and add it to list
Scanner sc=new Scanner(System.in);
//loop 5 time to create 5 objects
for(int i=0;i<5;i++)
{
System.out.println("Please enter the Movie star name : ");
String s=sc.nextLine();
//create object
MovieStars star=new MovieStars(s);
//add to list
stars.add(star);
}
//print all stars
System.out.println(stars);
//sort based on names
Collections.sort(stars);
//again print the result
System.out.println("Sorted Data based on names is : ");
System.out.println(stars);
}
// default constructor
public MovieStars() {
}
// parameterized constructor
public MovieStars(String name) {
this.name = name;
}
@Override
public int compareTo(MovieStars m) {
// TODO Auto-generated method stub
return this.name.compareTo(m.getName());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MovieStar Name is :"+this.getName();
}
}
Output:
Please enter the Movie star name :
abcccc
Please enter the Movie star name :
xyz
Please enter the Movie star name :
ssss
Please enter the Movie star name :
pppp
Please enter the Movie star name :
ssssfss
[MovieStar Name is :abcccc, MovieStar Name is :xyz, MovieStar Name is :ssss, MovieStar Name is :pppp, MovieStar Name is :ssssfss]
Sorted Data based on names is :
[MovieStar Name is :abcccc, MovieStar Name is :pppp, MovieStar Name is :ssss, MovieStar Name is :ssssfss, MovieStar Name is :xyz]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.