Question

Arrays and Methods Worksheet 2 (10 pts) Copy the following into a new file called ArrayMethods2.java....

Arrays and Methods Worksheet 2 (10 pts)

  • Copy the following into a new file called ArrayMethods2.java.
  • Write the methods one-by-one. Compile and run your code after you write each method to make sure it was written properly.
  • When your methods pass all of the tests, upload your file to Canvas.

/**
* Assignment 20.2
* @author Your Name

* CIS 36A

*/
public class ArrayMethods2 {

    /**
    * Given an array of ints, return the index of the first appearance of the
    * number 42. If not found return -1.
    * @param data array of ints
    * @return the index of the first number 42, -1 if not found.
    * Test cases:
    * findFirst42({1, 1, 42, 3, 1}) --> 2
    * findFirst42({1, 1, 2, 42, 1}) --> 3
    * findFirst42({1, 1, 2, 1, 2, 3}) --> -1
    */
    public static int findFirst42(int[] data) {
        
        return -1;
    }

    /**
    * Given an array of ints, return true if
    * the sequence .. 1, 2, 3, .. appears
    * in the array somewhere.
    * @param data array of ints
    * @return true if .. 1, 2, 3, .. appears in the array,
    * false otherwise.
    * Test cases:
    * is123({1, 2, 3, 1}) --> true
    * is123({1, 2, 4, 1}) --> false
    * is123({1, 2, 1, 2, 3}) --> true
    */
    public static boolean is123(int[] data) {
        
        return false;
    }

    /**
    * Prints all the elements in an array to the console
    * on one line with a space between each element.
    * @param data The array to print.
    */
    public static void printArray(int[] data) {
        
        System.out.println();
    }

    /**
    * Given an array of ints, delete first appearance of the number 42.
    * By replacing it with a 0.
    * If the number does not exist in the array, leave the array unchanged.
    * @param data array of ints
    * Hint: use return; to end the method early!
    * Test cases:
    * eraseFirst42({1, 2, 42, 3, 1}) --> {1, 2, 0, 3, 1}
    * eraseFirst42({1, 2, 3, 42, 42, 1}) --> {1, 2, 3, 0, 42, 1}
    * eraseFirst42({1, 2, 3}) --> {1, 2, 3}
    */
    public static void eraseFirst42(int[] data) {

        return;

    }



    public static void main(String[] args) {
        boolean answer = false;
        int num = 0;
        
        System.out.println("***Testing findFirst42***");
        final int A3 = 3, A42 = 42;
        int[] data1 = {1, 2, A42, A3, 1};
        num = findFirst42(data1);
        System.out.println("findFirst42a should be 2: " + num);
        int[] data2 = {1, 1, 2, A42, 1};
        num = findFirst42(data2);
        System.out.println("findFirst42a should be 3: " + num);
        int[] data3 = {1, 1, 2, 1, 2};
        num = findFirst42(data3);
        System.out.println("findFirst42a should be -1: " + num);

        System.out.println("\n***Testing is123***");
        final int A4 = 4;
        int[] data4 = {1, 2, A3, 1};
        answer = is123(data4);
        System.out.println("is123a should be true: " + answer);
        int[] data5 = {1, 2, A4, 1};
        answer = is123(data5);
        System.out.println("is123a should be false: " + answer);
        int[] data6 = {1, 2, 1, 2, A3};
        answer = is123(data6);
        System.out.println("is123a should be true: " + answer);
    
        System.out.println("\n***Testing printArray***");
        System.out.println("printArray1 should be 1 2 3 1: ");
        printArray(data4);
        System.out.println("printArray2 should be 1 2 4 1: ");
        printArray(data5);
        final int A7 = 7;
        int[] data7 = {A7};
        System.out.println("printArray1 should be 7: ");
        printArray(data7);
   
        System.out.println("\n***Testing eraseFirst42***");
        eraseFirst42(data1);
        System.out.println("eraseFirst42a should be 1 2 0 3 1: ");
        printArray(data1);
        int[] data8 = {1, 2, A3, A42, A42, 1};
        eraseFirst42(data8);
        System.out.println("eraseFirst42b should be 1 2 3 0 42 1: ");
        printArray(data8);
        int[] data9 = {1, 2, A3};
        eraseFirst42(data9);
        System.out.println("eraseFirst42c should be 1 2 3: ");
        printArray(data9);
    
        System.out.print("\n***End of Tests***");

    }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class ArrayMethods2 {

    /**
     * Given an array of ints, return the index of the first appearance of the
     * number 42. If not found return -1.
     *
     * @param data array of ints
     * @return the index of the first number 42, -1 if not found.
     * Test cases:
     * findFirst42({1, 1, 42, 3, 1}) --> 2
     * findFirst42({1, 1, 2, 42, 1}) --> 3
     * findFirst42({1, 1, 2, 1, 2, 3}) --> -1
     */
    public static int findFirst42(int[] data) {
        for (int i = 0; i < data.length; ++i) {
            if (data[i] == 42) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Given an array of ints, return true if
     * the sequence .. 1, 2, 3, .. appears
     * in the array somewhere.
     *
     * @param data array of ints
     * @return true if .. 1, 2, 3, .. appears in the array,
     * false otherwise.
     * Test cases:
     * is123({1, 2, 3, 1}) --> true
     * is123({1, 2, 4, 1}) --> false
     * is123({1, 2, 1, 2, 3}) --> true
     */
    public static boolean is123(int[] data) {
        for (int i = 0; i < data.length - 2; ++i) {
            if (data[i] == 1 && data[i + 1] == 2 && data[i + 2] == 3) {
                return true;
            }
        }
        return false;
    }

    /**
     * Prints all the elements in an array to the console
     * on one line with a space between each element.
     *
     * @param data The array to printTriangleRecursiveHelper.
     */
    public static void printArray(int[] data) {
        for (int i = 0; i < data.length; ++i) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }

    /**
     * Given an array of ints, delete first appearance of the number 42.
     * By replacing it with a 0.
     * If the number does not exist in the array, leave the array unchanged.
     *
     * @param data array of ints
     *             Hint: use return; to end the method early!
     *             Test cases:
     *             eraseFirst42({1, 2, 42, 3, 1}) --> {1, 2, 0, 3, 1}
     *             eraseFirst42({1, 2, 3, 42, 42, 1}) --> {1, 2, 3, 0, 42, 1}
     *             eraseFirst42({1, 2, 3}) --> {1, 2, 3}
     */
    public static void eraseFirst42(int[] data) {
        for (int i = 0; i < data.length; ++i) {
            if (data[i] == 42) {
                data[i] = 0;
                break;
            }
        }
    }


    public static void main(String[] args) {
        boolean answer = false;
        int num = 0;

        System.out.println("***Testing findFirst42***");
        final int A3 = 3, A42 = 42;
        int[] data1 = {1, 2, A42, A3, 1};
        num = findFirst42(data1);
        System.out.println("findFirst42a should be 2: " + num);
        int[] data2 = {1, 1, 2, A42, 1};
        num = findFirst42(data2);
        System.out.println("findFirst42a should be 3: " + num);
        int[] data3 = {1, 1, 2, 1, 2};
        num = findFirst42(data3);
        System.out.println("findFirst42a should be -1: " + num);

        System.out.println("\n***Testing is123***");
        final int A4 = 4;
        int[] data4 = {1, 2, A3, 1};
        answer = is123(data4);
        System.out.println("is123a should be true: " + answer);
        int[] data5 = {1, 2, A4, 1};
        answer = is123(data5);
        System.out.println("is123a should be false: " + answer);
        int[] data6 = {1, 2, 1, 2, A3};
        answer = is123(data6);
        System.out.println("is123a should be true: " + answer);

        System.out.println("\n***Testing printArray***");
        System.out.println("printArray1 should be 1 2 3 1: ");
        printArray(data4);
        System.out.println("printArray2 should be 1 2 4 1: ");
        printArray(data5);
        final int A7 = 7;
        int[] data7 = {A7};
        System.out.println("printArray1 should be 7: ");
        printArray(data7);

        System.out.println("\n***Testing eraseFirst42***");
        eraseFirst42(data1);
        System.out.println("eraseFirst42a should be 1 2 0 3 1: ");
        printArray(data1);
        int[] data8 = {1, 2, A3, A42, A42, 1};
        eraseFirst42(data8);
        System.out.println("eraseFirst42b should be 1 2 3 0 42 1: ");
        printArray(data8);
        int[] data9 = {1, 2, A3};
        eraseFirst42(data9);
        System.out.println("eraseFirst42c should be 1 2 3: ");
        printArray(data9);

        System.out.print("\n***End of Tests***");
    }
}

Add a comment
Know the answer?
Add Answer to:
Arrays and Methods Worksheet 2 (10 pts) Copy the following into a new file called ArrayMethods2.java....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi All, Can someone please help me correct the selection sort method in my program. the...

    Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /**...

    /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; }    /** * Create a String that...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...

    The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • I have a Graph.java which I need to complete four methods in the java file: completeGraph(),...

    I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...

  • Need help with this Java project implementing an interpolation search, bolded the missing parts: /**   ...

    Need help with this Java project implementing an interpolation search, bolded the missing parts: /**    A class that implements an interpolation search and    a binary search of a sorted array of integers.    @author Frank M. Carrano    @author Timothy M. Henry    @version 4.0 */ public class SearchComparer {    private int[] a;    private int interpolationSearchCounter;    private int binarySearchCounter;    private static final int CAPACITY = 50;    public SearchComparer(int[] array, int n)    {...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT