Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC124 TA names and s have been added to the course web site.

Similar presentations


Presentation on theme: "CISC124 TA names and s have been added to the course web site."— Presentation transcript:

1 CISC124 TA names and emails have been added to the course web site.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s have been added to the course web site. You have been added to two groups in onQ: The “Lab Chosen” group reflects the lab you indicated in the lab section survey. The “Grader” group uses the first name of your grader to indicate who will be grading your assignments. Labs start this week in JEFF 155. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

2 Lab Choices, Cont. 16 people did not fill out the survey. I have assigned you to graders in a way that balances out the grading load. It is too late to tell me your lab choice now. 63 people indicated the Tuesday 2:30 lab (I wonder why…). The lab cannot hold this many people. At quiz time you may have to wait for a vacant seat to write the quiz. Fall 2018 CISC124 - Prof. McLeod

3 QWIC Program Tutorial Open to all students in this course.
First one will be held: On Sept. 24 (Monday) at 8pm In Mac-Corry D201. By Hannah LeBlanc. I will broadcast by more information and reminders. Fall 2018 CISC124 - Prof. McLeod

4 Today Continue Java Syntax: Building Expressions:
Constants Type Casting Building Expressions: Operators and Precedence Keywords Invoking Methods (Coming up next lecture – conditionals and loops). Fall 2018 CISC124 - Prof. McLeod

5 Constant Attribute Declaration
Syntax: [private|public] [static] final type ATTRIBUTE_NAME = literal_value; The Java keyword, final can be used to make sure a variable value is no longer “variable”. Usually these are declared public static. The value must be assigned – this part is no longer optional. Fall 2018 CISC124 - Prof. McLeod

6 Constants, Cont. Java will not allow your program to change a constant’s value once it has been declared. For example: final int NUM_DAYS_IN_YEAR = 365; final double MM_PER_INCH = 25.4; Note that constant names are all in upper-case, by convention. You can also declare constants inside a method. Fall 2018 CISC124 - Prof. McLeod

7 byte > short > int > long > float > double
Type Casting When a value of one type is stored into a variable of another type. Casting of primitive types in one direction is automatic, you do not have to deliberately or “explicitly” cast: A value to the left can be assigned to a variable to the right without explicit casting: byte > short > int > long > float > double Fall 2018 CISC124 - Prof. McLeod

8 Type Casting - Cont. For example in the statement:
double myVar = 3; the number 3 is automatically cast to a double (3.0) before it is stored in the variable “myVar”. However, if you tried the following: int anotherVar = ; the compiler would protest loudly because a double cannot be stored in an int variable without loss of precision. Wrong direction! Fall 2018 CISC124 - Prof. McLeod

9 Casting Operator If you really want to cast in the other direction, then you must make an explicit cast. For example: int anotherVar = (int) ; is legal. The “(int)” part of the statement casts the double to an int. The variable anotherVar would hold the value 345 Note how numbers are truncated, not rounded! Fall 2018 CISC124 - Prof. McLeod

10 Aside – Casting Objects
We will find (later) that you can cast objects using the casting operator too. Objects must have an inheritance relationship in order for the cast to succeed. For example: Integer aVal = new Integer(45); Number aNum = (Number)aVal; The Integer class extends the Number class. More about this later… Fall 2018 CISC124 - Prof. McLeod

11 What Makes an Expression?
What are all the components available to a programmer to use to put a line of code together? Variables Literal Values Keywords Operators Method Invocations Punctuation We are working our way through this list! Look at Operators next: Fall 2018 CISC124 - Prof. McLeod

12 Arithmetic Operators The standard binary arithmetic operators in Java are: Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulus or Remainder (%) (ie. 12 % 5 yields 2) All of these operations apply to all numeric primitive data types. All require values on both sides of the operator (why they are called “binary operators”). Java does not have ** or ^ (exponentiation). Use Math.pow(x, y) to get xy. Fall 2018 CISC124 - Prof. McLeod

13 Integer Arithmetic Arithmetic operations between integers produce integer results by truncating the answer; fractional parts are discarded: Examples: 3 / 4 stores as 0 4 / 4 stores as 1 5 / 4 stores as 1 If you have an integer on both sides of an arithmetic operator, the result will be an integer. What happens if there is a double on one side? Fall 2018 CISC124 - Prof. McLeod

14 Python vs Java Division (Round 3)
In Python version 3.#, division always yields a float: >>> 3 / 4 0.75 Floor division yields an integer: >>> 3 // 4 In Java there is only / and the result depends on the operand types. Fall 2018 CISC124 - Prof. McLeod

15 Mixed Type Arithmetic Expressions
Suppose you have a “mixed type” expression involving an arithmetic operator. To evaluate the expression, Java will cast one side to match the other. For example if one side is an int and the other side is a double, the int will be automatically cast to a double before the operation takes place. For example: 9 / 2 stores as 4 9 / 2.0 stores as 4.5 4 * 12 stores as 48 4.0 * 12 stores as 48.0 Fall 2018 CISC124 - Prof. McLeod

16 Strings and the “+” Operator
Not only can “+” operate on numeric values, but it can also handle String’s on either or both sides. If one side is not a String, it will be changed to one, and then it will be concatenated to the String on the other side: 4 + “you” stores as “4you” “apples” + “oranges” stores as “applesoranges99” “little piggies” stores as “10little piggies” Expressions are evaluated from left to right, unless precedence rules apply. Fall 2018 CISC124 - Prof. McLeod

17 Unary Arithmetic Operators
Unary operators include “+” and “-”, where -aNum negates the value produced by aNum, for example. They also include the increment (++) and decrement (--) operators which increase or decrease an integer value by 1. Preincrement and predecrement operators appear before a variable. They increment or decrement the value of the variable before it is used in the expression. Example: int i = 4, j = 2, k; k = ++i - j; // i = 5, j = 2, k = 3 Fall 2018 CISC124 - Prof. McLeod

18 Unary Arithmetic Operators - Cont.
Postincrement and postdecrement operators appear after a variable. They increment or decrement the value of the variable after it is used in the expression. Example: int i = 4, j = 2, k; k = i++ - j; // i = 5, j = 2, k = 2 It is better to keep increment and decrement operators out of larger expressions! Fall 2018 CISC124 - Prof. McLeod

19 Assignment Operators = set equal to *= multiply and set equal to
/= divide and set equal to -= subtract and set equal to += add and set equal to Fall 2018 CISC124 - Prof. McLeod

20 Assignment Operators - Cont.
An assignment statement in Java has the form variableName = expression; The expression is evaluated, and the result of the expression is stored in the specified variable. And, as seen on the previous slide, for example: variableName += expression; is equivalent to variableName = variableName + expression; Fall 2018 CISC124 - Prof. McLeod

21 Logical Binary Operators
Return either true or false. == equals to != not equals to > greater than < less than >= greater than or equal to <= less than or equal to &, && logical “And” |, || logical “Or” Fall 2018 CISC124 - Prof. McLeod

22 Aside – “|” or “||”? What’s the difference?
A single | or & always evaluates both sides of the expression, whether it is necessary or not. && stops if the left side is false, || stops if the left side is true. When would this make a difference? For example: int x = 0, y = 4; boolean test = x != 0 & y / x > 1; Gives a runtime error, using && would not. Why? Fall 2018 CISC124 - Prof. McLeod

23 Logical Operators - Cont.
The one unary logical operator is “!”. Called the “Not” operator. It reverses the logical value of a boolean. For example: !(5 > 3) evaluates to false Fall 2018 CISC124 - Prof. McLeod

24 Precedence Rules Operator precedence rules determine which operations take place in what order: Unary operations and casting are done first. Then *, /, % Then +, - Then <, >, <=, >= Then ==, != Then &, &&, |, || Then =, *=, +=, -=, /= Use “( )” to control order of operations, as the expression inside “( )” will be evaluated before stuff outside of “( )”. Why are assignment operators always last? Fall 2018 CISC124 - Prof. McLeod

25 Expressions Expressions are combinations of variables, literal values, operators, keywords, method calls, etc. For example: int aNum = * 7; // aNum is 25 int aNum = (4 + 3) * 7; // aNum is 49 (4 > 7) || (10 > -1) // yields true (5.5 >= 5) && (4 != 1.0) // yields true double circ = 3.14 * 2 * r; Fall 2018 CISC124 - Prof. McLeod

26 What’s Left on Expressions?
What are all the components available to a programmer to use to put a line of code together? Variables Literal Values Keywords Operators Method Invocations Punctuation Still need to do these ones. Fall 2018 CISC124 - Prof. McLeod

27 50 Java Keywords                           
abstract double int super assert else interface switch boolean enum long synchronized break extends native this byte for new throw case final package throws catch finally private transient char float protected try class goto public var const if return void continue implements short volatile default import static while do instanceof strictfp the ones we will use: Fall 2018 CISC124 - Prof. McLeod

28 Java Keywords – Primitive Types
abstract double int super assert else interface switch boolean enum long synchronized break extends native this byte for new throw case final package throws catch finally private transient char float protected try class goto public var const if return void continue implements short volatile default import static while do instanceof strictfp Fall 2018 CISC124 - Prof. McLeod

29 Java Keywords – Loops abstract double int super assert else interface
switch boolean enum long synchronized break extends native this byte for new throw case final package throws catch finally private transient char float protected try class goto public var const if return void continue implements short volatile default import static while do instanceof strictfp Fall 2018 CISC124 - Prof. McLeod

30 Java Keywords – Conditionals
abstract double int super assert else interface switch boolean enum long synchronized break extends native this byte for new throw case final package throws catch finally private transient char float protected try class goto public var const if return void continue implements short volatile default import static while do instanceof strictfp Fall 2018 CISC124 - Prof. McLeod

31 Java Keywords – Exceptions
abstract double int super assert else interface switch boolean enum long synchronized break extends native this byte for new throw case final package throws catch finally private transient char float protected try class goto public var const if return void continue implements short volatile default import static while do instanceof strictfg Fall 2018 CISC124 - Prof. McLeod

32 Java Keywords – Class & Method Headers
abstract double int super assert else interface switch boolean enum long synchronized break extends native this byte for new throw case final package throws catch finally private transient char float protected try class goto public var const if return void continue implements short volatile default import static while do instanceof strictfg Fall 2018 CISC124 - Prof. McLeod

33 The “Other” Keywords (from Oracle)
const and goto are “not used by current versions of the Java programming language.” native “is used in method declarations to specify that the method is not implemented in the same Java source file, but rather in another language.” synchronized “when applied to a method or code block, guarantees that at most one thread at a time executes that code.” transient “indicates that a field is not part of the serialized form of an object.” volatile “specifies that the variable is modified asynchronously by concurrently running threads.” Fall 2018 CISC124 - Prof. McLeod

34 Method Invocations Three aspects to consider: Naming the method.
Fall 2018 CISC124 Method Invocations Three aspects to consider: Naming the method. Providing arguments(s) or not. Doing something with the return value or not. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

35 Method Invocations – 1. Naming the Method
If the method is in the same class, then just use the name of the method. (The compiler assumes the existence of the object reference to the current object as this. Or it assumes the name of the current object, when static) If the method is not in the same class you must first identify the object owning the method and obtain the method using the dot operator. If the method is declared as static, then you can invoke the method without instantiating the class that contains the method. Fall 2018 CISC124 - Prof. McLeod

36 Method Invocations – 2. Providing Arguments for the Parameters
If the method has been declared to accept arguments, ie. it has a non-empty parameter list, then you must supply a matching list of arguments. If the method does not have any parameters, then you must still use empty brackets, ( ), when you invoke the method. Fall 2018 CISC124 - Prof. McLeod

37 Method Invocations – 3. Using a Return Value
A non-void method will return something. You can use that “something” in an expression, or just store it in a variable – your choice. The method has declared the type of that “something”. If the method was declared as void, you will not get a return value and you can only invoke the method by itself, not as part of an expression or an assignment statement. Fall 2018 CISC124 - Prof. McLeod

38 Method Invocations - Examples
See the MethodInvocations.java program. Fall 2018 CISC124 - Prof. McLeod


Download ppt "CISC124 TA names and s have been added to the course web site."

Similar presentations


Ads by Google