Question

In: Computer Science

Java programming Create a application with a method void (after main() ) that creates an array...

Java programming

Create a application with a method void (after main() ) that creates an array and asks for the user fill it with float numbers.
This array have infinite elements until the user decide that it is enough and input a command to stop.

Display this array's elements data in another method void.

Thanks for your help!

Solutions

Expert Solution

The below code will do your requirements. Note that, when the code ask you for Do you want to enter array element? Enter 1(if yes) Enter 0(if no): you must respond with an integer else the program will raise an exception. If you enter a integer not(0/1) then an error message is displayed and ask you to eneter correct option. See the code below for better understanding:

import java.util.Scanner;  
public class MyClass {
    //function to display array
    public static void disparray(float[] a, int n){
        int i; //for loop through array elements
        System.out.println("Array elements are: ");       //printing message that array elements are printing below
        //for loop to displaying array elements 
        for(i=0;i<n;i++)
            System.out.println(a[i]);       //displaying each array elements
    }
    
    //function to input array
    public static void inputarray(){
        //creating scanner object
        Scanner sc = new Scanner(System.in);
        float[] a = new float[100]; //initializing a floating array
        int f = 1; //variable to store flag
        int i=0; //variable to loop arrray elements
        
        //loop for getting array elements
        do{
            //asking user whether to eneter element or not user must respond with a integer(entry message)
            System.out.println("Do you want to enter array element? Enter 1(if yes) Enter 0(if no): ");
            
            //getting user response
            f=sc.nextInt();
            
            //cheking user response
            if(f==1){                                           //if user response is 1
                System.out.println("Enter array element: ");     //message to enter array element
                a[i] = sc.nextFloat();                          //getting array element
                i++;                                            //incrementing array index
            }
            else if(f==0)                                       //if user response is 0           
                System.out.println("Array entry stopped!");         //showing message for stopping entry
            else{                                                        //if user response is a integer but neither 0 or 1
                System.out.println("ERROR: Enter the correct option");         //showing to enter the correct option
                f=1;                    //setting flag to 1 (for asking the entry message)
            }
        }while(f==1);            //continue loop when user response is 1
        //calling function to print array with the entered index
        disparray(a, i);
    }
    public static void main(String args[]) {
      
      //calling the method to insert array
      inputarray();
    }
}

Sample OUTPUT:


Related Solutions

Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text and String key consisting of integers only, such that int rows=(int)Math.ceil(text.length()/key.length()); // or int rows=(int)Math.ceil(text.length()/key.length()); ? int columns= key.length(); int remainder= text.length() % key.length(); // such that the last row avoids taking an index beyond the string text by making columns - remainder The method fills the 2d array with letters a String entered by the use (row by row)....
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
in java. using the following template as a method, public static void shellSort(int[] array) { create...
in java. using the following template as a method, public static void shellSort(int[] array) { create a program that counts the number of comparisons and swaps of the sorting method does using int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; //...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based...
java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based stack to read a sentence from the keyboard, and reverse the order of the letters in each word. Print the initial sentence, as well as the result of the reversal operation. Your Tasks: Using the information given in your textbook, create a class named ArrayBasedStack, that will help you solve the problem given above by providing the stack operations you need for this application....
Create a program “Fib.java” and a method called “double[] getFib)” that creates an array with a...
Create a program “Fib.java” and a method called “double[] getFib)” 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.
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
Create a Java application that creates a window in which multiple bouncing balls can be animated....
Create a Java application that creates a window in which multiple bouncing balls can be animated. have attached a text file of the source found on that site. You can paste the entire application into an Eclipse source file named BounceThread.java and it will compile and run. Optionally, you may prefer to put some or all of the classes into separate files to organize your work. Your task is to add the following capability to the application: (5%)    Increase the...
Create a project plan on the game or application you are creating. Using java programming. The...
Create a project plan on the game or application you are creating. Using java programming. The project plan should include the following: A description of the game or application The IDE or game engine your plan to use to create the game or app and information on how you are going to develop the game or app If you choose to create a game, how are you going to approach the game design and game development process or if you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT