ONLY ACCEPTING AS A TEXT FILE. Write a program (java) that determines the value of the coins in a jar and prints the total in dollars and cents. Read integer values that represent the number of quarters, dimes,nickels. and pennies.
Code - Main,java , and read input for quarters, dimes,nickels. and pennies. from input.txt
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException
{
//variabled declare to be used
int quarters, dimes, nickels, pennies;
int dollor;
//read value of quarters, dimes, nickels, pennies from file
input.txt
Scanner scanner = new Scanner(new File("input.txt"));
//store the value read from file in input array
int [] input = new int [4];
int i = 0;
//reading values from file
while(scanner.hasNextInt())
{ //storing the value in input array
input[i++] = scanner.nextInt();
}
//initialize the variable to their corresponding value
quarters = input[0];
dimes = input[1];
nickels = input[2];
pennies = input[3];
//print out the value read from file and display
System.out.println("Number of quarters in the jar:
"+quarters);
System.out.println("Number of dimes in the jar: "+dimes);
System.out.println("Number of nickels in the jar: "+nickels);
System.out.println("Number of pennies in the jar: "+pennies);
//calculate the value in cents
int cents = 25*quarters + dimes*10 + nickels*5 + pennies;
//convert it to dollor
dollor = cents/100;
cents = cents %100;
//print the last result
System.out.println("Total is " + dollor + " dollars and " + cents +
" cents ");
}
}
input.txt -
Output -
Get Answers For Free
Most questions answered within 1 hours.