1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
import java.util.Scanner; import java.util.Arrays; public class Range { private int count; private double[] values; public Range(int size) { count = 0; values = new double[size]; } /* Inserts the value of the parameter value into the array*/ public void insert(int values) { values[count] = values; count++; } /* Returns a string with the array */ public String toString() { String str = ""; for (int i = 0; i < count; i++) str += " " + values[i]; return str; } /* Returns a string with the array in reverse */ public String reverseString() { String[] tmp = new String[this.values.length]; for (int i=0;i<this.values.length;i++) { tmp[i] = this.values[this.values.length-i-1]; } return new Range(tmp); } public void add(double number) { values[count] = number; count++; } /* Test */ public static void main(String[] args) { System.out.println("\nRange begin\n"); Scanner keyboard = new Scanner(System.in); Range q; int arraysize; System.out.print("Size of array? "); arraysize = keyboard.nextInt(); q = new Range(arraysize); System.out.print("Enter " +arraysize +" values: "); for (int i = 0; i < arraysize; i++) { q.add(keyboard.nextDouble()); } System.out.println(""); System.out.println("\ttoString= " +q); System.out.println("\treverseString= " +q.reverseString()); System.out.println("Invoking orderIt()"); // q.orderIt(); System.out.println("\ttoString= " +q); System.out.println("\treverseString= " +q.reverseString()); System.out.println("\nRange end\n"); } }
Refactorings
No refactoring yet !
nyilas
November 10, 2007, November 10, 2007 13:17, permalink
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
import java.util.Scanner; import java.util.Arrays; public class Range { private int count; private double[] values; public Range(int size) { count = 0; values = new double[size]; } /* Inserts the value of the parameter value into the array*/ public void insert(int values) { this.values[count] = values; count++; } /* Returns a string with the array */ public String toString() { String str = ""; for (int i = 0; i < count; i++) str += " " + this.values[i]; return str; } /* Returns a string with the array in reverse */ public String reverseString() { String tmp = ""; for (int i=this.values.length-1; i>=0; i--) { tmp += " " + this.values[i]; } return tmp; } public void add(double number) { values[count] = number; count++; } /* Test */ public static void main(String[] args) { System.out.println("\nRange begin\n"); Scanner keyboard = new Scanner(System.in); Range q; int arraysize; System.out.print("Size of array? "); arraysize = keyboard.nextInt(); q = new Range(arraysize); System.out.print("Enter " +arraysize +" values: "); for (int i = 0; i < arraysize; i++) { q.add(keyboard.nextDouble()); } System.out.println(""); System.out.println("\ttoString= " +q); System.out.println("\treverseString= " + q.reverseString()); System.out.println("Invoking orderIt()"); // q.orderIt(); System.out.println("\ttoString= " +q); System.out.println("\treverseString= " +q.reverseString()); System.out.println("\nRange end\n"); } }
Marco Valtas
November 10, 2007, November 10, 2007 15:01, permalink
Hi, here some explanation of what was going on in your code.
In line 15:
When you have a local variable with the same name of a instance variable you should tell the java compiler what is what. In fact is a good practice always refer to instance variables with "this.variableName".
In line 38:
That's is a incompatible type, you begin a String array and try to put one double inside. Remember that your "values" instance variable is of double type, so the "tmp" variable should be a double too.
In line 40:
Two problems here, first is that you should return a String not a Range object, remember the your method declaration:
public String reverseString()
The second problem is that you try to construct a Range object with your "tmp" variable (of String[] type before refactoring), but your Range should be constructed only with integers, javac will complain about this too.
Hope this helps.
It has some problems incompatible types - referencing the array (I think)