New to Java - Making a Palindrome

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 2325 Views - Last Post: 30 August 2013 - 02:57 PM Rate Topic: -----

#1 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 203
  • Joined: 19-September 12

New to Java - Making a Palindrome

Posted 29 August 2013 - 01:24 PM

Hi all,

I am new to the Java language and have a class that deals with the basics of the language as well as one dealing with the OOP of it all. I am stuck trying to figure out how to finish a program that creates a palindrome from an array. Basically I need to conform to these:

- makePalindrome: takes a single parameter, an array of integers, each containing a single digit of the odometer. This function should modify the numbers in the higher-numbered positions of the array so that the odometer forms a palindrome. For example, given the the array {1, 0, 2, 4, 1, 7} as input, the function should rewrite this to {1, 0, 2, 2, 0, 1}.

- getValue: takes a single input parameter, an array of integers, each containing a single digit of the odometer. As output returns a long integer containing the equivalent value of that odometer. For example, given the the array {1, 0, 2, 4, 1, 7} as input, the function should return 102417.

- addOne: takes two parameters. The first is an array of integers, each containing a single digit of the odometer. The second parameter should be an integer, denoting a position within the array. The function should add one to the element in the indicated position. If that element should then exceed 9, then 10 is subtracted from that element, andthe process repeated for the element in the next lower position (unless we are already in position 0).
For example, given the input array {9, 9, 0, 4, 1} and a position number of 4, the array should be rewritten as {9, 9, 0, 4, 2}. But given that same input array and a position number of 1, the array would be rewritten as {0, 0, 0, 4, 1}.

Input & Interaction

Input to the program will be taken from the standard input ( System.in).

There will be several test cases in the input, each consisting of an odometer reading on its own line. Each odometer reading will be from 2 to 9 digits long. The odometer in question has the number of digits given in the input - so, if the input is 00456, the odometer has 5 digits. There will be no spaces in the input, and no blank lines between input sets.

The input will end with a line with a single 0.

Here is what I have tried:
import java.io.*;
import java.util.*;

public class Odometer 
{
    public Scanner sc;
    public PrintStream ps;
    
    
    public void makePalindrome (int[] s)
    {
        // your code here
    	
    	try
    	{
    		sc = new Scanner(System.in);
    		
    		while (sc.hasNextInt()) //Loop through while there are ints and load them into a temp array
    		{
    			int [] temp;
    			int i = sc.nextInt();
    			temp = new int[i];
    		}
    	}
      sc.close();
    	
    }
    
    public long getValue(int[] s)
    {
        // your code here
        // System.out(array[i]); after I have made the palindrome
    }
    
    public void addOne (int[] digits, int pos)
    {
        // your code here
        //Something like?
    	int[] temp;
    	temp.sort(digits);
    	temp.binarysearch(digits, pos);
    	temp [] + 1;
    }
    
    public int[] parseInput (String s)
    {
        int[] digits = new int[s.length()];
        for (int i = 0; i < digits.length; ++i)
            digits[i] = s.charAt(i) - '0';
        return digits;
    }
    
    /**
     * Driver.
     * @throws Exception
     */
    public void execute(BufferedReader input)
    {
        try {
            while (true)
            {
                String reading = input.readLine();
                if (reading.equals("0")) break;
                
                int[] digits = parseInput(reading);

                long startingValue = getValue (digits);
                
                makePalindrome (digits);
                
                long target = getValue (digits);
                
                if (target < startingValue) {
                    addOne (digits, (digits.length + 1) / 2 - 1);
                    makePalindrome (digits);
                    target = getValue (digits);
                }


                System.out.println(reading + " " + (target - startingValue) );
            }
        } catch (IOException e) {
            // Thrown on illegal input or end of input
        }
    }
    
    /**
     * The main just creates an object and calls doit().
     * 
     * @param args Command line args, necessary but unused.
     * @throws FileNotFoundException 
     */
    public static void main( String[] args ) throws FileNotFoundException
    {
        BufferedReader input = null;
        if (args.length == 0) {
            input = new BufferedReader(new InputStreamReader(System.in));
        } else {
            input = new BufferedReader (new FileReader (args[0]));
        }
        new Odometer().execute(input);
    }

}



Thanks in advance for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: New to Java - Making a Palindrome

#2 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 647
  • Joined: 26-June 13

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 02:39 PM

Is there a particular problem you are having trouble with?
Was This Post Helpful? 0
  • +
  • -

#3 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 203
  • Joined: 19-September 12

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 02:45 PM

To start with, I would narrow it down to using the Scanner "sc" to get the user input and store it into an array (is that even possible?). Then modifying the numbers in the higher-numbered positions of the array so that the odometer forms a palindrome.

What I have in the makePalindrome function I'm sure has a "general idea" to what I'm supposed to do but I don't think I did it correctly.
Was This Post Helpful? 0
  • +
  • -

#4 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 647
  • Joined: 26-June 13

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 03:37 PM

Where are you trying to reading the input from? It seems you like your trying to read from the makePalindrome method. Shouldn't this a be placed within the execute method instead?

It doesn't look like you need the Scanner 'sc', since you are reading using BufferedReader to read input.


Here is something that might work. It needs testing though.
public void makePalindrome (int[] s)
    {
        // your code here
    	
    int length = s.length();
    int middle = 0;

    if (length % 2 == 0)
    {
        middle = length / 2;
    }
    else
    {
        middle = length / 2 + 1;
    }


    for (int i = middle, j = middle + 1; j < length; --i, ++j)
    {
        s[j] = s[i];
    }	
 }


This post has been edited by salazar: 29 August 2013 - 03:39 PM

Was This Post Helpful? 1
  • +
  • -

#5 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 203
  • Joined: 19-September 12

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 03:50 PM

I'm assuming I need the Scanner for this here:

Input & Interaction

Input to the program will be taken from the standard input ( System.in).

There will be several test cases in the input, each consisting of an odometer reading on its own line. Each odometer reading will be from 2 to 9 digits long. The odometer in question has the number of digits given in the input - so, if the input is 00456, the odometer has 5 digits. There will be no spaces in the input, and no blank lines between input sets.

The input will end with a line with a single 0.

So I need to "scan" for a single line of 0 to signal end of one odometer reading correct?
Was This Post Helpful? 0
  • +
  • -

#6 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 647
  • Joined: 26-June 13

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 04:03 PM

You should replace the BufferedReader you're using in the execute method with Scanner instead.
Was This Post Helpful? 0
  • +
  • -

#7 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 203
  • Joined: 19-September 12

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 05:35 PM

Am i on the right track for my getValue?

public long getValue(int[] s)
    {
        // your code here
    	long value = 0;
    	for (int i = 0; i < s.length; ++i)
    	{
    		
    		value = s[i];
    		
    	}
    	
    	return value;
    }



Recall it should:

getValue: takes a single input parameter, an array of integers, each containing a single digit of the odometer. As output returns a long integer containing the equivalent value of that odometer. For example, given the the array {1, 0, 2, 4, 1, 7} as input, the function should return 102417.
Was This Post Helpful? 0
  • +
  • -

#8 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 203
  • Joined: 19-September 12

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 05:54 PM

and for the addone function?

  public void addOne (int[] digits, int pos)
    {
        // your code here
    	
    	for (int i = pos; i < digits.length; ++i)
    	{
    		pos += 1;
    		if (i > 9)
    		{
    			i -= 10;
    		}
    	}
    	
    	
    }



Recall it should:

addOne: takes two parameters. The first is an array of integers, each containing a single digit of the odometer. The second parameter should be an integer, denoting a position within the array. The function should add one to the element in the indicated position. If that element should then exceed 9, then 10 is subtracted from that element, andthe process repeated for the element in the next lower position (unless we are already in position 0).

For example, given the input array {9, 9, 0, 4, 1} and a position number of 4, the array should be rewritten as {9, 9, 0, 4, 2}. But given that same input array and a position number of 1, the array would be rewritten as {0, 0, 0, 4, 1}.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 07:30 PM

this is kind of completly useless code

     {                            // { <--- begin of the block
          int [] temp;            // I will eventually define a int[] array
          int i = sc.nextInt();   // I read a int
          temp = new int[i];      // I declare this array to have the size of that int
     }                            // }  <--- end of the block the temp[] array stop to exists here


Was This Post Helpful? 2
  • +
  • -

#10 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 647
  • Joined: 26-June 13

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 07:40 PM

View PostR2B Boondocks, on 29 August 2013 - 05:35 PM, said:

Am i on the right track for my getValue?

public long getValue(int[] s)
    {
        // your code here
    	long value = 0;
    	for (int i = 0; i < s.length; ++i)
    	{
    		
    		value = s[i];
    		
    	}
    	
    	return value;
    }



Recall it should:

getValue: takes a single input parameter, an array of integers, each containing a single digit of the odometer. As output returns a long integer containing the equivalent value of that odometer. For example, given the the array {1, 0, 2, 4, 1, 7} as input, the function should return 102417.


You're on the right track in going through each value. However, you should be adding to the value not reassigning it with a the next value. Just replace the = with +=. Then you have to account for the magnitudes associated with each position in the array. The magnitude for lowest position should be the 10 raised to the n - 1 power, where n is the size of the list.

For example, if the array 2 digits, then the magnitude would be 10^n-1 = 10^(2-1) = 10.

Then, for each lower index, the power would be one less then the previous.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 07:45 PM

I don't know why you do that

    if (length % 2 == 0)
    {
        middle = length / 2;
    }
    else
    {
        middle = length / 2 + 1;
    }


but in any case replace that weird (probably useless) if by
        middle = length / 2 + (length % 2);


Was This Post Helpful? 1
  • +
  • -

#12 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 647
  • Joined: 26-June 13

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 08:14 PM

View Postpbl, on 29 August 2013 - 07:45 PM, said:

I don't know why you do that

    if (length % 2 == 0)
    {
        middle = length / 2;
    }
    else
    {
        middle = length / 2 + 1;
    }


but in any case replace that weird (probably useless) if by
        middle = length / 2 + (length % 2);



Yes, they're probably equivalent, and the second optimized one is shorter. But the first is more readable. So its not completely useless. :smile2:

This post has been edited by salazar: 29 August 2013 - 08:16 PM

Was This Post Helpful? 1
  • +
  • -

#13 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 647
  • Joined: 26-June 13

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 08:32 PM

Accidental post.

This post has been edited by salazar: 29 August 2013 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

#14 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 647
  • Joined: 26-June 13

Re: New to Java - Making a Palindrome

Posted 29 August 2013 - 08:39 PM

View PostR2B Boondocks, on 29 August 2013 - 05:54 PM, said:

and for the addone function?

  public void addOne (int[] digits, int pos)
    {
        // your code here
    	
    	for (int i = pos; i < digits.length; ++i)
    	{
    		pos += 1;
    		if (i > 9)
    		{
    			i -= 10;
    		}
    	}
    	
    	
    }



Recall it should:

addOne: takes two parameters. The first is an array of integers, each containing a single digit of the odometer. The second parameter should be an integer, denoting a position within the array. The function should add one to the element in the indicated position. If that element should then exceed 9, then 10 is subtracted from that element, andthe process repeated for the element in the next lower position (unless we are already in position 0).

For example, given the input array {9, 9, 0, 4, 1} and a position number of 4, the array should be rewritten as {9, 9, 0, 4, 2}. But given that same input array and a position number of 1, the array would be rewritten as {0, 0, 0, 4, 1}.


You're really off with this one. You should be working with the digits array. Its generally bad practice to have method params that you complete ingnore. :)/>/> Since you're not modifying the digits array param, there is no way you're doing it correctly. Below, I have something it should look like.
public void addOne (int[] digits, int pos)
  {
      // your code here
  	/*
  	for (int i = pos; i < digits.length; ++i)
  	{
  		pos += 1;
  		if (i > 9)
  		{
  			i -= 10;
  		}
  	}
        */
        digits[pos] += 1;

        // If the value is greater then 9 then perform carry over
  	while (digits[pos]> 9 && pos > 0)
        {
            digits[pos] -= 10;
            pos--;
            digits[pos] += 1;        
        }
        
        if (digits[0] > 9)
        {
            digits[0] -= 10;
        }	
  }


This post has been edited by salazar: 29 August 2013 - 08:40 PM

Was This Post Helpful? 1
  • +
  • -

#15 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 143
  • View blog
  • Posts: 342
  • Joined: 22-April 13

Re: New to Java - Making a Palindrome

Posted 30 August 2013 - 03:35 AM

We also need a little more information. what happpens if the index is equal to the length of the array and the array at that index is 9??

Edit: Scratch that, I understood the problem backwards, thought you said the next index, not previous.

But still what do you want to happen when the index is 0 and the array[index] is equal to 9?

This post has been edited by schutzzz: 30 August 2013 - 03:43 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2