You are not allowed to get help from or talk to anyone but Dr. Liffick about this assignment.
Overview
In these assignments, you will write Java programs that
each contain a recursive method.
The first, hourglass, prints a symmetric graphic with text.
The second, commas, prints a long integer with commas in the correct places.
The names of the classes containing main should start with upper case letters:
Hourglass, and Commas.
Classes (other than the needed class containing main),
arrays, loops, stacks, queues, and linked lists are inappropriate here
and should not be used in the recursive methods.
The hourglass program should not contain any loops. None.
The commas program will have an input loop but no other loops.
Do not use data structures beyond Strings in the hourglass program.
Include comments at the top of your programs and on any method other
than main.
Carefully note the restrictions on language feature use in the specifications for each program.
You must do these assignments by yourself without asking anyone,
other than your instructor, any questions.
No pairs. Any collaboration is cheating.
Hourglass Specification
The program reads a single integer from the user.
If that integer is less than 1, it prints an error message and stops.
The hourglass base case with input 1 displays two lines with
one star on each line.
Otherwise, it prints a pattern of asterisks or stars (*) as shown below for
an input of 4.
Note that there are four stars (and spaces) on the first and last lines.
* * * * * * * * * * * * * * * * * * * *
The program must not contain a loop.
Approaching the Hourglass Problem
Obtain the input and check its validity in the main method.
In main, output a newline both before and after
your recursive method's total output.
Write a public static recursive method. There are many ways to write this method, but most of them involve having two parameters representing the number of stars and the number of spaces in front of the stars on the top and bottom lines. For the overall hourglass design, there are no spaces before the stars on the first and last lines.
It will be extremely helpful to write a supporting method called printMany that takes in an integer count and a String s. It prints count copies of s without finishing the line (i.e. without outputing a newline). I strongly suggest you write that method first and call it from main as practice. You wouldn't normally write such a method recursively in Java, but this is recursion practice. Do even that recursively. No loops.
Test your program thoroughly. When you have completed the program, submit it as the hourglass assignment.
Commas Specification
Your program should contain a recursive method that prints a
long value with commas separating clumps of three digits as
shown below.
Use / and % to break the number apart.
The method should deal with negative numbers (by printing
the minus sign and making a recursive call with the
negation of the original number),
with numbers that are less than 1000 (by printing them in minimal space),
and with numbers that are greater than or equal to 1000
(by recursively calling itself with the number divided by 1000 and then
printing the last three digits).
The main method should repeatedly call this method with the integers that are read from the input until input fails. Each formatted integer should be printed on a separate line. Note that to receive any credit, you must use a recursive method as outlined above.
You may not use NumberFormat or DecimalFormat. This is a recursion assignment.
Note also that the program is limited to numbers that are between -(263 - 1) and 263 - 1. Inputs outside that range will not give correct results. Your program should not assume anything about this limit. That is, the program should work correctly with the types changed to a type that allows unlimited size numbers. Do not check the range.
|
Approaching the Commas Problem
When you have completed the program, submit it as the commas assignment. |
Example Input (Bold) and Output 1024 1,024 42 42 0 0 -365 -365 -76543 -76,543 12345678 12,345,678 -1234567890123456789 -1,234,567,890,123,456,789 9223372036854775807 9,223,372,036,854,775,807 -9223372036854775807 -9,223,372,036,854,775,807 |
You are not allowed to get help from or talk to anyone about this assignment.