LAB #1: LOOPS


PART 1: Exercises

 

1. How many times will the loop execute in the following exercises? For each – what is the output?

 

  //1

  int x1 = 100;

        while(x1 > 0) {

          System.out.print(x1 / 10 + " ");

          x1 /= 2;

        }

        System.out.println();

 

           

        //2

        int x2 = 2;

        do {

          System.out.print(x2 + " ");

          x2 *= x2;

        } while(x2 < 200);

        System.out.println();       

 

        //3

  int x3 = 250;

        while(x3 % 3 != 0) {

          System.out.print(x3 + " ");

        }

        System.out.println();

       

 

        //4

        int x4 = 10;

        while(x4 < 10) {

          System.out.print(x4 + " ");

          x4 --;

        }

        System.out.println();

       

 

        //5

        int x5 = 1;

        while(x5 < 100) {

          System.out.print(x5 + " ");

          x5 += 10;

        }

        System.out.println();

       

 

        //6

        for(int i = 0; i <= 2; i++) {

          for(int j = 1; j <= 4; j++) {

            for (int k = 1; k <= 5; k++) {

              System.out.print("*");

            }

            System.out.print("#");

          }

          System.out.println();

        }

 

 

2. Write if statements to indicate the season based on 2 variables for month (assume integer 1-12) and day (assume integer (1-31).  The rules are:

12/16 – 3/15 à winter

 3/16 – 6/15 à spring

 6/16 – 9/15 à summer

 9/16 –12/15 à fall

 

SOLUTION:

 

int day, month;

     ...

 


PART 2: Programming

 

1. Task: Suppose you are writing a game-playing program that involves 2-digit numbers, each number being composed of 2 different digits. Test if whether numbers entered in a sequence are accepted to be used in this game. Test for errors in input (including type)

 

SAMPLE OUTPUT:

Enter a 2-digit number. The digits should be different. Zero to stop: rt

Not an integer, try again: f

Not an integer, try again: 34.5

Not an integer, try again: -4

NOT good for your game! 

Enter a 2-digit number. The digits should be different. Zero to stop: rds

Not an integer, try again: 99

NOT good for your game! 

Enter a 2-digit number. The digits should be different. Zero to stop: 23

Good for your game! Play!

Enter a 2-digit number. The digits should be different. Zero to stop: 11

NOT good for your game! 

Enter a 2-digit number. The digits should be different. Zero to stop: kjhgfgfd

Not an integer, try again: 8

NOT good for your game! 

Enter a 2-digit number. The digits should be different. Zero to stop: 0

 


2. Task: Use nested for loops statements to draw empty boxes of any character (input from user). The boxes have the same number of rows and columns (input from the user; valid range: 5 to 21). Test for errors in input (including type)

 

SAMPLE OUTPUT:

Do you want to start(Y/N): y

How many chars/last row? n

Not an integer! Try again! How many chars/last row? fgfgfg

Not an integer! Try again! How many chars/last row? 7.6

Not an integer! Try again! How many chars/last row? 34

ERROR! Valid range 5 - 21. How many chars/last row? 7

What character? k

 

kkkkkkk

k     k

k     k

k     k

k     k

k     k

kkkkkkk

 

Do you want to continue(Y/N): y

How many chars/last row? y

Not an integer! Try again! How many chars/last row? 9

What character? #

 

#########

#       #

#       #

#       #

#       #

#       #

#       #

#       #

#########

 

Do you want to continue(Y/N): n


Notes:
A. The lab will NOT be graded, do NOT hand anything in to the instructor.
B. The lab should be completed by the start of the next scheduled lab class. Save the .java files on your disk and e-mail them (attachments) to Vishal Patel at vpatel12@students.towson.edu
Very important: Make sure that you have COSC 237.section, your name, and Lab#1 in the Subject box of your e-mail.
C. In case you have any problems, contact the instructor or the TA for assistance.