Question

PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each

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

Code:

import java.util.Scanner;

public class PrintArray{

               static Scanner in = new Scanner(System.in);

               public static void main(String args[]){

                             

                              int[] aa = getArray();

                              //code to print each element of aa on a separate line

                              for(int i = 0; i < aa.length; i++)

                                             System.out.println(aa[i]);

                              //the code ends here

               }

               public static int[] getArray(){

                              String nums;

                              int numint = 0;

                              int xx;

                             

                              System.out.print("enter ints: ");

                              nums = in.nextLine();

                             

                              Scanner scan = new Scanner(nums);

                             

                              while (scan.hasNextInt()){

                                             xx = scan.nextInt();

                                             numint++;

                              }

                              int[] intArray = new int[numints];

                              scan = new Scanner(nums);

                             

                              for(int i = 0; i < numint; i++)

                                             intArray[i] = scan.nextInt();

                             

                              return intArray;

               }

}

- import java.util.Scanner; public class PrintArray static Scanner in = new Scanner (System.in); public static void main(Stri

Output:

run: enter ints: 10 20 30 40 50 10 20 30 50 BUILD SUCCESSFUL (total time: 15 seconds)

Add a comment
Know the answer?
Add Answer to:
PrintArray vi Create a class called PrintArray. This is the class that contains the main method....
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
  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

  • Searching an array: --Using the template provided, create a method called search that takes an int...

    Searching an array: --Using the template provided, create a method called search that takes an int and an array as parameters. The method search should traverse the array to look for the int. search should print whether it found the int or not, for example: found: 10 did not find: 11 examples (bold face is text entered by user) % java SearchArray 20 found: 20 % java SearchArray 13 did not find: 13 % java SearchArray 100 found: 100 %...

  • File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute...

    File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute the factorials of integers entered by the user. In this program, two things can possibly occur: The program always returns 1 if negative integers are entered by the user. Returning 1 as the factorial of any negative integer is not correct. The program also returns negative numbers when the user enters integers greater than 16. Returning a negative number for values over 16 also...

  • 5. Write a static method "f(n)" in the space provide, that returns O if n is...

    5. Write a static method "f(n)" in the space provide, that returns O if n is even, and if n is odd, returns 1 or -1 according as n is greater than or less than 0. importjava.util.Scanner; public class Q_05 Your "f(n)" method: public static void main(String args[]) mana int n; Scanner input = new Scanner(System.in); System.out.print("Please enetrn: "); n=input.nextInt(); System.out.println(f(n)); "Method f(n)" 7. Write a static method "max" in the space provide, that returns the maximum value from 3...

  • Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random;...

    Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class RQS { public static int[] prepareData(int[] patients){ /* Data is prepared by inserting random values between 1 and 1000. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ // what is our range? int max = 1000; // create instance of Random class Random randomNum...

  • Using java: Use the following starter code: public class HandsOn01 { public //// insert your code...

    Using java: Use the following starter code: public class HandsOn01 { public //// insert your code here, after the word public, on the same line public static void main(String[] aaa){ } } -- declare an array of int -- creates the array in memory capable of containing 5 ints -- the name of the array must be arrayOfInts -- put your code in the place indicated in the starter code

  • So this is all I have done right now. Next steps: 1. Create a method called "printArray" which wo...

    So this is all I have done right now. Next steps: 1. Create a method called "printArray" which would put the code : for(int i = 0; i < myArray.length; i++) System.out.println(myArray[i]); at the bottom of that screen and put it into a method and then call it. 2. Create another method thats going to count all the numbers that are evenly divisible by 10 and call the method whatever you like. * Just make sure to return, pass at...

  • LE 7.1 Create a separate class called Family. This class will have NO main(). Insert a...

    LE 7.1 Create a separate class called Family. This class will have NO main(). Insert a default constructor in the Family class. This is a method that is empty and its header looks like this: public NameOfClass() Except, for the main(), take all the other methods in LE 6.2 and put them in the Family class. Transfer the class variables from LE 6.2 to Family. Strip the word static from the class variables and the method headers in the Family...

  • Q: Create a version of the UnitConverter application to convert from inches to foot. Read the...

    Q: Create a version of the UnitConverter application to convert from inches to foot. Read the inches value from the user. A: import java.util.*; class UnitConverter{    public static void main(String[] args){ int inches; int foot; Scanner scan=new Scanner(System.in); System.out.println("Enter inches:"); inches=scan.nextInt(); System.out.println(inches/12+" feet and "+inches%12+" inches."); } } Q: Write a program that converts grams to pounds. (One pound equals 453.592 grams.) Read the grams value from the user as a floating point value. A: import java.util.*; class GramsToPounds{...

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