Download presentation

Presentation is loading. Please wait.

1
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”

2
Aalborg Media Lab 21-Jun-15 Lecture Overview Chapter 2 of “Java Software Solutions” Slides and exercises can be found on: http://www.media.aau.dk/sd http://www.media.aau.dk/sd

3
Aalborg Media Lab 21-Jun-15 Character Strings Every character string is an object in Java, defined by the String class: “Hello World” Every string literal, delimited by double quotation marks, represents a String object The string concatenation operator (+) is used to append one string to the end of another It can also be used to append a number to a string

4
Aalborg Media Lab 21-Jun-15 String concatenation The plus operator + is also used for arithmetic addition If both operands are strings, or if one is a string and one is a number, it performs string concatenation The + operator is evaluated left to right Parentheses can be used to force the operation order

5
Aalborg Media Lab 21-Jun-15 Variables and Assignment A variable is a name for a location in memory A variable must be declared by specifying the variable's name and the type of information that it will hold Multiple variables can be created in one declaration int total; int result, sum;

6
Aalborg Media Lab 21-Jun-15 Variables A variable can be given an initial value in the declaration int total = 0; int base = 32, max = 149; When a variable is referenced in a program, its current value is used.

7
Aalborg Media Lab 21-Jun-15 An assignment statement changes the value of a variable The assignment operator is the = sign The expression on the right is evaluated and the result is stored in the variable on the left You can assign only a value to a variable that is consistent with the variable's declared type Assignments total = 55;

8
Aalborg Media Lab 21-Jun-15 Constants A constant is an identifier that is similar to a variable except that it holds one value while the program is active The compiler will issue an error if you try to change the value of a constant during execution In Java, we use the final modifier to declare a constant final int MIN_HEIGHT = 69 ;

9
Aalborg Media Lab 21-Jun-15 Primitive Data Types There are exactly eight primitive data types in Java Four of them represent integers: –byte, short, int, long Two of them represent floating point numbers: –float, double One of them represents characters: –char And one of them represents boolean values: –boolean

10
Aalborg Media Lab 21-Jun-15 Characters A char variable stores a single character from the Unicode character set The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters It is an international character set, containing symbols and characters from many world languages Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n'

11
Aalborg Media Lab 21-Jun-15 Boolean A boolean value represents a true or false condition A boolean also can be used to represent any two states, such as a light bulb being on or off The reserved words true and false are the only valid values for a boolean type boolean done = false;

12
Aalborg Media Lab 21-Jun-15 Arithmetic Expressions An expression is a combination of one or more operands and their operators Arithmetic expressions compute numeric results and make use of the arithmetic operators If either or both operands associated with an arithmetic operator are floating point, the result is a floating point Addition + Subtraction - Multiplication * Division / Remainder % (modulus)

13
Aalborg Media Lab 21-Jun-15 Division and Remainder If both operands to the division operator ( / ) are integers, the result is an integer (the fractional part is discarded) The remainder operator (%) returns the remainder after dividing the second operand into the first 14 / 3 equals4 8 / 12 equals0 14 % 3 equals2 8 % 12 equals8

14
Aalborg Media Lab 21-Jun-15 Operator Precedence Operators have a well-defined precedence which determines the order in which they are evaluated result = total + count / max - offset; Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Arithmetic operators with the same precedence are evaluated from left to right Parentheses can be used to force the evaluation order

15
Aalborg Media Lab 21-Jun-15 Precedence OperatorOperationAssociates 1+-+- unary plus unary minus R to L 2*/%*/% multiplication division remainder/modulus L to R 3+-++-+ addition subtraction string concatenation L to R 4=assignmentR to L

16
Aalborg Media Lab 21-Jun-15 Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e 1432 a + b * c - d / e 3241 a / (b + c) - d % e 2341 a / (b * (c + (d - e))) 4123

17
Aalborg Media Lab 21-Jun-15 Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators –First the expression on the right hand side of the = operator is evaluated –Then the result is stored in the variable on the left hand side answer = sum / 4 + MAX * lowest; 1432

18
Aalborg Media Lab 21-Jun-1518 Increment and Decrement The increment and decrement operators are arithmetic and operate on one operand The increment operator ( ++ ) adds one to its operand The decrement operator ( -- ) subtracts one from its operand count++; equivalent count = count + 1;

19
Aalborg Media Lab 21-Jun-1519 Increment and Decrement The increment and decrement operators can be applied in prefix form (before the operand) or postfix form (after the operand) Equivalent if one statement count++; = ++count; When used in a larger expression form matters (total =15). –Total = count-- new value(14) –Total = --count old value(15)

20
Aalborg Media Lab 21-Jun-1520 Assignment Operators Java provides assignment operators to combine increment/ decrement op. with the assignment op. num += count; equivalent num = num + count; The entire right-hand expression is evaluated first, then the result is combined with the original variable += on string equal to String Concatenation

21
Aalborg Media Lab 21-Jun-15 Information may get lost by Information may be lost through conversion through narrowing conversions (double int) double timer = 2.3; int countdown = timer Data Conversion timer = timer + 0.5;

22
Aalborg Media Lab 21-Jun-15 Data Conversion Techniques Assignment –Compiler will warn but still compile! double timer = 2.3; int countdown = timer; Casting –Treat a data type as a different double timer = 2.3; int countdown = (int)timer;

23
Aalborg Media Lab 21-Jun-15 Data Conversion Techniques Promotion, automatically conversion when operators need certain data types result = sum / count double int

24
Aalborg Media Lab 21-Jun-15 Interactive Programs Use of the Scanner class (new Java 1.5!) –Instantiate an object of the Scanner Class ( Scanner.create(“source”) ) static method! –read input from keyboard, file, string –token/line at a time

25
Aalborg Media Lab 21-Jun-15 Echo Application import java.util.Scanner; public class Echo { public static void main(String args[]) { String message; Scanner scan = Scanner.create(System.in); System.out.println("Enter a line of text:"); message = scan.nextLine(); System.out.println("You entered \"" + message + "\""); }

26
Aalborg Media Lab 21-Jun-15 Graphics A black and white picture can be stored using one bit per pixel (0 = white and 1 = black) In Java, each color is represented by three numbers between 0 and 255 that collectively are called an RGB value, (use in Color object) Position of each pixel can be identified using a two-dimensional coordinate system

27
Aalborg Media Lab 21-Jun-15 Coordinate Systems Referring to a pixel in a coordinate system is used with the origin in the top-left corner Y X(0, 0) (112, 40) 112 40

28
Aalborg Media Lab 21-Jun-15 Applets Stand-alone program have a main method, applets don’t. An applet also can be executed using the applet viewer or the web browser The paint method, is executed automatically and is used to draw the applet’s contents (accepting Graphics object ) A Graphics object defines a graphics context on which we can draw shapes and text

29
Aalborg Media Lab 21-Jun-15 Drawing Shapes Graphics class used to draw shapes, Shapes with curves, like an oval, are usually drawn by specifying the shape’s bounding rectangle An arc can be thought of as a section of an oval Every drawing surface has a background color Every graphics context has a current foreground color

30
Aalborg Media Lab 21-Jun-15 Drawing a Line X Y 10 20 150 45 page.drawLine (10, 20, 150, 45);

31
Aalborg Media Lab 21-Jun-15 Drawing a Rectangle page.drawRect (50, 20, 100, 40); X Y 50 20 100 40

32
Aalborg Media Lab 21-Jun-15 Drawing an Oval page.drawOval (175, 20, 50, 80); 175 20 50 80 X Y

33
Aalborg Media Lab 21-Jun-15 Exercises 2.3, 2.5, 2.9, 2.11, 2.13, (2.15 creative!!)

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google