Advertisement
MrDoyle

Methods) Fill-in Functions

Apr 9th, 2021
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 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 = randchar();
  9.  
  10.         System.out.println("The character is: " + c + " (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(begin, end);
  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.         y = abso(x);
  25.         System.out.println("|" + x + "| = " + y );
  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 void credits() {
  33.         // displays some boilerplate text saying who wrote this program, etc.
  34.  
  35.         System.out.println();
  36.         System.out.println("Programmed by Graham Mitchell");
  37.         System.out.println("Modified by S Doyle");
  38.         System.out.print("This code is distributed under the terms of the standard ");
  39.         System.out.println("BSD license.  Do with it as you wish.");
  40.     }
  41.  
  42.  
  43.     // chooses a random character in the range "A" to "Z"
  44.     public static char randchar() {
  45.         int numval;
  46.         char charval;
  47.  
  48.         // pick a random number from 0 to 25
  49.         numval = (int)(Math.random()*26);
  50.         // now add that offset to the value of the letter 'A'
  51.         charval = (char) ('A' + numval);
  52.  
  53.         return charval;
  54.     }
  55.  
  56.  
  57.     public static void counter( int start, int stop )
  58.     {
  59.         // counts from start to stop by ones
  60.         int ctr;
  61.         ctr = start;
  62.         while ( ctr <= stop )
  63.         {
  64.             System.out.print(ctr + " ");
  65.             ctr = ctr+1;
  66.         }
  67.     }
  68.  
  69.  
  70.     public static int abso( int value )
  71.     {
  72.         // finds the absolute value of the parameter
  73.         int absval;
  74.  
  75.         if ( value < 0 )
  76.             absval = -value;
  77.         else
  78.             absval = value;
  79.  
  80.         return absval;
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement