Categories
Foundations of Programming

FIT9017: Week 3

Judy Sheard’s third week lecture continued on the introduction to Java and programming as a whole. Although all of the concepts covered were very basic some of the terminology needs to be learned, particularly for when in the work place and the need to communicate arises. So, a summary of the terms covered and their contextual definitions:

  • Selection – if construct, 3 components: condition,consequent, alternative. if(keyword) (condition){consequent}else{alt}
  • relational operator –  ==, !=, <, >, <=, =>  result of these operators is a boolean TRUE/FALSE
  • boolean expression –  using one of the above operators.
  • compound statement – multiple lines in consequent block.
  • nested if – if within and if construct.
  • logical operators – !, && , || NOT, AND, OR can be used in conjunction with boolean expressions.
  • switch statement – can be used to neaten cascading if statements (compiles as cascading ifs however) ideal for day of the week statements etc. Can only be used with int and char expression evaluations.
  • shorthand arithmetic operators – a += 1; a -=1; a *=2; a /=3; a %=5; //reassign a with the result of the operators. a++; ++a; b–; –b; // if ++, — is after the variable then the arithmetic will be applied after the entire statement.
  • local variables –  variables within methods which exist only as long as the method is being executed.
  • base input –  using scanner method to read from keyboard

Precedence rule for evaluating expressions (JAVA):

  1. Parenthesis
  2. Unary Operators (++, –, !)
  3. Binary Arithmetic Operators (*, /, %, +, -)
  4. Relational Operators (<, >, <=, =>, ==, !=)
  5. Logical Operators ( &&, ||)
  6. Shorthand arithmetic ( =, +=, -=, *=, /=, %=) // these ones are evaluated right to left.

When in doubt of any of the above its much easier just to use parenthesis…

Pearl of the week: (I was always under the impression that a leap year is simple every 4th year, this is not the case however)

Algorithm for leap years:

if (year modulo 4 is 0) and (year modulo 100 is not 0) or (year modulo 400 is 0)
       then is_leap_year
else
       not_leap_year

Hope all banking systems have this correctly coded otherwise the 2100 bug might turn out to be more exciting than y2k.

The tut again was simply a revision of the lecture with the chance to ask question and get explanations. We are getting the assignment outline this week, Judy has said it is not really challenging…

if (year modulo 4 is 0) and (year modulo 100 is not 0) or (year modulo 400 is 0)
       then is_leap_year
else
       not_leap_year

Leave a Reply

Your email address will not be published. Required fields are marked *