CS 145 Lab 4 – Conditionals and Loops
October 17, 2015 by Chris Johnson. Filed under
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
cs145, fall 2015, labs.
First, if you have checkpoints left over from last lab, get them inspected during the first 15 minutes of this lab. No credit will be awarded past these 15 minutes.
Don’t forget to work in pairs! Please work with someone that you did not work with last lab.
Objective
In this lab, we use logical operators to make our code branch and repeat.
Checkpoint 1
Person A types.
Write and carefully test a method named getOrdinal that takes in an int parameter and returns a String in which the parameter is followed by its correct ordinal suffix: st, nd, rd, or th.
Checkpoint 2
Person B types.
Complete at least two of the following problems. Use loops even when alternative solutions may be feasible.
- Write and test a method
zeroPad that takes in two parameters: an int we’ll call n and an int number of digits. It returns a String with n padded with leading zeroes to have the specified number of digits. For example, zeroPad(12, 5) yields "00012".
- Write a method
reverse that accepts a String and returns its reverse. For example, reverse("oomph") yields "hpmoo".
- Write a method
printFactors that accepts an int and prints all its factors—those numbers which cleanly divide the given number. For example, printFactors(60) prints 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, and 60.
- Write a method
filterNumbers that accepts a String and prints only those whitespace-separated chunks within it that are numbers. For example, filterNumbers("4 score and 7 years ago, 7 8 9.") prints 4, 7, 7, 8, and 9.