Question

Can you modify this code so that it display the number of coins and subtotal for...

Can you modify this code so that it display the number of coins and subtotal for each type of coin(dimes, penny, quarter, nickel) and the total value in the wallet. But still reads the data from file coins.txt. Which is below MyCode.

The original question was this:

Two classes are given: Coin and Wallet. Write an application named MyWallet.java that reads all coins from a data file named coins.txt. The first line in the data file contains the number of coins (N) in the file, followed by N lines containing N numbers each represents a coin. You need to create a coin object from each number then create a Wallet object. At the end of your application, display number of coins and subtotal for each type, and total value in the wallet.

MyCode:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class MyWalletScratch {
public static void main(String[] args) {
File inFile = null;
if (0 < args.length) {
inFile = new File(args[0]);
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(inFile));
int numberOfCoins = Integer.parseInt(br.readLine());
Coin[] coins = new Coin[numberOfCoins];//Initialize coin array
System.out.println("Number Of coins: " + numberOfCoins+" Coins in Wallet: ");
int i = 0;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
Coin coin = new Coin(Integer.parseInt(sCurrentLine));//create coin
coins[i] = coin;//add coin to coins array
i++;
}
Wallet walet=new Wallet(coins);//Create wallet
System.out.println("Total In My Wallet: "+walet.totalValue());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} else {//exit if no cmd arguments are provided
System.err.println("Invalid arguments count:" + args.length);
System.exit(1);
}
}
}

coins.txt

10
5
10
1
25
5
1
25
25
25
10

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any douhbts, please give me comment...

import java.io.*;

import java.util.*;

public class MyWalletScratch {

public static void main(String[] args) {

if (0 < args.length) {

try {

Scanner sc = new Scanner(new File(args[0]));

int numberOfCoins = sc.nextInt();

Coin[] coins = new Coin[numberOfCoins]; // Initialize coin array

System.out.println("Number Of coins: " + numberOfCoins + " Coins in Wallet: ");

int i = 0;

while (sc.hasNextInt()) {

Coin coin = new Coin(sc.nextInt()); // create coin

coins[i] = coin; // add coin to coins array

i++;

}

Wallet walet = new Wallet(coins); // Create wallet

System.out.println("Total In My Wallet: " + walet.totalValue());

sc.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

} else { // exit if no cmd arguments are provided

System.err.println("Invalid arguments count:" + args.length);

System.exit(1);

}

}

}

Add a comment
Know the answer?
Add Answer to:
Can you modify this code so that it display the number of coins and subtotal for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have my assignment printing, but I can find why the flowers is printing null and...

    I have my assignment printing, but I can find why the flowers is printing null and not the type of flower. Instructions Make sure the source code file named Flowers.java is open. Declare the variables you will need. Write the Java statements that will open the input file, flowers.dat, for reading. Write a while loop to read the input until EOF is reached. In the body of the loop, print the name of each flower and where it can be...

  • Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming)...

    Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException {    if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...

  • I need help with my IM (instant messaging) java program, I created the Server, Client, and...

    I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...

  • I'm working with Java and I have a error message  1 error Error: Could not find or...

    I'm working with Java and I have a error message  1 error Error: Could not find or load main class Flowers. This is the problem: Instructions Make sure the source code file named Flowers.java is open. Declare the variables you will need. Write the Java statements that will open the input file, flowers.dat, for reading. Write a while loop to read the input until EOF is reached. In the body of the loop, print the name of each flower and where...

  • Please answer question correctly and show the result of the working code. The actual and demo...

    Please answer question correctly and show the result of the working code. The actual and demo code is provided .must take command line arguments of text files for ex : input.txt output.txt from a filetext(any kind of input for the text file should work as it is for testing the question) This assignment is about using the Java Collections Framework to accomplish some basic text-processing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map,...

  • Please complete the give code (where it says "insert code here") in Java, efficiently. Read the...

    Please complete the give code (where it says "insert code here") in Java, efficiently. Read the entire input one line at a time and then output a subsequence of the lines that appear in the same order they do in the file and that are also in non-decreasing or non-increasing sorted order. If the file contains n lines, then the length of this subsequence should be at least sqrt(n). For example, if the input contains 9 lines with the numbers...

  • Modify the socket-based date server below so that the server services each client request in a...

    Modify the socket-based date server below so that the server services each client request in a separate thread. import java.net.*; import java.io.*; public class DateClient {   public static void main(String[] args)  {     try {       /* make connection to server socket */       Socket sock = new Socket(“127.0.0.1”,6013);       InputStream in = sock.getInputStream();       BufferedReader bin = new         BufferedReader(new InputStreamReader(in));       /* read the date from the socket */       String line;       while ( (line = bin.readLine()) != null)         System.out.println(line);       /* close the socket connection*/       sock.close();     ...

  • A Chinese restaurant wants to have a computer-based search system that will display a recipe for...

    A Chinese restaurant wants to have a computer-based search system that will display a recipe for each dish. The owner of the restaurant wants the system to be flexible in that its recipes are stored in a text file. This way, the number of dishes can be expanded by adding more entries to the text file without changing any program code. After analyzing the coding the restaurant realizes that Current coding only outputs first two line of the recipe. Goal:...

  • There is a problem in the update code. I need to be able to update one...

    There is a problem in the update code. I need to be able to update one attribute of the students without updating the other attributes (keeping them empty). package dbaccessinjava; import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; import java.sql.*; import java.io.*; import java.util.Scanner; public class DBAccessInJava { public static void main(String[] argv) {    System.out.println("-------- Oracle JDBC Connection Testing ------"); Connection connection = null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con= DriverManager.getConnection("jdbc:oracle:thin:@MF057PC16:1521:ORCL", "u201303908","pmu"); String sql="select * from student"; Statement st; PreparedStatement ps; ResultSet rs;...

  • In java. You are provided an abstract class called MyAbstract. You can only modify the writeFile...

    In java. You are provided an abstract class called MyAbstract. You can only modify the writeFile () class. You are provided another class called Finally you are provided the file you must read from in the MyAbstract class. You will need to extend MyAbstract to MyTestClass. You should have the following outputs. The content of myData employee avg salary number of employees File that was created in myFile() Everything I was provided is listed already. l. Servers ary 1 35...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT