Introductory Programming In Java Comp6700/Comp2140 Week 2, 2016 Henry Gardner/ Alexei Khorev/ Charles Martin Please be aware that this course moves fast. Some of you may need to spend more time on lab and homework exercises than others. Some of you may need to read widely (web, textbooks, etc) to catch up. We provide you with a lot of material as a starting point. Some in lectures More in lecture notes More in lecturettes etc Remember that Introductory Programming in Java is a “messy” subject and that core concepts are interlinked. We will continue to reinforce core concepts as the course proceeds. How are you coping……? 2 From 2015, ANU introduced what computer science interprets as a “no extensions” policy for work submitted for assessment during the semester: Extensions are only to be granted for reasons such as illness or personal/family difficulties. Reasons need to be documented and a formal application needs to be made. Computer Science cannot allow late submissions with a marks penalty. Important note on assessment 3 Some CS courses allow all continuous marks to be redeemed on the final examination. We propose that COMP6700/COMP2140 follow this pattern This will mean that if you do not submit homeworks, or the assignments, or the mid-semester exam, then your final exam marks will be normalised to cover these unsubmitted items. To be fair, if students have submitted items and their scaled final exam mark is higher than the marks that they received for those items then they should receive the final mark. But we can allow marks to be redeemed 4 Presently: Final mark = HW (10) + max(MSE1, MSE2*0.8) (10) + asst1 (15) + asst2 (15) + Exam (50) We propose Final Mark = max(HW, Ex*0.2) (10) + max(MSE1, MSE2, Ex*0.2) (10) + max(asst1,Ex*0.3) (15) + max(asst2,Ex*0.3) (15) + Exam (50) So deadlines will have no late extensions, but marks can be redeemed. The second mid-semester exam can redeem the first MSE to 100%. Comments? For COMP6700/COMP2140 5 See Alexei’s J2 notes on the “FTA” (free-to-air) website Identify types and literals in the following code: J2 – Primitive data types and type conversion 6 Some common literals: Java “literals” 7 Every variable must be declared by specifying its type. There are eight primitive types: boolean (1-bit, true/false) byte (8-bit signed, range -128 ... +127) char (16-bit unicode, character symbols from Unicode Table) short (16-bit signed, range -32768 ... +32767) int (32-bit signed, range -2,147,483,648 ... +2,147,483,647) long (64-bit signed range -9,223,372,036,854,775,808 ... +9,223,372,036,854,775,807) float (32-bit FP number) double (64-bit FP number) “int” and “double” are the default numerical types Java is strongly typed 8 Strings are sequences of characters (char) You create and manipulate String objects! https://docs.oracle.com/javase/8/docs/api/java/lang/String.html String is not a primitive type 9 Types have different lengths 10 • Java can automatically convert between compatible types. • (Where the type on the left hand side of an assignment statement is larger than the type on the right hand side) • Sometimes java needs to be told to cast one type to another • Syntax: var1 = (type1) var2; 11 12 13 Default behaviour of (int) is to truncate the decimal part. Math.round, Math.ceil, Math.floor can be used to change this behaviour Rounding and finite precision 14 Some numbers can be tricky than they look because of binary representation 15 You should use Math.round in situations like this where a long binary representation could be truncated to the wrong integer. See J2 notes on the char data type to represent single characters “char” literal values have an order, like the sequencing of characters in the alphabet Java uses Unicode character encoding: it can represent any character in any language in the world! Strings are sequences of characters Characters 16 An array is an ordered collection of data elements indexed from 0 (first element) up to length - 1. All elements have the same type. Array declarations and instantiations can be done in different ways. Note that you can use the “new” command to create arrays. “new” creates objects in Java. Java arrays are objects. Arrays 17 18 “A nightmare of epic proportions” – Cay Horstmann Can use “args” array of Strings and convert to whatever primitive data type you require. Can use “Scanner” Can use “JOptionPane” Can read from files, from databases, from web pages, from streams, from twitter, from ……. Getting information into your Java program 19 Scanner 20 Scanner…. 21