Question

1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line.

public static void printArray (int[] values){

Methods that do NOT return a result have “void” as return

type. Notice how an array parameter type is passed, int []

}// end printArray

2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all entries in the array initialized to 3.0.

public static double[] arrayDouble (int num) {

Note the return type of this method: it is double[]

indicating that an array of type double is to be returned

What is the printArray? Can you explain this question? Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//printArray prints out the input array elements one element per line.
public static void printArray (int[] values){
  for(int i = 0;i<values.length;i++){
    System.out.println(values[i]);
  }
}


public static double[] arrayDouble (int num) {
  double res[] = new double[num];
  for(int i = 0;i<num;i++){
    res[i] = 3.0;
  }
  return res;
}

Please upvote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
1.Write code for a Java method, printArray, that takes an int array as parameter and prints...
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 method named printReverse() that accepts an int array as its parameter and prints that...

    Write a method named printReverse() that accepts an int array as its parameter and prints that array in reverse order. This method should not have a return statement (void). - All elements should be printed on the same line, separated by a space - A new line should be printed after the entire array prints - If the array passed in held the values: {3,2,1} - Output: 1 2 3 //newline printed here

  • Write the definition of a method printArray, which has one parameter, an array of ints. The...

    Write the definition of a method printArray, which has one parameter, an array of ints. The method does not return a value. The method prints out each element of the array, on a line by itself, in the order the elements appear in the array, and does not print anything else.

  • Write a static method called printWithSpaces that takes a String as its parameter and prints the...

    Write a static method called printWithSpaces that takes a String as its parameter and prints the characters of the string separated by spaces. For example: > Methods.printWithSpaces("method") m e t h o d You should have a single space after the last character. This method should not return a value. That is similar to this code for printing the string vertically public static void printVertical(String s) { for (int i = 0; i < s.length(); i++) { char c =...

  • X43: countEvens Write a function in Java that takes an int array as its parameter and...

    X43: countEvens Write a function in Java that takes an int array as its parameter and returns the number of even ints it contains. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1. Complete the code bellow. Complete the code below. Use: logic conditionals if arrays loops mod public int countEvens(int[] nums) { }

  • JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers,...

    JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers, and returns the sum of all the odd numbers in the array. The method should return 0 if the array contains no odd numbers. For example, if the given array is [12, 10, 5, 8, 13, 9] then the method should return 27 (5+13+9=27). Starter Code: public class Odds { public static int sumOdds(int[] numbers) { //TODO: complete this method    } }

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

  • All JAVA code 6.2.2: Define a method printFeetInchShort, with int parameters numFeet and numInches, that prints...

    All JAVA code 6.2.2: Define a method printFeetInchShort, with int parameters numFeet and numInches, that prints using ' and " shorthand. Ex: printFeetInchShort(5, 8) prints: 5' 8" Hint: Use \" to print a double quote Sample program: import java.util.Scanner; public class HeightPrinter { /* Your solution goes here */ public static void main (String [] args) { printFeetInchShort(5, 8); System.out.println(""); return; } } 6.4.1: Define stubs for the methods called by the below main(). Each stub should print "FIXME: Finish...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • AP computer science: please do not use other advanced ways to solve this problem, it is...

    AP computer science: please do not use other advanced ways to solve this problem, it is just an AP class in high school. Thank you so much! publie class ArrayMethods public class Array Methods // precondition: anArray is not null. target is an element in the array // postcondition: absolute value of difference between target and average of // precondition: anArray is not null // postcondition: anArray is output anArray is returned. public static void PrintArray (int [0 anArray) public...

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

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