Question
I am using Java and BlueJ. The program I am writing should be able to do the following: Print your name and lab number Create
I am using Java and BlueJ. The program I am writing should be able to do the following:
Print your name and lab number
Create Purse as myPurse
Load myPurse with the coins in coins.data
Print the contents of myPurse
Print myCoin.maxValue()
Print name and value of max coin
remove a penny
Add two dimes
Print the contents of myPurse
Print program termination message
I am having trouble changing the code so that I can read from a coins.data file instead of doing it all in the programming. Also I am having trouble getting the value of DIME to read 0.10 instead of 0.1
Code I have currently is:
public class Coin { private String name; private double value;
public Coin() { name="PENNY"; value=0.01; }
public Coin(String name, double value) { this.name = name; this.value = value; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getValue() { return value; }
public void setValue(double value) { this.value = value; } @Override public String toString() { return "Coin{" + "name=" + name + ", value=" + value + '}'; }
public class Purse { Coin[] coins; private static final int MAX=10; private int coinCnt=0;
public void Purse() { coins=new Coin[MAX]; } public boolean add(Coin c) { if(coinCnt>=10) return false; else { coins[coinCnt]=c; coinCnt++; return true; } }
public double sum() { double sum=0; for(Coin c:coins){ sum=sum+c.getValue(); } return sum; }
public double maxValue() { double mx=0; for(int i=0;i { if(mx mx=coins[i].getValue(); } return mx; }
public Coin maxCoin() { Coin mx=coins[0]; for(int i=0;i { if(mx.getValue() mx=coins[i]; } return mx; }
public boolean remove(String name) { int ind=-1; for(int i=0;i { if(coins[i].getName().equals(name)) ind=i; } if(ind>=0) { coinCnt--; coins[ind]=coins[coinCnt]; return true; } return false; }
public void print() { for(Coin c:coins) System.out.println(c); } }
public class PurseTester
{ public static void main(String[] args) {
Coin[] coin_data= {new Coin("DIME",0.10), new Coin("NICKEL",0.05), new Coin("PENNY",0.01), new Coin("PENNY",0.01), new Coin("QUARTER",0.25), new Coin("DIME",0.10) };
String name="John Smith"; int lab_number=2; System.out.println(" "+name+" Lab Number"+lab_number);
Purse mypurse=new Purse();
mypurse.Purse(); for(Coin c:coin_data) mypurse.add(c);
mypurse.print(); System.out.println("The max value is :"+mypurse.maxValue());
System.out.println("The max Containing Coin is :"+mypurse.maxCoin()); System.out.println("Removing PENNY :"+mypurse.remove("PENNY"));
mypurse.add(new Coin("DIME",0.10)); mypurse.add(new Coin("DIME",0.10)); mypurse.print(); System.out.println("Program is going to terminate with exit number same as your lab number"); System.exit(lab_number); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started