Question

In: Computer Science

Can you please explain in detail what each line of code stands for in the Main...

Can you please explain in detail what each line of code stands for in the Main method

import java.util.Scanner;

public class CashRegister {

private static Scanner scanner = new Scanner(System.in);

private static int dollarBills[] = {1, 2, 5, 10, 20, 50, 100};
private static int cents[] = {25, 10, 5, 1};

public static void main(String[] args) {

double totalAmount = 0;
int count = 0;
for (int i = 0; i < dollarBills.length; i++) {
count = getBillCount("How many $" + dollarBills[i] + " bills do you have");
totalAmount += count * dollarBills[i];
}

for (int i = 0; i < cents.length; i++) {
count = getBillCount("How many " + cents[i] + " cents do you have");
totalAmount += count * cents[i]/100.0;
}

System.out.printf("You have total $%.2f in your register\n",totalAmount);
if(totalAmount>150){
System.out.printf("Deposit Amount $%.2f\n",(totalAmount-150));
}else if(totalAmount==150){
System.out.println("You have exactly $150. You dont have any deposit.");
}else {
System.out.println("You have less than $150 in your register.");
}
}

private static int getBillCount(String message) {

int count = 0;
while (true) {
System.out.print(message + ": ");
try {

count = scanner.nextInt();
} catch (Exception exception) {
System.out.println("Invalid number passed.");
}

if (count < 0) {
System.out.println("Invalid number passed.");
} else {
return count;
}
}

}
}

Solutions

Expert Solution

import java.util.Scanner;

public class CashRegister {

   private static Scanner scanner = new Scanner(System.in);
   //here we are creating an array named dollarBills which contain all type of dollars
   private static int dollarBills[] = {1, 2, 5, 10, 20, 50, 100};
   //here we are creating an array named cents which contain all type of cents
   private static int cents[] = {25, 10, 5, 1};

   public static void main(String[] args)
   {
       //intially assign the total amount to 0
       double totalAmount = 0;
       //the count will also be 0
       int count = 0;
       //iterating over the array dollarBills to find the number of dollars of each type
       for (int i = 0; i < dollarBills.length; i++)
       {
           //this count variable is to find the total number of dollars of each type suppose if we have 10 coins of 1$ the count will be 10
           count = getBillCount("How many $" + dollarBills[i] + " bills do you have");
           //we find the total amount based on the count and the value of the coin suppose if we have 10 coins of 1 dollar it will be (1*10) and we are adding it to fint the total amount
           totalAmount += count * dollarBills[i];
       }
       //now int the same way we are iterating through cents array which contains all types of cents
       for (int i = 0; i < cents.length; i++)
       {
           //it is same as of count in dollar bills but it is for cents
           count = getBillCount("How many " + cents[i] + " cents do you have");
           //it is also same as total amount in dollar bills since they are cents we are dividing with 100
           totalAmount += count * cents[i]/100.0;
       }
       //printing the total amount that you have
       System.out.printf("You have total $%.2f in your register\n",totalAmount);
       if(totalAmount>150)
       {
           //if the amount is greater than 150 we are depositing amount-150 dollars
           System.out.printf("Deposit Amount $%.2f\n",(totalAmount-150));
       }
       else if(totalAmount==150)
       {
           //if the amount is equal to 150 there is no money to deposit
           System.out.println("You have exactly $150. You dont have any deposit.");
       }
       else
       {
           //it doesn't have the minimum money in the register
           System.out.println("You have less than $150 in your register.");
       }
   }
   private static int getBillCount(String message)
   {

       int count = 0;
       while (true)
       {
           System.out.print(message + ": ");
           try
           {
               count = scanner.nextInt();
           }
           catch (Exception exception)
           {
               System.out.println("Invalid number passed.");
           }

           if (count < 0)
           {
               System.out.println("Invalid number passed.");
           }
           else
           {
               return count;
           }
       }

   }
}

If you have any doubts please comment and please don't dislike.


Related Solutions

analyze the assembly code and explain what each line is doing 000000000000063a <main>: 63a: 55 push...
analyze the assembly code and explain what each line is doing 000000000000063a <main>: 63a: 55 push ebp 63b: 48 89 e5 mov ebp,esp 63e: 48 83 ec 10 sub esp,0x10 642: c7 45 fc 00 00 00 00 mov DWORD PTR [ebp-0x4],0x0 649: eb 16 jmp 661 <main+0x27> 64b: 83 7d fc 09 cmp DWORD PTR [ebp-0x4],0x9 64f: 75 0c jne 65d <main+0x23> 651: 48 8d 3d 9c 00 00 00 lea edi,[eip+0x9c] # 6f4 <_IO_stdin_used+0x4> 658: e8 b3 fe...
Python 3 can you explain what every single line of code does in simple terms please...
Python 3 can you explain what every single line of code does in simple terms please so basically # every single line with an explanation thanks def longest(string): start = 0;end = 1;i = 0; while i<len(string): j = i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: end = j start = i i = j; avg = 0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('The average is',avg/(end-start)) s = input('Enter a string ')...
please let me know reference of this MATLAB code. please explain this code line by line....
please let me know reference of this MATLAB code. please explain this code line by line. . . N=256; %% sampling number n=linspace(0,1,N); fs=1/(n(2)-n(1)); x=5*(sin((2*pi*10*n))); %% create signal N=length(x); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(211) plot(f,fftshift(abs(fft(x)))); title('|G(f)|');grid; xlabel('frequency'); ylabel('|G(f)|'); %Zero padding xx=[zeros(1,128) x zeros(1,128)]; N=length(xx); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(212) plot(f,fftshift(abs(fft(xx)))); title('|Gz(f)|');grid; xlabel('frequency'); ylabel('|Gz(f)|');
Can someone please write clear and concise comments explaining what each line of code is doing...
Can someone please write clear and concise comments explaining what each line of code is doing for this program in C. I just need help tracing the program and understand what its doing. Thanks #include <stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/wait.h> int join(char *com1[], char *com2[]) {    int p[2], status;    switch (fork()) {        case -1:            perror("1st fork call in join");            exit(3);        case 0:            break;        default:...
4. Explain what is happening on each line of the following AVR assembly code. If you...
4. Explain what is happening on each line of the following AVR assembly code. If you were to execute this code what would be the final decimal values in R20, R21 and SREG registers? BCLR 0 BCLR 1 BCLR 2 BCLR 3 BCLR 4 BCLR 5 BCLR 6 BCLR 7 LDI ​R19, 0x02 LDI​R20, 0x74 LDI​R21, 0x04 LDI​R22, 0x22 ADD​R20, R22 SUB​R22, R21 ADD​R20, R21 MOV​R20, R21 JMP​DONE ADD​R21, R20 SUB​R21, R22 DONE:​SUB​R20, R21 -embedded system-
can you please explain each and every statement of the code without missing. it would be...
can you please explain each and every statement of the code without missing. it would be really helpful AVA PROJECT CREATING GUI WITH ARRAY Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using...
Please can you explain in detail with diagrams what a bonding and an anti-bonding orbital are,...
Please can you explain in detail with diagrams what a bonding and an anti-bonding orbital are, and what the difference is between them?
Please explain the main provisions of the Taft- Hartley Act in detail.
Please explain the main provisions of the Taft- Hartley Act in detail.
explain the code for a beginner in c what each line do Question 3. In the...
explain the code for a beginner in c what each line do Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable...
As simply as you can but with complete and comprehensive detail, state and explain each of...
As simply as you can but with complete and comprehensive detail, state and explain each of Newtons Three Fundamental Laws of Motion. After you have explained all Three Laws look come up with and explain an example problem of your own. It must include an explanation of how at least one of Newton's laws is used if not more than one. (can not use online sites)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT