Arraylist, Collections

Asked

Viewed 432 times

0

I have an exercise list on Arraylist , but only the first question has left me with several doubts :

  1. Implement an interface with abstract methods getName, getValor, which should be implemented in the concrete class Currency. The currency class must have a constructor that starts 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 Piggy Bank class must implement methods for:

- Count the number of coins stored

- Count the number of coins of a given value

- Provide the largest currency

I’ve done these classes:

But I think that perhaps the question is poorly formulated, if you can help me .. my doubt is 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());
        }           
    }    
}

1 answer

2

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

Change your class Cofrinho to receive a ArrayList of Moedas thus:

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 the implementation 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);
}  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.