CS 145 Lab 4 – Conditionals and Loops
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
zeroPadthat takes in two parameters: anintwe’ll call n and anintnumber 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
reversethat accepts aStringand returns its reverse. For example,reverse("oomph")yields"hpmoo". - Write a method
printFactorsthat accepts anintand 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
filterNumbersthat 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.