Guest User

Untitled

a guest
Jul 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package oving2;
  2.  
  3. public class ArgumentStack {
  4. // This global variable holds the array
  5. // representing the argument/operand stack of the RPN calculator
  6. // All the methods read/write this variable
  7.  
  8. static double [] arguments = {};
  9. // initialize to avoid NullPointerException!
  10.  
  11. public static double [] copyArray (double [] original, int newSize) {
  12. double [] newArray = new double [newSize];
  13. // copy the old elements into the same positions
  14. System.arraycopy(original, 0, newArray, 0, Math.min (original.length, newArray.length));
  15. // return new array
  16. return newArray;
  17. }
  18.  
  19. public static double peek (double d) {
  20. double highest;
  21. if(arguments.length == 0) {
  22. highest = d;
  23. }
  24. else {
  25. highest = arguments[arguments.length-1];
  26. }
  27. return highest;
  28. }
  29.  
  30.  
  31. public static void push (double v) {
  32. double[] temp;
  33. temp = copyArray(arguments, arguments.length+1);
  34. temp[temp.length-1] = v;
  35. arguments = temp;
  36. }
  37.  
  38. public static double pop (double d) {
  39. double resultat;
  40. if (arguments.length == 0) {
  41. resultat = d;
  42. }
  43. else {
  44. double [] temp;
  45. temp = copyArray(arguments, arguments.length-1);
  46. resultat = arguments[arguments.length];
  47. arguments = temp;
  48. }
  49. return resultat;
  50. }
  51.  
  52. // prints the arguments stack to Standard.out, with the provided
  53. // prefix, separator and suffix
  54. public static void print (String prefix, String separator, String suffix) {
  55. System.out.print(prefix);
  56. for (int i = 0; i<arguments.length; i++) {
  57. // make sure the separator is not printed before the first value
  58. if (i > 0){
  59. System.out.print (separator);
  60. }
  61. System.out.print(arguments[i]);
  62. }
  63. System.out.print(suffix);
  64. }
  65.  
  66. public static void main (String [] args) {
  67. }
  68. }
Add Comment
Please, Sign In to add comment