Home > front end >  How can I return true when buying two mixed figures that are the same but have the components in a d
How can I return true when buying two mixed figures that are the same but have the components in a d

Time:03-31

The mixed figures are composed of 4 parts, 2 of land and 2 of water, how can I make a comparator method so that it returns True in case they are figures composed of the same proportions but that are stored in a different order?

here is my uml

package ar.edu.unlp.info.oo2.Ejercicio1;

import java.util.\*;

public class TopografiaMixta extends Topografia {
private Vector\<Topografia\> componentes;

    public TopografiaMixta(Topografia t1, Topografia t2, Topografia t3, Topografia t4) {
        this.componentes = new Vector<Topografia>(4);
        this.componentes.add(t1);
        this.componentes.add(t2);
        this.componentes.add(t3);
        this.componentes.add(t4);
    }
    
    
    @Override
    public double calcularProporcion() {
        return this.componentes.stream().mapToDouble(p -> p.calcularProporcion()).sum() / 2;
    }
    
    @Override
    public boolean comparar(Topografia t1) {
        if(this.componentes.equals(t1)) {
            return true;
        };
        return false;
    }

}

here is my abstract class!


package ar.edu.unlp.info.oo2.Ejercicio1;

public abstract class Topografia {
    public abstract double calcularProporcion();
    public abstract boolean comparar(Topografia t1);
}

CodePudding user response:

First check if t1 is instance of TopografiaMixta, if is return this.calcularProporcion() == t1.calcularProporcion();, if is not return false;.

Or

You can make a loop and count how many proportions of water and how many of land are in each figure. Then compare if the proportions of water and land are equal in both figures.

CodePudding user response:

I think this should suit you for just comparing a TopografiaMixta with a generic Topografia instance.

public class TopografiaMixta extends Topografia {
    private Vector<Topografia> componentes;

    public TopografiaMixta(Topografia t1, Topografia t2, Topografia t3, Topografia t4) {
        this.componentes = new Vector<Topografia>(4);
        this.componentes.add(t1);
        this.componentes.add(t2);
        this.componentes.add(t3);
        this.componentes.add(t4);
    }


    @Override
    public double calcularProporcion() {
        return this.componentes.stream().mapToDouble(p -> p.calcularProporcion()).sum() / 2;
    }

    @Override
    public boolean comparar(Topografia t) {
        if (t == null || getClass() != t.getClass()) {
            return false;
        }

        TopografiaMixta tm = (TopografiaMixta) t;

        //Retrieving a hashmap from the current object with the number of occurrences per Topografia
        Map<String, Integer> map1 = componentes.stream()
                .collect(Collectors.groupingBy(x -> x.getClass().toString(), Collectors.reducing(0, y -> 1, Integer::sum)));

        //Retrieving a hashmap from the parameter with the number of occurrences per Topografia
        Map<String, Integer> map2 = tm.componentes.stream()
                .collect(Collectors.groupingBy(x -> x.getClass().toString(), Collectors.reducing(0, y -> 1, Integer::sum)));

        //Checking if the two objects have the same number of Topografia types
        if (map1.keySet().size() != map2.keySet().size()) {
            return false;
        }

        //Checking if they have the same number of occurrences per Topografia
        for (String key : map1.keySet()) {
            if (!map2.containsKey(key) || map1.get(key) != map2.get(key)) {
                return false;
            }
        }

        return true;
    }
}
  •  Tags:  
  • java
  • Related