Reputation: 21
I'm trying to compare two cards object and get the biggest one. The Program is made of 2 classes, but I can't figure how to get the biggest value between 2 cards.
Class 1 contains this piece of code:
Pelikortti kortti1 = new Pelikortti("risti",7);
Pelikortti kortti2 = new Pelikortti("ruutu",8);
System.out.println(suurempi(kortti1,kortti2)); //expected: ruutu 8
This line is supposed to print ruutu 8
as it is the card with the biggest value. "suurempi" means "bigger" in my language.
Class 2 looks like this so far:
public class Pelikortti {
private String maa1;
private int arvo1;
public Pelikortti(String maa, int arvo){
if(parametritOK(maa,arvo)){
this.maa1 = maa;
this.arvo1 = arvo;
}else{
this.maa1 = "?";
this.arvo1 = -1;
}
}
public Pelikortti() {
this.maa1 = "?";
this.arvo1 = -1;
}
private static boolean parametritOK(String maa, int arvo){
return (maa.equals("risti")||maa.equals("ruutu")||
maa.equals("pata")||maa.equals("hertta"))
&& arvo > 1 && arvo < 15;
}
public void setKortti(String maa, int arvo){
if(parametritOK(maa,arvo)){
this.maa1 = maa;
this.arvo1 = arvo;
}else{
this.maa1 = "?";
this.arvo1 = -1;
}
}
public String getMaa() {
return maa1;
}
public int getArvo() {
return arvo1;
}
public int suurempi(Pelikortti kortti1, Pelikortti kortti2){
if(kortti1>kortti2){
return kortti1;
}
return kortti2;
}
public String toString(){
return(this.maa1 + " " + this.arvo1);
}
}
The second class has a lot of other methods too as it is a part of another assignment, but the problem here is that I can't get the comparing method to work when called from Class 1.
Upvotes: 2
Views: 123
Reputation: 32
The problem is that you can't directly compare objects as you would integers, and that's why you're getting a compilation error in int suurempi(Pelikortti kortti1, Pelikortti kortti2)
Your goal is to have System.out.println(suurempi(kortti1,kortti2));
to print out ruutu 8
from Class 1.
First, the method int suurempi(Pelikortti kortti1, Pelikortti kortti2)
is defined on Pelikortti
and the call is made from Class 1 without using a Pelikortti
object, so the method won't be used. Also, it returns an integer and not the bigger Pelikortti
.
Then, to keep things simple, let's make this suurempi
method return a Pelikortti
object, so that System.out.println
prints out the biggest card by getting the card object String
representation via its toString()
instance method, overriden from java.lang.Object
.
We need to define the method as follows in Class 1:
public Pelikortti suurempi(Pelikortti kortti1, Pelikortti kortti2) {
return kortti1.getArvo() >= kortti2.getArvo() ? kortti1 : kortti2;
}
This assumes you're calling suurempi
from an instance method of Class 1, if you're calling it from a static method like public void main(String[] args)
, then you need to make suurempi
static too.
The comparison implementation suggested above returns the first Pelikortti
if the cards have the same value (which is how Math.max()
works for example), but you can look into the interfaces java.lang.Comparable
and java.util.Comparator
for standard ways of comparing objects in Java, which allow to have an equal outcome, in case the two cards have the same value.
Upvotes: 0
Reputation: 3691
What you want to compare is the card value. And the value is not the card object itself but the attribute containing the value: arvo1
.
So you need to compare the correct attribute of each object: kortti1.arvo1
and kortti2.arvo2
.
Note that this attribute is private, you won't access it directly from another instance, so you will need to use the getter getArvo()
.
Also, suurempi
is not a static method, it needs to be called from a class instance. So it will need only one parameter (kortti2
) as kortti1
will be the instance the method is called from.
You also need to change the method returned type to Pelikortti
as you want to get the card object and not just it's value (Thanks eltabo for the edit)
public Pelikortti suurempi(Pelikortti kortti2){
if(this.arvo1>kortti2.getArvo()){
return this;
}
return kortti2;
}
The method call:
System.out.println(kortti1.suurempi(kortti2));
EDIT:
If the call to suurempi
has to be from another class (let's call it ClassA
) and follow this syntax:
System.out.println(suurempi(kortti1,kortti2));
That means suurempi
is defined in ClassA
and not in Pelikortti
and you need to move your code.
Then it's very similar excepted you don't use this
but the correct parameter.
public Pelikortti suurempi(Pelikortti kortti1, Pelikortti kortti2){
if(kortti1.getArvo()>kortti2.getArvo()){
return kortti1;
}
return kortti2;
}
Upvotes: 1
Reputation: 1082
That's because you are comparing cards, instead of cards values
public static Pelikortti suurempi(Pelikortti kortti1, Pelikortti kortti2){
if(kortti1.getArvo()>kortti2.getArvo()){
return kortti1;
}
return kortti2;
}
you can then use the function like this:
System.out.println(Pelikortti.suurempi(kortti1,kortti2));
Upvotes: 0