Advertisement
Shavit

P. 103 Ex. 11.20

Feb 25th, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class MrSocks {
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         Scanner in = new Scanner(System.in);
  11.        
  12.         int size;
  13.         String color;
  14.         String sample;
  15.        
  16.         boolean found = false;
  17.         boolean comparison;
  18.        
  19.         System.out.printf("Mr. Socks, please enter your sock details.\n");
  20.        
  21.         System.out.printf("Size: ");
  22.         size = in.nextInt();
  23.        
  24.         System.out.printf("Color: ");
  25.         color = in.next();
  26.        
  27.         System.out.printf("Sample: ");
  28.         sample = in.next();
  29.        
  30.         Socks mainSock = new Socks(size, color, sample);
  31.        
  32.         System.out.printf("Try to find a pair.\n");
  33.        
  34.         while(!found)
  35.         {
  36.             System.out.printf("Size: ");
  37.             size = in.nextInt();
  38.            
  39.             System.out.printf("Color: ");
  40.             color = in.next();
  41.            
  42.             System.out.printf("Sample: ");
  43.             sample = in.next();
  44.            
  45.             Socks currentSock = new Socks(size, color, sample);
  46.             comparison = mainSock.compareToSock(currentSock);
  47.            
  48.             System.out.printf("%s", comparison ? "Congrats, these are a pair!" : "Keep trying Mr. Socks.\n");
  49.             found = comparison ? true : false;
  50.         }
  51.        
  52.         in.close();
  53.     }
  54. }
  55.  
  56. // Next class
  57.  
  58. public class Socks
  59. {
  60.     private int size;
  61.     private String color;
  62.     private String sample;
  63.    
  64.     public Socks(int size, String color, String sample)
  65.     {
  66.         this.size = size;
  67.         this.color = color;
  68.         this.sample = sample;
  69.     }
  70.    
  71.     public int getSize()
  72.     {
  73.         return size;
  74.     }
  75.    
  76.     public String getColor()
  77.     {
  78.         return color;
  79.     }
  80.    
  81.     public String getSample()
  82.     {
  83.         return sample;
  84.     }
  85.    
  86.     public boolean compareToSock(Socks nextSock)
  87.     {
  88.         boolean isValid = true;
  89.         if(size != nextSock.getSize())
  90.             isValid = false;
  91.         if(!(color.equals(nextSock.getColor())))
  92.             isValid = false;
  93.         if(!(sample.equals(nextSock.getSample())))
  94.             isValid = false;
  95.         return isValid;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement