05-Number Crunching and Character Patterns
05-Number Crunching and Character Patterns
Learning Outcomes
By the end of the lab exercise, you will be able to:
• Write a complete Java program from scratch incorporating all the required pieces
• Write a program using methods and loops
• Transform a word problem into an algorithm
Preamble
This is the first lab where you need to create a program from scratch. Remember what you have
learned in the prior labs – start with a good template, write an algorithm first, translate it line-by-
line into Java code, work on a smaller version of the problem – do this and you will have an
easier time completing the lab. Read the following word problems before continuing with the lab
activities.
Problem 1: Number Pairs
Sometimes within an integer, digits come in adjacent pairs. For instance, in the number 1337 the
digit 3 is in a pair but in the number 1373 there are no pairs even though the digit 3 appears
twice. Write a Java method to determine the largest digit that comes in a pair within an integer.
If there are no pairs return -1. (Do not use strings in your solution – these are integers!)
Problem 2: Perfect Numbers
In mathematics, a perfect number is a positive integer that is the sum of its proper positive
divisors, that is, the sum of the positive divisors excluding the number itself. The smallest
perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Write
a Boolean method that will determine if a number given as an argument is a perfect number or
not. (Do not use strings in your solution – these are integers!)
Problem 3: Fibonacci Numbers
Fibonacci numbers are kind of fun! Here is a partial list:
n 0 1 2 3 4 5 6 7 8 9 10 11 12 …
Fib(n) 0 1 1 2 3 5 8 13 21 34 55 89 144 …
They have the interesting property that the sum of the nth and n+1st Fibonacci numbers is the
n+2nd Fibonacci number. Also, the 0th and 1st Fibonacci numbers are defined as being 0 and 1,
respectively.
Write a program that outputs the first 50 Fibonacci numbers. Calculate and output the ratio of
the current Fibonacci number to the previous one. Output 3 columns of values: the Fibonacci
number, its value and the ratio. Make the columns line up vertically and output the ratio to 9
decimal places.
1
LANGARA COLLEGE
COMPUTER SCIENCE 1150
Lab Activities
1) From the list of problems in the preamble, solve Problem 4: Starry Patterns and any two of
the other problems. (If you have time, attempt the third problem.)
What to Hand in
Submit a zip file to Brightspace before the due date containing:
• Source code (*.java) of your final programs (minimum 3) after all the modifications
have been made. Be sure that the comments and printIdentification method contain
your name, student number, and course and section numbers.
• Output listings (*.txt) from running your programs using sample input data. Make
sure you show samples of all types of valid and invalid inputs.
Mark Allocation
OUT OF CRITERIA
Starry Patterns
/3 • Nice solution
/1 • Output listing
Number Pairs
/3 • Nice solution
/1 • Output listing
Perfect Numbers
/3 • Nice solution
/1 • Output listing
Fibonacci
/3 • Nice solution
/1 • Output listing
/+1 Bonus for completing all 4 problems
/12 TOTAL (FOR 3 REQUIRED PROBLEMS)