Results 1 to 7 of 7
  1. #1
    sd1234 is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Java rotater program help

    This is a method that needs to rotate an array by a given number of positions, d. if d is positive the array goes forward, negative backwards. if 0 then the array is unchanged. this is what i have so far but dont know where to go.......im new to java so i suck

    Java Code:
    public class Rotater
    {
    
        private static int nums[]={1,2,3,4,5,6,7,8,9};
    
    public static void main(String[] args)
        {
    printOut(nums);
    rotate(nums,3);
    printOut(nums);
    rotate(nums,-4);
    printOut(nums);
    rotate(nums,0);
    printOut(nums);
        }
    
    public static void printOut(int[] a)
        {
    for(int x=0; x<a.length; x++)
    System.out.print(a[x]+"    ");
    System.out.println("\n");
        }
    
    public static void rotate(int a[], int d)
        {
    int temp=0;
    if(d<1)
    
    
        }
    private static void oneLeft(int a[])
        {
    int temp = a[1];
    int temp2;
    for(int j=a.length-1; j>=0; j++)
            {
    if(j==a.length-1)
                  {
    temp=a[j-1];
    a[j-1]=a[j];
                  }
    
    else if(j==0)
                {
    a[a.length-1]=a[0];
    
    
                }
    else
                {
                    temp2=a[j-1];
    a[j-1]=temp;
    temp=temp2;
    
                }
            }
        }
    private static void oneRight(int a[])
        {
    int temp = a[1];
    int temp2;
    for(int j=0; j<a.length; j++)
            {
    if(j+1>a.length-1)
    a[0]=a[a.length-1];
    else if(j==0)
                {
    temp=a[j+1];
    a[j+1]=a[j];
    
                }
    else
                {
                    temp2=a[j+1];
    a[j+1]=temp;
    temp=temp2;
    
                }
            }
        }
        }
    Last edited by Norm; 01-31-2012 at 01:38 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Java rotater program help

    Can you explain your problem? What does the code do now? Show its output.

    For debugging use the Arrays toString() method to format an array for printing.
    Print out the array before and after the program executes.

    You need to format your code so the {}s are correctly positioned. Your's are all over the place. It is very hard to read you code and understand the nesting of loops and if statements with the way you are using {}s.
    Last edited by Norm; 01-31-2012 at 01:40 AM.

  3. #3
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Java rotater program help

    To start off... In the rotate method there is not any body for the if(d<1) if you fix that you might be able to compile it. It's a good start.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Java rotater program help

    @santa - You could help the OP by fixing the {}s and the indentations. His code is a mess.

  5. #5
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Java rotater program help

    Quote Originally Posted by Norm View Post
    @santa - You could help the OP by fixing the {}s and the indentations. His code is a mess.
    I don't feel like it sense he double posted just now, hurt my feelings hehe

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

    Default Re: Java rotater program help

    If you can reverse part of an array, you're in business: suppose you have a method reverse(a, i, j) that reverses all elements in an array a, starting at position i (inclusive), ending at position j (exclusive); rotating array a, m elements to the left is done as follows:

    Java Code:
    reverse(a, 0, m);
    reverse(a, m, a.length);
    reverse(a, 0, a.length);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Java rotater program help

    sd1234, your other thread with the same question has been closed. Please don't double post again.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 04:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 12:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 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
  •