CSCI 1933 Lab 2

Working with Arrays and Scanner

Procedures for Lab

Labs will be run asynchronously with the exception of those in Section 2. Attendance at your

scheduled lab section is strongly encouraged, but not required in order to accommodate for those

who may not be comfortable meeting in person. You are strongly encouraged to work with a partner

within your lab section. The TAs will assist you in finding one, if needed. Have a TA evaluate your

progress on each milestone before you move onto the next. The labs are designed to be mostly

completed by the end of the lab. If you are unable to complete all of the milestones by the end of

the lab, you will have up until 10:00 p.m. CDT on the Monday following the release of the lab to

have any remaining milestones graded (on Discord) by a TA during office hours. We suggest you

get your milestones checked off as soon as you complete them since Monday office hours tend to

become extremely crowded. You will only receive credit for the milestones you have checked off by

a TA. Regrades of a milestone are available but only if submitted before the last office hours on

the following Monday. There is nothing to submit to Canvas for this lab.

Due to the observance of a University holiday on June 20, the due date for this lab

has been extended to Tuesday, June 21, at the end of office hours.

Introduction

Welcome to our second week of CSCI 1933! This lab will further your knowledge of the Scanner

class and get you more familiar with how arrays work. We have seen Scanner used extensively in

last weekâ€™s lab, but both these components are crucial to understand throughout your experience

coding with Java.

It is important that you do not copy and paste code from online into your files. This may

result in undesirable effects such as your code not compiling. Be sure to type out any

code that you write so this does not occur.

1

Fibonacci Sequence

The Fibonacci sequence is named after the Italian mathematician Leonardo of Pisa, also known as

Fibonacci. The steps for the for the Fibonacci sequence are to start with the 0th and 1st terms,

which are 0 and 1 respectively, and to calculate the next term, sum the previous two terms.

Using the initial numbers 0+ 1 = 1 the 2nd term in the Fibonacci sequence is 1. Now that you know

the pattern, here are the 0th through 8th numbers in the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21.

Now letâ€™s write a Fib class to help us find the nth Fibonacci number. Write the following functions

in the Fib class:

1

CSCI 1933 LAB 2

2. FIND THE MAXIMUM DIGIT IN A NUMBER

â€¢ public static int fibonacciRecursive(int n) – this will return the nth Fibonacci number as an int using a recursive approach.

â€¢ public static int fibonacciIterative(int n) – this will return the nth Fibonacci number as an int using an iterative approach.

Here are some test cases:

Example Test Cases:

â€¢ fibonacciRecursive(3) will return 2

â€¢ fibonacciRecursive(5) will return 5

â€¢ fibonacciIterative(8) will return 21

â€¢ fibonacciIterative(10) will return 55

Once you have your methods working, use the Scanner class to prompt for input in the main

function of Fib.

Hint: For a reminder on how to use Scanner class, the Scan.java file on Canvas is a good

place to start. This example explains how to take ints as input. For example, to take in an

integer for the Fibonacci functions, your Java code could be the following (in main):

public static void main(String args[]) {

System.out.println(“Enter an integer n to get the nâ€™th Fibonacci

number:”);

Scanner myScanner = new Scanner(System.in);

int n = myScanner.nextInt(); // gets an integer from command line

System.out.println(“The ” + n + “â€™th Fibonacci number using

fibonacciRecursive is ” + fibonacciRecursive(n));

System.out.println(“The ” + n + “â€™th Fibonacci number using

fibonacciIterative is ” + fibonacciIterative(n));

}

Reflection: When you run your method with large values like 80 or 200 what happens? Are

there redundant calculations that are slowing down your method? How might you speed up

your method by using an array, and how do your recursive and iterative approaches compare?

(This is not a coding question, but rather something to think about)

Milestone 1:

Show a TA your methods and have them input numbers into your Scanner prompt to show

correctness and then answer the following question: How does your Fibonacci method handle

a negative value for n?

2

CSCI 1933 LAB 2

2

2. FIND THE MAXIMUM DIGIT IN A NUMBER

Find the Maximum Digit in a Number

In this section, you will find the maximum digit in a given positive int both recursively and

iteratively. Write a Max class which will have the following methods:

â€¢ public static int recursiveMaxDigit(int num) – This will find the maximum digit in

num recursively

â€¢ public static int iterativeMaxDigit(int num) – This will find the maximum digit in

num iteratively

Hint: Use integer division [/] and the modulo (remainder) operation [%] to separate out the

individual digits. For example, 1634/10 = 163 and 1634%10 = 4 using Java logic.

Once the functions are working, use the Scanner class in the main method to prompt for input.

We have included some example return values below for you to get started.

Example Test Cases:

â€¢ recursiveMaxDigit(578) will return 8

â€¢ recursiveMaxDigit(10) will return 1

â€¢ iterativeMaxDigit(9999) will return 9

â€¢ iterativeMaxDigit(13442) will return 4

Milestone 2:

Show a TA your code for recursiveMaxDigit(int) and iterativeMaxDigit(int) and have

them input numbers into your Scanner prompt to show correctness.

3

CSCI 1933 LAB 2

3

3. HISTOGRAM PART 1

Histogram Part 1

Recall that a histogram is a visual representation of a distribution of discrete data. For example,

the data set {3, 2, 1, 2, 3, 0, 1, 5, 3} over the range [0, 5] will have the following histogram:

0:*

1:**

2:**

3:***

4:

5:*

Figure 1: Example Histogram

Your histogram will be stored as an array. Remember, an array element has essentially two components: its index, the order in which it appears in the array (starting at 0), and its value, the

mutable piece of data that corresponds to each index. For the histogram, notice that the value

of each array element is the frequency of the corresponding number in the data set.

When programming it is important to break your problems down into small pieces. For this section,

you will create a Histogram class with a way to store the information in a histogram as follows:

â€¢ Add a constructor public Histogram(int lowerbound, int upperbound) which will initialize your histogram by setting the range.

This constructor is also where you should create the class member array that keeps track of

the frequency of each number between lowerbound and upperbound inclusive.

â€¢ Add a method public boolean add(int i) – If i is between lowerbound and upperbound

inclusive, then add i to the histogram and return true. Otherwise, return false.

Hint: If you are trying to increase the frequency of a number i in the histogram array,

then you will need to subtract the number i by the lower bound to access the correct

array index.

For example, if lowerbound = 5, upperbound = 10, and i = 6, then to increase the

frequency of i = 6 in our histogram array, we would need to access the array at index

[i – lowerbound] = [1].

â€¢ Add a method public String toString() – This will return a String formatted as in

Figure 1. That means you are not printing anything in this method. Make sure your

data points are in order from lowerbound to upperbound.

â€¢ Create the main method. Inside, implement some tests on an instance of Histogram to

confirm that everything is working correctly.

4

CSCI 1933 LAB 2

4. HISTOGRAM PART 2

For instance, the following code would create the example histogram as seen in figure 1:

public static void main(String args[]) {

Histogram histo = new Histogram(0, 5);

histo.add(3);

histo.add(2);

histo.add(1);

histo.add(2);

histo.add(3);

histo.add(0);

histo.add(1);

histo.add(5);

histo.add(3);

System.out.println(histo);

}

Hint: In the constructor if the upperbound is smaller than the lowerbound, simply swap

the bounds. Also, your data range should not include negative numbers or fractions. In the

toString() method, the character n adds newlines.

Milestone 3:

Show a TA the Histogram class code and the tests you wrote to ensure the histogram is

working correctly. This should include (at a minimum) printing a histogram after adding 10

numbers to it.

4

Histogram Part 2

Next, create a new class called HistogramApp which will utilize Scanner to collect input from the

user and run a more thorough test of Histogram.

â€¢ Start by prompting the user to enter lowerbound and upperbound for their histogram.

â€¢ Create an instance of Histogram that matches the userâ€™s desired range.

â€¢ Ask for user input until the program ends. This can be accomplished with a while loop.

Implement the following commands (i.e., what should be done when the user types in this

String):

â€“ add – This command will prompt the user for more numbers for the histogram. This

should involve calling your add() method from Histogram and will require you to receive

input again using Scanner.

â€“ print – This should give the user a view of their current histogram by printing the

Histogram instance. This command relies on your toString() method to work properly

(but note that System.out.println() will call toString() for you).

â€“ quit – Calling this command should cause the program to end.

5

CSCI 1933 LAB 2

4. HISTOGRAM PART 2

Remember to only accept the commands listed above. If the user types in something else, remind

them of their available options. An example output for the HistogramApp program can be found

below. Note that green text is typed by the user.

—Histogram Console–Options

add – used to add numbers to the histogram

print – prints the histogram to the screen

quit – leaves the program

Enter Range to get started: 10 13

>add

>Enter number(s): 10

>add

>Enter number(s): 9

9 is not in the range

>add

>Enter number(s): 10 12 13 11 13 13 25 10 77

25 is not in the range

77 is not in the range

>print

10:***

11:*

12:*

13:***

>quit

Bye!

Milestone 4:

Show a TA your HistogramApp class once it is successfully tested.

6

Purchase answer to see full

attachment