Question

Input and output of the contents of an array are such frequently used operations that they...

Input and output of the contents of an array are such frequently used operations that they ought to be packaged as methods. Define two methods that do just that. They should be general; that is, they should work for any array of integers. The heading of the array output method should have the form void ( [ ] int lenth) where the parameter length represents the number of data elements in the array. The heading of the array input method should have the form void ( [ ] int lenth)where the parameter length represents the size of the array before the data are input, and the number of data values actually entered after input. Test your methods until they behave as expected. ( Needs to be coded in JAVA)

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

//ArrayOperatoons.java

import java.util.Scanner;
public class ArrayInOutOperations
{
public void input(int[]arr,int length)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter Array elements: ");
for(int i = 0; i < length; i++)
{
arr[i]=in.nextInt();
}
}
public void output(int[]arr,int length)
{
System.out.println("Elements of array are: ");
for(int i = 0; i < length;i++)
{
System.out.printf("%d ",arr[i]);
}
System.out.println("\n");
}
}

----------------------------------------------------

//Main.java

public class Main

{

public static void main(String[] args) {

ArrayInOutOperations inOut= new ArrayInOutOperations();

int arr[] = new int[5];

  

inOut.input(arr,5);

inOut.output(arr,5);

//System.out.println("Hello World");

}

}

---------------------------------------------

//input

Enter Array elements:

1 2 3 4 5

//output

Elements of array are:

1 2 3 4 5

Add a comment
Know the answer?
Add Answer to:
Input and output of the contents of an array are such frequently used operations that they...
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
  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

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

  • Java code COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing...

    Java code COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing Problem Description Ali Wali owns a coconut plantation and a number of monkeys for bringing down coconuts. Every day, Ali would record the number of coconuts brought down by his monkeys. At the end of a certain time period, he would determine from the data recorded, the lowest and the highest number of coconuts as well as their number of occurrences. Write a program...

  • Exercise 5: Design and implement a Java program (name it ArrayMethods), that defines 4 methods as...

    Exercise 5: Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) determines and returns the largest value within an array int arrayMin(int[] arr) determines and returns the smallest value within an array void arrayReverse(int[] arr) reverses the array (for example: 7 8 1 2 becomes 2 1 8 7) void arraySquared(int[] arr) changes every value within the array to value² Test your methods by creating an array of length 5 within...

  • Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing!...

    Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing! Problem Description The inner product (also known as the dot product or scalar product) of the elements of set A and the elements of set B of size is defined as the sum of the products of corresponding terms. For example, given set A as the array (2, 3, 4, 5, 6, 7, 8, 9; and set B as 6,5, 4, 3, 2, 7,...

  • Java Array The method rotateLeft() takes in a reference a to an integer array and a...

    Java Array The method rotateLeft() takes in a reference a to an integer array and a positive integer n and it returns a new array whose contents is the contents of the input array rotated to the left n places. So each element a[i] of the input array should be placed at location b[i-n] of the returned array. If the index i-n is negative, then that index should "wrap" around to the end of the output array. For example, if...

  • Using JAVA, write an application that uses an Array of 30 Numbers (random integers from 1...

    Using JAVA, write an application that uses an Array of 30 Numbers (random integers from 1 - 100) and returns the maximum number, minimum number, average of all numbers, and prints a Bar Chart to show the number distribution (1-9, 10-19,20-29, …80-89, 90-100). Note; maximum number, minimum number, average number, and the Bar Chart must use implemented as separate methods in a separate class. Method call should pass an array as an argument. Methods should accept an array as an...

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

  • Consider the following program that reads students’ test scores into an array and prints the contents of the array. You will add more functions to the program. The instructions are given below.

    Consider the following program that reads students’ test scores into an array and prints the contents of the array.   You will add more functions to the program.  The instructions are given below.              For each of the following exercises, write a function and make the appropriate function call in main.Comments are to be added in the program. 1.       Write a void function to find the average test score for each student and store in an array studentAvgs. void AverageScores( const int scores[][MAX_TESTS],                       int...

  • +array:int[] This is provided to facilitate testing and grading. Use it as your internal array. +size:int...

    +array:int[] This is provided to facilitate testing and grading. Use it as your internal array. +size:int i.e. after items are added to the array, or deleted from it. One default constructor (receives nothing and initializes an array of size 0), and another constructor that receives an int[] and initializes the internal array with the input parameter. both of the constructors exist and work fine. In initializing the internal array with the input parameter, you should make a copy of the...

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