CSCI A201/A597

Lab Notes 4

Spring 2000


Lab 4 QuizSite exercises. Nested loops. Practice with code from lecture.

Lab 4 quiz in QuizSite contains your next assignment.

Work with the items that have been posted as "Lab4". You can submit up to ten times and you can run the programs before you submit the answers, so identifying the right answers will not be a problem. Understanding why they are the right ones is the important part, so next time you need to turn in a file that contains the right answers and a justification why those are the right answers, for each question.

If you still have trouble with the Crossed problem think for a minute about the following.

You are supposed to print a square of characters, spaces and asterisks. This square of characters is printed in slices (because you have no other way of printing it). So you will be printing lines of characters, one by one.

On each line you will be printing the characters one by one until, at the end of the line, you need to print an end of line.

Unless you count all the characters you won't be able to know for sure how many you printed. And since you're printing a certain numbers of characters per line, and a certain numbers of lines overall, in the end each of the characters that you print (be it space or an asterisk) will be end up on a line and a column, so the number of the line, and the number of the column will make it clear what asterisk we are talking about.

In the end we will be printing columns characters per line, on a total of lines lines. In our case the user requests a certain size, and the number (s)he specifies will determine both the width and the height of the resulting square of characters (will be equal to both).

So the square of characters will actually be mapped on the following grid of pair of numbers (in each cell the numbers identify the line and the column on which the cell is):

So if you know how to create a square of asterisks like this all you need to do to create the pattern that we want to produce will be to identify its components.

What's the property of the asterisks on the left border of the square? Their column is indexed as 0 (zero). Whatever variable we use to keep track of the column on which we are printing, checking its value against zero will give us the answer to whether the character is on the first column or not at the time we are printing it.

Same thing about the other vertical border, whose index however is 3 (or, in fact, size - 1).

The horizontal border has also index 0 (zero) but the index (or counter) that keeps track of lines is different from the ones that keeps track of columns. (One of them is in the first position and the other one is in the second).

We need to find similar relationships for the diagonals and then we would be set.

The program simply counts all the cells in the square and for each cell, if the indices for the line and the column on which the cell is satisfy one of the six conditions (for the four borders and the two diagonals) then an asterisk needs to be printed. Otherwise a space is printed.

I also include the text of the eleven questions here. This is just for your reference. Please go into QuizSite to submit your answers to these questions.

Question 1.
What does the following program's output look like?
public class One {
    public static void main(String[] args) {
        int i = 0; 
	for (i = 0; i < 10; i++) {
	    System.out.println(" " + i); 
	} 
    } 
} 

a line of 10 numbers
a column of 10 numbers
a line of 9 numbers
a column of 9 numbers


Question 2.
What does the following program's output look like?
public class Two {
    public static void main(String[] args) {
        int i = 0; 
	for (i = 0; i < 10; i++) {
	    System.out.print(" " + i); 
	} 
    } 
} 

a line of 10 numbers
a column of 10 numbers
a line of 9 numbers
a column of 9 numbers


Question 3.
What does the following program's output look like?
public class Three {
    public static void main(String[] args) {
        int i = 0; 
        int j = 0; 
	for (j = 0; j < 4; j++) { 
  	  for (i = 0; i < 10; i++) {
	      System.out.print(" " + i); 
	  } 
	}
    } 
} 

4 lines of 10 numbers each
10 lines of 4 numbers each
1 line with 40 numbers on it
40 lines of 1 number each


Question 4.
What does the following program's output look like?
public class Four {
    public static void main(String[] args) {
        int i = 0; 
        int j = 0; 
	for (j = 0; j < 4; j++) { 
  	  for (i = 0; i < 10; i++) {
	      System.out.println(" " + i); 
	  } 
	}
    } 
} 

4 lines of 10 numbers each
10 lines of 4 numbers each
1 line with 40 numbers on it
40 lines of 1 number each


Question 5.
What does the following program's output look like?
public class Five {
    public static void main(String[] args) {
        int i = 0; 
        int j = 0; 
	for (j = 0; j < 4; j++) { 
  	  for (i = 0; i < 10; i++) {
	      System.out.print(" " + i); 
	  } 
	  System.out.println(); 
	}
    } 
} 

4 lines of 10 numbers each
10 lines of 4 numbers each
1 line with 40 numbers on it
40 lines of 1 number each


Question 6.
What does the following program's output look like?
public class Six {
    public static void main(String[] args) {
        int i = 0; 
        int j = 0; 
	for (j = 0; j < 10; j++) { 
  	  for (i = 0; i < 4; i++) {
	      System.out.print(" " + i); 
	  } 
	  System.out.println(); 
	}
    } 
} 

4 lines of 10 numbers each
10 lines of 4 numbers each
1 line with 40 numbers on it
40 lines of 1 number each


Question 7.
What does the following program's output look like?
public class Seven {
    public static void main(String[] args) {
        int i = 0; 
        int j = 0; 
	for (j = 0; j < 4; j++) { 
  	  for (i = 0; i < 10; i++) {
	      System.out.print(" " + j); 
	  } 
	  System.out.println(); 
	}
    } 
} 

4 lines of 10 numbers each (which count the columns)
10 lines of 4 numbers each (which count the columns)
4 lines of 10 numbers each (that count the lines)
10 lines of 4 numbers each (that count the lines)


Question 8.
What does the following program's output look like?
public class Eight {
    public static void main(String[] args) {
        int i = 0; 
        int j = 0; 
	for (i = 0; i < 4; i++) { 
  	  for (j = 0; j < 10; j++) {
	      System.out.print(" " + j); 
	  } 
	  System.out.println(); 
	}
    } 
} 

4 lines of 10 numbers each (which count the columns)
10 lines of 4 numbers each (which count the columns)
4 lines of 10 numbers each (that count the lines)
10 lines of 4 numbers each (that count the lines)


Question 9.
What does the following program's output look like?
public class Nine {
    public static void main(String[] args) {
        int i = 0; 
        int j = 0; 
	for (i = 0; i < 10; i++) { 
  	  for (j = 0; j < 10; j++) {
	      if (i == 0 || j == 0 || i == j) {
                System.out.print(" " + j); 
              } else { 
                System.out.print("  "); 
	      }
	  } 
	  System.out.println(); 
	}
    } 
} 

numbers organized in an "X" sign
numbers organized in a square (full of numbers)
numbers in the shape of an arrow
an empty square whose contour (border) is made out of numbers


Question 10.
What does the following program's output look like?
public class Ten {
    public static void main(String[] args) {
        int i = 0; 
        int j = 0; 
	for (i = 0; i < 10; i++) { 
  	  for (j = 0; j < 10; j++) {
	      if ((i == j) || (i + j == 10)) {
                System.out.print(" " + j); 
              } else { 
                System.out.print("  "); 
	      }
	  } 
	  System.out.println(); 
	}
    } 
} 

numbers organized in an "X" sign
numbers organized in a square (full of numbers)
numbers in the shape of an arrow
an empty square whose contour (border) is made out of numbers


Question 11.
What does the following program's output look like?
public class Eleven {
    public static void main(String[] args) {
        int i = 0; 
        int j = 0; 
	for (i = 0; i < 10; i++) { 
  	  for (j = 0; j < 10; j++) {
	      if ((i == j) && (i + j == 10)) {
                System.out.print(" " + j); 
              } else { 
                System.out.print("  "); 
	      }
	  } 
	  System.out.println(); 
	}
    } 
} 

a long line of numbers
a long column of numbers
a single number will be printed, the number 5
this program won't compile


Last updated: February 3, 2000 by Adrian German