Lab 3: More Simple Methods, Scanner Class, While Loops, For Loops


Due: 10pm February 15

Purpose: In this assignment, you will get input from users with the Scanner class, print output with slightly more sophistication, use while loops with boolean logic, for loops, and methods to eliminate unnecessary redundancy. When you finish this lab, you will have improved your basic skills on input and output, loops and using methods.


You will write a program Octagon.java that takes as input an odd number and a character and prints out an octagon as follows:

Octagon Printing Program

Please enter an odd width, or 0 to quit: 7
Please print a character: *

  ***
 *****
*******
*******
*******
 *****
  ***

Please enter an odd width, or 0 to quit: 9
Please print a character: $

  $$$$$
 $$$$$$$
$$$$$$$$$
$$$$$$$$$
$$$$$$$$$
$$$$$$$$$
$$$$$$$$$
 $$$$$$$
  $$$$$

Please enter an odd width, or 0 to quit: 0


The width variable corresponds to the width (in symbols) of the longest lines in the middle of the shape.

Your program must include at least the following two methods with the specified signatures:

    /* 	  Prints an octagon of odd width with a minimum size of 1 by 
* repeating a single character symbol. The base length is half the
* width, and the sides are also half the width. From the base to the
* sides, the row width increases by two for the top and decreases by
* two for the bottom. 
*/
public void printOctagon(int width, String symbol) {
// your code here
}
/* Prompts the user for an odd integer or 0 to quit playing.
* Reads in an integer and if it is not odd, prints out an error
* message, and prompts the user again. Keeps trying until the user
* enters an odd number or 0. Returns the number.
*/
public int readOdd(Scanner stdin) {
// your code here
}

Your program does not need to check that the user entered an integer or just one character (it can crash for a non-integer, and print incorrect output for a multi-character input). But it does need to make sure the number is odd.

Tip 1: You need to put the following statement at the beginning of your class in order to tell the Java compiler where to find the Scanner class:

import java.util.*;

Tip 2: In order to calculate the width of the upper and lower side of the octagon, use the formula:

base = width / 2
if (base % 2 == 0)
    base= base + 1

We want the width of the upper and the lower side of the octagon to be half the value of the width the user inputs. In case this number turns out to be even,
the whole symmetry of the shape will break. That's why, in this case, we increment it by one.

Tip 3: The width of the 2 veritcal sides should be the same as base.

5 pts Extra Credit:
Add error checking code for non-integer input as well as not odd, and add the error checking code that checks if the character is a single input.

15 pts Extra Credit: Generalize your printOctagon routine to accept words instead of just a single character. For example, 5 Hey would result in:

Octagon Printing Program

Please enter an odd width, or 0 to quit: 5
Please print a character: Hey

   HeyHeyHey
HeyHeyHeyHeyHey
HeyHeyHeyHeyHey
HeyHeyHeyHeyHey
   HeyHeyHey




Submission and Grading:

Always include your names, slip days, and a comment at the top of your file. Here is the template for Octagon.java

import java.util.*;

/**
* @author name 1:
* UT EID:
* CS account user name:
* Section Unique Number: 
* slip days used on this assignment: ??/4
* total slip days used: ??/6
*
* @author name 2:
* UT EID:
* CS account user name:
* Section Unique Number:
 * slip days used on this assignment: ??/4
* total slip days used: ??/6
*
* On our honor, we followed the pair programming rules of splitting
* keyboard time evenly and 80% or more joint development, and we have
* neither read nor copied code, nor have we shown or given our code to
* others.
*
* @version Date
*
* Extra Credit Attempted? (Yes/No) :
*
* Describe what this program does.
*
*/

Submit your version of Octagon.java using the turnin program . If you need help goto turnin program help.

Half your score will come from "external correctness" which means the output of the program exactly matches the example specification.

The remaining score comes from "internal correctness", which means you used the specified Java constructs correctly, you captured the redundancy and structure correctly, you commented the header and code appropriately, and you followed the basic Java style guidelines.

If your are submitting without a partner, please include your initial partner and include correspondence with the instructor or the TA in your header.