Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Heres the code I have so far: import java.io.File; import java.io.PrintWriter; i

ID: 3686258 • Letter: H

Question

Heres the code I have so far:

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.IOException;

public class CookieJar {
  
   public static void cashingIn(File input, File output){
   String line="";
   double value=0.00;
       try{
           Scanner scan=new Scanner(input);
          
           PrintWriter print =new PrintWriter(output);
          
          
           while(scan.hasNextLine()){
              
               line=line+scan.nextLine();
           }
          
           Scanner scan2= new Scanner(line);
          
           while(scan2.hasNextInt()){
               int a=scan2.nextInt();
               if(scan2.next()=="quarters"){
                   double tempval=a*0.25;
                   value=value+tempval;
                  
               }else if(scan2.next()=="quarter"){
                   double tempval=a*0.25;
                   value=value+tempval;
                  
                  
               }else if(scan2.next()=="dimes"){
                   double tempval=a*0.10;
                   value=value+tempval;
                  
               }else if(scan2.next()=="dime"){
                   double tempval=a*0.10;
                   value=value+tempval;
                  
               }else if(scan2.next()=="nickels"){
                   double tempval=a*0.05;
                   value=value+tempval;
                  
               }else if(scan2.next()=="nickel"){
                   double tempval=a*0.05;
                   value=value+tempval;
                  
               }else if(scan2.next()=="pennies"){
                   double tempval=a*0.01;
                   value=value+tempval;
                  
               }else if(scan2.next()=="penny"){
                   double tempval=a*0.01;
                   value=value+tempval;
               }
           }
          
          
          
           if(value==0.00){
               print.println("You have no money in the jar");
           }else {
               print.println(value);
           }
          
           print.close();
           scan.close();
           scan2.close();
          
       }catch(IOException e){
          
       }
   }

}

What I believe is happening is that my .next() is reading the entire string instead of the next denomination of money. I converted it into a single string in order to try and make things easier but I'm not sure what the next step is to get each denomination with the number of each.

Here is the test bench:

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Scanner;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class CookieJarTest {
   @Rule
public TemporaryFolder folder = new TemporaryFolder();
  
   @Test
   public void testReflection() {
       Class<?> cClass = CookieJar.class;
       Field[] cFields = cClass.getDeclaredFields();

       for (Field f : cFields) {
           if (!f.isSynthetic()) {
               assertTrue ( "Field ""+f.getName()+"" should be private", Modifier.isPrivate( f.getModifiers() ));
               assertFalse( "Field ""+f.getName()+"" can't be static", Modifier.isStatic ( f.getModifiers() ));
           }
       }
   }
   @Test
   public void testEmptyFile() {
       try {
           // create file
           File input = folder.newFile( "input.txt" );
           File output = folder.newFile( "output.txt" );

           // invoke program
           CookieJar.cashingIn( input, output );
          
           // verify file results
           assertTrue ( "Output file does not exist", output.exists() );
           Scanner scan = new Scanner( output );
           String expected = "You have no money in the jar";
           assertTrue ( "Unexpected end of file: expected "%s"" + expected, scan.hasNext() );
           String actual = scan.nextLine();
           assertEquals( "Incorrect result", expected, actual );
           assertFalse ( "File contains more data than expected", scan.hasNext() );
           scan.close();
       }
       catch (IOException e) {
           fail( "No exception should be thrown" );
       }
   }
   @Test
   public void test0() {
       try {
           // create file
           File input = folder.newFile( "input.txt" );
           File output = folder.newFile( "output.txt" );

           PrintWriter write = new PrintWriter( input );
           write.println( "0 quarters 0 pennies 0 nickels 0 dimes" );
           write.close();

           // invoke program
           CookieJar.cashingIn( input, output );
          
           // verify file results
           assertTrue ( "Output file does not exist", output.exists() );
           Scanner scan = new Scanner( output );
           String expected = "You have no money in the jar";
           assertTrue ( "Unexpected end of file: expected "%s"" + expected, scan.hasNext() );
           String actual = scan.nextLine();
           assertEquals( "Incorrect result", expected, actual );
           assertFalse ( "File contains more data than expected", scan.hasNext() );
           scan.close();
       }
       catch (IOException e) {
           fail( "No exception should be thrown" );
       }
   }
   @Test
   public void test1() {
       try {
           // create file
           File input = folder.newFile( "input.txt" );
           File output = folder.newFile( "output.txt" );

           PrintWriter write = new PrintWriter( input );
           write.println( "2 quarters 4 dimes 1 penny 3 nickels 3 pennies" );
           write.close();

           // invoke program
           CookieJar.cashingIn( input, output );
          
           // verify file results
           assertTrue ( "Output file does not exist", output.exists() );
           Scanner scan = new Scanner( output );
           String expected = "You have $1.09 in the jar";
           assertTrue ( "Unexpected end of file: expected "%s"" + expected, scan.hasNext() );
           String actual = scan.nextLine();
           assertEquals( "Incorrect result", expected, actual );
           assertFalse ( "File contains more data than expected", scan.hasNext() );
           scan.close();
       }
       catch (IOException e) {
           fail( "No exception should be thrown" );
       }
   }
   @Test
   public void test2() {
       try {
           // create file
           File input = folder.newFile( "input.txt" );
           File output = folder.newFile( "output.txt" );

           PrintWriter write = new PrintWriter( input );
           write.println( " " );
           write.println( " 423 quarters" );
           write.println( " 102 nickels " );
           write.println( " 99 pennies " );
           write.println( " 3 dimes " );
           write.println( " " );
           write.close();

           // invoke program
           CookieJar.cashingIn( input, output );
          
           // verify file results
           assertTrue ( "Output file does not exist", output.exists() );
           Scanner scan = new Scanner( output );
           String expected = "You have $112.14 in the jar";
           assertTrue ( "Unexpected end of file: expected "%s"" + expected, scan.hasNext() );
           String actual = scan.nextLine();
           assertEquals( "Incorrect result", expected, actual );
           assertFalse ( "File contains more data than expected", scan.hasNext() );
           scan.close();
       }
       catch (IOException e) {
           fail( "No exception should be thrown" );
       }
   }
   @Test
   public void test3() {
       try {
           // create file
           File input = folder.newFile( "input.txt" );
           File output = folder.newFile( "output.txt" );

           PrintWriter write = new PrintWriter( input );
           write.println( "32 nickels" );
           write.println( " 1" );
           write.println( " nickel 42" );
           write.println( "quarters 1 penny" );
           write.println( "1 quarter 23 pennies 16" );
           write.println( "" );
           write.println( "dimes 1 dime 1 dime 1 dime 1 dime" );
           write.close();

           // invoke program
           CookieJar.cashingIn( input, output );
          
           // verify file results
           assertTrue ( "Output file does not exist", output.exists() );
           Scanner scan = new Scanner( output );
           String expected = "You have $14.64 in the jar";
           assertTrue ( "Unexpected end of file: expected "%s"" + expected, scan.hasNext() );
           String actual = scan.nextLine();
           assertEquals( "Incorrect result", expected, actual );
           assertFalse ( "File contains more data than expected", scan.hasNext() );
           scan.close();
       }
       catch (IOException e) {
           fail( "No exception should be thrown" );
       }
   }
   @Test
   public void test4() {
       try {
           // create file
           File input = folder.newFile( "input.txt" );
           File output = folder.newFile( "output.txt" );

           PrintWriter write = new PrintWriter( input );
           write.println( "4321 quarters" );
           write.println( "1234 pennies" );
           write.println( "4567 dimes" );
           write.println( "9876 nickels" );
           write.close();

           // invoke program
           CookieJar.cashingIn( input, output );
          
           // verify file results
           assertTrue ( "Output file does not exist", output.exists() );
           Scanner scan = new Scanner( output );
           String expected = "You have $2,043.09 in the jar";
           assertTrue ( "Unexpected end of file: expected "%s"" + expected, scan.hasNext() );
           String actual = scan.nextLine();
           assertEquals( "Incorrect result", expected, actual );
           assertFalse ( "File contains more data than expected", scan.hasNext() );
           scan.close();
       }
       catch (IOException e) {
           fail( "No exception should be thrown" );
       }
   }
}

1. Line Numbers When editors review articles submitted to journals, they need to identify the line numbers in which corrections must be made; however, articles are submitted as text file without line numbers. To help editors, you're asked to modify submitted text files to add a line number at the beginning of each line. For example, given the following file (for simplicity, only the first 3 lines of text are shown) Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sed scelerisque dolor, (and so on) 1 Lorem ipsum dolor sit amet, 2 I consectetur adipiscing elit. 3 In sed scelerisque dolor, .. (and so on) Line numbers are formatted right-aligned in 3 spaces, followed by a | character (with one space before and after), and followed by the text in that line. Exercise1 Write a class "LineNumbers" that implements the method "process" below. public static void process(File input, File output) This method receives an input file with lines of text. The method writes all lines from the input file to the output file with each line preceded by its line number (formatted as explained above)

Explanation / Answer

Please find the required function :