ArrayList, Collections

0

I have a list of exercises on ArrayList, but only the first question has left me with several doubts:

  • Implement an interface with abstract methods getNome, getValor, which should be implemented in the concrete Currency class. The currency class must have a constructor that initiates the name and value attributes of the currency and, in addition to the interface methods, the set methods. Using ArrayList, implement a coin bank (another class) with the ability to: -receive coins and -calculate the total deposited in the piggy bank.
  • The Cofine class should implement methods for:

    - Count the number of coins stored

    - Count the number of coins of a given value

    - Report which currency is the most valuable

    I've done these classes:

    But I think that maybe the question is badly formulated, if you can help me .. my doubt stays in this part Using ArrayList, implement a coin bank (another class) with the ability to: -receive coins and -calculate the total deposited in the piggy bank.

    package Banco;
    
    public class Moeda implements Interface {    
        private String nome;
        private float valor;
    
        // CONSTRUTOR
    
        public Moeda(String nome, float valor) {
            this.nome = nome;
            this.valor = valor;
        }
    
        public String getNome() {
            return nome;
        }
    
        public void setNome(String nome) {
            this.nome = nome;
        }
    
        public float getValor() {
            return valor;
        }
    
        public void setValor(float valor) {
            this.valor = valor;
        }
    
        @Override
        public void getnome() {
            // TODO Auto-generated method stub    
        }
    
        @Override
        public void getnalor() {
            // TODO Auto-generated method stub    
        }    
    }   
    
    package Banco;
    
    public class Cofrinho  {
        public void recebemoedas(float moeda){ }
        public void moedasnocofre(){}
        public void nmoedas_valor(){}
        public void maiormoeda(){}              
    }
    
    
    package Banco;
    
    import java.util.ArrayList;
    
    public class Array {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            ArrayList<Moeda> a = new ArrayList<Moeda>();
            Cofrinho add = new Cofrinho ();
            Moeda moeda1 = new Moeda ("euro", 5.00f);
            Moeda moeda2 = new Moeda("dolar", 3.00f);
    
            a.add(moeda2);
            a.add(moeda1);
    
            for(int i = 0; i< a.size();i++){
                System.out.println("Moeda : "+a.get(i).getNome());
            }           
        }    
    }
    
        
    asked by anonymous 30.08.2017 / 20:54

    1 answer

    2

    I think your question asks the piggy bank to receive an array of coins, just as it would be in "reality", although it is more correct to receive one coin at a time.

    Change your class Cofrinho to receive ArrayList of Moedas like this:

    public class Cofrinho  {
        ArrayList<Moeda> moedas;
    
        public void recebemoedas(ArrayList<Moeda> moedas){
            this.moedas = moedas;
        }
        public void moedasnocofre(){}
        public void nmoedas_valor(){}
        public void maiormoeda(){}              
    }
    

    Then in the implementation it would look like this:

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
        ArrayList<Moeda> pilhaMoedas = new ArrayList<Moeda>();
        Cofrinho cofre = new Cofrinho();
        Moeda moeda1 = new Moeda("euro", 5.00f);
        Moeda moeda2 = new Moeda("dolar", 3.00f);
    
        pilhaMoedas.add(moeda1);
        pilhaMoedas.add(moeda2);
    
        cofre.recebeMoedas(pilhaMoedas);
    }  
    
        
    30.08.2017 / 21:21