Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with Java coding problem!

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with Java coding problem!

    This is a problem I have been struggling with, think I'm almost there with it just need a nudge in the right direction. I'm not looking for the answer, just some 'hints'

    The Problem
    ////////////////////////////// PROBLEM STATEMENT //////////////////////////////
    // Given an array of ints of any length, print an array with the elements //
    // "rotated left" by the specified amount. Use methods for array input, //
    // rotating the array and printing the array. //
    // {1, 2, 3} 1 -> {2, 3, 1} //
    // {5, 11, 9} 2 -> {9, 5, 11} //
    // {7, 0, 0} 3 -> {7, 0, 0} //
    ///////////////////////////////////////////////////////////////////////////////

    I have all my methods set up, just not sure how to write the code for the rotateArray(); method.

    So far I've tried swapping each element in the array within a decreasing for loop, but I having trouble with the front element that has to be carried to the end of the array.

    (In the problem statement - {1, 2, 3} is the actual given array and the number following is how many times it must be 'rotated left'. Then the second set of curly braces is the expected result.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Help with Java coding problem!

    How would you do this by hand, with a piece of paper and a pencil instead of a computer? Pretend you are given a deck of cards, and you're supposed to implement this algorithm on it. How do you do it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    eyesackery (June 4th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java coding problem!

    Wow, great advice. The physical deck of cards made me visualize the problem a lot better.

    This is how I plan to solve this problem.

    I will create a loop that calls the rotateLeft(); method 'n' amount of times.

    Within the rotateLeft() method I will store element 0 into a temp variable then swap each element going up the array until I reach array.length - 1, where I will place the int stored in my temp variable.

    Does that sound about right?

  5. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java coding problem!

    Quote Originally Posted by KevinWorkman View Post
    How would you do this by hand, with a piece of paper and a pencil instead of a computer? Pretend you are given a deck of cards, and you're supposed to implement this algorithm on it. How do you do it?
    import java.util.*; public class H1D4 {public static void main (String[] args) {while (JPL.test()) {

    ////////////////////////////// PROBLEM STATEMENT //////////////////////////////
    // Given an array of ints of any length, print an array with the elements //
    // "rotated left" by the specified amount. Use methods for array input, //
    // rotating the array and printing the array. //
    // {1, 2, 3} 1 -> {2, 3, 1} //
    // {5, 11, 9} 2 -> {9, 5, 11} //
    // {7, 0, 0} 3 -> {7, 0, 0} //
    ///////////////////////////////////////////////////////////////////////////////

    // >>>>>> Your Java Code Fragment starts here <<<<<<
    Scanner keyboard = new Scanner(System.in);
    int[] mainArray = getArray(keyboard);
    int[] result = rotateArray(mainArray, keyboard);
    printArray(result);
    // >>>>>> Your Java Code Fragment ends here <<<<<<
    }}

    // >>>>>> Your Java Methods start here <<<<<<
    static int[] getArray(Scanner kb)
    {
    int n = kb.nextInt();
    int[] a = new int[n];
    for (int i = 0; i < a.length; i++)
    a[i] = kb.nextInt();
    return a;
    }

    static int[] rotateArray(int[] a, Scanner kb)
    {
    int n = kb.nextInt();
    for (int k = n; k > 0; k--)
    {
    int lastChar = a[0];
    for (int i = 0; i < a.length -1 ; i++)
    {
    a[i] = a[i+1];
    }
    a[a.length - 1] = lastChar;
    }
    return a;
    }

    static void printArray(int[] a)
    {
    for (int i = 0; i < a.length; i++)
    System.out.print(a[i]);
    }




    // >>>>>> Your Java Methods end here <<<<<<
    }


    Thank you very much for your help, this algorithm solved the problem. Simple after I had visualized it with the deck of cards, what great advice!

  6. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Help with Java coding problem!

    Quote Originally Posted by eyesackery View Post
    Thank you very much for your help, this algorithm solved the problem. Simple after I had visualized it with the deck of cards, what great advice!
    No problem. A big part of programming is actually taking a step back and thinking about the problem without a computer. It's hard to figure out how to write a program that solves the problem if you don't know how you would solve the problem in the first place. And a lot of problems have solutions that are like "I don't know, I just do it in my head!", so thinking about a concrete instance of the problem helps think about what your brain is actually doing to solve the problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. java coding
    By deq wan in forum Member Introductions
    Replies: 1
    Last Post: April 14th, 2012, 08:48 AM
  2. Coding Problem
    By BohmfalkCW in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 26th, 2012, 05:10 PM
  3. Replies: 2
    Last Post: February 19th, 2012, 06:36 AM
  4. Java coding help
    By Javalover1 in forum The Cafe
    Replies: 0
    Last Post: April 12th, 2010, 08:11 PM
  5. [SOLVED] Is "Public void closeFile()" a problem in the program for AS-Level computing project
    By muffin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 5th, 2009, 09:12 PM