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

can anybody help me with this? its for java netbeans.. A sequence of n > 0 integ

ID: 3815394 • Letter: C

Question

can anybody help me with this? its for java netbeans..

A sequence of n > 0 integers is called a jolly jumper if the absolute values of the difference between successive elements take on all the values 1 through n-1. For instance, 1 4 2 3 is a jolly jumper, because the absolutes differences are 3, 2, and 1 respectively. The definition implies that any sequence of a single integer is a jolly jumper. You are to write a program to determine whether or not each of a number of sequences is a jolly jumper. Each line of the input file contains an integer n

Explanation / Answer

Below is your code. I have added comments which will help you to understand the code better: -

1. JollyJumper.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class JollyJumper {
   // Throws FileNotFoundException because of reading input from file
   public static void main(String[] args) throws FileNotFoundException {
      
       try {
           Scanner in = new Scanner(new File("input.txt"));
           // Saving each line as a string in arrayList
           ArrayList<String> linesOfFile = new ArrayList<String>();
           while (in.hasNextLine()) {
               linesOfFile.add(in.nextLine());
           }
           //Calling function to Print Jolly or Not Jolly
           printJollyNotJolly(linesOfFile);
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
       }
   }
  
   public static void printJollyNotJolly(ArrayList<String> linesOfFile) {
       String line;
       String strArr[];
       int diff,newDiff;
       boolean isJolly;
       for(int i = 0;i < linesOfFile.size();i++) {
           line = linesOfFile.get(i);
           strArr = line.split(" ");// Converting to string array of elements. Using Integer.parseInt to parse the elements to integer
           newDiff = 0;
           isJolly = true;
           if(Integer.parseInt(strArr[0]) > 2) { // If there are 2 or less elements it will always be jolly
               diff = java.lang.Math.abs(Integer.parseInt(strArr[2]) - Integer.parseInt(strArr[1])); // taking absolute of the difference
               for(int j = 2; j < Integer.parseInt(strArr[0]);j++) {
                   newDiff = java.lang.Math.abs(Integer.parseInt(strArr[j+1]) - Integer.parseInt(strArr[j]));
                   if(newDiff == diff-1) {//Checking if next difference is 1 less then previous difference
                       diff = newDiff;
                       isJolly = true;
                   } else {
                       isJolly = false;
                       break;
                   }
                  
               }
               if(isJolly) {
                   System.out.println("Jolly");
               } else {
                   System.out.println("Not Jolly");
               }
           } else {
               System.out.println("Jolly");
           }
       }
   }
}

2. input file. It needs to be there in the project folder root.Along with test cases given by you I have added 2 more test cases.

input.txt

4 1 4 2 3
5 1 4 2 -1 6
6 5 10 6 3 1 0
3 3 4 2

3. Output: -

Jolly
Not Jolly
Jolly
Not Jolly

Hope its satisfies you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote