Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Chapter 4 – Chapter 4 – Fundamental Data Fundamental Data TypesTypesChapter GoalsChapter GoalsTo understand integer and floating-point To understand integer and floating-point numbers numbers To recognize the limitations of the To recognize the limitations of the numeric types numeric types To become aware of causes for overflow To become aware of causes for overflow and roundoff errors and roundoff errors To understand the proper use of To understand the proper use of constants constants To write arithmetic expressions in Java To write arithmetic expressions in Java To use the String type to define and To use the String type to define and manipulate character strings manipulate character strings To learn how to read program input and To learn how to read program input and produce formatted output produce formatted output4.1 Number Types4.1 Number TypesRecall: All values are either references Recall: All values are either references to objects or one of 8 primitivesto objects or one of 8 primitivesA numeric computation overflows if the A numeric computation overflows if the result falls outside the range for the result falls outside the range for the number typenumber typeint n = 1000000;int n = 1000000;System.out.println(n * n); System.out.println(n * n); // prints -727379968 // prints -727379968PrimitivesPrimitivesSo far: 6 number typesSo far: 6 number typesbyte, short, int, long for whole numbersbyte, short, int, long for whole numbersfloat, double for realsfloat, double for realsNumbers can have overflow(value Numbers can have overflow(value too large)too large)Reals can also have Reals can also have underflow(rounding errors)underflow(rounding errors)double f = 4.35;double f = 4.35;System.out.println(100 * f); System.out.println(100 * f); // prints 434.99999999999994 // prints 434.99999999999994ConversionConversion2 types of conversion – increase in 2 types of conversion – increase in precision, decrease in precisionprecision, decrease in precisionint+/- 2E9double+/- 1E308Increase in precisionJava won’t complainDecrease in precisionJava will say it’s an errorType castingType castingError is because compiler wants us Error is because compiler wants us to know that we are going to possibly to know that we are going to possibly lose informationlose information4.0 to 4, not a big deal4.0 to 4, not a big deal0.99 to 0, is a big deal0.99 to 0, is a big dealTo override this (i.e. tell the compiler To override this (i.e. tell the compiler to convert anyways) we must use the to convert anyways) we must use the type cast operatortype cast operatorType castingType casting(<type>) (<type>) expressionexpressionExampleExampledouble balance = 12.45;double balance = 12.45;int dollar = (int) balance; //stores 12int dollar = (int) balance; //stores 12Truncates, removes fractional partTruncates, removes fractional partCan cast to any type you want Can cast to any type you want (including objects)(including objects)RoundingRoundingWhat if you want to round instead of What if you want to round instead of truncate?truncate?2 solutions:2 solutions:Hack: add 0.5 to number then truncateHack: add 0.5 to number then truncateReal solution: Use Math.round()Real solution: Use Math.round()long rounded = Math.round(balance);long rounded = Math.round(balance);4.2 Constants4.2 ConstantsIn math, equations have variables In math, equations have variables and constantsand constantsConstants are numbers that do not Constants are numbers that do not change but have specific meanings change but have specific meanings are importantare importantRandomly placed numbers in a Randomly placed numbers in a program can be confusing (and hard program can be confusing (and hard to change later), so we give them to change later), so we give them symbolic namessymbolic namesConstantsConstantsUse a Use a constantconstant to hold the value to hold the valueValue cannot change once it is setValue cannot change once it is setUse reserved word Use reserved word finalfinal to modify to modify declarationdeclarationfinal doublefinal double PI = 3.14159 PI = 3.14159Remember Naming ConventionRemember Naming ConventionUSE_ALL_CAPS_WITH_UNDERSCORESUSE_ALL_CAPS_WITH_UNDERSCORESUsesUsesConstants often used for important Constants often used for important numbersnumbersMake meaning of code easier to Make meaning of code easier to understandunderstandEx. Go back to Ex. Go back to BankAccountBankAccountWant to add something to withdraw Want to add something to withdraw methodmethodpublic void withdraw(double amount)public void withdraw(double amount){{double newBalance = balance – amount - 5;double newBalance = balance – amount - 5;balance = newBalance;balance = newBalance;}}What is the 5 doing?What is the 5 doing?Why is it there?Why is it there?public void withdraw(double amount)public void withdraw(double amount){{final int BANK_FEE = 5; final int BANK_FEE = 5; double newBalance = balance – amount – BANK_FEE;double newBalance = balance – amount – BANK_FEE;balance = newBalance;balance = newBalance;}}Constant FieldsConstant FieldsOften, we want to use the same Often, we want to use the same constant across several methods in a constant across several methods in a classclassMake a constant field (class Make a constant field (class constant)constant)Constant FieldsConstant FieldsTwo differencesTwo differencesMake it public since we don’t have to worry Make it public since we don’t have to worry about the value being changedabout the value being changedAdd static modifier to make it part of the class, Add static modifier to make it part of the class, so all objects of that class share it (more later)so all objects of that class share it (more later)public class Math{public class Math{……public static final double E = 2.71828public static final double E = 2.71828public static final double PI = 3.14159public static final double PI = 3.14159……}}public static final int BANK_FEE = 5;public static final int BANK_FEE = 5;public void
View Full Document