CSCI 157 - HW 2

Homework 2 - Loopy Homework


Important for all loops:
  • I   -   initialize the variable(s) for controlling the loop
  • C   -   condition involving the variable(s) for controlling the loop
  • U   -   update the value(s) of the variable(s) for controlling the loop
If you get ALL THREE RIGHT, correct loop, yeah!!!

And a correct loop:

  • starts & stops when it should
  • does what it's supposed to


Getting yourself into a pattern that helps you remember all the parts...

// comment about what the loop is supposed to be doing
initialize
while (condition){
      ----------------;
      -------------------;
      --------------;
      ---------------;
      -----------;
      ---------------;
      update
}
// NOTE: default size of a loop body is ONE STATEMENT
// but this one has several statements because of {}'s

Examples
// count 0 to 9 on a single line with spaces between digits
System.out.printf("\n");
int count = 0;
while ( count < 10 ) {
   System.out.printf( "%d  ",count);
   count ++;
}
System.out.printf("\n");

Scanner inputScanner = new Scanner(System.in);

// keep asking until user provides a negative integer
int x = 999;
while ( x >= 0 ) {
   System.out.printf("\nPlease enter a negative integer: ");
   x = inputScanner.nextInt();
}

  1. Written Work

    (3 pts each) The following (a thru e) are intended as written work but there is NO reason to get them wrong. Check your answers on the computer when you think you have the correct answer (good practice for exams!). If you have gotten some wrong, be sure you understand why then write up the CORRECT answer to submit as homework. I'm happy to receive all the right stuff, (-: But as hand-written, not not not not code. And do not submit typed work either. Write out every loop by legible beautiful hand.

    What I mean is.... hand write the code, don't submit compiling code. And all I want is the pertinent code fragment... So for example if there was an f) that said...

    f) Count from 2 to 18 by 2's

    A good answer would be:

    A bad answer would be....

                2  6  8 10 12 14 16 18 
    
    For full credit on the "counting loops" below, use System.out.printf appropriately and include the start and end numbers indicated. ALL SHOULD BE WHILE LOOPS

    1. Count from 50 to 150 by 5's

    2. Count from 3 to some number just less than 933 by 11's

    3. Count backwards from 75 to down to -75 by 25's

    4. Count from -10 to 5 by 0.5's

    5. (Nested Loops) Count from 10 to 55 by 5's all on one line but do this 20 times once per line

  2. REPEAT WRITTEN WORK

    1. Repeat a-e above but this time all should be FOR LOOPS

  3. Programs
    (10 pts) for well-written code that adheres to class coding standards to date.

    1. Program (10 pts)
      (Nested Loops.) Write a program to take two command line arguments. The first will be used to determine the number of alternating *'s and -'s in a row. The second will be used to determine the number of rows to print.
      For example, if the user enters
      
               java YourClientName  6  3 
      
      the printed output should be
      
               *-*-*-
               *-*-*-
               *-*-*-           

    2. Program (10 pts)
      (Nested Loops.) Write a new program similar to the previous one but this time, the first command line argument is the number of rows and the second is the number of characters on each row. Also this program should make every other row a row start with the opposite character of the one above.
               java YourOtherClientName  4  8 
      
      the printed output should be 
      
               *-*-*-*-
               -*-*-*-*
               *-*-*-*-
               -*-*-*-*