Today`s Class - University of St. Thomas
Download
Report
Transcript Today`s Class - University of St. Thomas
QMCS 230 – Today in Class
•
•
•
•
•
Authorizing Overtime
Weird Java stuff: conditionals, switch
DecimalFormat class
the printf method
Revision to Payroll
March 2005
R. Smith - University of St Thomas - Minnesota
1/18
Authorizing Overtime
• Remember the original long weekend lab?
• “Ask if overtime is authorized”
• HOW?
– inText.charAt(0) == “y”
– Nested IF + ELSE to sort out the options
• Dust off the old lab and let’s do it (9/18)
March 2005
R. Smith - University of St Thomas - Minnesota
2/18
Weird Java stuff: conditionals, switch
• Policy on Weird Java Stuff
– Example: Assignment Operators +=, -=, etc.
– Foreign language: listening vs speaking
– KNOW WHAT IT IS IF YOU SEE IT
• Conditionals: boolean ? Iftrue : iffalse
– Interesting but not necessary for us
– IF does the same for us, more or less
• SWITCH Statement
– More weird stuff
– We can do the same with IF – ELSE –IF
March 2005
R. Smith - University of St Thomas - Minnesota
3/18
How “switch” works
• Specify an integer-like value
– short, int, byte, long, char
• Provide ‘case arms’ for each value
–
–
–
–
Each arm has 1 or more statements
Ends with “break;”
If no break, falls through to next “case”
UGH!
• May provide an “else” at the end
March 2005
R. Smith - University of St Thomas - Minnesota
4/18
DecimalFormat class
• Convert numbers into strings NICELY
• Control decimal appearance
• Insert commas, force/suppress leading 0s
• import java.text.DecimalFormat;
• DecimalFormat twoDecimals =
– new DecimalFormat(“#0.00”);
March 2005
R. Smith - University of St Thomas - Minnesota
5/18
Decimal Format Options
• 0 = where you ALWAYS want a digit
– Digits to the right indicate rounding
• . = Decimal point, if any
• # = Optional leading digit
• , = Comma, like between 3 0s
– “#,##0.00” = commas, always one digit to
• the left of the decimal, exactly 2 digits to the right
of the decimal
• % = at end makes percentage (x100)
• Let’s do an example
March 2005
R. Smith - University of St Thomas - Minnesota
6/18
the printf method
• A variant of println that does formatting
• Descended from an ill-behaved Unix feature
• I only like it because
– I know it
– It’s easy to use
• Bad thing – it only works on output
– DecimalFormat works for all strings
March 2005
R. Smith - University of St Thomas - Minnesota
7/18
The printf secret code
• Make a text string
• Embed a % code for each number inserted
– d = integer decimal
– s = string
– f = double floating point
• Leading digit = # spaces to use
• Comma = insert commas in a number
• Decimal followed by digit = #decimal places
March 2005
R. Smith - University of St Thomas - Minnesota
8/18
Revision to Payroll
• Make all the amounts line up on the page
• Calculate to cents
• Suppress ill-behaved decimal points
March 2005
R. Smith - University of St Thomas - Minnesota
9/18
Creative Commons License
This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States
License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/
or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California,
94105, USA.
March 2005
R. Smith - University of St Thomas - Minnesota
10/18