Question

This is in Java being run through Eclipse. /**    * Creates an array of all...

This is in Java being run through Eclipse.

/**
   * Creates an array of all the powers of two, up to (and including)
   * the given exponent starting at 2^0.
   *
   * If the given exponent is less than zero, return an empty array.
   * You can do this by saying "return new int[0];"
   *
   * For example:
   * powersOfTwo(3) returns {1,2,4,8}
   * powersOfTwo(0) returns {1}
   * powersOfTwo(-66) returns {}
   *
   * Requires: arrays, for loops
   */

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Arrays;

public class ArrayPowers {
   public static void main(String[] args) {
       System.out.println(Arrays.toString(getPowers(3)));
   }

   public static double [] getPowers(int n) {
       if(n<0)
           return new double[0];
       // creating array with size
       double d []= new double[n+1];
       // finding powers
       for(int i=0;i<=n;i++){
           d[i]=Math.pow(2, i);
       }
       return d;
   }
}

Add a comment
Know the answer?
Add Answer to:
This is in Java being run through Eclipse. /**    * Creates an array of all...
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
  • Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array...

    Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array of 100 integers. Initialize the array to have all zeros. 2. Puts even numbers starting from 6 (excluding multiples of 12) into the first 50 slots of the array above. So numbers 6,8,10,14,16,18,20,22,26, etc. go into the first 50 positions in the array. Print the array. 3. Puts random numbers between 45 and 55 into the second 50 slots of the array above (second...

  • in java eclipse, Create a program “FibonacciFifteen.java” and a method called “double[] getFibonacci()” that creates an...

    in java eclipse, Create a program “FibonacciFifteen.java” and a method called “double[] getFibonacci()” that creates an array with a length of 15 that contains the first 15 numbers in the Fibonacci sequence and returns it. Set the first element to 0 and the second element to 1, then use a for loop to fill out the rest of the array by adding the prior two elements.

  • In java run through eclipse /**    * Adds a team.    *    * You...

    In java run through eclipse /**    * Adds a team.    *    * You can assume the student team name is unique.    *    * (for individual assignment): the student names should be students    * that have been already added    *    * PAIR ASSIGNMENT: implicitly create students if they don't exist    *    * @param teamName name of new team    * @param memberNames a list of student names that belong to the...

  • In java, write method, shuffle, which accepts an array of int, creates a copy of the...

    In java, write method, shuffle, which accepts an array of int, creates a copy of the formal parameter, shuffles the copy, then returns the copied, shuffled array, leaving the formal parameter unchanged. Shuffling is a process of swapping each element of array with another element randomly chosen from the array. For testing the shuffle method have main call shuffle, repeatedly having it shuffle its previously returned array until the returned (shuffled) array is identical (equal) to the original array that...

  • Based on the preconditions, did i coded this function correctly in c++? /* Creates an array...

    Based on the preconditions, did i coded this function correctly in c++? /* Creates an array of HuffmanNodes (more specifically, pointers to HuffmanNodes) based on the given character-frequency pairs. characters: an array of char frequencies: an array of int length: the length of characters and frequencies arrays returns: an array of HuffmanNode* Note: For 0 <= i < length, the frequency of the character characters[i] is found at frequencies[i]. */ HuffmanNode **genHuffmanNodes(char *characters, int *frequencies, int length) { // TODO...

  • in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in...

    in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in descending order. For example [5, 4, 2]. Return an array with all the elements of these two arrays sorted, also in descending order. partb: if the two arrays as input are size n each. What is the big-O run time and space complexity of your implementation public int[] merge(int[] arr1, int[] arr2){

  • (9) Complete the below Java method recursively, which takes in a String and returns if it's...

    (9) Complete the below Java method recursively, which takes in a String and returns if it's a palindrome. Remember a palindrome means a string that is the same reversed, like "deed", "racecar" or "tacocat". The empty string "" is also a palindrome. public boolean isPalindrome (String word) { (8) (a) (8 points) Implement merging two sorted arrays into one sorted ar- ray. These arrays are sorted in descending order. For example, (5, 4, 2). Return an array with all the...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

  • JAVA Help Please. Thank you! /**    * deDupAndReverse returns a new array containing the unique...

    JAVA Help Please. Thank you! /**    * deDupAndReverse returns a new array containing the unique values in the    * original array reversed. There should not be any extra space in the array --- there should    * be exactly one space for each unique element (Hint: numUnique tells you    * how big the array should be). You may assume that the list is sorted, as    * you did for numUnique.    *    * Your solution...

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

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