Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Is there a way to code this without using arrays and instead various loops Part 2: Programming: (16 points) Write a Java program called Assignment4.java.

image text in transcribedimage text in transcribed

Is there a way to code this without using arrays and instead various loops

Part 2: Programming: (16 points) Write a Java program called Assignment4.java. There are two tasks in this assignment. Your program will ask for user to choose task 1 or task 2 and based on which it will switch between the two tasks. (use switch for the 2 tasks) After performing the selected task, your program should ask the user if the user wants to do another execution of the program. If the user chooses to continue, then your program should loop again and display the option for the two tasks, else it should terminate. Read the instructions carefully for each task. Follow the instruction one by one. Don't go to the next step if your program does not return the proper output from previous step. Task 1: Read in a sequence of positive integers until a user enters - 1 to quit (we don't know how many numbers the user may enter). The loop continues to process input values until the user enters - 1: Below is sample of the program execution. Once user enters -1, then stop asking for more positive integers, and compute the largest, smallest among the integers, the number of even integers entered by the user, the number of odd integers entered by the user, and the cumulative sum for all integers entered by the user. DO NOT include -1 in the computation which is the last integer read by the program. (Note that largest, smallest, even and odd numbers of integers will be computed through iterations of a loop, see chapter 4 - section 4.7) Sample Output: User input is in RED Task 2: Ask user to enter an integer and then print out all prime numbers up to that integer. See the sample output below. Hint: A prime number (integer) is an integer that can be divided only by itself and 1. For example, 7 is a prime number and 6 is not (6 can be divided by 2 and 3). We can check if an integer can be divided by another integer using the remainder operator (%). If the remainder is 0, it can be divided. And we need to check this for all integers between 2 and n-1 for a positive integer n. You need to use nested loops, and print the prime numbers inside the outer loop. It may be helpful to write a separate program to find if a number is prime or not, and then include it to the first part mentioned above. Sample Output: User input is in Bold Enter an integer: 20 Here is sample output: user input is in RED Choice Action Compute Prime Numbers Quit Display Help

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions