Java Program Below is the code for testing a class called Co
Java Program
Below is the code for testing a class called Coin.
Based upon the code and the expected output, create the class Coin.
Test it with this driver.
Your class code should work without modifying the driver.
/***************************************************************
* TestCoin.java
*
* This tests the Coin class
***************************************************************/
public class TestCoin
{
public static void main(String[] args)
{
Coin penny = new Coin(.01, \"penny\");
Coin nickel = new Coin(.05, \"nickel\");
Coin dime = new Coin(.10, \"dime\");
Coin quarter = new Coin(.25, \"quarter\");
System.out.printf(\"The value of \" + penny.getName() +
\" is %2.2f\ \",penny.getValue());
System.out.printf(\"The value of \" + nickel.getName() +
\" is %2.2f\ \", nickel.getValue());
System.out.printf(\"The value of \" + dime.getName() +
\" is %2.2f\ \",dime.getValue());
System.out.printf(\"The value of \" + quarter.getName() +
\" is %2.2f\ \",quarter.getValue());
} // end main
} // end class TestCoin
/******************* Sample Run ***************/
M:\\Java253>java TestCoin
The value of penny is 0.01
The value of nickel is 0.05
The value of dime is 0.10
The value of quarter is 0.25
Solution
TestCoin.java
class Coin
{//class name
double value;//variable declaration
String name;
Coin(double value,String name)
{//constructor with arguments
this.value=value;
this.name=name;
}
public double getValue() {//getter and setter methods
return value;
}
public void setValue(float value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class TestCoin
{//driver name
public static void main(String[] args)
{//main method
Coin penny = new Coin(.01, \"penny\");//calling method
Coin nickel = new Coin(.05, \"nickel\");//calling method
Coin dime = new Coin(.10, \"dime\");//calling method
Coin quarter = new Coin(.25, \"quarter\");//calling method
System.out.printf(\"The value of \" + penny.getName() +
\" is %2.2f\ \",penny.getValue());
System.out.printf(\"The value of \" + nickel.getName() +
\" is %2.2f\ \", nickel.getValue());
System.out.printf(\"The value of \" + dime.getName() +
\" is %2.2f\ \",dime.getValue());
System.out.printf(\"The value of \" + quarter.getName() +
\" is %2.2f\ \",quarter.getValue());
} // end main
} // end class TestCoin
output
The value of penny is 0.01
The value of nickel is 0.05
The value of dime is 0.10
The value of quarter is 0.25

