i was wondering if someone can help me with this problem Design and implement a
ID: 3680622 • Letter: I
Question
i was wondering if someone can help me with this problem
Design and implement a class called Coin that contains instance data for a name, monetary value, and year minted. This class should have: a default constructor one that specifies all three values. Use the same names in your parameters as the instance variables. get and set methods for each variable. The monetary value must be non-negative; if an improper value is received, it should be set to zero. The year minted must be a four digit value in the range 1900 to 2015; if an improper input is received, it should be set to zero. The toString() method should ensure the monetary value is shown as a currency format. If the year minted is zero, then the word "Unknown" should show instead. Implement a test driver that instantiates and outputs one of each of the 6 Canadian coins (penny, nickel, dime, quarter, loonie, toonie) with a year minted equal to the year you first attended TRU. Also instantiate a coin using the default constructor, then use the set methods to attempt the following: set the name to be your first name, set the value to -12.34, set the value to your current GPA, set the year minted to be 1867, set the year minted to be your birth year. After each set, use the matching get to output that value (to show that your class handles the input correctly)Explanation / Answer
Code:(to be copied)
Coin.java:
package coin;
public class Coin {
String name;
double monetaryvalue;
int year;
public Coin() {
}
public Coin(String name,double monetaryvalue,int year)
{
this.name=name;
if(monetaryvalue<0)
{
this.monetaryvalue=0;
}
else
{
this.monetaryvalue = monetaryvalue;
}
if(year>1900 && year<2016)
{
this.year = year;
}
else
{
this.year=0;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMonetaryvalue() {
return monetaryvalue;
}
public void setMonetaryvalue(float monetaryvalue) {
if(monetaryvalue<0)
{
this.monetaryvalue=0;
}
else
{
this.monetaryvalue = monetaryvalue;
}
}
public int getYear() {
return year;
}
public void setYear(int year) {
if(year>1900 && year<2016)
{
this.year = year;
}
else
{
this.year=0;
}
}
public String toString() {
if(this.year==0)
{
return "" + monetaryvalue + " " + this.name + " " + "Minted Year:"+ "Unknown";
}
else
{
return "" + monetaryvalue + " " + this.name + " " + "Minted Year:"+ this.year;
}
}
public static void main(String[] args) {
Coin penny=new Coin("penny",0.01,1988);
System.out.println(penny);
Coin nickel=new Coin("nickel",0.05,1985);
System.out.println(nickel);
Coin dime=new Coin("dime",0.1,1989);
System.out.println(dime);
Coin quarter=new Coin("quarter",0.25,1993);
System.out.println(quarter);
Coin loonie=new Coin("loonie",1,2004);
System.out.println(loonie);
Coin toonie=new Coin("toonie",2,2016);
System.out.println(toonie);
}
}
Output:
0.01 penny Minted Year:1988
0.05 nickel Minted Year:1985
0.1 dime Minted Year:1989
0.25 quarter Minted Year:1993
1.0 loonie Minted Year:2004
2.0 toonie Minted Year:Unknown
Explanation:The above scenario is done based on provided Instructions. Similarly, you can create object using default Constructor & use setters methods to set value & later on print the object using System.out.println.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.