Ok, so I'm in a real bind. My lab instructors came into my Java class today to overhear the lecture about the lab which is to take place tomorrow. Apparently the lab is the hardest one all year, and is so hard the graduate students came in to refresh their memories.
Anyway, my problem is this: my x-win 32 client thing at home isn't connecting to the linux thing at my school. I type in the session name and everything but it's not working. So I can't even edit my program for tomorrow. So I look to BG Java experts for help. If you've had java or know anything about Java please lend your ear and give me a hand.
I will have one hour to work on this tomorrow in lab, but it's basically suicde if I go in there without a working program. I wrote out all the code, and even my instructor said it looked like it'd work, but when I type it all into Nedit and run it I get the wrong outputs, even though my code seems to be flawless.
Here is the assignment and my flawed (apparently) code:
// end assignmentOriginally Posted by Java Professor
Basically what he's asking is to make a program that will output that same hourglass shape for any number that is odd and is not equal to or below zero. Since all we know at this point to create a program that would do this are loops and nested loops, I was told to make the program with nested for loops. Here is my attempt at the program, please if you find errors or if you have a suggestion (i.e. omitting the 2 seperate for loops and making a single for loop). :
Please help me BG, this is the hardest lab, my instructor told me everything looked fine, so I didn't even think I'd have trouble after compiling and running it. But My program, when run just outputs the 2 halves of the hourglass without the middle row of one asterisk. Or for 1 and 3 it doesn't print out the correct shape, like for 3 it outputs:Code://******************************************************************************* // Name: ... * // Lab: L2 @ 9:30am on Tuesday, October 3rd 2006 * // Instructor: John Porter * // Lab Number: 6 * //******************************************************************************* import java.util.Scanner; public class lab6 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.printf("Please enter an odd-number of output-lines (Enter Zero to exit the program): "); int n = console.nextInt(); int m = n; if( ( n > 0 ) && ( (n%2) != 0 ) ) { for(int i = 1; i <= Math.round(n/2); i++) { for(int j = 1; j <= i-1; j++) { System.out.printf(" "); } for(int k = 1; k <= m; k++) { System.out.printf("*"); } System.out.printf("\n"); m -= 2; } m=1; for(int i2 = ( Math.round(n/2) + 1); i2 <= n; i2++) { for(int j2 = 1; j2 <= n-i2; j2++) { System.out.printf(" "); } for(int k2 = 1; k2 <= m+2; k2++) { System.out.printf("*"); } System.out.printf("\n"); m += 2; } } else if( n < 0 ) { System.out.printf("Error: Please enter a non-negative, odd integer.\n"); } else { System.out.printf("Thank you for using Noah Programs Inc.\n"); } } }
This obviously isn't aligned correctly or the correct number of asterisks. It should read:Code:*** *** *****
For one it outputs the following:Code:*** * ***
For nine ( a higher test number) it outputs the following:Code:***
This is a relief of sorts because it shows my program should work but doesn't. Here, we're missing the middle row of one asterisk; also the alignment is off on the second row of three by one space. Also the last row printed out 11 asterisks, for god knows why. (I think my idea to make m += 2; has blown up in my face and it shows here. But yeh that's what I've observed, please once again, help me BG !!Code:********* ******* ***** *** *** ***** ******* ********* ***********