+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 48

Thread: Java HELP :(     submit to reddit submit to twitter

  1. #1
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,581
    BG Level
    8

    Java HELP :(

    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:

    Quote Originally Posted by Java Professor
    Write a program that uses nested loops to create the following output shape:
    Code:
    *********
     *******
      *****
       ***
        *
       ***
      *****
     *******
    *********
    (This is an hourglass shape)

    1.Your program should be generalized to ask the user to input the number of output-lines. The hourglass shape shown above has nine (9) output lines.
    2.The input value provided by the user should always be a positive odd number or zero. Zero, an even number, or a negative number will cause the program to immediately exit.
    3.Note that the number of lines is also equal to the number of output stars (asterisks) on the first and last lines.
    4.Run your program for the input values –1, 1, 2, 7, 9, and 0 to demonstrate that it works properly.
    5.The only print statements you are allowed to use are:

    System.out.printf( “Please enter an odd-number of output-lines (enter zero to exit the program): ” );
    System.out.printf( “*” ); // this is a single asterisk
    System.out.printf( “ ” ); // this is a single blank
    System.out.printf( “\n” ); // this is a new line
    // end assignment

    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). :

    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");
    		}
    	}
    }
    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:
    ***
     ***
    *****
    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 !!

  2. #2

    0) i hate doing other peoples homework so feel lucky that I'm helping you out. also, disclaimer, i haven't written any java for years. but programming is programming.

    1) you're not following directions--you have 2 printfs that don't match the requirements in section 5. If your assignment is automarked, you're going to get fucked.

    2) your program looks overcomplicated. Your program should look like:

    for line 0-M {
    for column 0-M {
    print_character(line, column, M)
    }
    print "\n"
    }

    Now you just need a function that given line A and column B, it will print " " or "*".

    Here's a function that should work:
    if ((line+column =< M && column-line >= 0) || (line+column > M && line-column>=0)) print "*"
    else print " "

  3. #3

    =(

  4. #4
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,581
    BG Level
    8

    Quote Originally Posted by aurik
    0) i hate doing other peoples homework so feel lucky that I'm helping you out. also, disclaimer, i haven't written any java for years. but programming is programming.

    1) you're not following directions--you have 2 printfs that don't match the requirements in section 5. If your assignment is automarked, you're going to get fucked.

    2) your program looks overcomplicated. Your program should look like:

    for line 0-M {
    for column 0-M {
    print_character(line, column, M)
    }
    print "\n"
    }

    Now you just need a function that given line A and column B, it will print " " or "*".

    Here's a function that should work:
    if ((line+column =< M && column-line >= 0) || (line+column > M && line-column>=0)) print "*"
    else print " "
    I'm not quite sure I understand the suggestion your making. I'm studying it though, just going to reply though, the last 2 print statements are fine, we don't know how to exit a java program so the only way to follow his order to make the program exit if they input a negative number or a zero is to use if logic to alert the user before the program ends. He was fine with them being there. The main problem is just getting my code to work. I don't get "automarked" it it done by hand by a lab instructor; we print out the code and the results from the parameters we are told to input and turn that in. But yeh, I'm still looking at your way of doing it.

    I don't now if line and column are variables, but we don't know that yet. The way I'm doing it in the only way we know how to do it, so it may seem overcomplicated but it's what I have to turn in and if I turn in some java I don't know he is going to obviously know I had outside help. Gah this is pissing me off. this will be the first lab I might not get a 10 on because my freaking professor told me it looked fine!!! ><!!!

  5. #5
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,581
    BG Level
    8

    Quote Originally Posted by Axil
    You dont happen to be related to Scott ... do you =o
    No

    Just noticed my last name was still in there, I took it out so please delete your post or edit it idk, you know the answer now.

  6. #6

    Quote Originally Posted by Maguspk
    Quote Originally Posted by aurik
    0) i hate doing other peoples homework so feel lucky that I'm helping you out. also, disclaimer, i haven't written any java for years. but programming is programming.

    1) you're not following directions--you have 2 printfs that don't match the requirements in section 5. If your assignment is automarked, you're going to get fucked.

    2) your program looks overcomplicated. Your program should look like:

    for line 0-M {
    for column 0-M {
    print_character(line, column, M)
    }
    print "\n"
    }

    Now you just need a function that given line A and column B, it will print " " or "*".

    Here's a function that should work:
    if ((line+column =< M && column-line >= 0) || (line+column > M && line-column>=0)) print "*"
    else print " "
    I'm not quite sure I understand the suggestion your making. I'm studying it though, just going to reply though, the last 2 print statements are fine, we don't know how to exit a java program so the only way to follow his order to make the program exit if they input a negative number or a zero is to use if logic to alert the user before the program ends. He was fine with them being there. The main problem is just getting my code to work. I don't get "automarked" it it done by hand by a lab instructor; we print out the code and the results from the parameters we are told to input and turn that in. But yeh, I'm still looking at your way of doing it.

    I don't now if line and column are variables, but we don't know that yet. The way I'm doing it in the only way we know how to do it, so it may seem overcomplicated but it's what I have to turn in and if I turn in some java I don't know he is going to obviously know I had outside help. Gah this is pissing me off. this will be the first lab I might not get a 10 on because my freaking professor told me it looked fine!!! ><!!!
    You're lucky you don't get automarked; in my day, misplace a comma in the output and get a zero on the assignment, hurray

    You're still thinking way too hard about the problem. Basically, take a look at this code:

    for (int row = 0; row < N; row++) {
    for (int column = 0; column < N; column++) {
    System.out.printf("*");
    }
    System.out.printf("\n");
    }

    What this will do is print a N x N grid of asteriks. So if N = 3, it would print

    ***
    ***
    ***

    However, instead of a solid field of asteriks you want to remove certain ones...so what we're going to do is print either "*" or " " depending on the row and column that we are printing.

    For example,

    for (int row = 0; row < N; row++) {
    for (int column = 0; column < N; column++) {
    if (row >= column)
    {
    System.out.printf("*");
    }
    else
    {
    System.out.printf(".");
    }
    }
    System.out.printf("\n");
    }

    this should print

    *..
    **.
    ***

    So your main task is to develop a boolean test which takes the row and the column, and optionally "N" as well, and determines whether to put a "*" or " " at that coordinate.

  7. #7
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,581
    BG Level
    8

    AHAHHAHA OMFG I JUST FUCKING PWNED THAT BITCH OF A PROGRAM.

    It's gay how one thing can fuck up an entire program, but here is what was throwing it off:

    Line 43
    Code:
    				for(int k2 = 1; k2 <= m+2; k2++)
    That plus 2 was unnecessary and was throwing my entire code off. God this is how I know I love this stuff. One little thing and the entire thing fucks up. But ya, I'm really happy I got this entire thing down in a matter of hours when people talked about it like it was the plague. CS is for me ^.^

  8. #8

    ...

  9. #9
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,581
    BG Level
    8

    Quote Originally Posted by aurik
    Quote Originally Posted by Maguspk
    Quote Originally Posted by aurik
    0) i hate doing other peoples homework so feel lucky that I'm helping you out. also, disclaimer, i haven't written any java for years. but programming is programming.

    1) you're not following directions--you have 2 printfs that don't match the requirements in section 5. If your assignment is automarked, you're going to get fucked.

    2) your program looks overcomplicated. Your program should look like:

    for line 0-M {
    for column 0-M {
    print_character(line, column, M)
    }
    print "\n"
    }

    Now you just need a function that given line A and column B, it will print " " or "*".

    Here's a function that should work:
    if ((line+column =< M && column-line >= 0) || (line+column > M && line-column>=0)) print "*"
    else print " "
    I'm not quite sure I understand the suggestion your making. I'm studying it though, just going to reply though, the last 2 print statements are fine, we don't know how to exit a java program so the only way to follow his order to make the program exit if they input a negative number or a zero is to use if logic to alert the user before the program ends. He was fine with them being there. The main problem is just getting my code to work. I don't get "automarked" it it done by hand by a lab instructor; we print out the code and the results from the parameters we are told to input and turn that in. But yeh, I'm still looking at your way of doing it.

    I don't now if line and column are variables, but we don't know that yet. The way I'm doing it in the only way we know how to do it, so it may seem overcomplicated but it's what I have to turn in and if I turn in some java I don't know he is going to obviously know I had outside help. Gah this is pissing me off. this will be the first lab I might not get a 10 on because my freaking professor told me it looked fine!!! ><!!!
    You're lucky you don't get automarked; in my day, misplace a comma in the output and get a zero on the assignment, hurray

    You're still thinking way too hard about the problem. Basically, take a look at this code:

    for (int row = 0; row < N; row++) {
    for (int column = 0; column < N; column++) {
    System.out.printf("*");
    }
    System.out.printf("\n");
    }

    What this will do is print a N x N grid of asteriks. So if N = 3, it would print

    ***
    ***
    ***

    However, instead of a solid field of asteriks you want to remove certain ones...so what we're going to do is print either "*" or " " depending on the row and column that we are printing.

    For example,

    for (int row = 0; row < N; row++) {
    for (int column = 0; column < N; column++) {
    if (row >= column)
    {
    System.out.printf("*");
    }
    else
    {
    System.out.printf(".");
    }
    }
    System.out.printf("\n");
    }

    this should print

    *..
    **.
    ***

    So your main task is to develop a boolean test which takes the row and the column, and optionally "N" as well, and determines whether to put a "*" or " " at that coordinate.
    Wow, that is a good idea... I am surprised I didn't think of this because usually I'm off trying to find ways around doing exactly what he wants but still giving him what he wants. Heh, I think I'll write the code for your method tonight and turn in both just to show off. I have fun writing code anyway, especially when I finally get it working, it's better than a level up in RPGs

  10. #10
    ٩๏̯͡๏)۶

    Join Date
    Jun 2005
    Posts
    12,290
    BG Level
    9
    FFXI Server
    Asura
    WoW Realm
    Barthilas

    Quote Originally Posted by aurik
    ...
    And this marks the end of Aurik helping people on the internet.

  11. #11
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,581
    BG Level
    8

    Quote Originally Posted by Lordwafik
    Quote Originally Posted by aurik
    ...
    And this marks the end of Aurik helping people on the internet.
    I still don't understand what I did to make him sad

  12. #12

    Bonus: Do it with a single nested loop, not two nested loops in a row, in such a way that no conditional statements (e.g. if-then) are used.

  13. #13

    *walks into thread, sees all of this code stuff, head explodes trying to read/comprehend, dies*

  14. #14
    That SpellCast Guy
    Join Date
    Feb 2006
    Posts
    802
    BG Level
    5

    Had to read this thread right before I was going to go to bed, didn't I? I'm the type of person that can't sleep until I figure something like that out, divisortheory. :b Took me about 15 minutes, but I think the below should solve it. Have to have two loops nested inside the outer one, if you're not going to allow conditionals of any sort:

    Code:
    int half = floor(num_rows / 2);
    
    for (int i = 0; i < num_rows; i++)
    {
    	for (int j = 0; j < half - abs(half - i); j++)
    		printf(" ");
    
    	for (int j = 0; j < abs(half - i) * 2 + 1; j++)
    		printf("*");
    		
    	printf("\n");
    }

  15. #15

    Quote Originally Posted by Deimos
    Had to read this thread right before I was going to go to bed, didn't I? I'm the type of person that can't sleep until I figure something like that out, divisortheory. :b Took me about 15 minutes, but I think the below should solve it. Have to have two loops nested inside the outer one, if you're not going to allow conditionals of any sort:

    Code:
    int half = floor(num_rows / 2);
    
    for (int i = 0; i < num_rows; i++)
    {
    	for (int j = 0; j < half - abs(half - i); j++)
    		printf(" ");
    
    	for (int j = 0; j < abs(half - i) * 2 + 1; j++)
    		printf("*");
    		
    	printf("\n");
    }
    Bingo! Absolute value is awesome.

  16. #16
    Relic Weapons
    Join Date
    Apr 2006
    Posts
    361
    BG Level
    4
    FFXI Server
    Lakshmi

    Quote Originally Posted by Maguspk
    AHAHHAHA OMFG I JUST FUCKING PWNED THAT BITCH OF A PROGRAM.
    You get 'em tiger, right after you post on a video game forum for help.

  17. #17
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,581
    BG Level
    8

    Quote Originally Posted by Miji
    Quote Originally Posted by Maguspk
    AHAHHAHA OMFG I JUST FUCKING PWNED THAT BITCH OF A PROGRAM.
    You get 'em tiger, right after you post on a video game forum for help.
    You're stupid, my fix had nothing to do with what Aurik said, I finished it promptly after posting.

  18. #18
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,581
    BG Level
    8

    Quote Originally Posted by Deimos
    Had to read this thread right before I was going to go to bed, didn't I? I'm the type of person that can't sleep until I figure something like that out, divisortheory. :b Took me about 15 minutes, but I think the below should solve it. Have to have two loops nested inside the outer one, if you're not going to allow conditionals of any sort:

    Code:
    int half = floor(num_rows / 2);
    
    for (int i = 0; i < num_rows; i++)
    {
    	for (int j = 0; j < half - abs(half - i); j++)
    		printf(" ");
    
    	for (int j = 0; j < abs(half - i) * 2 + 1; j++)
    		printf("*");
    		
    	printf("\n");
    }
    I just put that all in and it worked, I was surprised...

    Code:
    import java.util.Scanner;
    
    
    public class test
    {
    	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): ");
    		double num_rows = console.nextDouble();
    		double half = Math.floor(num_rows / 2);
    
    		for (int i = 0; i < num_rows; i++)
    		{
       			for (int j = 0; j < half - Math.abs(half - i); j++)
          			{	
    				System.out.printf(" ");
    			}
    			
       			for (int j = 0; j < Math.abs(half - i) * 2 + 1; j++)
          			{
    				System.out.printf("*");
          			}
       			System.out.printf("\n");
    		} 
    	}
    }
    I had to change the value that the Math.floor corresponded to into a type double because it didn't accept the integer argument. I still don't understand how the program works though, maybe it's because I don't quite understand how math.floor works. If I recall correctly it has something to do with adding 1/2 to a number then rounding up towards +infinity or something. It's so weird. Or maybe it was just rounding up towards + infinity until you hit an integer or something. But explain that to me, I want to know how it works with so little code, so well.

  19. #19

    Math.floor just basically chops off the decimal.

    Pi becomes 3
    10.000001 becomes 10
    10.9999999999999999999999999999999999997 becomes 10

    etc.

    Basically floor(num_rows/2) gives you the index of the row right before the row with only 1 star.

    The absolute value is the key though. The idea came from the fact that abs(0-n) = abs(0+n). So you use a simple addition to shift your "origin", so that your "zero" is actually at the row with only one star. Then, using absolute value, you can generate a number that is the same for rows above it and rows after it.


    What program are you using to compile and run your java programs? If you get something with a graphical debugger, you can single step over lines of your code, and at each line you can see the value of all kinds of different variables. That's the best way to learn how a program works, by watching it in action.

  20. #20
    That SpellCast Guy
    Join Date
    Feb 2006
    Posts
    802
    BG Level
    5

    No, I'm not really sure what you're thinking of, but it's not floor. Floor just rounds down. So if they put in 5 lines, you divide by 2, get 2.5, then floor makes it 2. If you need to round up instead, the opposite of floor function is ceil (for ceiling).

    Anyway, as for how the code works, I just figured it out by looking at the case where num_rows is 5, because that's the lowest number that gives me enough information to make sure my equations are right. I wrote out a little table like this, where i is the row you're currently on (starting at 0), s is the number of spaces you need to write on that row, and a is the number of asterisks:

    Code:
    i s a
    =====
    0 0 5
    1 1 3
    2 2 1
    3 1 3
    4 0 5
    From there, it wasn't very hard to figure out the equations needed to get the number of spaces/asterisks for each line, the code could be made a little more readable by making a variable named "difference" or something and setting it to abs(half - i), it's the number of rows away from the middle that you are (which is what the number of asterisks and spaces depends on).

Similar Threads

  1. Need help fixing Java Code
    By Minardi in forum General Discussion
    Replies: 18
    Last Post: 2010-03-15, 23:56
  2. Java Help
    By jinkazama in forum General Discussion
    Replies: 28
    Last Post: 2010-01-30, 22:07
  3. Java Help.
    By Zilla in forum General Discussion
    Replies: 27
    Last Post: 2009-11-16, 18:41
  4. Java Programming Help
    By bori in forum General Discussion
    Replies: 9
    Last Post: 2008-09-19, 09:00
  5. Help with Java Programming Project. Get GF off my back plz
    By Azkarin in forum General Discussion
    Replies: 18
    Last Post: 2007-10-10, 10:59
  6. Need java help...
    By Sekkite in forum General Discussion
    Replies: 15
    Last Post: 2005-10-16, 15:58