Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

What is the java code for this. The goal of this lab is to write two programs, Summation and Prime, that execute simple tasks. The

What is the java code for this.image text in transcribedimage text in transcribed

The goal of this lab is to write two programs, Summation and Prime, that execute simple tasks. The first computes the summation of integers within a range with a gap that the user specifies. The second tests whether the integer that the user enters is a square and if not, whether it is composite or prime. Summation Let us see some execution examples first, to get the sense of how the program works. 1 % java Summation 2 Enter start end and gap: 10 100 3 3 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100 4 The total is 1705 5 % java Summation 6 Enter start end and gap: 100 10 3 7 100 97 94 91 88 85 82 79 76 73 70 67 64 61 58 55 52 49 46 43 40 37 34 31 28 25 22 19 16 13 10 8 The total is 1705 The input the user provides appears in blue. Between "Enter ..." and "The total ..." appears the numbers that the program is adding to the total. The program does the summation in two different methods, totalPlus and totalMinus. Both methods are void and receive three int parameters start, end, and gap. We anticipate that the value of gap is positive for both. In the case of totalPlus, we anticipate start end. The program, in its main method, receives values a, b, c for the three parameters and then, if a = end; i == gap ) in the case of the other. During the execution of the for-loop, the program prints the number it is adding with the statement System.out.print("" + i). At the conclusion of the loop, it executes System.out.println() to go to the next line. Prime Note that an integer > 2 is prime if no integer between 2 and itself completely divides it. An integer > 2 is composite otherwise. Let us see some execution examples first, to get the sense of how the program works. 1 % java Prime 2 Enter n: 256 3 The number is a square. 4% java Prime Enter n: 137 6 The number is prime. 7% java Prime 345 8 Enter n: 345 9 The number is composite. Again, the blue texts are user inputs. The program receives an integer n from the user and then calls a void method test with n as the actual parameter. The method test first computes the integer part of Math.sqrt of n and stores it in an integer variable limit. It then checks whether limit * limit == n. If the conditional evaluation returns true, n is a square, and so the method reports the discovery and executes return;. The return statement terminates the execution of the method immediately. Since the method has type void, return with no argument works. Then the program executes a for-loop with which it iterates the value of an integer i from 2 to limit. In the loop-body, the method tests whether n % limit == 0}. If the test returns ture, the method reports that n is composite and returns. Otherwise, the loop completes. After termination of the loop, the method reports that the number is prime

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Database Processing Fundamentals Design And Implementation

Authors: David M. Kroenke

5th Edition

B000CSIH5A, 978-0023668814

More Books

Students explore these related Databases questions

Question

What Is acidity?

Answered: 3 weeks ago

Question

Explain the principles of delegation

Answered: 3 weeks ago

Question

State the importance of motivation

Answered: 3 weeks ago