Download presentation

Presentation is loading. Please wait.

Published byLamont Boothby Modified about 1 year ago

1
March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Review of Primitive TypesReview of Primitive Types Arithmetic OperationsArithmetic Operations Casting and TypesCasting and Types Lab WorkLab Work

2
March 2005 2/18R. Smith - University of St Thomas - Minnesota Programming Style Comment block at ‘top’Comment block at ‘top’ –Name, date, assignment identification Indenting that matches the book styleIndenting that matches the book style –“minor” deviations may be OK (ask me!) Variable declarationsVariable declarations –At the start of the method block –Comment on EVERY variable declaration –Blank line between variables and statements Comment above major pieces of codeComment above major pieces of code –Show which pieces do which part of the assignment

3
March 2005 3/18R. Smith - University of St Thomas - Minnesota Primitive Types Java programs deal with 2 kinds of thingsJava programs deal with 2 kinds of things –Primitive Types, like int –Objects, like String (note it’s capitalized) Four ‘types’ of primitive typesFour ‘types’ of primitive types –Integers in 4 sizes –Floating point numbers in 2 sizes –Booleans - Chars WHY???WHY??? –It’s based on computer hardware –Computers can easily deal with those 8 types

4
March 2005 4/18R. Smith - University of St Thomas - Minnesota Relevant Primitive Sizes For Reading the Book Keep in mind the sizes of floats and intsKeep in mind the sizes of floats and ints –Ints smallest to biggest: –byte -> short -> int -> long –For floating point: float -> double For Most Assignments Just use int and doubleJust use int and double

5
March 2005 5/18R. Smith - University of St Thomas - Minnesota Arithmetic Operations The nuts and bolts of calculationThe nuts and bolts of calculation What are the usual operations?What are the usual operations? –Precedence? Grouping? Integers vs DoublesIntegers vs Doubles –double = double + int –All int --> int –Any double -> double

6
March 2005 6/18R. Smith - University of St Thomas - Minnesota Division: Integers vs Doubles Integer Division yields integersInteger Division yields integers –Toss the fraction –int = int / int Double division yields fractionsDouble division yields fractions –double = double / double –ANY double yields double division

7
March 2005 7/18R. Smith - University of St Thomas - Minnesota Example - Averaging Define and initialize 4 integer variablesDefine and initialize 4 integer variables –Use prime numbers for the 4 values Calculate the averageCalculate the average Print it outPrint it out

8
March 2005 8/18R. Smith - University of St Thomas - Minnesota Converting between types Java always allows “widening” conversionsJava always allows “widening” conversions –I.E. you copy “smaller” data to a “larger” variable In “narrowing” conversions, data may be lostIn “narrowing” conversions, data may be lost “Ranking of data types“Ranking of data types –Widening conversion: assignment higher = lower double -> highest rankdouble -> highest rank floatfloat longlong intint shortshort byte -> lowest rankbyte -> lowest rank

9
March 2005 9/18R. Smith - University of St Thomas - Minnesota Casting Syntax to force a variable to a typeSyntax to force a variable to a type –Put the type name in parentheses When to use itWhen to use it –Narrowing assignment statement –Need floating division on integers –Any case where the types are wrong What can go wrong?What can go wrong? –Narrowing assignment can be ‘truncated’ –Only use it when you know it’s the right thing.

10
March 2005 10/18R. Smith - University of St Thomas - Minnesota Calculations: what type? float, double, int, longfloat, double, int, long Integer calculationsInteger calculations –Uses int unless a long is present, too –Problem: sum of shorts is an int Floating point calculationsFloating point calculations –If no double, then it’s float (highest rank)

11
March 2005 11/18R. Smith - University of St Thomas - Minnesota A rectangle example Define 4 variables:Define 4 variables: –int x1, x2; –double y1, y2; –Make x1,y1 both less than x2,y2 Calculate lengthsCalculate lengths –Horizontal sides –Vertical sides –Average length of sides

12
March 2005 12/18R. Smith - University of St Thomas - Minnesota Combined Assignment Operators Neat shortcut stolen from “C”Neat shortcut stolen from “C” –Assign value and perform operation w/binary operator –Add, sub, mul, div, modulus –Same type rules Can we use it in that program we just did?Can we use it in that program we just did?

13
March 2005 13/18R. Smith - University of St Thomas - Minnesota Lab Problem Define 4 variables:Define 4 variables: –int x1, y1; //give low initial values –int x2, y2; // initial values greater than x1, y1 These 4 values define a RECTANGLEThese 4 values define a RECTANGLE –(x1, y1) is the lower left corner –(x2, y2) is the upper right corner Calculate the following:Calculate the following: –The length of the horizontal side –The length of the vertical side –The average length of the sides –The perimeter (sum of lengths of sides) –The area within the rectangle.

14
March 2005 14/18R. Smith - University of St Thomas - Minnesota Hints WATCH OUT FOR CONVERSIONS!WATCH OUT FOR CONVERSIONS! Make a variable for each resultMake a variable for each result

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google