Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230 – Today in Class Authorizing OvertimeAuthorizing Overtime Weird Java stuff: conditionals,

Similar presentations


Presentation on theme: "March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230 – Today in Class Authorizing OvertimeAuthorizing Overtime Weird Java stuff: conditionals,"— Presentation transcript:

1 March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230 – Today in Class Authorizing OvertimeAuthorizing Overtime Weird Java stuff: conditionals, switchWeird Java stuff: conditionals, switch DecimalFormat classDecimalFormat class the printf methodthe printf method Revision to PayrollRevision to Payroll

2 March 2005 2/18R. Smith - University of St Thomas - Minnesota Authorizing Overtime Remember the original long weekend lab?Remember the original long weekend lab? “Ask if overtime is authorized”“Ask if overtime is authorized” HOW?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)Dust off the old lab and let’s do it (9/18)

3 March 2005 3/18R. Smith - University of St Thomas - Minnesota Weird Java stuff: conditionals, switch Policy on Weird Java StuffPolicy on Weird Java Stuff –Example: Assignment Operators +=, -=, etc. –Foreign language: listening vs speaking –KNOW WHAT IT IS IF YOU SEE IT Conditionals: boolean ? Iftrue : iffalseConditionals: boolean ? Iftrue : iffalse –Interesting but not necessary for us –IF does the same for us, more or less SWITCH StatementSWITCH Statement –More weird stuff –We can do the same with IF – ELSE –IF

4 March 2005 4/18R. Smith - University of St Thomas - Minnesota How “switch” works Specify an integer-like valueSpecify an integer-like value –short, int, byte, long, char Provide ‘case arms’ for each valueProvide ‘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 endMay provide an “else” at the end

5 March 2005 5/18R. Smith - University of St Thomas - Minnesota DecimalFormat class Convert numbers into strings NICELYConvert numbers into strings NICELY Control decimal appearanceControl decimal appearance Insert commas, force/suppress leading 0sInsert commas, force/suppress leading 0s import java.text.DecimalFormat;import java.text.DecimalFormat; DecimalFormat twoDecimals =DecimalFormat twoDecimals = –new DecimalFormat(“#0.00”);

6 March 2005 6/18R. Smith - University of St Thomas - Minnesota Decimal Format Options 0 = where you ALWAYS want a digit0 = where you ALWAYS want a digit –Digits to the right indicate rounding. = Decimal point, if any. = Decimal point, if any # = Optional leading digit# = Optional leading digit, = Comma, like between 3 0s, = 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 decimalthe left of the decimal, exactly 2 digits to the right of the decimal % = at end makes percentage (x100)% = at end makes percentage (x100) Let’s do an exampleLet’s do an example

7 March 2005 7/18R. Smith - University of St Thomas - Minnesota the printf method A variant of println that does formattingA variant of println that does formatting Descended from an ill-behaved Unix featureDescended from an ill-behaved Unix feature I only like it becauseI only like it because –I know it –It’s easy to use Bad thing – it only works on outputBad thing – it only works on output –DecimalFormat works for all strings

8 March 2005 8/18R. Smith - University of St Thomas - Minnesota The printf secret code Make a text stringMake a text string Embed a % code for each number insertedEmbed a % code for each number inserted –d = integer decimal –s = string –f = double floating point Leading digit = # spaces to useLeading digit = # spaces to use Comma = insert commas in a numberComma = insert commas in a number Decimal followed by digit = #decimal placesDecimal followed by digit = #decimal places

9 March 2005 9/18R. Smith - University of St Thomas - Minnesota Revision to Payroll Make all the amounts line up on the pageMake all the amounts line up on the page Calculate to centsCalculate to cents Suppress ill-behaved decimal pointsSuppress ill-behaved decimal points

10 March 2005 10/18R. Smith - University of St Thomas - Minnesota 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.


Download ppt "March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230 – Today in Class Authorizing OvertimeAuthorizing Overtime Weird Java stuff: conditionals,"

Similar presentations


Ads by Google