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...
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.
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...
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.
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...
Instructions JAVA PROGRAMMING 1. Ask the user for a year input (positive integer - should be...
Instructions JAVA PROGRAMMING 1. Ask the user for a year input (positive integer - should be between 1500 and 2019, inclusive). 2. If the year input is not between 1500 and 2019, do not check for leap year, rather print that this year cannot be checked. 3. Take a year input from the user (should be between 1500 and 2019) 4. If the input year is a leap year, print that year is a leap year; otherwise, print that the...
JAVA Write code which takes three decimal inputs from the user, creates a circle with a...
JAVA Write code which takes three decimal inputs from the user, creates a circle with a radius equal to the first input and a rectangle with length and width equal to the second and third input respectively, then prints both of these shapes. Sample run: Type a radius: 3.7 Type a length: 4.9 Type a width: 8.6 circle with radius 3.7 rectangle with length 4.9, width 8.6
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT