Simple Programming Question

I am in an intro to programming class, and I am having difficulty with an exercise.

The problem is as follows :

4.32)Write an application that uses only the following output statements:

System.out.print( "* " );

System.out.print( " " );

System.out.println();

to display the checkerboard pattern below. Note that System.out.println cuases the program to output a single newline character. [Hint: nested repetition statements are required] (10 points)

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

As you can see, the problem is very simple, and I thought I was programming it the correct way, but the way I have it programmed, it refuses to work, it just prints one line in a fashion that it appears it's printing out correctly, but then it stops.

My code is as follows:

import java.util.Scanner;

public class DottedSquare

{

public void drawSquare()

{

Scanner input = new Scanner(System.in);

int width;

int rowCount;

int colCount;

System.out.print("Enter the size of the square:");

width = input.nextInt();

if(width < 1)

{

width = 1;

System.out.println("Size is too small, using default of 1");

}

if(width > 20)

{

width = 20;

System.out.println("Size is too large, using default of 20");

}

rowCount = 1;

if(rowCount % 2 == 0)

{

colCount=1;

while(colCount <= width)

{

if(colCount % 2 == 0)

{

System.out.print("*");

}

else if(colCount % 2 != 0)

{

System.out.print(" ");

}

colCount++;

}

System.out.println();

}

if(rowCount % 2 != 0)

{

colCount=1;

while(colCount <= width)

{

if(colCount % 2 == 0)

{

System.out.print(" ");

}

else if(colCount % 2 != 0)

{

System.out.print("*");

}

colCount++;

}

System.out.println();

}

rowCount++;

}

}

Sorry for the lack of commenting, it's a personal project so I can make sure I understand nested control statements, but apparently I do not.

If anyone has any insight, it would be greatly appreciated.

I have a driver class to actually run the program, I'm just looking for someone who can pick out why it isn't printing correctly.

Message was edited by:

sKZodiac

Since this forum doesn't allow for formatting, the checkered pattern should be a star, then a space, then a star. Then an opposite pattern on the next line.

[2748 byte] By [sKZodiaca] at [2007-11-26 18:23:33]
# 1
Please repost your code using [code][/code] tags. It's hard to read as it is.Also you can format plain text with [pre][/pre] tags.
paulcwa at 2007-7-9 5:57:33 > top of Java-index,Java Essentials,Java Programming...
# 2

Try to figure things out for one row first. Here are some observations:

1) you either print out star-space-star-space ... etc. or you print out

space-star-space-star ... etc.

2) for the even row numbers you start with a star, for the odd row

number you start with a space.

Given the observations above you can print out a row like this:public void printRow(int row, int nofCols) {

boolean isStar= (row%2) == 0;

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

if (isStar) print("*");

else print(" ");

isStar= !isStar;

}

println();

}

I leave it to you to print out the entire board.

kind regards,

Jos

JosAHa at 2007-7-9 5:57:33 > top of Java-index,Java Essentials,Java Programming...
# 3

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class DottedSquare {

public static void main(String ad[]) throws NumberFormatException,

IOException {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

int width;

int rowCount;

{

System.out.print("Enter the size of the square:");

width = Integer.parseInt(in.readLine().trim());

if (width < 1) {

width = 1;

System.out.println("Size is too small, using default of 1");

}

if (width > 20) {

width = 20;

System.out.println("Size is too large, using default of 20");

}

}

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

int colCount =0;

while (colCount ><= width*2) {

if (colCount % 2 == 0) {

System.out.print(" ");

} else if (colCount % 2 != 0) {

System.out.print("*");

}

colCount++;

}

System.out.println();

}

}

}

Your program works fine but its far from optimal...

;-)

-TC

Message was edited by:

AbiSS

AbiSSa at 2007-7-9 5:57:33 > top of Java-index,Java Essentials,Java Programming...
# 4

import java.util.Scanner;

public class DottedSquare

{

public void drawSquare()

{

Scanner input = new Scanner(System.in);

int width;

int rowCount;

int colCount;

System.out.print("Enter the size of the square:");

width = input.nextInt();

if(width < 1)

{

width = 1;

System.out.println("Size is too small, using default of 1");

}

if(width > 20)

{

width = 20;

System.out.println("Size is too large, using default of 20");

}

rowCount = 1;

if(rowCount % 2 == 0)

{

colCount=1;

while(colCount <= width)

{

if(colCount % 2 == 0)

{

System.out.print("*");

}

else if(colCount % 2 != 0)

{

System.out.print(" ");

}

colCount++;

}

System.out.println();

rowCount++;

}

if(rowCount % 2 != 0)

{

colCount=1;

while(colCount <= width)

{

if(colCount % 2 == 0)

{

System.out.print(" ");

}

else if(colCount % 2 != 0)

{

System.out.print("*");

}

colCount++;

}

System.out.println();

rowCount++;

}

}

}

the star pattern looks like this:

* * * * * *

* * * * * *

* * * * * *

* * * * * *

* * * * * *

sKZodiaca at 2007-7-9 5:57:33 > top of Java-index,Java Essentials,Java Programming...
# 5

Nevermind! I figured it out, I'm an idiot and forgot to put my IF statements inside a While statement, here's the finished-working code:

import java.util.Scanner;

public class DottedSquare

{

public void drawSquare()

{

Scanner input = new Scanner(System.in);

int width;

int rowCount;

int colCount;

System.out.print("Enter the size of the square:");

width = input.nextInt();

if(width < 1)

{

width = 1;

System.out.println("Size is too small, using default of 1");

}

if(width > 20)

{

width = 20;

System.out.println("Size is too large, using default of 20");

}

rowCount = 1;

while(rowCount <= width)

{

if(rowCount % 2 == 0)

{

colCount=1;

while(colCount <= width)

{

if(colCount % 2 == 0)

{

System.out.print("*");

}

else if(colCount % 2 != 0)

{

System.out.print(" ");

}

colCount++;

}

System.out.println();

rowCount++;

}

if(rowCount % 2 != 0)

{

colCount=1;

while(colCount <= width)

{

if(colCount % 2 == 0)

{

System.out.print(" ");

}

else if(colCount % 2 != 0)

{

System.out.print("*");

}

colCount++;

}

System.out.println();

rowCount++;

}

}

}

}

public class DottedSquareDriver

{

public static void main(String args[])

{

DottedSquare square1 = new DottedSquare();

square1.drawSquare();

}

}

the second set of code is literally just a driver class to run it, since I didn't put a main method in the DS class. My professor prefers us to break it up into two separate classes for some reason.

sKZodiaca at 2007-7-9 5:57:33 > top of Java-index,Java Essentials,Java Programming...
# 6

> the second set of code is literally just a driver

> class to run it, since I didn't put a main method in

> the DS class. My professor prefers us to break it up

> into two separate classes for some reason.

Having a separate class to act as your main will be important later, if you continue to use Java, for issues such as threading and for developing large applications.

Message was edited by:

Djaunl

Djaunla at 2007-7-9 5:57:33 > top of Java-index,Java Essentials,Java Programming...