Results 1 to 12 of 12
  1. #1
    thirdage is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Post I don't understand what's wrong with this !

    Hello :)

    I was writing this code as a part of my assignment. I wanna know what's wrong with it.

    Java Code:
    package asst3;
    
    public class Leap5 
    {
    	public static void main(String[] args)
    	{
    		for (int n=1; n<=5; n++)
    		{
    			int year = Integer.parseInt(args[n]);
    		}
    		
    		isLeap();
    		
    	}
    	
    
    	public static boolean isLeap(int year)
    	{
    		boolean leap = ((year % 4 ==0) && (year % 100 != 0))||(year % 400 == 0);
    		return leap;
    		
    	}
    
    }
    I have another question too.
    Lets say i have a pattern like this

    Java Code:
         z
        zzz
      zzzzzzz
     zzzzzzzzz
    where the second row have 2 Zs more than the first ... and the third has 4 Zs than the second... and the fourth has 2 Zs more than the third .. and the fifth has 4Zs more than the fourth. I wanna understand how to do this with If , else statements.

    Thanks in advance

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: I don't understand what's wrong with this !

    According to you there is something wrong with your code? Care to explain why you think anything is wrong with your code? Just telling us that something is wrong is too vague.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    thirdage is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: I don't understand what's wrong with this !

    Well Eclipse is showing an error on the 12 th line ... the problem is with isLeap() . should i insert something inside the parenthesis? Am i calling the method in a correct manner?

    hope everything is okay now.
    Thanks, thirdage.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: I don't understand what's wrong with this !

    The method isLeap( ... ) also takes a parameter; you are trying to call it without a parameter in line #12 and Eclipse protests against it (and rightly so).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    thirdage is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: I don't understand what's wrong with this !

    Thanks a lot :) but can you help me with the second one now cause I couldn't manage to write it.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: I don't understand what's wrong with this !

    Quote Originally Posted by thirdage View Post
    Thanks a lot :) but can you help me with the second one now cause I couldn't manage to write it.
    Just a few observations:

    1) lines start at line #1
    2) the first line has 1 Z
    3) even numbered lines have 2 more Zs than the previous line
    4) odd numbered lines have 4 more Zs than the previous line.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    s3ib is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Re: I don't understand what's wrong with this !

    ...and also the spaces.
    Each line has (numberOfZsInTheLastRow - nrOfZsInTheCurrentRow) / 2 spaces on each side.

  8. #8
    thirdage is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: I don't understand what's wrong with this !

    I came up with something like this, but i think i wrote too much..for something that could be done in few lines.

    Java Code:
    for (int c=1; c<=(rows/4-2); c++)
    		{
    			
    			for (int d=1; d<=6*c-5; d++ )
    			{
    				System.out.print("Z");
    				
    			}
    			System.out.println("");
    			for (int e=1; e<=6*c-3; e++)
    			{
    				System.out.print("Z");
    			}
    			System.out.println("");
    			
    		
    		}
    		for (int f=1;f<=(6*(rows/4-1)-5); f++)
    		{
    			System.out.print("Z");
    		}
    So what do you think ? and how could i improve it , shorten it maybe. Thanks in advance :)

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: I don't understand what's wrong with this !

    That looks a bit too complated for me; The increment of the number of z's is 2, 4, 2, 4 etc. starting with 2. The number of z's starts with 1 z. For a value of the increment, the increment for the next line is 6-increment. The number of leading spaces starts with s and is decremented by increment/2 steps. This leads to the code:

    Java Code:
    	private static void print(char c, int n) {
    		for (int i= 0; i < n; i++) System.out.print(c);
    	}
    	
    	public static void main(String[] args) {        
    		for (int line= 1, z= 1, inc= 2, s= 10; line <= 6; line++, z+= inc, s-= inc/2, inc= 6-inc) {
    			print(' ', s);
    			print('Z', z);
    			System.out.println();
    		}
    	}
    Maybe it can be done even more terse, but I haven't had my coffee yet so I wouldn't know ... if you take out the actual printing of a bunch of characters on a line (as I did) you can shorten your code even more ...

    kind regards,

    Jos

    ps. I just guessed an initial value for the number of spaces s.
    Last edited by JosAH; 10-23-2011 at 08:52 AM.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    thirdage is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: I don't understand what's wrong with this !

    Really thanks a lot Mr.JosAH.

    But there's still one problem. I want this to be done using If statements & loops, since i haven't took increment yet. I know am starting to be a pain in the *** but I couldn't reall figure out how to do this by myself .

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: I don't understand what's wrong with this !

    Quote Originally Posted by thirdage View Post
    Really thanks a lot Mr.JosAH.

    But there's still one problem. I want this to be done using If statements & loops, since i haven't took increment yet. I know am starting to be a pain in the *** but I couldn't reall figure out how to do this by myself .
    I saw a couple of increments (++) in your code and you don't need any if statements as you can see from your (and my) code ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: I don't understand what's wrong with this !

    Quote Originally Posted by thirdage View Post
    Hello :)

    I was writing this code as a part of my assignment. I wanna know what's wrong with it.

    Java Code:
    package asst3;
    
    public class Leap5 
    {
    	public static void main(String[] args)
    	{
    		for (int n=1; n<=5; n++)
    		{
    			int year = Integer.parseInt(args[n]);
    		}
    		
    		isLeap();
    		
    	}
    	
    
    	public static boolean isLeap(int year)
    	{
    		boolean leap = ((year % 4 ==0) && (year % 100 != 0))||(year % 400 == 0);
    		return leap;
    		
    	}
    
    }
    ur code should have-

    boolean val=Leap5.isLeap(year);

    instead of isLeap(year);

    and year must be declared outside the for loop
    Last edited by Cosmos; 10-24-2011 at 08:28 PM.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. GUI help. Don t understand
    By s0meb0dy in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2010, 09:40 PM
  3. Trying to understand
    By ladykrimson in forum New To Java
    Replies: 20
    Last Post: 10-12-2010, 11:10 PM
  4. I donŽt understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-22-2009, 11:22 PM
  5. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 07:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •