Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is for an assignment in my Java class, however, I don\'t seem to understand

ID: 3560917 • Letter: T

Question

This is for an assignment in my Java class, however, I don't seem to understand how it works.

please~~~ help I dont have any idea about that ~~ thanks at all

here is the problem


Consider a class Coin that represents a coin. The class has methods such as getValue, toss, and isHeads. The method getValue returns the value, or denomination, of a coin. The method toss simulates a coin toss in which the coin lands either heads up or tails up. The method isHeads returns true if a coin is heads up.
Suppose that coinList is an ADT list of coins that have randomly selected denominations. Toss each of these coins. If the result of a coin toss is heads, move the coin to a second list called headsList; if it is tails, leave the coin in the original list.
When you are finished tossing coins, compute the total value of the coins that came up heads. Assume that the list headsList has been created for you and is empty initially.

Explanation / Answer

import java.util.ArrayList;
import java.util.Random;


public class Coin {

   private double value;
   private boolean isHeads;
  
   public Coin(double value) {
       this.value = value;
   }
  
   public double getValue() {
       return value;
   }


   public void setValue(double value) {
       this.value = value;
   }


   public boolean isHeads() {
       return isHeads;
   }


   public void setHeads(boolean isHeads) {
       this.isHeads = isHeads;
   }


   public void toss() {
       Random randomGenerator = new Random();
  
       isHeads= randomGenerator.nextBoolean();  

   }
  
   public static void main(String[] a) throws Exception {
      
       ArrayList<Coin> coinList = new ArrayList<Coin>();
       ArrayList<Coin> headsList = new ArrayList<Coin>();
       //load temperary coins
       Random randomGenerator = new Random();
       int Low = 1;
       int High = 10;
       //Increase the number of coins
       for(int i =0; i <10; i++) {
           //Generate coins till 10 : change according to your denominations
          
           Coin coin = new Coin(randomGenerator.nextInt(High-Low) + Low);
           coin.toss();
           coinList.add(coin);
           //If heads
           if(coin.isHeads())
               headsList.add(coin);
       }
      
       //calculate the value of coins in headList
       double totalVal = 0;
       for(Coin coin: coinList) {
           if(coin.isHeads())
               totalVal +=coin.getValue();
           System.out.println("Coin Value: "+coin.getValue()+" Tossed Head: "+(coin.isHeads() ? "Yes" : "No"));
       }
       System.out.println(" No of coins which tossed head: "+ headsList.size()+"   Total value of this coins : "+totalVal);
      
      
   }
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote