Question

Please answer using java. For this lab you will practice accessing variables in an array and...

Please answer using java. For this lab you will practice accessing variables in an array and determining if the values are odd. You will finish the method makeOddArray. You will store only the odd values in another array and return that array. Hint: you will need to keep track of how many items you have currently put in the new array and increment that value as you add. This will keep track of your index for the new array.

Below is pre-made code that cannot be changed except in the TODO areas.

import java.util.Arrays;

public class OddArray {

   int[] makeOddArray(int[] orig, int numOdds){
      int[] newArr = new int[numOdds]; // new array that will store only odds
      int currentIndex = 0; // to keep track of how many values are stored in your new array for indexing
      // TODO
    
      // End TODO
      return newArr;

   }

   public static void main(String[] args) {
      // create instance of the class OddArray
      OddArray odd = new OddArray();
      // creating arrays for my oddArray method
      int[] iArr = {2, 4, 3, 9, 11, 10, 16};
      int[] myArr = new int[10];
    
      /* for extra practice add ten int values to myArr using a for loop (not need to finish the assignment) */
      // TODO
    
      // End TODO
    
      // testing your method with iArr
      // the next line should print [3, 9, 11] note that this is not how we normally want you to print arrays
      System.out.println(Arrays.toString(odd.makeOddArray(iArr, 3)));
      // test your method with myArr
      // TODO
      // End TODO
    
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Arrays;

public class OddArray {

    int[] makeOddArray(int[] orig, int numOdds) {
        int[] newArr = new int[numOdds]; // new array that will store only odds
        int currentIndex = 0; // to keep track of how many values are stored in your new array for indexing
        for (int i = 0; i < orig.length; i++) {
            if (orig[i] % 2 == 1) {
                newArr[currentIndex++] = orig[i];
            }
        }
        return newArr;
    }

    public static void main(String[] args) {
        // create instance of the class OddArray
        OddArray odd = new OddArray();
        // creating arrays for my oddArray method
        int[] iArr = {2, 4, 3, 9, 11, 10, 16};
        int[] myArr = new int[10];

        for (int i = 0; i < myArr.length; i++) {
            myArr[i] = i;
        }

        // testing your method with iArr
        // the next line should print [3, 9, 11] note that this is not how we normally want you to print arrays
        System.out.println(Arrays.toString(odd.makeOddArray(iArr, 3)));
        // test your method with myArr
        System.out.println(Arrays.toString(odd.makeOddArray(myArr, 5)));
    }
}

Add a comment
Know the answer?
Add Answer to:
Please answer using java. For this lab you will practice accessing variables in an array and...
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
  • 23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a...

    23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a second oversize array with size2 elements, write a method that returns the first array with the elements of the second appended to the end. If the capacity of the oversize array is not large enough to append all of the elements, append as many as will fit. Hint: Do not construct a new array. Instead, modify the contents of the oversize array inside the...

  • package homework; import java.util.Arrays; import stdlib.*; /** CSC300Homework4 version 1.0 * *    * * Find...

    package homework; import java.util.Arrays; import stdlib.*; /** CSC300Homework4 version 1.0 * *    * * Find the 3 Sections marked TODO: * * TODO #1: SumOddsRecursive * TODO #2: ReverseArray * TODO #3: MergeArrays * * You must not add static variables. You MAY add static functions. * * It is okay to add functions, such as * * <pre> * public static double sumOfOddsHelper (double[] list, int i) { * </pre> * * but it is NOT okay to...

  • 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...

  • please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need to...

    please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need to be done! * * TODO#1: Change the name and date accordingly * Created by , 11/25/2019 */ public class CS140_Arrays_Done { //TODO#2: Define two private static constants: rows (set to 5) & cols (set to 3) //array for the values private static double[][] values = { { 9, 10, 8 }, { 3.5, 10, 8.5 }, { 10, 8.5, 9 }, { 8.5, 10,...

  • import java.util.Arrays; import stdlib.*; public class CSC300Homework4 {    /**    * As a model for...

    import java.util.Arrays; import stdlib.*; public class CSC300Homework4 {    /**    * As a model for Problem 1, here are two functions to find the minimum value of an array of ints    * an iterative version and a recursive version    *    * precondition: list is not empty    /** iterative version */    public static double minValueIterative (int[] list) {        int result = list[0];        int i = 1;        while (i <...

  • JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array...

    JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...

  • Java template public class GiftCard { // Instance Variables // TODO: balance // Constructors /** *...

    Java template public class GiftCard { // Instance Variables // TODO: balance // Constructors /** * GiftCard(double initialBalance) * * Initialize a GiftCard object with a pre-defined amount of money. * * @param initialBalance double The desired starting balance */ public GiftCard(double initialBalance) { setBalance(initialBalance); } /** * GiftCard() * * Initialize a GiftCard object with a balance of $0.00. */ public GiftCard() {} // Instance Methods /** * setBalance * * This should set the balance of the gift...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • Programming in java In this part of this Lab exercise, you will need to create a...

    Programming in java In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...

  • HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a publ...

    HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a public non-final class named Quicksort that extends Partitioner. Implement a public static method void quicksort (int] values) that sorts the input array of ints in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. You should sort the array in place, which is why your function is declared to return void. If...

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