0

I'm confused on how I would add the random numbers generated to the array list and then add them up at the end.

ArrayList<Coin> change = new ArrayList<Coin>();

// Generate a random number for how many coins 1-10
ArrayList<Integer> a = new ArrayList<Integer>();
int rand = (int) Math.round(Math.random() * 10 + 1);
a.add(rand);
for(int i=0;i< 0;i++) 
    System.out.println( (int)(Math.random()*10 + 1) + " Coins");


 // Generate a random number 1,2,3,4 to represent which coin
 System.out.println( (int)(Math.random()*4 + 1));

 // Create a new Coin of that type and add it to the list
 change.add(new Quarter());
 System.out.println(change);

 // Evaluate the full worth of the coins in the list
1
  • Linking to an entire project of source code is generally frowned upon here on Stack Overflow. You are expected to put in the effort to write a MCVE. Commented Feb 18, 2022 at 1:34

2 Answers 2

1

You are already populating the List a with random numbers so I'll focus on the ' add them up at the end' part.

Any time you are performing some sort of operation it is a good idea to encapsulate that operation in a separate method. This helps to clarify where responsibilities begin and end. In this case, define a method to calculate the sum of the values in the List. Here's one using a for-each loop.

public int sumValues(List<Integer> list) {
  int sum = 0;
  for (Integer value : list) {
    sum += value;
  }
  return sum;
}

Using the Stream API can simplify this further.

public int sumValues(List<Integer> list) {
  return list.stream().reduce(0, Integer::sum);
}
Sign up to request clarification or add additional context in comments.

Comments

0

The Answer by vsfDawg may be more useful.

Just for fun, here is an alternative code example using type-safe objects defined in an enum, generated and totaled via streams. This code is not an good starting place for a beginning student of Java. But this code can make an interesting contrast. And you could deconstruct the logic of this code to write more conventional Java code as a way to do your schoolwork assignment.

Note that as of Java 16+, enums (and records, and interfaces) can be declared locally.

enum Coin
{
    PENNY( 1 ), NICKEL( 5 ), DIME( 10 ), QUARTER( 25 ), HALF_DOLLAR( 50 ), DOLLAR( 100 );

    // Member fields.
    private int cents;

    // Constructors.
    Coin ( int cents ) { this.cents = cents; }

    // Getters.
    public int getCents ( ) { return cents; }
}

Generate a series of random index numbers (annoying zero-based counting) to access the array of named objects defined in the array.

int size = 10, lowBound = 0, highBound = 6 ;
List < Coin > coins =
        ThreadLocalRandom
                .current()
                .ints( size , lowBound , highBound )
                .mapToObj( i -> Coin.values()[ i ] )
                .toList();

Calculate the total value of the coins.

int total =
        coins
                .stream()
                .mapToInt( coin -> coin.getCents() )
                .sum();

Dump to console.

System.out.println( "coins = " + coins );
System.out.println( "total = " + total );

When run.

coins = [PENNY, DIME, NICKEL, NICKEL, NICKEL, NICKEL, PENNY, DIME, NICKEL, PENNY]

total = 48

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.