Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Purpose: To become familiar Methods To the following programs, add two void methods and call them: printHeader, and printTrailer PartA [Prime Numbers] Write a method

Purpose: To become familiar Methods

To the following programs, add two void methods and call them: printHeader, and printTrailer

PartA

[Prime Numbers]Write a method with the following header to determine whether a given number is a prime or not: public static boolean isPrime(int num)

Check the next page for an algorithm to complete this task. Use it to develop this method and test it.

After testing the method, write a program to list all prime numbers in the range of 2 -1000.

Submit PA5a.java, PA5aOut.docx via Blackboard.

Part B

[Sum the digits in an integer]Write a method with the following header that computes the sum of the digits of an integer :

public static int sumDigits (int num)

For example, sumDigits (2371) returns 13 (which is the sum 2+3+7+1) Use % operation to extract last digit, and / operation to remove the last digit (e.g. 2371 % 10 gives you 1, and 2371/10 gives you 237). Hint: one of the participation activity did this operation in a loop until all digits are extracted.

Implement and test the method for various input values.

Submit PA7b.java, PA7bOut.docx for any 3 different sets of input values

Submit source code and outputs via Blackboard.

Part C:

[Display bar] Write a method using the following header to print a bar with a specified character:

public static void barDisplay (int n, char ch)

For example, call to the method: barDisplay(10, #); produces: ##########

Test this program inside a loop to generate rectangles, triangles of different shapes.

for (int j=1; j<=5; j++) { barDisplay(j, *); } would produce the following figure: *

**

***

****

*****

for (int j=1; j<=5; j++) { barDisplay(5+1-j, @); } would produce the following figure: @@@@@

@@@@

@@@

@@

@

Experiment with various codes to generate other interesting shapes as well.

Submit PA7c.java, PA7cOut.docx for any three different sets of shapes

You may submit all three sets of submissions on Blackboard at one single time.

An Algorithm to verify whether a given number is a 'prime number' or not

A prime number is a number that is only evenly divisible by itself and 1. That is to say, it does not have any divisors other than 1 and itself.

One way to check to see if a number (num) is evenly divisible by a divisor (div) is as follows:

Calculate remainder of the division of num by div ( hint: use num % div)

If the remainder is 0, then num is evenly divisible by div (hence num is not a prime)

We know that 2 is a prime number.

For all other numbers higher than 2, we will systematically generate all possible divisors between 2 and num (using a for loop).

We will check to see if any of these divisors evenly divide the number --- if so, we will declare num to be non-prime, and break out of the loop (using break; statement as done in switch statement)

If, at the end of the loop, if no divisor is found, we will declare num to be a prime number.

Here is the algorithm:

public static boolean isPrime (int num)

{ // body of the isPrime method

o Algorithmic steps are shown as bullets convert to java statements

o if num is exactly equal to 2, then return true; and you are done.

o For other values of num, generate divisors from 2 to half of num (which is enough!) as follows:

o for (int div =2; div <= num/2 ; div++)

{ // body of for loop

o check to see num is divided by div evenly if it does, then num is not prime we return false --Hint: code looks like this: if (num % div == 0) { return false; }

} // end of for loop

return true;

} //end of the isPrime method

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

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

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions

Question

Classify delivery styles by type.

Answered: 1 week ago

Question

Find the derivative of y= cos cos (x + 2x)

Answered: 1 week ago