Guest User

Untitled

a guest
Jan 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. // Daniel Reilly  Chapter 2, Exercise 11
  2.  
  3. import java.util.*;
  4.  
  5. public class Ch2_11
  6. {
  7.  
  8.     static Scanner console = new Scanner (System.in);
  9.    
  10.  
  11.     public static void main(String[] args)
  12.     {
  13.        
  14.          /**
  15.          *******************ALGORITHM*************************
  16.          *****************************************************
  17.          *****************************************************
  18.          * Prompt user for a length in centimeters.      
  19.          *  Convert centimeters to inches.             
  20.          *      centimeters / 2.54 (CENTIMETERS_PER_INCH).  
  21.          *  Round inches to the nearest whole number.    
  22.          *  Convert inches to yards.                       
  23.          *      yards = inchesRounded /36 (INCHES_PER_YARD)
  24.          *  Get remainder to get feet.                   
  25.          *      remainder = inchesRounded / INCHES_PER_YARD      
  26.          *  Get the remaining feet by dividing reaminder by
  27.          *      12 (INCHES_PER_FOOT)                     
  28.          *  Get the remaining inches using modulus division.
  29.          *          inches = remainder % INCHES_PER_FOOT    
  30.          *  Print out result in the format of:          
  31.          *      X yard(s), X feet(foot), and X inch(es)  
  32.          *****************************************************
  33.          *****************************************************
  34.          *****************************************************
  35.          */
  36.        
  37.        
  38.        
  39.        
  40.         final double CENTIMETERS_PER_INCH = 2.54;      
  41.         final int INCHES_PER_FOOT = 12;                                    
  42.         final int INCHES_PER_YARD = 36;
  43.        
  44.         double inches;             
  45.         int inchesRounded;     
  46.         int centimeters;           
  47.         int feet;                                  
  48.         int yards;
  49.         int remainder;
  50.        
  51.        
  52.         centimeters = console.nextInt();           
  53.        
  54.        
  55.         inches = centimeters / CENTIMETERS_PER_INCH;            
  56.         inchesRounded = (int)(Math.round(inches));           
  57.    
  58.         yards = inchesRounded / INCHES_PER_YARD;
  59.         remainder = inchesRounded % INCHES_PER_YARD;
  60.         feet = remainder / INCHES_PER_FOOT;
  61.         inches = remainder % INCHES_PER_FOOT;
  62.    
  63.        
  64.        
  65.        
  66.    
  67.  
  68.        
  69.        
  70.         System.out.println(yards +" yard(s), " + feet + " feet (foot), " +"and " + (int)inches + " inch(es)");  
  71.     }
  72.  
  73. }
Add Comment
Please, Sign In to add comment