Question

In: Computer Science

I have one error and it encounters my split array and it should output true or...


I have one error and it encounters my split array and it should output true or false


public class Main
{

private static int[] freq;

static boolean doubleOrNothing(int[] array, int size, int i)
{
  
if (size <= 1)
  
return false;

if (2 * array[i] == array[i+1])

return true;

return doubleOrNothing(array, size - 1, i++);
}

/**
*
* @param word
* @param sep
*
*
* @param count
* @return
*/
public static String wordSeparator(String word, String sep, int count)
{
  
if (count <= 0)
  
return "";
  
String new_word="";
if (count == 1)
return word;

return word + sep + wordSeparator(word, sep, count - 1);
  
}
static boolean mcNuggetNumber(int n) //checks for 3 or 7 piece chicken nugget
{
boolean l = false, r = false;
if (n == 3 || n == 7)
return true;
  
if (n >= 3)
l = mcNuggetNumber(n - 3);
  
if (n >= 7)
r = mcNuggetNumber(n - 7);
  
return l || r;
  
}

static boolean splitArray(int arr[], int n, int k)
{
// An odd length array cannot be divided into pairs
if (n == 1)
return false;

// Create a frequency array to count occurrences
// of all remainders when divided by k.
// map freq = null;

// Count occurrences of all remainders
for (int i = 0; i < n; i++)
freq[arr[i] % k]++;

// Traverse input array and use freq[] to decide
// if given array can be divided in pairs
for (int i = 0; i < n; i++)
{
// Remainder of current element
int rem = arr[i] % k;

// If remainder with current element divides
// k into two halves.
if (2 * rem == k)
{
// Then there must be even occurrences of
// such remainder
if (freq[rem] % 2 != 0)
return false;
}

// If remainder is 0, then there must be two
// elements with 0 remainder
else if (rem == 0)
{
if (1 & freq[rem])
{
return false;
}
else
{
}
}

// Else number of occurrences of remainder
// must be equal to number of occurrences of
// k - remainder
else if (freq[rem] != freq[k - rem])
return false;
}
return true;
}

  
public static void main(String[] args) {

int a1[] ={1};

int a2[] = { 1,2 };
  
int a3[] = { 0,0,1 };
  
int a4[] = { 2,3,4 };
  
int a5[] = { 1,3,5,10 };
  
int a6[] = { 1,3,5,11 };
  
int a7[] = { 9,8,7,6,5,4,3,2,1 };
  
int a8[] = { 9,8,7,6,12,4,3,2,1 };
  
int i1[] = { 7 };
  
int i2[] = { 3,4 };
  
int i3[] = { 3,4,5 };
  
int i4[] = { 9,10,11 };
  
int o1[] = { 3,4 };
  
int o2[] = { 2,3,4 };
  
int o3[] = { 3,4,5,6 };
  
int o4[] = { 1,2,3,4,5,6 };
  
int s1[] = { 2, 2 };
  
int s2[] = { 2, 3 };
  
int s3[] = { 5, 2, 3 };
  
int s4[] = { 5, 2, 2 };
  
int s5[] = { 1, 1, 1, 1, 1, 1 };
  
int s6[] = { 1, 1, 1, 1, 1 };
  
int s8[] = { 1 };
  
int s9[] = { 3, 5 };
  
int s10[] = { 5, 3, 2 };
  
int s11[] = { 2, 2, 10, 10, 1, 1 };
  
int s12[] = { 1, 2, 2, 10, 10, 1, 1 };
  
int s13[] = { 1, 2, 3, 10, 10, 1, 1 };
  
  
System.out.println("Jones Robert ");
System.out.println("Assignment 3");
System.out.println("Recursion.");
System.out.println("All calls must result in true or false.");
System.out.println();

System.out.println("Double Or Nothing." );

System.out.println("1. " + doubleOrNothing(a1, 1,0) );
  
System.out.println( "2. " + doubleOrNothing(a2, 2,0) );
  
System.out.println("3. " + doubleOrNothing(a3, 3,0) );
  
System.out.println("4. " + doubleOrNothing(a4, 3,0));
  
System.out.println("5. " + doubleOrNothing(a5, 4,0) );
  
System.out.println("6. " + doubleOrNothing(a6, 4,0) );
  
System.out.println("7. " + doubleOrNothing(a7, 9,0) );
  
System.out.println("8. " + doubleOrNothing(a8, 9,0) );
  
System.out.println();
System.out.println();
  
System.out.println("Word Separator.");

System.out.println("1. " + wordSeparator("Y", "X", 4));

System.out.println("2. " + wordSeparator("Y", "X", 2));

System.out.println("3. " + wordSeparator("Y", "X", 1));

System.out.println("4. " + wordSeparator("Y", "X", 0));

System.out.println();
  
System.out.println("McNugget Numbers" );

System.out.println(" 1. " + mcNuggetNumber(0));

System.out.println(" 2. " + mcNuggetNumber(1) );

System.out.println(" 3. " + mcNuggetNumber(2) );

System.out.println(" 4. " + mcNuggetNumber(3) );

System.out.println(" 5. " + mcNuggetNumber(4) );

System.out.println(" 6. " + mcNuggetNumber(5) );

System.out.println(" 7. " + mcNuggetNumber(6) );

System.out.println(" 8. " + mcNuggetNumber(7) );

System.out.println(" 9. " + mcNuggetNumber(8) );

System.out.println("10. " + mcNuggetNumber(9) );

System.out.println("11. " + mcNuggetNumber(10) );

System.out.println("12. " + mcNuggetNumber(11) );

System.out.println("13. " + mcNuggetNumber(12) );

System.out.println("14. " + mcNuggetNumber(13));

System.out.println("15. " + mcNuggetNumber(14));

System.out.println("16. " + mcNuggetNumber(15));

System.out.println("17. " + mcNuggetNumber(16));

System.out.println();


System.out.println ("Split the Array");
System.out.println ("1. " + splitArray (1));
System.out.println ("2. " + splitArray (2));
System.out.println ("3. " + splitArray (3));
System.out.println ("4. " + splitArray (4));
System.out.println ("5. " + splitArray (5));
System.out.println ("6. " + splitArray (6));
System.out.println ("7. " + splitArray (7));
System.out.println ("8. " + splitArray (8));
System.out.println ("9. " + splitArray (9));
System.out.println ("10. " + splitArray (10));
System.out.println ("11. " + splitArray (11));
System.out.println ("12. " + splitArray (12));
System.out.println ("13. " + splitArray (13));
System.out.println ("14. " + splitArray (14));
System.out.println ("15. " + splitArray (15));
System.out.println ("16. " + splitArray (16));
System.out.println();


  
  

}

private static String splitArray(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

I have an error

Solutions

Expert Solution

Hi,

Edited your given code for the pairs of array..it has compilation as well as run time error.i edited the code at many places..user hashmap for the frequency but you can chnage it with array as well.

Thanks

import java.util.HashMap;

public class Main
{

private static int[] freq;

static boolean doubleOrNothing(int[] array, int size, int i)
{
  
if (size <= 1)
  
return false;

if (2 * array[i] == array[i+1])

return true;

return doubleOrNothing(array, size - 1, i++);
}

/**
*
* @param word
* @param sep
*
*
* @param count
* @return
*/
public static String wordSeparator(String word, String sep, int count)
{
  
if (count <= 0)
  
return "";
  
String new_word="";
if (count == 1)
return word;

return word + sep + wordSeparator(word, sep, count - 1);
  
}
static boolean mcNuggetNumber(int n) //checks for 3 or 7 piece chicken nugget
{
boolean l = false, r = false;
if (n == 3 || n == 7)
return true;
  
if (n >= 3)
l = mcNuggetNumber(n - 3);
  
if (n >= 7)
r = mcNuggetNumber(n - 7);
  
return l || r;
  
}
                                                                
static boolean splitArray(int arr[], int n, int k)  // splitArray ({ 2, 2, 10, 10, 1, 1 },n=6,k=2)
{
// An odd length array cannot be divided into pairs
if (n % 2 == 1)
   return false;

// Create a frequency array to count occurrences
// of all remainders when divided by k.
// map freq = null;
HashMap<Integer, Integer> hashmap = new HashMap<>();

// Count occurrences of all remainders
for (int i = 0; i < n; i++){
//freq[arr[i] % k]++;
     int rem = ((arr[i] % k) + k) % k;
            if (!hashmap.containsKey(rem)) {
                hashmap.put(rem, 0);
            }
            hashmap.put(rem, hashmap.get(rem) + 1);

}
// Traverse input array and use freq[] to decide
// if given array can be divided in pairs
for (int i = 0; i < n; i++)
{
// Remainder of current element
//int rem = arr[i] % k;
int rem = ((arr[i] % k) + k) % k;

// If remainder with current element divides
// k into two halves.
if (2 * rem == k)
{
// Then there must be even occurrences of
// such remainder
//if (freq[rem] % 2 != 0)
//return false;
if (hashmap.get(rem) % 2 == 1)
        return false;
}

// If remainder is 0, then there must be two
// elements with 0 remainder
else if (rem == 0)
{
/*if (1 & freq[rem])
{
return false;
}
else
{
} */
if (hashmap.get(rem) % 2 == 1)
          return false;
}

// Else number of occurrences of remainder
// must be equal to number of occurrences of
// k - remainder
else 
if (hashmap.get(k - rem) != hashmap.get(rem))
                    return false;
}
return true;
}

  
public static void main(String[] args) {

int a1[] ={1};

int a2[] = { 1,2 };
  
int a3[] = { 0,0,1 };
  
int a4[] = { 2,3,4 };
  
int a5[] = { 1,3,5,10 };
  
int a6[] = { 1,3,5,11 };
  
int a7[] = { 9,8,7,6,5,4,3,2,1 };
  
int a8[] = { 9,8,7,6,12,4,3,2,1 };
  
int i1[] = { 7 };
  
int i2[] = { 3,4 };
  
int i3[] = { 3,4,5 };
  
int i4[] = { 9,10,11 };
  
int o1[] = { 3,4 };
  
int o2[] = { 2,3,4 };
  
int o3[] = { 3,4,5,6 };
  
int o4[] = { 1,2,3,4,5,6 };
  
int s1[] = { 2, 2 };
  
int s2[] = { 2, 3 };
  
int s3[] = { 5, 2, 3 };
  
int s4[] = { 5, 2, 2 };
  
int s5[] = { 1, 1, 1, 1, 1, 1 };
  
int s6[] = { 1, 1, 1, 1, 1 };
  
int s8[] = { 1 };
  
int s9[] = { 3, 5 };
  
int s10[] = { 5, 3, 2 };
  
int s11[] = { 2, 2, 10, 10, 1, 1 };
  
int s12[] = { 1, 2, 2, 10, 10, 1, 1 };
  
int s13[] = { 1, 2, 3, 10, 10, 1, 1 };
  
  
System.out.println("Jones Robert ");
System.out.println("Assignment 3");
System.out.println("Recursion.");
System.out.println("All calls must result in true or false.");
System.out.println();

System.out.println("Double Or Nothing." );
/*
System.out.println("1. " + doubleOrNothing(a1, 1,0) );
  
System.out.println( "2. " + doubleOrNothing(a2, 2,0) );
  
System.out.println("3. " + doubleOrNothing(a3, 3,0) );
  
System.out.println("4. " + doubleOrNothing(a4, 3,0));
  
System.out.println("5. " + doubleOrNothing(a5, 4,0) );
  
System.out.println("6. " + doubleOrNothing(a6, 4,0) );
  
System.out.println("7. " + doubleOrNothing(a7, 9,0) );
  
System.out.println("8. " + doubleOrNothing(a8, 9,0) );
  
System.out.println();
System.out.println();
  
System.out.println("Word Separator.");

System.out.println("1. " + wordSeparator("Y", "X", 4));

System.out.println("2. " + wordSeparator("Y", "X", 2));

System.out.println("3. " + wordSeparator("Y", "X", 1));

System.out.println("4. " + wordSeparator("Y", "X", 0));

System.out.println();
  
System.out.println("McNugget Numbers" );

System.out.println(" 1. " + mcNuggetNumber(0));

System.out.println(" 2. " + mcNuggetNumber(1) );

System.out.println(" 3. " + mcNuggetNumber(2) );

System.out.println(" 4. " + mcNuggetNumber(3) );

System.out.println(" 5. " + mcNuggetNumber(4) );

System.out.println(" 6. " + mcNuggetNumber(5) );

System.out.println(" 7. " + mcNuggetNumber(6) );

System.out.println(" 8. " + mcNuggetNumber(7) );

System.out.println(" 9. " + mcNuggetNumber(8) );

System.out.println("10. " + mcNuggetNumber(9) );

System.out.println("11. " + mcNuggetNumber(10) );

System.out.println("12. " + mcNuggetNumber(11) );

System.out.println("13. " + mcNuggetNumber(12) );

System.out.println("14. " + mcNuggetNumber(13));

System.out.println("15. " + mcNuggetNumber(14));

System.out.println("16. " + mcNuggetNumber(15));

System.out.println("17. " + mcNuggetNumber(16));

System.out.println();


System.out.println ("Split the Array");
System.out.println ("1. " + splitArray (1));
System.out.println ("2. " + splitArray (2));
System.out.println ("3. " + splitArray (3));
System.out.println ("4. " + splitArray (4));
System.out.println ("5. " + splitArray (5));
System.out.println ("6. " + splitArray (6));
System.out.println ("7. " + splitArray (7));
System.out.println ("8. " + splitArray (8));
System.out.println ("9. " + splitArray (9));
System.out.println ("10. " + splitArray (10));
System.out.println ("11. " + splitArray (11));
System.out.println ("12. " + splitArray (12));
System.out.println ("13. " + splitArray (13));
System.out.println ("14. " + splitArray (14));
System.out.println ("15. " + splitArray (15)); */
System.out.println ("16. " + splitArray (s11,6,2));
System.out.println();


  
  

}

private static String splitArray(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}


Related Solutions

How do I convert the integers in my 2D array list to output decimals numbers? My...
How do I convert the integers in my 2D array list to output decimals numbers? My program works accepting regular integers, but crashes when decimals are entered into my 2D array list. Can someone improve it so it accepts decimal numbers? Here is my code: import javax.swing.*; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class TwoDimArray { private int numbers[][]; public TwoDimArray() { loadArray(); } public void loadArray() { /* //loadArray() method loads the users defined filename //@return returns the...
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
I have an error on my code. the compiler says 'setLastName' was not declared in this...
I have an error on my code. the compiler says 'setLastName' was not declared in this scope. this is my code : #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> using std::string; class Employee {    public :               Employee(string fname, string lname, int salary)        {            setFirstName(fname);            setLastName(lname);            setMonthlySalary(salary);        }               void setFirstName(string fname)        {       ...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
Hello my name is Pete. I posted my previous question in error by forgetting to mention...
Hello my name is Pete. I posted my previous question in error by forgetting to mention that I am located in New York State and am required to go by those rules and regulations. Erica Swanson (SSN 376-38-4930), age 46, is a single parent with three children: Her biological son, Sean Swanson (age 19) is a part-time college student who works fulltime as a landscaper Her biological daughter, Brooklyn Swanson (age 14) Her adopted daughter, Sydney Swanson (age 12) Although...
The following code has some syntax error. Please fixed the error. Besides, I want the output...
The following code has some syntax error. Please fixed the error. Besides, I want the output in ASCII characters. Please give me the corrected code along with the screenshot of the output. def cbc_dec(ys): int xs = [] int iv = ("0XAA", 16) #in decimal int key = ("0X08", 16) int x0 = chr(((163 * (int (ys[0], 16) - key)) % 256) ^ iv) xs.append(x0) for i in range (1, len(ys)): int xi = chr((( 163 * (int (ys[i], 16)...
I am doing a lab and I get this error message in my unittest: junit.framework.AssertionFailedError: Hint:...
I am doing a lab and I get this error message in my unittest: junit.framework.AssertionFailedError: Hint: matches() should have returned true when item matches the item passed into IdLookup constructor. Here is my code. What changes should be made? /** * This class is a lookup that matches items whose id matches exactly * a specified id, to be passed into the constructor when creating * the instance of the lookup. * * @author Franklin University * @version 2.0 */...
My java program will not compile. I receive the following error; Test.java:9: error: incompatible types: int[]...
My java program will not compile. I receive the following error; Test.java:9: error: incompatible types: int[] cannot be converted to int int size = new int [start]; //Java Program import java.util.Scanner; public class Test{ public static void main(String []args) { int start, num; System.out.println("Enter the number of elements in a array : "); start = STDIN_SCANNER.nextInt(); int size = new int [start]; System.out.println("Enter the elements of array where last element must be '0' : "); for(int i = 0; i...
I am implementing a generic List class and not getting the expected output. My current output...
I am implementing a generic List class and not getting the expected output. My current output is: [0, 1, null] Expected Output in a separate test class: List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size 6: [0, 1, null, Three, null, 4]. list.set(3, "Three"); is going to produce a list of size 5 (unchanged): [0,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT