#How can I compare Type Parameter T2 with T?

47 messages · Page 1 of 1 (latest)

quartz lotus
#

I'm trying to compare this-quantity (T) with another quantity (T2), can someone give me a hint how to solve this?

ionic galleonBOT
#

This post has been reserved for your question.

Hey @quartz lotus! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

glad moss
#

T1 and T2 are different types, so they should never be equals anyways. This breaks all laws.

glad moss
#

I mean they CAN be the same type, but if you're comparing them then they SHOULD be the same type.

#

You can check if they are derrived from the same class and then cast them accordingly if you want

ionic galleonBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

worn moss
#

sure you don't want public Intervall<T> schnitt(Intervall<T> anderes) for the intersection?

#

that would enforce both being the same type

quartz lotus
#

This is the method signature which my professor gave me: public <T2> Intervall<T> schnitt(Intervall<T2> anderes) and I should think of an bound for T2 with which I can solve the task: form an intersection of this and anderes. And I thought about comparing the this-intervall with anderes-Intervall to check if this is within anderes

quartz lotus
worn moss
quartz lotus
worn moss
#

no

#

you need to provide some way of comparing them to each other

#

if you have a common superclass that is comparable, you could just use intervals of that

quartz lotus
#

Ok I’m going to try that I thought I already tried using lower bounds

quartz lotus
#

from my understanding firstly T2 needs to extend comparable, so it is comparable at all. Also T2 has to be a superclass of T in order to make sure that T is comparable with T2

worn moss
#

Can you show the whole class as text?

#

not as an image

quartz lotus
#
public class Intervall<T extends Comparable<T>> {
    private T untereGrenze;
    private T obereGrenze;


    public Intervall(T untereGrenze, T obereGrenze) {
        this.untereGrenze = untereGrenze;
        this.obereGrenze = obereGrenze;
    }

    public boolean isLeer() {
        return untereGrenze == null || obereGrenze == null
                || obereGrenze.compareTo(untereGrenze) < 0 || untereGrenze.compareTo(obereGrenze) > 0;
    }

    public <T1 extends Comparable<? super T>> boolean enthaelt(T1 wert) {
        return (wert.compareTo(untereGrenze) > 0 && wert.compareTo(obereGrenze) < 0
                || wert.compareTo(untereGrenze) == 0 && wert.compareTo(obereGrenze) == 0
                || wert.compareTo(untereGrenze) == 0 && wert.compareTo(obereGrenze) < 0
                || wert.compareTo(obereGrenze) == 0 && wert.compareTo(untereGrenze) > 0);
    }

    

    public <T2 extends Comparable<? super T>> Intervall<T> schnitt(Intervall<T2> anderes) {

        return null;
    }
}
worn moss
#

public <T2 extends Intervall<? super T>> Intervall<T> schnitt(T2 anderes){ should work

#

the issue is that you can't have Intervall<T2> as that would require T2 to extend Comparable<T2>

quartz lotus
#

my professor gave me this method signature: ```java
public <T2> Intervall<T> schnitt(Intervall<T2> anderes)

Think carefully about the bounds for T, T1, and T2 so that the following task is solvable! So i cant change the paramterlist nor the method signature only the bounds
worn moss
#

public <T2 extends Comparable<T2>> Intervall<T> schnitt(Intervall<T2> anderes) { would be possible but that wouldn't allow comparing T1 and T2

#

public <T2 extends Comparable<? extends T>> Intervall<T> schnitt(Intervall<? extends T2> anderes) { maybe?

#

my fault, should be public <T2 extends Comparable<? super T>> Intervall<T> schnitt(Intervall<? extends T2> anderes) { I think

quartz lotus
#

sadly this doesnt work either, maybe my approach with comparing the values is wrong? I really tried everything I could think of to make this work

worn moss
#

that compiles for me

public class Intervall<T extends Comparable<T>> {
    private T untereGrenze;
    private T obereGrenze;


    public Intervall(T untereGrenze, T obereGrenze) {
        this.untereGrenze = untereGrenze;
        this.obereGrenze = obereGrenze;
    }

    public boolean isLeer() {
        return untereGrenze == null || obereGrenze == null
                || obereGrenze.compareTo(untereGrenze) < 0 || untereGrenze.compareTo(obereGrenze) > 0;
    }

    public <T1 extends Comparable<? super T>> boolean enthaelt(T1 wert) {
        return (wert.compareTo(untereGrenze) > 0 && wert.compareTo(obereGrenze) < 0
                || wert.compareTo(untereGrenze) == 0 && wert.compareTo(obereGrenze) == 0
                || wert.compareTo(untereGrenze) == 0 && wert.compareTo(obereGrenze) < 0
                || wert.compareTo(obereGrenze) == 0 && wert.compareTo(untereGrenze) > 0);
    }

    

    public <T2 extends Comparable<? super T>> Intervall<T> schnitt(Intervall<? extends T2> anderes) {
          anderes.untereGrenze.compareTo(untereGrenze);
        return null;
    }
}
quartz lotus
#

it says "Provided:
capture of ? extends T2" "Required type:
T"

worn moss
#

you mean where you call it?

quartz lotus
#

omg wait I compared it the other way around like this: this.untereGrenze.compareTo(anderes.obereGrenze);

#

I think u got it right, it works now. Im writing the method now and test if it works, thank you vm for your help

ionic galleonBOT
worn moss
#

and maybe you need to change the class declaration to public class Intervall<T extends Comparable<? super T>>

quartz lotus
#

so for me to understand it: what we did is: T2 extends Comparable<? super T>> meaning, T2 has to be comparable and must be a Superclass of T. (Intervall <? extends T2> anderes) the paramter anderes must be from type Intervall and a Subclass of T2?

quartz lotus
ionic galleonBOT
worn moss
#
public class Intervall<T extends Comparable<? super T>> {
    private T untereGrenze;
    private T obereGrenze;


    public Intervall(T untereGrenze, T obereGrenze) {
        this.untereGrenze = untereGrenze;
        this.obereGrenze = obereGrenze;
    }

   
    public <T2 extends Comparable<? super T>> Intervall<T> schnitt(Intervall<? extends T2> anderes) {
          anderes.untereGrenze.compareTo(untereGrenze);
        return null;
    }
}
import java.time.*;
import java.time.chrono.*;

Intervall<LocalDate> a = new Intervall<>(LocalDate.now(), LocalDate.now());
Intervall<ThaiBuddhistDate> b = new Intervall<>(ThaiBuddhistDate.now(), ThaiBuddhistDate.now());
a.schnitt(b);

seems to work for me

quartz lotus
#

Yes it has to work, it makes all sense, thank you again! T

ionic galleonBOT
quartz lotus
#

Stay healthy!