I am using Java and BlueJ. The program I am writing should be able to do the fol
ID: 3749383 • Letter: I
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 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);
}
}
Explanation / Answer
Note: Could u plz tell me if u need any modifications further in this code.I will modify thank u.
_______________
coins.data
DIME 0.10
NICKEL 0.05
PENNY 0.01
PENNY 0.01
QUARTER 0.25
DIME 0.10
______________
Coin.java
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() {
System.out.printf("Name= %s, Value= %.2f",name,value);
//Coin{" + "name=" + name + ", value=" + value + '}
return "";
}
}
________________
Purse.java
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<coinCnt;i++)
{
if(mx<coins[i].getValue())
mx=coins[i].getValue();
}
return mx;
}
public Coin maxCoin()
{
Coin mx=coins[0];
for(int i=0;i<coinCnt;i++)
{
if(mx.getValue()<coins[i].getValue())
mx=coins[i];
}
return mx;
}
public boolean remove(String name)
{
int ind=-1;
for(int i=0;i<coinCnt;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)
for(int i=0;i<coinCnt;i++)
System.out.println(coins[i]);
}
}
________________
PurseTester.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class PurseTester
{
public static void main(String[] args) throws FileNotFoundException {
String cname;
double denom;
Scanner sc=new Scanner(new File("coins.data"));
ArrayList<Coin> coin_data=new ArrayList<Coin>();
while(sc.hasNext())
{
cname=sc.next();
denom=sc.nextDouble();
coin_data.add(new Coin(cname, denom));
}
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);
}
}
___________________
Output:
John Smith Lab Number2
Name= DIME, Value= 0.10
Name= NICKEL, Value= 0.05
Name= PENNY, Value= 0.01
Name= PENNY, Value= 0.01
Name= QUARTER, Value= 0.25
Name= DIME, Value= 0.10
The max value is :0.25
Name= QUARTER, Value= 0.25
The max Containing Coin is :
Removing PENNY :true
Name= DIME, Value= 0.10
Name= NICKEL, Value= 0.05
Name= PENNY, Value= 0.01
Name= DIME, Value= 0.10
Name= QUARTER, Value= 0.25
Name= DIME, Value= 0.10
Name= DIME, Value= 0.10
Program is going to terminate with exit number same as your lab number
_________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.