Question

In: Computer Science

Write in java please Instructions Your goal is to take N integer inputs from the user...

Write in java please

Instructions

  • Your goal is to take N integer inputs from the user -- N's value will be given by the user as well.
    • You can assume the user provides a valid value for N, i.e., >0.
  • Store the input integers in an array of size N in the order they are provided.
    • These tasks should be done in the main() method.
  • Create a new method called checkArray() that will take the previously created array as input from main().
  • Decide whether each member of the array is a multiple of 15 and 20 or not.
  • And, print the decision in the same way as shown below.
  • You can use the code structure given below.
  • If you do not follow any of the instructions, you will lose points.

Code Structure

main(){

N = take input from user;

arr = create a N-size array.

populate the array with user inputs.

call the checkArray() method and send arr.

}

checkArray(){

for each array member of arr, check if it is a multiple of 15 and 20 or not.

print the results.

}

Sample Output

  • Your output should look exactly like the following; otherwise, you will lose points.

What is the size of the array? 3

Give me 3 numbers. 10 20 15

10: !MULT15: !MULT20

20: !MULT15: MULT20

15: MULT15: !MULT20

Solutions

Expert Solution

import java.util.Scanner;
import java.util.Arrays;
public class multipliers
{
   public static void checkArray(int [] arr)
   {
       for(int i=0; i<arr.length; i++)
       {
           String str1;
           String str2;
           if(arr[i]%15 == 0)
           {
               str1="MULT15: ";
           }
           else
           {
               str1="!MULT15: ";
           }
           if(arr[i]%20 == 0)
           {
               str2="MULT20";
           }
           else
           {
               str2="!MULT20";
           }
           System.out.println(+arr[i] +": " +str1 +str2);
       }
   }
   public static void main(String args[])
   {
   int N = 0;
   int[] arr;
   Scanner scan = new Scanner(System.in);
   System.out.print("What is the size of the array? ");
   if (scan.hasNextInt())
   {
       N = scan.nextInt();
       scan.nextLine();
}
   arr = new int[N];
   System.out.print("Give me " +N +" numbers. ");
   String[] strNums = null;
if (scan.hasNextLine())
   {
       strNums = scan.nextLine().split(" ");
}
if (strNums != null)
   {
for (int i = 0; i < N; i++)
       {
try
           {
arr[i] = Integer.parseInt(strNums[i]);
}
           catch (Exception e)
           {
System.out.println("Invalid input");
break;
}
}
}
       checkArray(arr);
   }
}


Related Solutions

Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the...
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the user to think of a number between 0 and n−1, then makes guesses as to what the number is. After each guess, the program must ask the user if the number is lower, higher, or correct. You must implement the divide-and-conquer algorithm from class. In particular, you should round up when the middle of your range is in between two integers. (For example, if...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive...
In Java please. Prompt the user for two string inputs. Perform the following actions on these...
In Java please. Prompt the user for two string inputs. Perform the following actions on these inputs, and print the result of each action: Part I: Concatenate the two strings into one. Compare the two strings and check whether they are the same (ignore the case). Look up the method equalsIgnoreCase() from Java API Convert all the characters in a string to uppercase Look up the method toUpperCase() from Java API Part II: Replace all the 'd' characters with ‘e'...
PYTHON Let n denote an integer entered by the user. Write a program to print n...
PYTHON Let n denote an integer entered by the user. Write a program to print n multiples of 5 in the descending order, with the last number being 5. Print the average of those n multiples
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT