Advertisement
Shavit

P. 80 Ex. 11.4

Jan 26th, 2014
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class pencilCaseEvents {
  7.  
  8.     public static void main(String[] args)
  9.    
  10.     {
  11.         Scanner in = new Scanner(System.in);
  12.         pencilCase current;
  13.        
  14.         int pens;
  15.         int pencils;
  16.         int eventsNum;
  17.        
  18.         System.out.printf("Enter the number of pens you have in your pencil case: ");
  19.         pens = in.nextInt();
  20.        
  21.         System.out.printf("Enter the number of pencils you have in your pencil case: ");
  22.         pencils = in.nextInt();
  23.        
  24.         current = new pencilCase(pens, pencils);
  25.        
  26.         System.out.printf("How many events happened to your pencil case? ");
  27.         eventsNum = in.nextInt();
  28.        
  29.         int[] events = new int[2];
  30.        
  31.         System.out.printf("Enter the numbers of the events: ");
  32.         for(int i = 0; i < eventsNum; i++)
  33.         {
  34.             events[0] = in.nextInt();
  35.             events[1] = in.nextInt();
  36.             if(events[0] == 1)
  37.             {
  38.                 if(events[1] == 1)
  39.                     current.addPen();
  40.                 else
  41.                     current.addPencil();
  42.             }
  43.             else
  44.             {
  45.                 if(events[1] == 1)
  46.                     current.subPen();
  47.                 else
  48.                     current.subPencil();
  49.             }
  50.         }
  51.        
  52.         System.out.printf("Your pencil case has %d pens and %d pencils", current.pens(), current.pencils());
  53.        
  54.         in.close();
  55.     }
  56. }
  57.  
  58. // Next class
  59.  
  60. public class pencilCase
  61. {
  62.     private int pens;
  63.     private int pencils;
  64.    
  65.     public pencilCase(int pens, int pencils)
  66.     {
  67.         this.pens = pens;
  68.         this.pencils = pencils;
  69.     }
  70.    
  71.     public void addPencil()
  72.     {
  73.         pencils++;
  74.     }
  75.    
  76.     public void addPen()
  77.     {
  78.         pens++;
  79.     }
  80.    
  81.     public void subPencil()
  82.     {
  83.         pencils--;
  84.     }
  85.    
  86.     public void subPen()
  87.     {
  88.         pens--;
  89.     }
  90.    
  91.     public int pens()
  92.     {
  93.         return pens;
  94.     }
  95.    
  96.     public int pencils()
  97.     {
  98.         return pencils;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement