4

I may be making this a lot harder than it is...but my assignment was to create a showtimes class that implements Comparable. With this it constructs sort of a list of several different theatres w/ what movies are showing, with the times etc. Then I have to use the compareTo() method to sort it by first the cinema that it's showing in, and by the movie title. I have the following code...the confusion comes when I'm trying to output the sorted results. I have it to where it will output the - or + number from comparing the strings.

public class ShowTimeTest {


public static void main(String[] args) {
    ShowTime[] movieTimes= new ShowTime[20];
    // TODO Auto-generated method stub
    //******************************************************
    //Instantiates 20 showtime objects
    //******************************************************



ShowTime cinemark1 = new ShowTime("Cinemark", "Lorde", "Friday @ 3:30", "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
ShowTime cinemark2 = new ShowTime("Amstar Cinemas", "Star Wars", "Thursday @ 12:00AM", "Monday @ 4:00PM", "Sunday @ 3:45PM","R", 1, 2);
ShowTime cinemark3 = new ShowTime("Fayette Movies", "Pokemon", "Friday @ 3:30", "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
ShowTime cinemark4 = new ShowTime("Dollar Theatre", "Reincarnated", "Friday @ 3:30", "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
ShowTime cinemark5 = new ShowTime("Rad Chads Cinemas", "Lorde", "Friday @        3:30",     "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
movieTimes[0] = cinemark1;
movieTimes[1] = cinemark2;
movieTimes[2] = cinemark3;
movieTimes[3] = cinemark4;
movieTimes[4] = cinemark5;

for(int i = 1; i < 5; i ++){
    System.out.println(movieTimes[i].compareTo(movieTimes[i-1]));

 }
}
}

contd...

public class ShowTime implements Comparable<ShowTime>{

public String name, title, rating, showTime1, showTime2, showTime3;
public double ticket, adultTicket;
public String[] times = new String[3];

public ShowTime(String theatreName, String movieTitle, String showTime1, String showTime2, String showTime3 , String movieRating, double childTicket, double aTicket)
{
    // TODO Auto-generated constructor stub
name = theatreName; 
title = movieTitle;
times[0] = showTime1;
times[1] = showTime2;
times[2] = showTime3;
rating = movieRating;
ticket = childTicket;
adultTicket = aTicket;
}
public String toString(){
    String out = "Theatre: ";
    out += name+ "  " + "Movie: " + title + " " + "Rating " + rating + " " + "ShowTimes " + times[0] + " " + times[1] + " " +  times[2]
            +  " " + "Adult Cost: " + adultTicket + " " + "Child Ticket: " +ticket;
    return out;
}
public int compareTo(ShowTime other) {
    // TODO Auto-generated method stub
    //sorts the showtimes by theater(NAME) and movie(TITLE)
    int result;

    if(name.equals(((ShowTime)other).name)){
        result = title.compareTo(((ShowTime)other).title);
    }

    else{
        result = name.compareTo(((ShowTime)other).name);
    }
    return result;
}
}
4
  • 4
    You're almost done--now just sort! Commented Feb 27, 2014 at 3:15
  • Thanks! it's nice to hear I'm actually doing something right. I'm just so lost when it comes to sorting the darn thing, its like I spent all my brain power doing the above Commented Feb 27, 2014 at 3:20
  • you will get a NullPointerException if you call Arrays.sort(movieTimes); since you initialized only 5 elements but you've allocated 20. Commented Feb 27, 2014 at 3:29
  • Well the assignment was to make 20, I only made 5 to make sure I had everything working appropriately before making all 20 Commented Feb 27, 2014 at 3:34

2 Answers 2

5

The point of making your class implement the Comparable interface was so that you could sort an array of ShowTimes. You've done all the hard work. Now just apply Arrays.sort, sit back, and enjoy the movies.

Since this is an educational show, let me be 100% pellucidly clear: You already have yourself an Object[] whose elements implement Comparable, namely movieTimes. So, all you need to do is add the single line:

Arrays.sort(movieTimes);

Note that this will fail until you fill up your theater with 20 movies (or declare it as a miniplex that holds only 5 movies).

Sign up to request clarification or add additional context in comments.

3 Comments

will we get popcorn, too ?
I was writing an answer but I don't really think anything else needs to be said. Just call sort.
Thanks! That's a real help, I'm struggling to apply it though because there's so many to do with arrays of objects. I tend to make these small things way harder than they are, it just loses me because I have to sort by the cinema (Name) AND the movie, I just tried: sort(movieTimes, 0, 5); at i get syntax error saying sort is undefined
0

You can use the Collections class:

List<ShowTime> list = Arrays.asList(array);
Collections.sort(list);
movieTimes = list.toArray();

2 Comments

He could use better Arrays.sort(array)
Yeah, didn't see that answer at the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.