For this problem, submit an Eclipse project file named CoinStoreYourLastName wit
ID: 3795149 • Letter: F
Question
For this problem, submit an Eclipse project file named CoinStoreYourLastName with your corrected source code for the classes Coin and Main1, and the new class Main2 that you write in Problem 3.
. Find the errors in the Coin and Main1 classes. Submit a list of all the errors and their corrections in a Word text file.
coin class
package coinstore;
Main.Java
For the Coin class, write getter and setter methods for the _price instance variable. Include these methods in your project file.
Write a second class named Main2 with static methods main, getOnSaleCount, getTotalPrice methods:
The main method should:
read comma delimited data lines from the input file coins.txt,
coin.txt
use the String method split to split the line into fields,
use the values of the fields to create a Coin object,
insert the Coin object into an ArrayList collection defined by
ArrayList<Coin> col = new ArrayList<Coin>( );
The getOnSaleCount method should have this header:
It should use a for loop to look at all of the Coin objects in the collection and count the number of coins that are on sale and return that count. To test this method in Main2, here is how you can set the coins with indices 2 and 4 in the collection to be on sale:
Include a statement or statement that tests the getOnSaleCount method in Main2.
The getTotalPrice method should have this header:
It should use a loop to find the total of the prices of all Coin objects in the ArrayList collection and return that total. Include a statement or statement that tests the getOnSaleCount method in Main2.
Explanation / Answer
//The class represents the object of coin class
//Coin.java
package coinstore;
public class Coin {
private double _denomination;
private int _year;
private char _mintLocation;
private String _condition;
private double _price;
private boolean _onSale;
// Constructor
public Coin(double theDenom, int theYear, char theMint,
String theCondition, double thePrice) {
// Face value of the coin, for example
// Quarter = 0.25, Dime = 0.10.
_denomination = theDenom;
// Year that the coin was minted.
_year = theYear;
// Location coin was minted. Values are
// 'D'=Denver, 'S'=San Francisco,
_mintLocation = theMint;
_price = thePrice;
// Condition of the coin. Values are
// "XF"=Extra Fine, "VF"=Very Fine, "F"=Fine,
// "VG"=Very Good, "G"=Good, "P"=Poor.
_condition = theCondition;
// Is the coin on sale?
_onSale = false;
}
//Set price
public void setPrice(double thePrice){
this._price=thePrice;
}
//Return price
public double getPrice(){
return _price;
}
//Return boolean value _onSale
public boolean isOnSale() {
return _onSale;
}
public void setOnSale(boolean onSale) {
_onSale = onSale;
}
@Override
public String toString( ) {
return String.format("Denomination: %s/n" +
"Year: %d Mint Location: %s " +
"Condition: #s Price: $%6.2f ",
_denomination, _year, _mintLocation,
_condition, _price, _onSale);
}
}
------------------------------------------------------------------------------------------------
/**
* The java program that reads a text file coin.txt file
* and reads the data from the file and prints
* the total items on sale and total price value
* to console.
* */
//Main2.java
package coinstore;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
ArrayList<Coin> col = new ArrayList<Coin>( );
String fileName="coin.txt";
Scanner filescanner=null;
Coin coin=null;
try {
filescanner=new Scanner(new File(fileName));
//read until end of file
while(filescanner.hasNextLine())
{
//call nextLine to read line from file
String dataLine=filescanner.nextLine();
//split by comma
String[] data=dataLine.split(",");
//parset to corresponding data values
double theDenom=Double.parseDouble(data[0]);
int theYear=Integer.parseInt(data[1]);
char theMint=data[2].charAt(0);
String theCondition=data[3];
double thePrice=Double.parseDouble(data[4]);
//create a coin object
coin=new Coin(theDenom, theYear, theMint,theCondition, thePrice);
//add to array list collection
col.add(coin);
}
//Set sale true for coin 2
col.get(2).setOnSale(true);
//Set sale true for coin 4
col.get(4).setOnSale(true);
//calling getOnSaleCount method
System.out.printf("Items on sale : %d ",getOnSaleCount(col));
//calling getTotalPrice method
System.out.printf("Total sale :%5.2f ",getTotalPrice(col));
} catch (FileNotFoundException e) {
System.out.println("File does not exist...");
}
}
/**
* The method getTotalPrice that takes ArrayList of type coin
* and find the total price of sale
* */
private static double getTotalPrice(ArrayList<Coin> myCol)
{
double totSale=0;
for (Coin coin : myCol) {
totSale+=coin.getPrice();
}
return totSale;
}
/**
* The method getOnSaleCount thta takes ArrayList of type coin
* and find the total items on sale.
* */
private static int getOnSaleCount(ArrayList<Coin> myCol)
{
int> for (Coin coin : myCol) {
if(coin.isOnSale())
onSaleCount++;
}
return onSaleCount;
}
}
------------------------------------------------------------------------------------------------
coin.txt
25,2015,D,XF,0.30
5,1958,D,XF,0.06
1,1912,S,G,50.00
20,1875,C,XF,1200.00
50,1991,P,VG,0.75
25,2003,D,VF,0.30
------------------------------------------------------------------------------------------------
Sample output:
Items on sale : 2
Total sale :1251.41
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.