JAVA Find errors and test a class For this problem submit an
JAVA
Find errors and test a class.
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.
15 points. Find the errors in the Coin and Main1 classes. Submit a list of all the errors and their corrections in a Word text file.
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, setTotalPrice methods:
The main method should:
read comma delimited data lines from the input file coins.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.
Solution
package chegg.bunnies;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Main2 {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader(
\"d://deepak.txt\"))) {
String sCurrentLine;
ArrayList<Coin> coins = new ArrayList<Coin>();
while ((sCurrentLine = br.readLine()) != null) {
if (sCurrentLine != null) {
String[] fields = sCurrentLine.split(\",\");
int coin = 0;
for (int indx = 0; indx < fields.length; indx++) {
try {
coin = Integer.parseInt(fields[indx]);
} catch (Exception e) {
coin = 0;
}
coins.add(new Coin(coin));
}
}
}
// 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:
coins.get(2).setOnSale(true);
coins.get(4).setOnSale(true);
int coinsCount = getOnSaleCount(coins);
System.out.println(\"Coins Count \" + coinsCount);
double totalCoinsvalue = getTotalPrice(coins);
System.out.println(\"total coins value :\" + totalCoinsvalue);
} catch (IOException e) {
e.printStackTrace();
}
}
private static int getOnSaleCount(ArrayList<Coin> myCol) {
int count = 0;
for (Coin coin : myCol) {
if (coin.isOnSale()) {
count++;
}
}
return count;
}
private static double getTotalPrice(ArrayList<Coin> myCol) {
double total = 0;
for (Coin coin : myCol) {
if (coin.isOnSale()) {
total = total + coin.get_price();
}
}
return total;
}
}

