Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110: Spring 20091 Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.

Similar presentations


Presentation on theme: "COMP 110: Spring 20091 Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today."— Presentation transcript:

1 COMP 110: Spring 20091 Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today

2 COMP 110: Spring 20092 Questions? Last week’s lecture: Declarations Variables Assignments Operators Pseudocode

3 COMP 110: Spring 20093 Today in COMP 110 Type Casting More Operators Strings Console I/O

4 COMP 110: Spring 20094 From Last Time Type Name Kind of Value Memory Used Range of Values byteInteger1 byte-128 to 127 shortInteger2 bytes-32,768 to 32,768 intInteger4 bytes-2,147,483,648 to 2,147,483,647 longInteger8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 floatFloating-point4 bytes±3.40282347 x 10 +38 to ±1.40239846 x 10 -45 doubleFloating-point8 bytes±1.79769313486231570 x 10 308 to ±4.94065645841246544 x 10 -324 charCharacter2 bytes0 to 65,535 boolean 1 bitTrue or False (0 to 1)

5 COMP 110: Spring 20095 Assignment Compatibilities You cannot store a value of one type in a variable of another type int i = 0.1; //this does not compile! However, some types can be converted into other types double d = 1; //1 is automatically converted to 1.0

6 COMP 110: Spring 20096 Assignment Compatibilities byte » short » int » long » float » double A value of any type in the list can be assigned to a type further down the list Example int iVar = 7; double dVar = iVar; //this is legal

7 COMP 110: Spring 20097 Assignment Compatibilities In some cases, you may want to override the assignment compatibilities This can be done using type casting

8 COMP 110: Spring 20098 Type Casting Syntax (Type_Name) Expression Example double distance = 9.0; int points = (int)distance; //cast distance to an int //distance is not changed in any way //the value of points will be 9

9 COMP 110: Spring 20099 Type Casting Type casting is not rounding! The effect of a type cast is truncation Any fractional portion is discarded Example double bill = 25.75; int dollars = (int)bill ; //cast bill to an int //bill is not changed in any way //the value of dollars will be 25

10 COMP 110: Spring 200910 From Last Time Operators Addition (+) a + b Subtraction (-) a - b Multiplication (*) a * b Division (/) a / b Remainder or Modulo (%) a % b

11 COMP 110: Spring 200911 Parentheses and Precedence Parentheses can be used to indicate the order in which operations should be performed Example What is the value of “result”? int a = 3, b = 1, c = 2, result = 0; result = (a + b) * c; result = a + (b * c); result = 8 result = 5

12 COMP 110: Spring 200912 Binary Operators Binary operators take two operands An operand is just a number or variable For example, +,-,*,/ are binary operators i = a + b; i = a - b; i = a * b; i = a / b;

13 COMP 110: Spring 200913 Unary Operators Unary operators take a single operand + and – can also be unary operators i = -7; f = +100.2;

14 COMP 110: Spring 200914 Specialized Assignment Operators += a += 3; //same as a = a + 3; -= a -= 4; //same as a = a – 4; *= a *= 2; //same as a = a * 2; /= a /= 3; //same as a = a / 3;

15 COMP 110: Spring 200915 Increment and Decrement Operators Increment Operator (++) count++; //same as count = count + 1; Decrement Operator (--) count--; //same as count = count – 1;

16 COMP 110: Spring 200916 Increment and Decrement Operators Can also be used in expressions in either prefix (++m) or postfix (m++) form Example n * (m++) Increases m by 1 after the multiplication n * (++m) Increases m by 1 before the multiplication

17 COMP 110: Spring 200917 Increment and Decrement Operators int n = 2; int m = 4; int result = n * (m++); //produces result = 8, m = 5 result = n * (++m); //produces result = 12, m = 6

18 COMP 110: Spring 200918 Operator Precedence Java evaluates expressions in an order specified by precedence rules Certain operations are performed before others Example i = 4 + 2 * 3 – 1; // the value of i will be 9

19 COMP 110: Spring 200919 Java Precedence Rules Highest Precedence First: the unary operators +, -, ++, -- Second: the binary operators *, /, % Third: the binary operators +, - Lowest Precedence Binary operators of equal precedence are evaluated from left to right

20 COMP 110: Spring 200920 Precedence Examples -3 * 7 % 3 – 4 – 6 -21 % 3 – 4 – 6 0 – 4 – 6 -10 3 * (3 % 2 ) / -2 3 * 1 / -2 3 / -2 NOTE: Truncation of division operator on integers

21 COMP 110: Spring 200921 Constants Constants or literals are values that don’t change 2, 6, ‘B’, 5.433e8 NOTE: 5.433e8 means 5.433 x 10 8 Example uses of constants int i = 2; // i is a variable, 2 is a constant char l = ‘J’; // l is a variable, ‘J’ is a constant double d = 6.1e38; //d is a variable, 6.1e38 is a //constant

22 COMP 110: Spring 200922 Named Constants A constant that is given a name and can be used much like a variable Syntax final Type Variable = Constant; Examples final double PI = 3.14159; final int HOURS_PER_DAY = 24;

23 COMP 110: Spring 200923 Strings How to represent sequences of characters in Java programs such as “Enter a whole number from 1 to 99.” Java provides the class “String” for this purpose

24 COMP 110: Spring 200924 Strings Messages like these are called String literals or String constants “Enter a whole number from 1 to 99.” “What is your name?” “I will add two numbers for you.” They have the type “String”

25 COMP 110: Spring 200925 Strings We can declare Strings in Java programs Example String greeting; greeting = “Hello!”; System.out.println(greeting); //prints Hello! to screen System.out.println(“Hello!”); //also prints Hello! to //screen

26 COMP 110: Spring 200926 String Indices Strings consist of a sequence of characters Each character has a position in the String UNCisGreat 01234567891011 Positions Characters

27 COMP 110: Spring 200927 String Concatenation We can concatenate two Strings together using the (+) operator Example String name = “Bob”; String greeting = “Hi ” + name; System.out.println(greeting); //prints “Hi Bob” to the screen

28 COMP 110: Spring 200928 String Concatenation We can also concatenate Strings with other data types System.out.println("I am " + 21 + " years old!"); //prints “I am 21 years old!” to screen

29 COMP 110: Spring 200929 Strings as a Class String is a class type, not a primitive type Class types have both data and methods (actions)

30 COMP 110: Spring 200930 String Length The “length()” method of the String class returns the # of characters in the String String myString = “Hi there!”; int len = myString.length(); Object 9 Method Class

31 COMP 110: Spring 200931 Strings All objects of the same type have the same methods, but may have different data Example String s1 = “First!”; String s2 = “Second!”; s1.length() returns 6 s2.length() returns 7

32 COMP 110: Spring 200932 String Methods string.indexOf(A_String) Returns the index of the first occurrence of A_String in string Example String phrase = “UNC is Great”; int ind = phrase.indexOf(“Great”); UNCisGreat 01234567891011

33 COMP 110: Spring 200933 String Methods string.substring(Start) Returns a new string having the same characters as the substring of string beginning at index Start through the end of string Example String phrase = “UNC is Great”; String sub = phrase.substring(2); UNCisGreat 01234567891011

34 COMP 110: Spring 200934 String Methods Other methods of the String class include string.charAt(index) Returns the character at index in string string.toLowerCase() Returns a new String with the same characters as string, but with any uppercase characters changed to lowercase string.equals(A_String) Returns true if string and A_String are the same, otherwise returns false See http://java.sun.com/javase/6/docs/api/java/lang/S tring.html

35 COMP 110: Spring 200935 Quotations Marks in Strings Problem Quotation marks in Java indicate the beginning and end of a String literal String phrase = “Enter a whole number from 1 to 99.”; How to put quotation marks into Strings?

36 COMP 110: Spring 200936 Quotation Marks in Strings Example We’d like to print the message This class is so "fun" But we cannot use System.out.println("This class is so "fun""); because the Java compiler sees it as System.out.println("This class is so "fun"");

37 COMP 110: Spring 200937 Quotation Marks in Strings The way to do this in Java is with the escape character '\' We can use the sequence \" inside a string to indicate a quotation mark Example System.out.println("This class is so \"fun\""); will print to screen This class is so "fun"

38 COMP 110: Spring 200938 Backslashes in Strings If backslash '\' is the escape character, how can I put backslashes into Strings? Use the sequence \\ for this Example System.out.println("This is a backslash \\"); will print to screen This is a backslash \

39 COMP 110: Spring 200939 New Line Sequence The sequence \n starts a new line of text Example System.out.println("Line 1\nLine 2"); will print to screen Line 1 Line 2

40 COMP 110: Spring 200940 Escape Characters \"\" Double quote \'\' Single quote \\Backslash \nNew line \rCarriage return \tTab

41 COMP 110: Spring 200941 String Exercise What is the output? System.out.println("abc\ndef"); abc def System.out.println("abc\\ndef"); abc\ndef

42 COMP 110: Spring 200942 Programming Demo Fahrenheit to Celsius Converter Problem Given a temperature in degrees Fahrenheit, convert to a temperature in degree Celsius

43 COMP 110: Spring 200943 Programming Demo Fahrenheit to Celsius Steps Pseudocode Programming Testing/Debugging

44 COMP 110: Spring 200944 The Algorithm Comes directly from the formula for conversion tempC = (5/9)(tempF – 32)

45 COMP 110: Spring 200945 Fahrenheit to Celsius Pseudocode Ask user for temp in Fahrenheit Convert temp in Fahrenheit to temp in Celsius Output the result


Download ppt "COMP 110: Spring 20091 Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today."

Similar presentations


Ads by Google