Advertisement
Shavit

P. 85 Ex. 11.9

Jan 28th, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class candiesMain {
  7.  
  8.     public static void main(String[] args)
  9.    
  10.     {
  11.         Scanner in = new Scanner(System.in);
  12.        
  13.         String input;
  14.         int amount;
  15.         int small = 0;
  16.         int big = 0;
  17.         int unbalanced = 0;
  18.        
  19.         System.out.printf("How many bags of candies would you like to make? ");
  20.         amount = in.nextInt();
  21.        
  22.         candies[] obj = new candies[amount];
  23.        
  24.         for(int i = 0; i < amount; i++)
  25.         {
  26.             input = in.next();
  27.             obj[i] = new candies(input);
  28.             small += obj[i].small();
  29.             big += obj[i].big();
  30.             unbalanced += obj[i].unbalanced();
  31.         }
  32.        
  33.         System.out.printf("The amount of small bags you made is %d. The amount of big bags you made is %d. You have made %d unbalanced bags today.", small, big, unbalanced);
  34.        
  35.         in.close();
  36.     }
  37. }
  38.  
  39. // Next class
  40.  
  41. public class candies
  42. {
  43.    
  44.     private String input;
  45.     private int red;
  46.     private int yellow;
  47.     private int green;
  48.     private int brown;
  49.     private int size;
  50.    
  51.     public candies(String input)
  52.     {
  53.         this.input = input;
  54.         ini();
  55.     }
  56.    
  57.     private void ini()
  58.     {
  59.         for(int i = 0; i < input.length(); i++)
  60.         {
  61.             char current = input.charAt(0);
  62.             if(current == 'r')
  63.                 red++;
  64.             else if(current == 'y')
  65.                 yellow++;
  66.             else if(current == 'g')
  67.                 green++;
  68.             else if(current == 'b')
  69.                 brown++;
  70.         }
  71.        
  72.         size = input.length();
  73.     }
  74.    
  75.     public int small()
  76.     {
  77.         if(size == 20)
  78.             return 1;
  79.         else
  80.             return 0;
  81.     }
  82.    
  83.     public int big()
  84.     {
  85.         if(size == 28)
  86.             return 1;
  87.         else
  88.             return 0;
  89.     }
  90.    
  91.     public int unbalanced()
  92.     {
  93.         if(red == yellow && red == green && red == brown)
  94.             return 0;
  95.         else
  96.             return 1;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement