Advertisement
IanO-B

Untitled

Mar 10th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. public class Main {
  2.  
  3. public static void main( String[] args )
  4. { System.out.println("Watch as we demonstrate functions.");
  5.  
  6. System.out.println();
  7. System.out.println("I'm going to get a random character from A-Z");
  8. char c = '!';
  9. randchar();
  10. System.out.println("The character is: " + randchar() +" (or whatever)" );
  11.  
  12. System.out.println();
  13. System.out.println("Now let's count from -10 to 10");
  14. int begin, end;
  15. begin = -10;
  16. end = 10;
  17. counter(-10, 10);
  18. System.out.println("How was that?");
  19.  
  20. System.out.println();
  21. System.out.println("Now we take the absolute value of a number.");
  22. int x, y = 99;
  23. x = -10;
  24. abso(-10);
  25. System.out.println("|" + x + "| = " + abso(x) );
  26.  
  27. System.out.println();
  28. System.out.println("That's all. This program has been brought to you by:");
  29. credits();
  30. }
  31.  
  32. public static String credits()
  33.  
  34. { String result="";
  35. String name="Ian Ogilvie-Browne";
  36. System.out.println();
  37. System.out.println("programmed by Graham Mitchell");
  38. System.out.println("modified by "+name);
  39. System.out.print("This code is distributed under the terms of the standard ");
  40. System.out.println("BSD license. Do with it as you wish.");
  41. return result;
  42. }
  43.  
  44. public static char randchar()
  45.  
  46. { int numval;
  47. char charval;
  48. numval = (int)(Math.random()*26);
  49. charval = (char) ('A' + numval);
  50. return charval;
  51. }
  52.  
  53. public static int counter(int start, int stop)
  54.  
  55. { int ctr;
  56. ctr = start;
  57. while ( ctr <= stop )
  58. {
  59. System.out.print(ctr + " ");
  60. ctr = ctr+1;
  61. }
  62. return ctr;
  63. }
  64.  
  65. public static int abso( int value )
  66. {
  67. int absval;
  68. if ( value < 0 )
  69. absval = -value;
  70. else
  71. absval = value;
  72. return absval;
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement