Starting from:
$30

$24

CS Algorithms & Programming Lab 6 Solution


The objective of this lab is to learn static methods. Remember that analyzing your problems and designing them on a piece of paper before starting implementation/coding is always a best practice.

For the methods below you should not use String methods or any other built-in methods (except for standard input/output methods and Math.sqrt()).

0. Setup Workspace

Start VSC and open the previously created workspace named labs_ws. Now, under the labs folder, create a new folder named lab6.

In this lab, you are to have one Java class/file (under labs/lab6 folder) as described below. A second Java file containing the revision should go under this folder as well. We expect you to submit a total of 2 files including the revision. Do not upload other/previous lab solutions in your submission. The user inputs in the sample runs are shown with blue color.

1. Fibonacci number checker

First we will design and implement some static methods. Then these methods will be used in a program to determine the associated properties of the integers input by the user.

A Fibonacci Number is a member of a sequence such that the next number is found by adding up the two numbers before it ( =   −1 +   −2, > 1) and the first two numbers are given as 0 = 0 and 1 = 1. Some Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
Tip: A number is Fibonacci if and only if one or both of (5  2 + 4) or (5  2 − 4) is a perfect square.

Implement helper method:

    • boolean isPerfectSquare(int): should determine if the given input is perfect square

Use helper method in:

        ◦ boolean isFibonacci(int): returns whether or not given integer is a Fibanocci number

    2. Harshad number checker

A Harshad Number is an integer that is divisible by the sum of its digits in a given number base. Some example Harshad numbers: 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, ...

Implement helper method:

    • int sumOfDigits(int): should return sum of input number’s digits

Use helper method in:

●    boolean isHarshad(int): returns whether or not given integer is Harshad
3. Prime number checker

A prime number is a number greater than 1 with only two factors – themselves and 1. There are different ways to check the prime property of a number. You can use any of these. Some example prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, ...

Implement the method:

        ◦ boolean isPrime(int): returns whether or not the given integer is prime

    4. Narcissistic number checker

Write a method to check if a given input is a Narcissistic Number. An    -digit number that is the sum of the

-th powers of its digits is called an n-narcissistic. Example: 153 = 13 + 53 + 33. Some Narcissistic numbers:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407,1634, 8208,...

Implement helper method:

    • int power(int, int): this is your own implementation of Math.pow(r,n)

Implement helper method:

    • int countDigits(int): should return digit count of the input

Use helper methods in:

        ◦ boolean isNarcissistic(int): returns whether or not input number is Narcissistic

    5. Program to check number properties

Create a new/empty file of your own under the lab6 folder named Lab06_Q1.java with a class with the same name that examines the properties of numbers input by the user. Call the methods you implemented previously inside your main method appropriately. Your program should output the examination results.

Sample Runs:

Enter number to be examined: 59
Enter number to be examined: 153
Enter number to be examined: 7
Number of digits: 2
Number of digits: 3
Number of digits: 1
Sum of digits: 14
Sum of digits: 9
Sum of digits: 7
59 is not a Fibonacci number.
153 is not a Fibonacci number.
7 is not a Fibonacci number.
59 is not a Harshad number.
153 is a Harshad number.
7 is a Harshad number.
59 is not Narcissistic.
153 is Narcissistic.
7 is Narcissistic.
59 is a Prime number.
153 is not a Prime number.
7 is a Prime number.



More products