Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
assigment help
Evan Cristofori
Greenhorn
Posts: 18
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is my assignment:
The program should first ask for the number of lines: How many lines? user input Then it prints the output based on the user-input number, which should be an odd integer number between 3 and 79. (This is the same as assignment 5) This time you are asked to print a different pattern. If the user types "3", your program should produce the following output: #*# *** #*# If the user types "9", your program should produce: ####*#### ###***### ##*****## #*******# ********* #*******# ##*****## ###***### ####*#### and so on. Notice: � Please use appropriate and realistic data types for the variables involved � You can safely assume that the user always inputs an integer. However, your program should check and make sure the number has the expected properties. � Every print( ) or println( ) method call in your program can only output up to one character. E.g, you can not use the following statement in your program: System.out.print("***"); � Your program should contain at least two methods (feel free to use more if you think it is more elegant to do so): - A main( ) method which reads and validates the user input - Another method defined by you, called by the main( ) method, which does the actual printing job
Here is what I came up with:
import java.io.*; //needed for user input import java.util.*; //needed for StringTokenizer public class Test { public static void main(String[] args) throws java.io.IOException { String inputString; String s1; int num1; int i; InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.print("How many lines? "); inputString = br.readLine(); StringTokenizer st = new StringTokenizer(inputString); s1 = st.nextToken(); num1 = Integer.parseInt(s1); if ((num1 % 2 == 1) && (num1 <= 79) && (num1 >= 3)) { for (i = 1; i <= num1; i++) { System.out.println(part1(num1, i) + part2(num1, i) + part1(num1, i)); } } } public static String part1(int x, int y) //int x is num1(user input), int y is current i { char holder[] = { '#' }; String display = new String(holder); int k; int max; int g = x - y; if (g % 2 == 0) max = g / 2; else max = (g - 1) / 2; for (k = 1; k <= max; k++){} return (display); } public static String part2(int a, int b) { char star[] = { '*' }; String display2 = new String(star); int h; int max2; if (b == 1) max2 = b; else max2 = b + (b - 1); for (h = 1; h <= max2; h++){ } return (display2); } }
And the output I am getting looks like this:
How many lines? 3 #*# #*# #*#
Any insight?
Christophe Verré
Sheriff
Posts: 14691
16
I like...
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
you're not doing anything in your h and k for-loops
[My Blog]
All roads lead to JavaRanch
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
program assignment help
comparison methods
Where are the dates
translate to pig latin!? completely lost....
Test Input Calculator Help
More...