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

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

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

  • Q1) How would you declare an array of doubles called myDoubles? Arrays can be initialized in...

    Q1) How would you declare an array of doubles called myDoubles? Arrays can be initialized in one of two ways. In one method, the array elements are placed in a list enclosed in curly braces after the array name definition. For example, the code below creates an array of ints with 3 elements: 1, 2 and 3. int[] a = {1, 2, 3, 4}; We can also initialize an array with a new construct, indicating how many elements we want...

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

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

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

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