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

Write a class named Coin. Coins have a year, a face value, a weight, and a state

ID: 664383 • Letter: W

Question

Write a class named Coin. Coins have a year, a face value, a weight, and a state of heads or tails.

A coin can be flipped and a coin can be sold for more than its face value if it is made of precious metal or is rare. Precious metal coins are valued by their weight and the current price of their metal on the world market. Rare coins are categorized by age:

Extremely rare coins are over 1500 years old and sell for their face value plus $1500
Moderately rare coins are between 1500 and 1000 years old and sell for their face value plus $1000
Recently rare coins are between 1000 and 500 years old and sell for their face value plus $500
Maybe rare coins are between 500 and 200 years old and sell for their face value plus $200
All coins less than 200 years old are not rare coins and are not sold. Only precious metal and rare coins can be sold.

Create a class hierarchy with a Coin class, a preciousCoin class, and a rareCoin class. Create a flippable interface so that all the coin objects can be flipped. Create a metal class. Determine the necessary instance variables and methods for the metal class. For this exercise just assume gold and silver as the precious metals involved. Rare coins can also be precious metal coins, in which case, they would sell for their rare calculated value plus the value of the weight of their metal.

Write a test client program to construct coins, flip coins, and sell coins if applicable. Develop sufficient test data to test all cases of all classes and the interface thoroughly.

Explanation / Answer

import java.util.Random; public class Coin { private final int HEADS = 0; private final int TAILS = 1; private int face; float weight; int year; //----------------------------------------------------------------- // Sets up the coin by flipping it initially. //----------------------------------------------------------------- public Coin () { flip(); } //----------------------------------------------------------------- // Flips the coin by randomly choosing a face value. //----------------------------------------------------------------- public void flip () { face = (int) (Math.random() * 2); } //----------------------------------------------------------------- // Returns true if the current face of the coin is heads. //----------------------------------------------------------------- public boolean isHeads () { return (face == HEADS); } //----------------------------------------------------------------- // Returns the current face of the coin as a string. //----------------------------------------------------------------- public String toString() { String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; } public int checkcategory() { int val; if (year>1500) val=face+1500; else if (year>1000) val=face+1000; else if (year>500) val = face+500; else if (year>200) val = face+200; else val = face; return val; } }

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