Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do I make a method in CoinsMain that takes the ArrayList in as a parameter and returns the total value of the coins. Then

How do I make a method in CoinsMain that takes the ArrayList in as a parameter and returns the total value of the coins.
Then test the method in Main with a println statement.

 

public class Coin {

   double radius;
   public String coinName;
   public String material;
   public int cents;
   
   

   public Coin(String thisCoinName, double thisRadius, String thisMaterial, int thisCents) {

       radius = thisRadius;
       coinName = thisCoinName;
       material = thisMaterial;
       cents = thisCents;

   }

   public double getCircumference() {
       return 2 * 3.14 * radius;
   }

   public double getArea() {
       return 3.14 * radius * radius;
   }
}
 

 

import java.util.ArrayList;

public class CoinMain {

   public static void main(String[] args) {

       Coin coin1 = new Coin("Coin1", 1, "Gold", 200);
       Coin coin2 = new Coin("Coin2", 2, "Silver", 100);
       Coin coin3 = new Coin("Coin3", 3, "copper", 50);
       Coin coin4 = new Coin("Coin4", 4, "tin", 10);

       // Four coins
       //Coin coin1 = new Coin("Coin1", 1);
       //Coin coin2 = new Coin("Coin2", 2);
       //Coin coin3 = new Coin("Coin3", 3);
       //Coin coin4 = new Coin("Coin4", 4);
       //For Coin1
       System.out.println("The surface area of one side of a " + coin1.coinName + " is " + coin1.getArea() + " millimetres squared.");
       System.out.println("A " + coin1.coinName + "'s circumference is " + coin1.getCircumference() + " millimetres.");
       System.out.println("The toonie is made of " + coin1.material + ". It has a value of " + coin1.cents + " cents.");

       System.out.println();
       //For Coin2
       System.out.println("The surface area of one side of a " + coin2.coinName + " is " + coin2.getArea() + " millimetres squared.");
       System.out.println("A " + coin2.coinName + "'s circumference is " + coin2.getCircumference() + " millimetres.");
       System.out.println("The toonie is made of " + coin2.material + ". It has a value of " + coin2.cents + " cents.");

       System.out.println();
       //For Coin3
       System.out.println("The surface area of one side of a " + coin3.coinName + " is " + coin3.getArea() + " millimetres squared.");
       System.out.println("A " + coin3.coinName + "'s circumference is " + coin3.getCircumference() + " millimetres.");
       System.out.println("The toonie is made of " + coin3.material + ". It has a value of " + coin3.cents + " cents.");

       System.out.println();
       //For Coin4
       System.out.println("The surface area of one side of a " + coin4.coinName + " is " + coin4.getArea() + " millimetres squared.");
       System.out.println("A " + coin4.coinName + "'s circumference is " + coin4.getCircumference() + " millimetres.");
       System.out.println("The toonie is made of " + coin4.material + ". It has a value of " + coin4.cents + " cents.");

       System.out.println();

       System.out.println("The total value of all the coins was ");

       ArrayList allCoin = new ArrayList<>();

       allCoin.add(coin1);
       allCoin.add(coin2);
       allCoin.add(coin3);
       allCoin.add(coin4);

       System.out.println();

       //System.out.println("The total value of all the coins was " + ""));
       for (Coin coin : allCoin) {
           System.out.println("The surface area of one side of a " + coin.coinName + " is " + coin.getArea() + " millimetres squared.");
           System.out.println("A " + coin.coinName + "'s circumference is " + coin.getCircumference() + " millimetres.");
           System.out.println("The toonie is made of " + coin.material + ". It has a value of " + coin.cents + " cents.");
           System.out.println();

       }

       System.out.println();

       System.out.println("The total value of all the coins was ");

       /*System.out.println(allCoin.get(0).cents);
       System.out.println(allCoin.get(1).cents);
       System.out.println(allCoin.get(2).cents);
       System.out.println(allCoin.get(3).cents);*/
   }

   public double totalCoins(String Coinlist) {

       return;
   }
}

 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microeconomics An Intuitive Approach with Calculus

Authors: Thomas Nechyba

1st edition

538453257, 978-0538453257

More Books

Students also viewed these Programming questions

Question

56 What is a normal probability plot and how is it used?

Answered: 1 week ago