DOC PREVIEW
UW-Madison CS 302 - Chapter 4 – Fundamental Data Types

This preview shows page 1-2-3-22-23-24-44-45-46 out of 46 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 GoalsTo 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 TypesRecall: All values are either references Recall: All values are either references to objects or one of 8 primitivesto objects or one of 8 primitivesA 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 -727379968PrimitivesPrimitivesSo far: 6 number typesSo far: 6 number typesbyte, short, int, long for whole numbersbyte, short, int, long for whole numbersfloat, double for realsfloat, double for realsNumbers 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.99999999999994ConversionConversion2 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 castingError 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 information4.0 to 4, not a big deal4.0 to 4, not a big deal0.99 to 0, is a big deal0.99 to 0, is a big dealTo 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 12Truncates, removes fractional partTruncates, removes fractional partCan cast to any type you want Can cast to any type you want (including objects)(including objects)RoundingRoundingWhat 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 truncateReal solution: Use Math.round()Real solution: Use Math.round()long rounded = Math.round(balance);long rounded = Math.round(balance);4.2 Constants4.2 ConstantsIn math, equations have variables In math, equations have variables and constantsand constantsConstants are numbers that do not Constants are numbers that do not change but have specific meanings change but have specific meanings are importantare importantRandomly 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 namesConstantsConstantsUse a Use a constantconstant to hold the value to hold the valueValue cannot change once it is setValue cannot change once it is setUse reserved word Use reserved word finalfinal to modify to modify declarationdeclarationfinal doublefinal double PI = 3.14159 PI = 3.14159Remember Naming ConventionRemember Naming ConventionUSE_ALL_CAPS_WITH_UNDERSCORESUSE_ALL_CAPS_WITH_UNDERSCORESUsesUsesConstants often used for important Constants often used for important numbersnumbersMake meaning of code easier to Make meaning of code easier to understandunderstandEx. Go back to Ex. Go back to BankAccountBankAccountWant 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 FieldsOften, 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 classclassMake a constant field (class Make a constant field (class constant)constant)Constant FieldsConstant FieldsTwo differencesTwo differencesMake 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 changedAdd 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

UW-Madison CS 302 - Chapter 4 – Fundamental Data Types

Download Chapter 4 – Fundamental Data Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 4 – Fundamental Data Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 4 – Fundamental Data Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?