11-Expressions-And-Idioms-1 - CS106A Handout 11 April 8th...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106A Handout 11 Spring 2011 April 8 th , 2011 Expressions and Idioms Centimeters To Feet And Inches /** * File: CentimetersToFeet.java * ---------------------------- * Prompts the user to enter a length in centimeters, and * then prints the same length in feet and inches. This * requires a nontrivial understanding how to mix ints and * doubles in expressions, and why the cast operator is necessary * when initializing the fullFeet variable. */ import acm.program.*; public class CentimetersToFeet extends ConsoleProgram { /** * Presents the logic of the entire program, which helps * the user convert lengths from centimeters to feet and inches. */ public void run() { println("This program converts centimeters to feet and inches."); println(); double cm = readDouble("Please enter a length (in centimeters): "); double totalInches = cm / CM_PER_INCH ; int fullFeet = ( int ) (totalInches / INCHES_PER_FOOT ); double remainingInches = totalInches - fullFeet * INCHES_PER_FOOT ; println(cm + " cm => " + fullFeet + " ft and " + remainingInches + " in"); } /* Private constants */ private static final double CM_PER_INCH = 2.54; private static final int INCHES_PER_FOOT = 12; } This is expression involves one double and one int . Whenever Java encounters traditional arithmetic expressions involving whole numbers and real numbers, it promotes all of the int s to be double s so that everything is compatible. In this case, the result is a double , but we cast the result to an int to inspire truncation. Without the cast, fullFeet can’t be set equal to the expression on the right, because Java fears you may not realize you’re losing the fractional part. By inserting the cast, you’re acknowledging that the truncation is okay. We typically associate capitalized identifiers with constants when those constants are only meaningful given some context. See that 12? Is that the number of months per year? The number of items per dozen? No, in this case, it’s the number of inches in a foot, and the constant identifier we choose makes that clear. The implementation of the program can now be framed in terms of INCHES_PER_FOOT instead of some random 12s.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Converting Raw Seconds to Days, Hours, and Minutes This program is similar to the previous one, but it’s clearly more involved. In a nutshell, the user is asked to enter the number of seconds that have lapsed since January 1 and stores his/her response in remainingSeconds . Over the next few lines, remainingSeconds is decremented to incrementally reflect the number of seconds that can’t be accounted for by entire hours, days, and minutes. /** * File: SecondsSinceNewYear.java * ------------------------------ * More advanced program designed to extract an arbitrary * second amount (assumed to be the number of seconds that * have lapsed since December 31 became January 1) and then * print out the number of days, hours, minutes, and extra seconds * it corresponds to.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

11-Expressions-And-Idioms-1 - CS106A Handout 11 April 8th...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online