Question

Instructions Write a Java code Your goal is to take N integer inputs from the user...

Instructions

Write a Java code

  • 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

Homework Answers

Answer #1

Have a look at the below code. I have put comments wherever required for better understanding.

import java.util.*;

class Main {
  // create the checkArray function
  public static void checkArray(int[] arr, int n){
    // loop in through the array
    for(int i=0;i<n;i++){
      // check if number is divisible by both 15 and 20
      if ((arr[i]%15==0)&&(arr[i]%20==0)){
        System.out.println(arr[i]+":"+"MULT15"+":" + "MULT20");
      }
      // check if number is divisible by 15 but not 20
      if ((arr[i]%15==0)&&(arr[i]%20!=0)){
        System.out.println(arr[i]+":"+"MULT15"+":" + "!MULT20");
      }
      // check if number is divisible by 20 but not 15
       if ((arr[i]%15!=0)&&(arr[i]%20==0)){
        System.out.println(arr[i]+":"+"!MULT15"+":" + "MULT20");
      }
      // check if number is not divisible by both 15 and 20
       if ((arr[i]%15!=0)&&(arr[i]%20!=0)){
        System.out.println(arr[i]+":"+"!MULT15"+":" + "!MULT20");
      }
    }



  }
  public static void main(String[] args) {
    // create scanner object for user input
    Scanner sc = new Scanner(System.in);
    // create variable for n
    int n;
    // take user input for n
    n = sc.nextInt();
    // create an array of size n
    int arr[] = new int[n];
    // populate the array
    for (int i=0;i<n;i++){
      arr[i] = sc.nextInt();
    }
    // call the checkArray function
    checkArray(arr,n);
  }
}

Happy Learning!

Know the answer?
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
Similar Questions
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two...
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two different classes in the program being made as objects in the main (Bunny class and Dog Class). ask the user "what kind of animal would you like to sort?" user chooses the type of animal (bunny or dog), then you ask the user how big would you like the array of animals to be (maximum array size = 20)? and then you ask what...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
JAVA Write code which takes two inputs from the user, a number of sides followed by...
JAVA Write code which takes two inputs from the user, a number of sides followed by a side length, then creates a regular polygon with that number of sides and side length. Sample run: Type the number of sides: 8 Type a side length: 7.5 regular octagon with side length 7.5 Hint: Make sure you use the right data types when taking user input.
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input and output with the Java console. Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. private static Scanner scanner = new Scanner(System.in); Part A: Create a static method called “divide”. Your method should do the following: Accept two integers as parameters, a...
Write a program which inputs an integer value, incr, from the user at the keyboard, and...
Write a program which inputs an integer value, incr, from the user at the keyboard, and then displays a graph of the sine of the degree values from 0 to 180, in increments of incr. You must use the sin function (in a similar manner to ceil and floor) to find the sine. Note that the sin function only operates on radians so the degree value must first be converted to radians. To do so, multiply the degree value by...
in java pls Write a method that will receive 2 numbers as parameters and will return...
in java pls Write a method that will receive 2 numbers as parameters and will return a string containing the pattern based on those two numbers. Take into consideration that the numbers may not be in the right order. Note that the method will not print the pattern but return the string containing it instead. The main method (already coded) will be printing the string returned by the method. Remember that you can declare an empty string variable and concatenate...
WITH JAVA Follow the instructions in the attached to complete Task#2 and submit work along with...
WITH JAVA Follow the instructions in the attached to complete Task#2 and submit work along with screenshots of your output. I have attached the class code for Task#1 that you can use while completing Task#2. Task#1 CODE /** SocSecException exception class */ public class SocSecException extends Exception { public SocSecException(String error) { super("Invalid the social security number, " + error); } } Task #2 Writing Code to Handle an Exception 1. In the main method: a. The main method should...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...