Docsity

Writing and Using Methods - Lab #7 - Computer Science I | MAT 2170, Lab Reports of Computer Science

Toggle sidebar
bg1
MAT 2170: Laboratory 7
Key Concepts
1. Writing and using methods
Instructions
•As usual, create these projects in a lab7 directory
•Use dialog programs.
•Just to shake things up a bit, don’t sit next to anyone you sat next to in Lab 6.
•Come to lab with a written plan for each exercise, noting the required inputs, a few test cases, and
an algorithm for each. Do not come in “cold,” expecting to start from scratch and finish the lab.
Exercises
1. (Page 172, Exercise 4) Your program, TestDigits, is to include a countDigits() method.
2. (Page 175, Exercise 12) Include an isPerfect() predicate method in your program TestPerfect.
3. Palindromes, from Denison University — An integer is a palindrome if its digits read the same
left–to–right as right–to–left. For example, 23132 and 578875 are both palindromes. Note that
single digit numbers are palindromes.
Here is one way to produce a palindrome from any integer: if the integer is a palindrome, stop.
Otherwise, invert the digits in the integer and add 2, continuing until you get a palindrome (which
you always will). For example suppose you start with 108. The sequence of numbers you’d get using
the above process would be: 108, 803, 310, 15 (notice the leading zero is dropped when inverting),
53, 37, 75, 59, 97, 81, 20, 4. This took 11 steps.
Write a program, NumericPal, which asks for input from the user – a series of positive integers
with two or more digits – and for each, displays the palindrome it produces (using the method
described in the previous paragraph), and the number of steps it took to produce the palindrome.
The expected display format is given in the example on the back of this sheet.
Each input should produce two lines of output (in a single dialog box). Use a sentinel to signal
end of input. You must create and utilize at least two methods, isPalindrome() — to determine
whether or not a number is a palindrome, and ReverseDigits() — which reverses the digits in a
number. The isPalindrome() method should make use of the ReverseDigits() method, then
compare the digits in the original with those in the reversed number.
Continued on reverse.
pf2

Partial preview of the text

Download Writing and Using Methods - Lab #7 - Computer Science I | MAT 2170 and more Lab Reports Computer Science in PDF only on Docsity!

MAT 2170: Laboratory 7

Key Concepts

  1. Writing and using methods

Instructions

  • As usual, create these projects in a lab7 directory
  • Use dialog programs.
  • Just to shake things up a bit, don’t sit next to anyone you sat next to in Lab 6.
  • Come to lab with a written plan for each exercise, noting the required inputs, a few test cases, and an algorithm for each. Do not come in “cold,” expecting to start from scratch and finish the lab.

Exercises

  1. (Page 172, Exercise 4) Your program, TestDigits, is to include a countDigits() method.
  2. (Page 175, Exercise 12) Include an isPerfect() predicate method in your program TestPerfect.
  3. Palindromes, from Denison University — An integer is a palindrome if its digits read the same left–to–right as right–to–left. For example, 23132 and 578875 are both palindromes. Note that single digit numbers are palindromes.

Here is one way to produce a palindrome from any integer: if the integer is a palindrome, stop. Otherwise, invert the digits in the integer and add 2, continuing until you get a palindrome (which you always will). For example suppose you start with 108. The sequence of numbers you’d get using the above process would be: 108, 803, 310, 15 (notice the leading zero is dropped when inverting), 53, 37, 75, 59, 97, 81, 20, 4. This took 11 steps.

Write a program, NumericPal, which asks for input from the user – a series of positive integers with two or more digits – and for each, displays the palindrome it produces (using the method described in the previous paragraph), and the number of steps it took to produce the palindrome. The expected display format is given in the example on the back of this sheet.

Each input should produce two lines of output (in a single dialog box). Use a sentinel to signal end of input. You must create and utilize at least two methods, isPalindrome() — to determine whether or not a number is a palindrome, and ReverseDigits() — which reverses the digits in a number. The isPalindrome() method should make use of the ReverseDigits() method, then compare the digits in the original with those in the reversed number.

Continued on reverse.

MAT 2170: Laboratory 7 2

Sample Execution:

input: output (in a dialog box): 108 The Palindrome generated for 108 is 4. This took 11 steps.

342 The Palindrome generated for 342 is 8. This took 13 steps.

1002 The Palindrome generated for 1002 is 111. This took 9 steps.

999 The Palindrome generated for 999 is 999. This took 0 steps.

-1 Thank you for using my program. Bye.

Finishing Up

When you have completed the lab:

  1. Publish the programs to your web site
  2. Submit an electronic copy
  3. Staple together the program printouts for each exercise in this lab, and hand in at the beginning of Lab 8