Colorado State University Logo | CS 163/4: Java Programming (CS 1) Colorado State University Logo | CS 163/4: Java Programming (CS 1)
CS 163/4: Java Programming (CS 1)
Computer Science

Lab 08 - More Loops

Introduction

Break and Continue Statements

Break statements are useful tools when manipulating the order of execution within loops. The keyword break causes an immediate exit of the loop.

Continue statements cause an immediate jump to the loop condition check. If in a for loop, it will jump to the next iteration. If in a while loop, it will jump to the condition and check whether true.

Nested For Loops

Nested For Loops are loops that appear in the body of another loop.

1
2
3
4
5
    for(int i = 0; i < 3; i++){ // This is the outer loop
        for(int j = 0; j < 5; j++){ // This is the inner loop
            System.out.print(i + "." + j + "  ");
        }
    }  

For example, the above code would output:

0.0  0.1  0.2  0.3  0.4  1.0  1.1  1.2  1.3  1.4  2.0  2.1  2.2  2.3  2.4 

Notice that in one iteration of the outer loop, the inner loop completes all iterations. In other words, the inner loop runs all the way through each time the outer loop runs through.

Nested for loops come in handy if you are counting multiple variables at once, as well as with other types of data that you’ll see later in this course, like arrays.

Do-While Loops

Do-while loops are similar to while loops, but the order in which the condition is checked differs.

In while loops, the condition is checked before entering the loop. In do-while loops, the code within the loop executes once before the condition is checked.

    while(condition is true){ 
        // code to execute 
    }

    do{
        // code to execute
    }while(condition is true)

For some examples of do-while loops, you can check out the Java Docs explanation here, or these examples from an outside resource.

For Today’s Lab

In this lab, you will working with various methods that handle different operations with numbers, as well as user and file input.

Each method you write has an associated test method already written for you. After completing each method, please call the test method from main to ensure your methods are working properly.

Step 1: getAreaCode(String phoneNumber)

Review

Last lab, you worked with branching and common classes.

To review this, you will write a method that, given a phone number, will return the three-digit area code. The phone number will be given in a String with a phone number in the format “***-***-****” or “***-****”. Phone numbers are typically represented as seven digit numbers, with or without a three-digit area code. Between those digits are dashes.

For this method, you will check to see if the string given has an area code or not (is seven digits plus the dash, or 10 with dashes).

If the area code is present, return it using the relevant String and Integer class methods.

If the area code is not present, return the default, which for Fort Collins, is 970.

You will write the method signature from scratch, but here are a few specifications:

  • The method is self-contained and doesn’t rely on an instance of the class. What keyword should you use?
  • The method returns an int.
  • The method is called getAreaCode.
  • The method takes one parameter, a String, for the phone number.

Step 2 : multiplicationTable(int rows, int columns)

Multiplication Using Nested For Loops

Write a method that returns a multiplication table based on two input values that specify what two ranges of numbers to multiply together. For example, if the method were given 3 and 4 as input, it would return a string that when printed, would look like this:

1   2   3   4
2   4   6   8
3   6   9   12

Output Requirements:

  • Each number must be followed by a tab character.
  • Each line must be followed by a new line character (including the last row).
  • The columns and rows should range from 1 to the input number.

The method signature should look as follows:

public static String multiplicationTable(int rows, int columns){}

Hint: Review String formatting, as it may be helpful in returning the correct string from the method.

Call the testMT() method from main after completing this method to ensure it is working as expected.

Step 3: bruteForceEquationSolver(int one, int two, int three, int constant)

Brute Force Equation Solver using Nested For Loops

Brute force algorithms are methods of finding a solution by trying all possibilities until a solution is found. They do not consider the most efficient path, but systematically test possibilities until one works. For more information about brute force algorithms, check out this outside resource.

In this lab, you will be writing your own method using the brute force algorithm, to solve a second degree polynomial.
This method will take in four parameters, three coefficients, and one constant, and it will have the following signature:

public static String bruteForceEquationSolver(int one, int two, int three, int constant){}

The equation you are trying to solve is of the following format, given the parameters stated above:

(constant) = (one) * x + (two) * y + (three) * z

X, Y, and Z are variables that yoo are finding solutions for.

For example, if you were to call the method with parameters (2, 3, 4, 42), you would be finding the solution for the equation 2x + 3y + 4z = 42.

Some specifications for the method:

  • Your method should try every possibility within the range (1-10) for each variable. Check possibilities using nested for loops, starting with x as the outermost loop, then y, then z.
  • Your method should return if a solution is found, and return a string with the solutions in the following format:

    x: xSolution y: ySolution z: zSolution

    There should be a space between the solution and the next variable, and each variable letter should be followed by a colon and a space.

  • If no solution is found, return the string “Solution not found.”.

Call the testBFES() method from main after completing this method to ensure it is working as expected.

Computer Science Department

279 Computer Science Building
1100 Centre Avenue
Fort Collins, CO 80523
Phone: (970) 491-5792
Fax: (970) 491-2466

CS 163/4: Java Programming (CS 1)

Computer Programming in Java: Topics include variables, assignment, expressions, operators, booleans, conditionals, characters and strings, control loops, arrays, objects and classes, file input/output, interfaces, recursion, inheritance, and sorting.