Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Last lecture Lecture 3 Looked at refinement – converting specification to code Seminars Added input statements Seminar 2s begin today at 5pm integer variables (int) were used to store values These will take place in the rooms indicated on the timetable we looked at declaration and assignment You should complete problem sheet 2 studied the capacity (range) of these variables Hand in your solutions beforehand if you want feedback other number types including byte, short and long Added a calculation part Post the solutions in the CS118 letterbox at the back of 006 Make sure they include your name and your seminar tutors name that needed some comparison operators (==, !=, <) that repeated code using the Java while construct Added an output part Coursework 1 Start reading the material and looking at the exercises More info at the end of this lecture CS118: Programming for Computer Scientists need to strengthen the precondition (e.g. a and b > 0) CS118: Programming for Computer Scientists 2 3 Numeric type conversion Java’s primitive number types We have seen four primitive number types using Java library functions (e.g. System.out.print) Discussed the importance of pre- and post-conditions Primitive datatypes 5&6 Java is strict with the types of different variables int i; We need two more to represent floating point numbers Type float Size 4 bytes double 8 bytes Range -3.4e38 to 3.4e38 6 to 7 significant digits of accuracy -1.7e308 to 1.7e308 14 to 15 significant digits of accuracy Numeric operators include: i = 10.3 * x; illegal - i is less accurate type However, Java automatically converts some types x = i + 10; legal - x is more accurate type This is called Automatic Type Conversion 2/4=0 2.0 / 4.0 = 0.5 4%2=0 +, -, *, /, % CS118: Programming for Computer Scientists Type conversion allows smaller formats to be converted to larger formats (with increased level of accuracy) byte short int long float double CS118: Programming for Computer Scientists 5 Type casting 6 Introducing features of Java Java provides the programmer with a means of converting a variable of one type to another double x = 3.1; When introducing new language features, we will consider the Syntax this is called Type Casting rules governing what the layout in Java looks like i.e. the grammatical arrangement of the language Difference between Type Casting and Type Conversion: type conversion (automatic - Java driven) type casting (manual - programmer driven) the Semantics Type casting allows larger formats to be converted to smaller formats (with possible loss of accuracy) byte short int long float double CS118: Programming for Computer Scientists 7 rules governing what the Java means i.e. the relationship between the symbols and what they represent Compilation & execution the Java compiler is good at spotting syntax errors you will only discover semantic errors by running the code CS118: Programming for Computer Scientists 8 Type casting (again) Type casting cont… Example Type casting shortcuts double d = 10.125; float f = (float) 10.1; int i = 0; float f = 10.1f; i = (int) d; // equivalent statements Long (trailing l or L); Float (trailing f or F); Double (trailing d or D) etc. results in i = 10 Syntax Type casting can of course create errors :-( (targetType) variableName/Value int i; Semantics // ...some assignment and calculation involving i The variable or value (to the right of the cast) has its type narrowed to the given target type; there may be a loss in accuracy. Default Java type for fractional numbers is double; for whole numbers it is int CS118: Programming for Computer Scientists This will work if -128 <= i <= 127, otherwise you may get a runtime error! Or even worse … it may fail quietly! CS118: Programming for Computer Scientists 9 10 Variable assignment Variable declaration Syntax: dataType variableName; Example: int x; // x is declared as an integer variable = value; variable = expression; Example: x = 24; // assign a value to x Semantics: Store the value or the result of the expression in the variable location named variable Use of variables distinguishes imperative programming languages (e.g. C, Java, Pascal, VB etc.) from declarative languages (e.g. purely functional languages like Haskell) Expressions that denote locations (e.g. x) are known as lvalues; expressions that denote values (e.g. 5) are known as r-values. Interestingly x is both an l-value and an r-value Languages that use variables are known as state-based Operations on variables are known as side effects s1 s1 The declaration of a variable x creates a program state CS118: Programming for Computer Scientists Syntax: x = (x * 34) / y; Semantics: Create a variable container of type dataType whose name is variableName x byte b = (byte) i; x=0 x = x + 1; s2 x=1 print(x); CS118: Programming for Computer Scientists 12 Variable declaration and assignment Dog’s dinner Syntax: dataType variable = value; dataType variable = expression; Because Java is based on the C programming language, a number of programming short cuts exist Example: int x = 24; // declare and assign value to x Semantics: Create a variable container of type dataType whose name is variable, then store the value or the result of the expression in that variable int x; x = x + 1; System.out.println(x); i = i + 2; same as i += 2; i = i - 1; i -= 1; same as same as 18 i- -; While these might seem like a good idea they are the cause of common mistakes And they do not improve the readability of your program: what values do x and y have? int x = 5; int y = ++x; CS118: Programming for Computer Scientists x=1 i =+ 2; This is useful as declaration on its own does not guarantee an initial value int x; s3 20 x = 7; y=x++; CS118: Programming for Computer Scientists 22 Primitive datatype #7 Boolean data type Boolean data types cont... Comes from Boolean Algebra (true, false) Boolean operators (have boolean operands) boolean lightsOn = true; Comparison operators (return boolean values) Operator Name Example Answer < less than 1<2 true <= less than or equal 1 <= 2 true > greater than 1>2 false >= greater than or equal 1 >= 2 false == equal 1 == 2 false != not equal 1 != 2 true Operator Name Example ! not (negation) !true Answer && and (conjunction) true && true || or (disjunction) true || false true ^ exclusive or false ^ false false false true (exclusion) Note that ! is a unary operator, it only requires one operand Note these are binary operators, i.e. they take two operands CS118: Programming for Computer Scientists CS118: Programming for Computer Scientists 23 Strict and lazy operators Character data type Java (and C) have strict (e.g. &) and lazy (e.g. &&) versions of the Boolean operators a b a&b a F F F F b F F T F F F T F F T F F T T T T T T 24 Primitive datatype #8 The char data type is used to represent a single character. A character is enclosed in single ‘ ’s a && b char letter = ‘a’; char number = ‘4’; For multiple characters, or a string, we use the String class. A string is enclosed in “ ”s String message = “this is a string”; Need to be careful which operator you use String is not one of Java’s 8 basic data types - strings are objects not primitive values If x=0, for example, then what happens here? (x == 1) && ((100/x) != 2) (x > -2) | (x++ < 2) CS118: Programming for Computer Scientists CS118: Programming for Computer Scientists 26 Operator precedence RobotRobot-maze Determines the order in which expressions are evaluated Two pieces of coursework 3 + 4 * 4 > 5 * (4 + 3) - 1++ Wednesday 26 October (week 5) Friday 2 December (week 10) 27 Expressions in parentheses evaluated first Precedence Operator Highest ++, -*, /, % +, <, <=, >, => ==, != && || Lowest Both are based on a robot-maze problem You have a robot, a target and a maze The aim is to control the robot so that it reaches the target You are required to design the robot-controller code =, +=, -=, *=, /=, %= 18 exercises for coursework 1 8 exercises for coursework 2 Parentheses tend to improve readability CS118: Programming for Computer Scientists 28 CS118: Programming for Computer Scientists 29 RobotRobot-maze environment Controlling the robot Where do you want the robot to move? Getting started Select maze Select robot New Maze 1. Draw maze 2. Draw robot 3. Enact move Robot Direction Start position Speed Maze Controller Target 1. Detect walls etc 2. Point robot 3. Signal move Moveable Maze I want the robot to move left… Sizeable Editable This control loop is carried out once for each move CS118: Programming for Computer Scientists CS118: Programming for Computer Scientists 30 Robot controller code 39 Dumbo controller code import uk.ac.warwick.dcs.maze.logic.IRobot; Stored in a small Java file Library methods public class DumboController { public void controlRobot(IRobot robot) { int randno; int direction; The robot interacts with the maze through a set of library methods Control method // Select a random number (0..3 inclusive) robot.look(direction) randno = (int) Math.round(Math.random()*3); robot.face(direction) Where direction can be AHEAD, BEHIND, LEFT and RIGHT robot.look returns PASSAGE, WALL or BEENBEFORE The library method robot.advance() instructs the robot to move in the specified direction // Convert this to a direction Selects a random number (0..3) if (randno == 0) direction = IRobot.LEFT; else if (randno == 1) direction = IRobot.RIGHT; else if (randno == 2) direction = IRobot.BEHIND; else direction = IRobot.AHEAD; Convert to a direction robot.face(direction); robot.advance(); // face // move Face robot in that direction and move } } CS118: Programming for Computer Scientists CS118: Programming for Computer Scientists 40 Coursework 1 What should you do next? Your job is to modify this controller code Read The Guide and lecture notes Part 1 Source a book Stop the robot crashing into walls Print out log information Smarten the way in which directions are chosen Download the robot-maze Read up on control statements Part 2 Design a homing robot Sense the target, aim robot towards target 46 if else while do-while Get started! CS118: Programming for Computer Scientists 47 CS118: Programming for Computer Scientists 48