1 / 16

Week 5

Week 5. Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham. Week 5 Topics. 5.1.1 Local Variables 5.1.2 Implicit Parameters 5.2.1 Number Types 5.2.2 Constants 5.2.3 Arithmetic Operations and Mathematical Functions

winklerm
Download Presentation

Week 5

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. Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

  2. Week 5 Topics 5.1.1 Local Variables 5.1.2 Implicit Parameters 5.2.1 Number Types 5.2.2 Constants 5.2.3 Arithmetic Operations and Mathematical Functions 5.2.4 Calling Static Methods

  3. 5.1.1 Local Variables • Instance variables • Local variables • Parameter variables All hold values. Difference is lifetime. Instance variable will exist so long as there is a reference to the object it belongs to. Parameter and local variables come to life when method is called, and die after call.

  4. 5.1.1 Local Variables Cont. public class BankAccount { private double balance; public void deposit(double amount) { double newBalance = balance + amount; balance = newBalance; } . . . balance: instance field amount: parameter variable newBalance: local variable Example: myAcct.deposit(500); Method is called, amount is created and set to 500, newBalance is created and set to balance + amount, balance is set to newBalance. After method call, amount and newBalance dies, balance still lives.

  5. 5.1.2 Implicit Parameters public class BankAccount { private double balance; public void deposit(double amount) { double newBalance = this.balance + amount; this.balance = newBalance; this.withdrawal(1.00); // pretend we won’t lose all our customers } myAcct.deposit(500); // the reference in myAcct is the implicit parameter The amount parameter is an explicit parameter. An instance variable in a class method can be denoted as this.instanceFieldName. this.balance is equivalent here to “myAcct.balance”, that is to say this refers to the implicit object parameter (myAcct object). A method call in a class with no implicit parameter will be called on the this object. The this keyword is not required, but is good programming style.

  6. 5.2.1 Number TypesPrimitive Types

  7. 5.2.1 Number Types Cont. int n = 1000000; System.out.println(n * n); // Overflow double f = 4.35; System.out.println(100 * f); // Rounding error (due to binary to decimal conversion, will print 434.99999…) int dollars = 100; double balance = dollars; // OK double balance = 13.75; int dollars = balance; // Error int dollars = (int) balance;// OK because of cast double d = 5/2;// d is 2double d = 5/2.0; // d is 2.5

  8. 5.2.2 Constants • A constant is a numeric value that does not change and is used for computations • final denotes that a variable is a constant • static means that the constant belongs to the class (not to an object like an instance field) • OK to make class constants public since the value cannot be changed • Good style to make a static constant all caps Example of a method constant: final double quarterValue = 0.25; Example of a class constant: public static final double QUARTER_VALUE = 0.25;

  9. 5.2.2 Constants Cont. // Example of using a constant in a computation double amount = numQuarters * QUARTER_VALUE; // A public static constant can be used by methods // in other classes using the syntax: // className.constantName double amount = numQuarters * Coins.QUARTER_VALUE; double circumference = Math.PI * diameter; In above examples Coins is assumed to be a user-defined class. Math is a Java API class.

  10. 5.2.3 Arithmetic Operations and Mathematical Functions A modulus B is the remainder of A divided by B. Example: 5%2 resolves to 1. Use parentheses to change the operator precedence. Example: 4 + 2 * 10 resolves to 24 whereas (4 + 2) * 10 = 60

  11. 5.2.3 Arithmetic Operations and Mathematical Functions Cont.

  12. 5.2.3 Arithmetic Operations and Mathematical Functions Cont. 5 to the 3rd power can be expressed in Java as: int x = Math.pow(5, 3);// x equals 125 The square root of 25 can be expressed in Java as: double x = Math.sqrt(25);// x equals 5 Reminder from a previous slide (worth repeating), integer division truncates the remainder, so use a floating point literal value or cast to a double to avoid this pitfall: double x = 5/2;// x equals 2

  13. 5.2.3 Arithmetic Operations and Mathematical Functions Cont. • = is the assignment operator • ++ is the increment operator • myVar = myVar + 1; can be expressed as myVar++; • -- is the decrement operator • ++myVar; is a pre-increment • myVar++; is a post-increment • --myVar; is a pre-decrement • myVar--; is a post-decrement

  14. 5.2.3 Arithmetic Operations and Mathematical Functions Cont. Assuming that myVar equals 10 at the outset of each example statement below: System.out.println(myVar++); // displays 10 System.out.println(++myVar); // displays 11 System.out.println(myVar--); // displays 10 System.out.println(--myVar); // displays 9 Note that the final value of incremented myVar is 11 and decremented myVar is 9. Make sure to understand the “pre vs. post” distinction when doing an increment or decrement as part of an assignment statement! Example: x = myVar++; // stores 10 in x, NOT 11

  15. 5.2.4 Calling Static Methods • The Math class methods per the previous section are examples of calling static methods • A static method call uses the class name rather than an object name • Format is: ClassName.methodName(parameters) • Example Math class static method definition: public static double sqrt(double a) • Classes with static methods only, such as the Java API Math class, are not used to create objects. Think of such classes as general purpose utility classes.

  16. Reference: Big Java 4th Edition by Cay Horstmann 5.1.1 Local Variables (section 3.7 in Big Java) 5.1.2 Implicit Parameters (section 3.8 in Big Java) 5.2.1 Number Types (section 4.1 in Big Java) 5.2.2 Constants (section 4.2 in Big Java) 5.2.3 Arithmetic Operations and Mathematical Functions(section 4.3 in Big Java) 5.2.4 Calling Static Methods (section 4.4 in Big Java)

More Related