-2

i'm working with BST data structure and i have a class called TimeInterval

private TimeInterval StartTime,EndTime;

public TimeInterval(Time time,Time time2){
    StartTime = (TimeInterval) time; //cannot convert from Time to TimeInterval
    EndTime = (TimeInterval) time2; //cannot convert from Time to TimeInterval
}

@Override
public int compareTo(TimeInterval that) {

    if (StartTime.compareTo(that.EndTime) > 0) {
        return 1;
    }
    if (EndTime.compareTo(that.StartTime) < 0) {
        return -1;
    }
    return 0;
}

What can i do to fix this problem and let the method compareTo work fine? NOTE : i've tried to do this and it didn't work :

private Time StartTime,EndTime;

public TimeInterval(Time time,Time time2){
    StartTime =  time;
    EndTime =  time2;
}

@Override
public int compareTo(TimeInterval that) {

    if (((TimeInterval)StartTime).compareTo((TimeInterval)that.EndTime) > 0) {
        return 1;
    }
    if (((TimeInterval)EndTime).compareTo((TimeInterval)that.StartTime) < 0) {
        return -1;
    }
    return 0;
}
xxMghoL
  • 3
  • 3
  • 1
    Is this TypeScript? Flow? JavaScript doesn't have type declarations. – Pointy Dec 18 '17 at 22:14
  • sorry i'm new but i'm using eclipse – xxMghoL Dec 18 '17 at 22:19
  • Eclipse is your **IDE** (Interactive Development Environment). It's what you use to type in and manipulate your programs. It is **not** the programming language that you're using. It *appears* that you're trying to write Java code. – Pointy Dec 18 '17 at 22:20
  • please tag your question accordingly – derloopkat Dec 18 '17 at 22:21
  • ok sorry i thought that java and javascript are the same – xxMghoL Dec 18 '17 at 22:23
  • It looks like the first problem is that your `StartTime` and `EndTime` fields should be of type `Time`, not `TimeInterval`. (They should also be named `startTime` and `endTime` to meet normal Java naming conventions.) – Jon Skeet Dec 18 '17 at 22:25
  • 1
    Well, a time interval is composed of a start time and an end time. Not of two other time intervals. These casts don't make any sense, StartTime and EndTime should be Times, not TimeIntervals, and should be spelt startTime and endTime. Your compareTo doesn't respect the contract of Comparable: it should be transitive: if A = B and B = C, then A = C. That's not the case with your implementation. – JB Nizet Dec 18 '17 at 22:26
  • https://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java – Moob Dec 18 '17 at 22:27
  • i did all that but compareTo doesn't work it says that i have to cast the startTime and endTime to TimeInterval. then the problem doesn't solved – xxMghoL Dec 18 '17 at 22:31
  • also my class is implements 'Comparable' – xxMghoL Dec 18 '17 at 22:35
  • I think you need to take a step back, and answer a few questions for yourself. What *is* a `TimeInterval`? How is a `TimeInterval` different from a `Time`? What does it mean for one `TimeInterval` to be "greater" than another? (Longer duration? Later starting time? Later ending time? Later starting *and* ending times?) – Ian McLaird Dec 18 '17 at 23:26

1 Answers1

0

You shouldn't need to do any casting, as both StartTime and EndTime have the type Time, and you are comparing the two directly.

private Time StartTime,EndTime;

public TimeInterval(Time time,Time time2){
    StartTime =  time;
    EndTime =  time2;
}

@Override
public int compareTo(TimeInterval that) {

    if (StartTime.compareTo(that.EndTime) > 0) {
        return 1;
    }
    if (EndTime.compareTo(that.StartTime) < 0) {
        return -1;
    }
    return 0;
}

Also, camelCase is recommended for naming your variables.

mckuok
  • 744
  • 6
  • 11
  • i did that and it shows me an error on method compareTo that is inside compareTo(TimeInterval that) and it says "the method cmpareTo(Time) is undefined for the type Time" – xxMghoL Dec 18 '17 at 23:04
  • and the solutions that he gave me is 1- cast the startTime and endTime to TimeInterval 2- create method compareTo(Time) " this is not allowed in the project " – xxMghoL Dec 18 '17 at 23:05
  • which `Time` class are you using? – mckuok Dec 18 '17 at 23:13
  • Time is an interface – xxMghoL Dec 18 '17 at 23:13
  • `public interface Time { int getHH(); int getMM(); int getSS(); int getMS(); void setHH(int hh); void setMM(int mm); void setSS(int ss); void setMS(int ms); }` – xxMghoL Dec 18 '17 at 23:15
  • `Time` should implement `Comparable` too then if it makes sense for `Time` instances to be comparable. – mckuok Dec 18 '17 at 23:18
  • The problem is that `Time` is an interface , i cant let it implements `Comparable ` – xxMghoL Dec 18 '17 at 23:24
  • you can extends `Comparable` – mckuok Dec 18 '17 at 23:28
  • to give you the complete idea of the program , i have 5 interfaces "List, SortedMap, Time, Subtitle, SubtitleSeq" and the important classes are "Time1(that implements Time), Subtitle1(that implements Subtitle), SubtitleSeq1(that implements SubtitleSeq), TimeInterval(that implements Coparable)" – xxMghoL Dec 18 '17 at 23:29
  • this is a project and they gave us the interfaces and we are not allowed to change any thing on them – xxMghoL Dec 18 '17 at 23:31
  • `Time` can extends `Comparable`, thus solving the missing `compareTo` method for `Time`. Ofc you will need to implement the `compareTo` method in `Time1`. Anything other than that is getting pretty fundamental, and some Java tutorial will help, not from StackOverflow. – mckuok Dec 18 '17 at 23:32