1 / 43

Chapter 3

Chapter 3. Fundamental Data Types Number Types (3.1) Assignment (3.2) Constants (3.3) Arithmetic and Mathematical Functions (3.4) Calling Static methods (3.5) Quality Tips 3.1, 3.2 Advanced Topics 3.1, 3.3 Common Error 3.1, 3.2. Java primitive types - examples. int

Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Fundamental Data Types Number Types (3.1) Assignment (3.2) Constants (3.3) Arithmetic and Mathematical Functions (3.4) Calling Static methods (3.5) Quality Tips 3.1, 3.2 Advanced Topics 3.1, 3.3 Common Error 3.1, 3.2

  2. Java primitive types - examples int 3 4 -700 32767 -21211 0 double 3.456 -3456.789 0.6 0.0 char 'd' 'a' '*' boolean true false

  3. Numbers • Integers • int type denotes integers. • Integers can have a range of -2,147,483,648 to 2,147,483,647. • Remember, an int is represented using 32 bits. • If you need to handle integral values beyond that range, use long (64 bits).

  4. Numbers • Floating-point numbers • double type denotes floating point numbers. • go to about 10300. • Remember, an double is represented using 64 bits. • double numbers suffer from precision problems. They store only about 15 significant digits.

  5. Precision Problem: double originalPrice = 3E14;(3*1014) 300,000,000,000,000.00 double discountedPrice = originalPrice – 0.05; 299,999,999,999,999.95 double discount = originalPrice - discountedPrice; System.out.println(discount); //Should print 0.05, prints 0.0625

  6. Arithmetic Operations • addition + • subtraction - • multiplication * • division / • remainder % (integer operation) • 7 % 3 = 1 • 1 % 3 = 1 • 8 % 4 = 0

  7. 2 + 3 = 5 2 + 3.0 = 5.0 2.0 + 3 = 5.0 2.0 + 3.0 = 5.0 7.0 / 2.0 = 3.5 7 / 2 = 3 3 / 4 = 0 3.0 / 4 = 0.75 5 % 2 + 6 = 1 + 2 * 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.0 / 4 = 0.75 5 % 2 + 6 = 7 1 + 2 * 3 / 5 = 2 6 / 7 * 4 = 0 6.0 / 2.0 * 5 = 15.0 1 - 8 / 3 = -1 4 + 4 / 4 % 4 * 4 = Rules of Arithmetic:

  13. 2 + 3 = 5 2 + 3.0 = 5.0 2.0 + 3 = 5.0 2.0 + 3.0 = 5.0 7.0 / 2.0 = 3.5 7 / 2 = 3 3 / 4 = 0 3.0 / 4 = 0.75 5 % 2 + 6 = 7 1 + 2 * 3 / 5 = 2 6 / 7 * 4 = 0 6.0 / 2.0 * 5 = 15.0 1 - 8 / 3 = -1 4 + 4 / 4 % 4 * 4 = 8 Rules of Arithmetic:

  14. Assignment public Circle() { diameter = 30; xPosition = 20; yPosition = 6; color = "magenta"; isVisible = false; } • The = operator is called the assignment operator.

  15. Assignment • On the left side of the assignment operator you must have a variable name. • On the right side of the assignment operator you may have a single value or an expression. • diameter = 30; • balance = balance + howMuch; • circumference = 3.14 * diameter;

  16. Assignment • Arithmetic operations can be combined with the assignment operator. • balance += howMuch; • balance -= howMuch; • Operations combined with assignment += -= *= /= %=

  17. Increment/Decrement Operators int count = 0; count++; //count is now 1; count += 3; //count is now 4; count--; //count is now 3;

  18. Operator Order of Precedence • ++ -- • * / % • + - • = *= /= %= += -=

  19. Constants • A finalvariable is a constant. Once its value is set, it cannot be changed. • final variables are named with all capital letters. • Do not use magicnumbers in your programs. If you have a need to do this, rethink….should this be a final variable?

  20. Circle class • Let's add • findCircumference() • findArea()

  21. Example public class Circle { // Constructor goes here // Returns the circumference of the Circle public double findCircumference() { //code goes here } // Returns the area of the Circle public double findArea() { //code goes here }

  22. Example public class Circle { // Other code goes here // Returns the circumference of the Circle public double findCircumference() { return 3.14 * diameter; //magic number } // Returns the area of the Circle public double findArea() { double radius = diamter/2; return 3.14 * radius * radius; //magic number } }

  23. Example public class Circle { // Other code goes here // Returns the circumference of the Circle public double findCircumference() { return PI * diameter; //magic number replaced } // Returns the area of the Circle public double findArea() { double radius = diamter/2; return PI * radius * radius; //magic number replaced } private static final double PI = 3.1415926; //private instance fields declared here }

  24. Other useful constants • TAX_RATE • DIME_VALUE • QUARTER_VALUE • PENNIES_PER_DOLLAR • HOURS_IN_DAY • MINUTES_IN_HOUR • MAX_SEATS

  25. the Math class • Java provides certain math finctions for us. The Math class contains methods that can be very useful (page 95 of your text).

  26. Useful methods in Math class

  27. All methods in the Math class • Our first look at the API !

  28. Static methods • The Math class contains static methods. • This means that the method does not operate on a particular object. • You do not instantiate an object of the Math class in order to use the methods of the Math class. • You invoke the square root method by • Math.sqrt(4)

  29. The quadratic formula

  30. The quadratic formula Write assignment statements that will return the two roots, x1 and x2.

  31. Answers x1 = (-b + Math.sqrt(b*b – 4*a*c))/(2*a); x2 = (-b - Math.sqrt(b*b – 4*a*c))/(2*a);

  32. A better way… double root = Math.sqrt(b * b – 4 * a * c); x1 = (-b + root) / (2*a); x2 = (-b - root) / (2*a); Why is this way "better"?

  33. The Math class • Java API – look at field summary. • How does this change our findCircumference and findArea methods in out Circle class?

  34. Example public class Circle { // Other code goes here // Returns the circumference of the Circle public double findCircumference() { return PI * diameter; //magic number replaced } // Returns the area of the Circle public double findArea() { double radius = diamter/2; return PI * radius * radius; //magic number replaced } private static final double PI = 3.1415926; //private instance fields declared here }

  35. Example public class Circle { // Other code goes here // Returns the circumference of the Circle public double findCircumference() { return Math.PI * diameter; //final variable replaced } // Returns the area of the Circle public double findArea() { double radius = diamter/2; return Math.PI * radius * radius; //final variable replaced } //private instance fields declared here }

  36. Our BankAccount Class • Let's add a method computeInterest(). • computeInterest will increase our current balance by 2.5%. • Let's add a method chargeServiceFee(). • chargeServiceFee()will deduct $9.95 from our balance.

  37. Reminders about naming conventions • A variable name must be a legal identifier and not a keyword (double, int, new, etc.) or null. • By Convention :  Variable names begin with a lowercase letter, and class names begin with an uppercase letter. If a variable name consists of more than one word, the words are joined together, and each word after the first begins with an uppercase letter, like this: isVisible. The underscore character (_) is used to separate words in constants(final variables are named with all caps).

  38. Reminders about object variables and object references • Circle sun; • defines an object variable or type Circle. • new Circle(); • constructs a Circle object. • Circle otherSun = new Circle(); • constructs a Circle object and assigns it to the object variable otherSun.

  39. Reminders aboutmethods and parameters • Circle sun(30,10,40,"yellow"); • 30, 10, 40, "yellow" are constructionparameters (provide information for the construction of the object). • b1.deposit(100); • 100 is a value passed to the method deposit. • deposit (double howMuch); • howMuch is a parameter of the method deposit. • In a method definition, each parameter has a type and a name. The parameters are separated by commas. • In a method call, the values passed must be consistent with the parameters listed in the method header. • A method definition specifies the access modifier, the return type, the method name, the parameters, and the statements for performing the task.

  40. Reminders about errors • A compile-time error • a syntax error is a violation of the rules of the language • missing semicolon • misspelled identifier • missing { or } • A runtime error • division by 0; • NullPointerException (didn't initialize an object field) • A logic error (an error of intent?) • program executes but performs a task that was not intended by the programmer.

  41. Reminders about types • Primitive types • int • double • char • boolean • Everything else is a reference

  42. Number variables versesObject variables • Number variables hold values: diameter = 30; anotherNumber = diameter diameter = 40; • Changing diameter DOES NOT affect the value of anotherNumber. diameter anotherNumber 30 30

  43. Number variables versesObject variables • Object variables hold references: BankAccount b1 = new BalkAccount(1000); BankAccount b2 = b1 b1.deposit(200); • Changing b1 DOES affect the b2. b1 balance = 1000 b2

More Related