Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 200 Primitives and Expressions

Similar presentations


Presentation on theme: "CS 200 Primitives and Expressions"— Presentation transcript:

1 CS 200 Primitives and Expressions
Jim Williams, PhD

2 This Week Chap 1 Programs - 3 parts (P1)
Due Thursday Team Lab 1: Tuesday or Wednesday Piazza: Don't Post Code Unless you post only to "Instructors" which makes it private. Lecture: Primitive Data Types, Expressions and Methods

3 Team Labs First meeting this week. 1350 cs and 1370 cs Meet Assistants
1st floor, around corner from elevators Meet Assistants Work in pairs and small groups Discuss Terms, Trace & Explain code Edit-Compile-Run Cycle on the command-line

4 Pair Programming 2 people working together on 1 computer.
One person types, the other provides direction and reviews. Many report more confidence in solution and more enjoyment programming. Important to switch roles (who has the keyboard). Provide respectful, honest and friendly feedback.

5 Names for Numbers of Bits
Memory Names for Numbers of Bits bit nibble (4 bits) byte (8 bits) 2 bytes (16)

6 Infinite Numbers How do we represent and do calculations on a finite machine?

7 Integer Data Types -192, 0, 42, 2000000000 byte short int long
Bytes in Memory

8 Floating Point Data Types
, 2.0, -5.2, float double Bytes in Memory

9 All 8 Primitive Data Types
char single character (2 bytes) boolean true or false value (>= 1 bit) Integer byte, short, int, long Floating Point float, double

10 Primitive Data Types How many can you list?

11 Literals Data literally typed into a program. 2 2.3 12l 12L 2.4F true
double long float boolean char String

12 Magic Numbers (bad practice)
Numbers with unexplained meaning: public class H { public static void main(String []args) { double s = 71 / ; }

13 Variable Names Useful variable names can help with code readability.
public class H { public static void main(String []args) { double height = 71; height = height / ; }

14 Constants Constants (final variables) can help with readability.
public class Height { public static void main(String []args) { final double INCHES_IN_METER = ; double heightInInches = 71; double heightInMeters = heightInInches / INCHES_IN_METER; }

15 Primitive Data Types byte short int long float double char
boolean 1 bit or more Bytes in Memory

16 Widening Primitive Conversion
Narrower to Wider Keeps magnitude may lose some precision byte -> short -> int -> long -> float -> double float f = 23; //implicit conversion double d = f; //implicit conversion int n = (int)43.2; //must explicitly cast

17 Operators If operands have different data types compiler implicitly converts to same data type following widening conversion.

18 Division Meaning depends on data type of operands.
integer divide vs floating point divide 1 / vs / 2.0 integer remainder (modulus operator) 1 % 2

19 Variable Declaration & Assignment
int j; j = 5; int k = 4; j 5 k 4 a variable is a name for an area of memory = is "assignment" operator, Not equals (==) value on right copied into variable on left "initialization" is assigning the first value

20 What are the values in a, b & c?
int a = 5; int b = 7; int c = a; b = c; a = b; a: 5 b: 7 c: 5 b: 5 c: 7 try it in Java Visualizer

21 What are the values in a, b & c?
int a = 2; int b = 1; int c = a + b; a = b; a = c; a: 2 b: 1 c: 3 a: 3 c: 2 try it in Java Visualizer

22 Operator Precedence & Associativity

23 What should the results be?
double result1 = 4 / 8 * 2.0; double result2 = 2.0 * 4 / 8; double result3 = 2.0 * (4 / 8);

24 What are the values of d & e?
int d = 4; int e = d / (d = 2);

25 What should the result be?
"3"

26 What are the values of a, b & c?
int a = 1; int b = 2; int c = a = b = 3;

27 What are the values b & c? int b = 1; int c; c = b + (b = 2 + b);

28 Resource Precedence, Associativity & Order of Evaluation

29 Application: Temperature Conversion
(Degrees Fahrenheit – 32) x 5 / 9 = Degrees Celsius What symbols have different meanings in Java? What changes must be made to implement this equation in Java? public class TemperatureConversion { public static void main(String[] args) { //testFahrenheitToCelsius(); //comment out when tests are all passing //method call; the result is a double value double result = fahrenheitToCelsius( 212.0); //212.0 is the argument } //test method with a set of test cases. Run to test initially or //after changing the method to verify the method is still working //correctly. If a bug is found in the method being tested, then //create a different test case to demostrate the bug. Fix your code //then run all the tests to make sure all work correctly. public static void testFahrenheitToCelsius() { System.out.println( fahrenheitToCelsius( 212.0) + " expect 100.0"); System.out.println( fahrenheitToCelsius( 0.0) + " expect "); System.out.println( fahrenheitToCelsius( 98.6) + " expect 37"); System.out.println( fahrenheitToCelsius( -20) + " expect "); //method definition //for most of this course, methods you create will be public static //return type method name (parameter list) public static double fahrenheitToCelsius(double degreesFahrenheit) { //the parameter degreesFahrenheit (a variable) is initialized to //the argument passed when the method is called. //the equation we are converting to Java //(Degrees Fahrenheit – 32) x 5 / 9 = Degrees Celsius double degreesCelsius; degreesCelsius = (degreesFahrenheit ) * (5.0 / 9); return degreesCelsius; //the value return as a result of the method

30 My List X vs * equals (==) vs assignment (=)
value is stored on the left hand side of assignment (=) operator Variables: name areas of computer memory, declare before use, declare type of data, initialize Variable names: start with letter, include letters numbers and _, but no spaces Conventions: camelCasing, spell out names Semicolon at the end of statements

31 Java Visualizer Implement Equation Create and call a method

32 Calling Class (static) Methods
double numInts = Math.pow( 2, 32); double root = Math.sqrt( 16); int num1 = 16; int num2 = 3; double result; result = num2 + Math.sqrt( num1); API: Course website or search for: Java 8 Math

33 CS Core Principles: Algorithms: A step-by-step set of operations to be performed. Analogy: Recipe, Instructions Abstraction: a technique for managing complexity. Analogy: Automobile, CS200 Computer View

34 Methods A named section of code that can be "called" from other code.
Lots of existing methods that you can use rather than writing yourself. To use, "call the method". The method executes (runs) and may return a value.

35 Which is area method Call?
top bottom static int area(int length, int width) { System.out.println(length + "," + width); return length * width; } public static void main(String []args) { int len = 4; int result = area( len, 5); B, bottom is the correct answer

36 Returning a Value from a Method
static int triple(int num) { return num * 3; } public static void main(String []args) { int value = 5; int result = triple( value);

37 Is count: Argument or Parameter?
public static void main(String []args) { int num = 10; printCount( 23); printCount( num+3); } static void printCount(int count) { System.out.println("count:" + count); argument parameter count is a parameter (zyBooks) or formal parameter the number 23, for example is an argument (also called an actual parameter).

38 What is the value of area method?
static int area(int length, int width) { System.out.println(length + "," + width); return length * width; } public static void main(String []args) { int len = 4; int result = area( len, 5); printed output? return value B, return value, is the correct answer A method may do other things, such as print out, call other methods, change memory (later) but the value of the method is what is returned when it is called.

39 What prints out? static void calc(int num) { num = 3; }
5 35 error static void calc(int num) { num = 3; } public static void main(String []args) { int n = 5; calc( n); System.out.println( n); try it.

40 Which is called first: calc or println?
error static int calc(int num) { num -= 33; return num; } public static void main(String []args) { int n = 55; System.out.println( calc( n)); put a print statement within the calc method to see if it is called before the println method.

41 Testing Methods Methods written to run test cases to help validate and debug your code.

42 Programming Process Study the Problem Design an Algorithm (pseudocode)
Write Java Compile the Code Run the code Repeat

43 Problem Obtain 3 numbers from the user.
IF the 3 numbers could be lengths of the sides of a triangle, output "COULD be lengths of sides of a triangle" OTHERWISE output "Cannot be lengths of sides of a triangle" import java.util.Scanner; public class ClassNameHere { public static void main(String[] args) { Scanner input = new Scanner(System.in); double one = input.nextDouble(); double two = input.nextDouble(); double three = input.nextDouble(); if ( one + two > three && one + three > two && two + three > one) { System.out.println("COULD be the lengths of the sides of a triangle."); } else { System.out.println("Cannot be the lengths of the sides of a triangle."); } Reference: Frank Ackerman

44 Study the Problem What are the goals of the problem?
What are the inputs, outputs, relationship? Can you solve a small example by hand? Can you describe the algorithm in words (pseudocode)?


Download ppt "CS 200 Primitives and Expressions"

Similar presentations


Ads by Google