Logout

HomeTopic 4 Last Next

Methods Vis-a-vis Parameters & Return Values Example

RobotsMain.java
  1 
 11 public class RobotCompetitionMain {
 12     static int score = 0;
 13     
 14     public static void main(String[] args) {
 15 
 16         Scanner snr = new Scanner(System.in);
 18         printOutIntroduction();
 19         System.out.println("How many balls were picked up?");
 20         int numBalls = snr.nextInt( );
 21         System.out.println("Did you get the bowling ball up, in place, on the ramp? true/false");
 22         boolean ballInPlace = snr.nextBoolean( );
 23         System.out.println("Your basic score is " + calculateBasicPoints(numBalls, ballInPlace));
 24 
 25         System.out.println("");
 26         System.out.println("Penalty Calculations:");
 27         System.out.println("Were there any penalties? true/false");
 28         boolean stealing = false;
 29         boolean roughing = false;
 30         int stolenNumber = 0;
 31         if(snr.nextBoolean( )){
 32             System.out.println("Did you get penalties for ball stealing? true/false");
 33             stealing = snr.nextBoolean( );
 34             if(stealing){
 35                 System.out.println("How many balls were stolen?");
 36                 stolenNumber = snr.nextInt( );
 37             }
 38             System.out.println("Did you get a penalty for roughing");
 39             if(snr.nextBoolean( )){
 40                 roughing = true;
 41             }
 42         }
 43         penaltyCalculation(stealing, stolenNumber, roughing);
 44         System.out.println("");
 45         System.out.println("Random Bonus Calculation:");
 46         System.out.println("And now you will get the random value " + randomBonusAmount() + " added to your total");
 47         score += randomBonusAmount();
 48         System.out.println("Your final score is: " + score);
 54 
 55     }
 56     //Method Example Type-A: Returns nothing, and takes no parameters.
 57     public static void printOutIntroduction(){
 58         System.out.println("                              Welcome!");
 59         System.out.println("");
 60         System.out.println("###########################*************#############################");
 61         System.out.println("#####This will calulate your points for this FTC challenge match#####");
 62         System.out.println("###########################*************#############################");
 63         System.out.println("");
 64     }
 65 
 66 
 67     //Method Example Type-B: Returns nothing, and takes in parameters.
 68 
 69     public static void penaltyCalculation(boolean stealing, int stolenNumber, boolean roughing){
 70         int penaltyPointsDeducted = 0;
 71         if(stealing && !roughing){
 72             penaltyPointsDeducted += stolenNumber;
 73         }else if(roughing && !stealing){
 74             penaltyPointsDeducted += 5;
 75         }else if(roughing && stealing){
 76             penaltyPointsDeducted = 2*stolenNumber + 5;
 77         }else{ //i.e. not roughing or stealing
 78             //no affect on the score.
 79             System.out.println("Congratulations for your clean play. You get 5 bonus points");
 80             penaltyPointsDeducted -= 5;
 81         }
 82         System.out.println("The points deducted for penalties will be: " + penaltyPointsDeducted + ".");
 83         score -= penaltyPointsDeducted;
 84     }
 85 
 86 
 87     //Method Example Type-C: Returns something, and takes in no parameters.
 88 
 89     private static int randomBonusAmount(){
 90         return (int) (Math.random() * 5);
 91     }
 92 
 93     
 94     //Method Example Type-D: Returns something, and takes in parameters.
 95     private static int calculateBasicPoints(int numBalls, boolean ballInPlace){
 96         int pointsToAddToScore = 0;
 97         if(numBalls <= 10){ //So if 10 or under, a point per ball.
 98             pointsToAddToScore = numBalls;
 99         }else{
100             pointsToAddToScore = 10 + (2 * (numBalls -10)); //Balls over the 10th count for two points each.
101         }
102 
103         if(ballInPlace){
104             pointsToAddToScore += 25;
105         }
106 
107         return pointsToAddToScore;
108     }
109 
110 }