3

I have a class that implements the Comparable interface. In this class I need to override compareTo method in order to sort objects by String values.

If you scroll down to the bottom I'm attempting to make my method & I need to in the main method sort an array of Courses (using the compareTo method I'm making) by first Department type and then by course number, etc. but this requires comparing 2 strings.

   import java.io.Serializable;
   import java.io.*;
   import java.util.*;
public class Course implements Comparable<Course>, Serializable  {
private String prefix;
private int number;
private String Department;
private String grade;
/**
 * Constructs the course with the specified information.
 * 
 * @param prefix the prefix of the course designation
 * @param number the number of the course designation
 * @param Department the Department of the course
 * @param grade the grade received for the course
 */
public Course(String prefix, int number, String Department, String grade)
{
    this.prefix = prefix;
    this.number = number;
    this.Department = Department;
    if (grade == null)
        this.grade = "";
    else
        this.grade = grade;
}

/**
 * Constructs the course with the specified information, with no grade
 * established.
 * 
 * @param prefix the prefix of the course designation
 * @param number the number of the course designation
 * @param Department the Department of the course
 */
public Course(String prefix, int number, String Department)
{
    this(prefix, number, Department, "");
}

public String getPrefix()
{
    return prefix;
}

/**
 * Returns the number of the course designation.
 * 
 * @return the number of the course designation
 */
public int getNumber()
{
    return number;
}

/**
 * Returns the Department of this course.
 * 
 * @return the prefix of the course
 */
public String getDepartment()
{
    return Department;
}
/**
 * Returns the grade for this course.
 * 
 * @return the grade for this course
 */
public String getGrade()
{
    return grade;
}

/**
 * Sets the grade for this course to the one specified.
 * 
 * @param grade the new grade for the course
 */
public void setGrade(String grade)
{
    this.grade = grade;
}

/**
 * Returns true if this course has been taken (if a grade has been received).
 * 
 * @return true if this course has been taken and false otherwise
 */
public boolean taken()
{
    return !grade.equals("");
}

 * Determines if this course is equal to the one specified, based on the
 * course designation (prefix and number).
 * 
 * @return true if this course is equal to the parameter
 */
public boolean equals(Object other)
{
    boolean result = false;
    if (other instanceof Course)
    {
        Course otherCourse = (Course) other;
        if (prefix.equals(otherCourse.getPrefix()) &&
                number == otherCourse.getNumber())
            result = true;
    }
    return result;
}

The compareTo function:

public int compareTo(Course o)
{
    if(getDepartment().equals(o.getDepartment()))
    {
        return 0;
    }
    else if()
    {
        return -1;
    }
    else
    {
        return 1;
    } 
}   

/**
 * Creates and returns a string representation of this course.
 * 
 * @return a string representation of the course
 */
public String toString()
{
    String result = prefix + " " + number + ": " + Department;
    if (!grade.equals(""))
        result += "  [" + grade + "]";
    return result;
    }
}

Main class thus far:

import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import java.*;


public class StackCourse 
{

 public static void main(String[] args) 
    {
        Course a = new Course("EEE", 230, "Engineering");
        Course b = new Course("MAT", 150, "Liberal Arts");
        Course c = new Course("PHY", 150, "Liberal Arts");
        Course d = new Course("PHI", 304, "Liberal Arts");
        Course e = new Course("ECN", 214, "W.P. Carey");
        Course f = new Course("EEE", 120, "Engineering");


        Course[] courses = {a,b,c,d,e,f};
        for(int i=0; i<courses.length; i++)
        System.out.println(courses[i].getDepartment());       
    }
}
Adam Staples
  • 396
  • 2
  • 7
  • 19
  • Can you elaborate on your question? – Jake Chasan Jul 07 '14 at 00:33
  • In main method: Course a = new Course("EEE", 230, "Engineering"); Course b = new Course("MAT", 150, "Liberal Arts"); Course c = new Course("PHY", 150, "Liberal Arts"); Course[] courses = {a,b,c}; Now sort courses first by Department type then... – Adam Staples Jul 07 '14 at 00:35
  • Can you add your 'main' method to the question body? – Jake Chasan Jul 07 '14 at 00:36
  • Why are you returning `-1`, `0`, and `1`, when you could just return `getDepartment().compareTo(o.getDepartment())`? – aliteralmind Jul 07 '14 at 00:39
  • @JakeChasan the 'main' method is added. Now I'd like to sort that array using the compareTo method I'm attempting to create. – Adam Staples Jul 07 '14 at 00:42

3 Answers3

2
public int compareTo(Course o)
{
    if(getDepartment().compareTo(o.getDepartment()) ==0){
        if(getNumber() < o.getNumber()) return -1;
        else if(getNumber() > o.getNumber()) return 1;
        else return 0;
    }

    return getDepartment().compareTo(o.getDepartment());

}  

Output:

EEE 120: Engineering EEE 230: Engineering MAT 150: Liberal Arts PHY 150: Liberal Arts PHI 304: Liberal Arts ECN 214: W.P. Carey

Maheedhar Pamuru
  • 139
  • 1
  • 1
  • 7
  • Hmm... which sorting algorithm did you use? I mean without using Arrays.sort or anything like that. That is in the main method. – Adam Staples Jul 07 '14 at 01:04
  • I used Collections.sort(list); // ArrayList – Maheedhar Pamuru Jul 07 '14 at 01:16
  • Only thing that is different is the compareTo method which I posted above... Here is the main method : List list =new ArrayList(); list.add(a); list.add(b); list.add(c); list.add(d); list.add(e); list.add(f); Collections.sort(list); for(int i=0;i – Maheedhar Pamuru Jul 07 '14 at 03:38
1

If I follow your question, I believe you wanted something like this

public int compareTo(Course o)
{
    int a = getDepartment().compareTo(o.getDepartment());
    if(a != 0)
    {
        return a;
    }
    return Integer.valueOf(getNumber()).compareTo(o.getNumber());
}

Note: You can return when the fields you are comparing aren't 0, because those fields aren't equal.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Just use String's built-in compareTo method:

@Override
public int compareTo(Course o) {
    return this.Department.compareTo(o.Department);
}
shmosel
  • 49,289
  • 6
  • 73
  • 138