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 C++ program that will take and validates from the user an integer n between...
Write a C++ program that will take and validates from the user an integer n between 1 and 4. The program will then continue by taking 5 characters from the user. The program will ask the user whether he needs to rotate the characters to the left or to the right. If the user enters a wrong answer, the program will do a rotation to the right. Depending on the answer, the program will rotate the characters "n" times and...
Write a java program that asks the user for a positive integer N and then calculates...
Write a java program that asks the user for a positive integer N and then calculates a new value for N based on whether it is even or odd: if N is even, the new N is N/2. (Use integer division.) if N is odd, the new N is 3*N + 1. Repeat with each new N until you reach the value 1. For example, say the initial value is 12. Then the successive values are: 12 (even, next value...
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...
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
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.
Please write a JAVA program which reads a number n from the user and check whether...
Please write a JAVA program which reads a number n from the user and check whether n is a perfect number or not. For example, when n = 7, the print out should be 7 is not a perfect number. If the input n is 6, then the program prints out 6 = 1 * 2 * 3
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...
Java Write a program that reads 4 integer numbers from the user combine it into a...
Java Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu.                       4. Write an application...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them into a Text File. The inputs are Student Name, Student Address and student Date of Birth. Also write a simple program that reads and display the input from the first program text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT