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

Ul 4. The that are larger than 5 lows. Tour program for game of \"Jump It\" cons

ID: 3850749 • Letter: U

Question

Ul 4. The that are larger than 5 lows. Tour program for game of "Jump It" consists of a board with n positive integers in a row except to the first column, which always contains zero. These numbers represent the cost enter each column. Here is a sample game board where n is 6: 80 The object of the game is to move from the first column to the last column in the lowest total cost. The number in each column represents the cost to enter that column. Always start the game in the first column and have two types of moves. You can either move to the adjacent column or jump over the adjacent column two columns over. The cost of a game is the sum of the costs of the visited columns In the board shown above, there are several ways to get to the end. Starting the in first column, our cost so far is 0. We could jump to 80, then jump to 57, then move to 10 for a total cost of 80 57 10 147. However, a cheaper path would be to move to 3, jump to 6, then jump to 10, for a total cost of 3 6 10 19 rite a recursive solution to this problem that computes the cheapest cost of the game and outputs this value for an arbitrarily large game board represented as an array. Your program does not have to output the actual sequence of jumps, only the cheapest cost of this sequence. After making sure that your solution works on small arrays, test your solution on boards of larger and larger values of n to get a feel for how efficient and scalable your solution is

Explanation / Answer

Here is the code , input and output for the question. In case of any issues, please post a comment and I shall respond. Please don't forget to rate the answer if it helped. Thank you very much.

BadInputException.java

public class BadInputException extends Exception {

   public BadInputException()

   {

       super("The first int should be 0");

   }

  

   public BadInputException(String message)

   {

       super(message);

   }

}

JumpIt.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class JumpIt {

   private String fileName; //name of file

   private int count = 0; // number of valid integers in the file

   private final int MAX_SIZE = 15; //the size of the array

   private int row[]; //the numbers array for the game

   public JumpIt(String theName)

   {

       this.fileName = new String(theName);

       row = new int[MAX_SIZE];

       loadFile();

   }

  

   public int play() throws BadInputException

   {

       System.out.print(" play game");

       if(row[0] != 0)

       {

           throw new BadInputException();

       }

       return play(row, 0, count-1);

   }

  

   private int play(int[] a, int first, int last)

   {

       if(last < first )

           return 0;

       if(first == last)

           return a[first];

       //using recursive calls, calculate the cost of reaching the last element from

       // the element just 1 before and then 2 elements before it

       int cost1 = play(a, first, last-1) + a[last]; //cost to reach the previous element + cost of last

       int cost2 = play(a, first, last-2) + a[last]; //cost to reach 2 elements before + cost of last

       if(cost1 < cost2) //return the lowest cost of the 2

           return cost1;

       else

           return cost2;

   }

   //prints the numbers in the game , 10 numbers per line

   public void printGame()

   {

       System.out.print(" File has " + count + " ints ");

       for(int i = 0 ; i < count; i++)

       {

           if(i % 11 == 0)

               System.out.println();

      

           System.out.print(row[i]+ " ");

       }

   }

  

   private void loadFile()

   {

       try {

           System.out.print(" opening file " + fileName);

           Scanner infile = new Scanner(new File(fileName));

           count = 0;

           while(infile.hasNext() && count < MAX_SIZE)

           {

               try

               {

                   row[count] = Integer.parseInt(infile.next());

                   count++;

               }

               catch(NumberFormatException e)

               {

                   System.out.print(" File contains a incorrectly written int");

               }

           }

          

           if(infile.hasNext())

           {

               System.out.print(" The file has more than " + MAX_SIZE +" ints. " );

               System.out.print("Only the first " + MAX_SIZE + " ints are considered");

           }

          

          

       } catch (FileNotFoundException e) {

           System.out.println(" "+e.getMessage());

       }

      

   }

}

JumpItDriver.java

public class JumpItDriver {

   public static void main(String[] args) {

       String testFiles[] = {"file1.txt", "file2.txt", "file3.txt"};

       for(int i = 0; i < testFiles.length; i++)

       {

           JumpIt game = new JumpIt(testFiles[i]);

           game.printGame();

           try{

               System.out.println(" the cost is " + game.play());

           }

           catch(BadInputException e)

           {

               System.out.println(" bad input: "+e.getMessage());

           }

          

           System.out.println();

       }

   }

}

input file file1.txt

0 1 3 4 1

input file file2.txt

3 1 3 4 1

input file file3.txt

0

4

23

566

34

xxx

45

555

11

34

35

45

xxx

65

55

98

344

54

output

opening file file1.txt

File has 5 ints

0 1 3 4 1

play game

the cost is 4

opening file file2.txt

File has 5 ints

3 1 3 4 1

play game

bad input: The first int should be 0

opening file file3.txt

File contains a incorrectly written int

File contains a incorrectly written int

The file has more than 15 ints. Only the first 15 ints are considered

File has 15 ints

0 4 23 566 34 45 555 11 34 35 45

65 55 98 344

play game

the cost is 591