pages.cs.wisc.edu

pages.cs.wisc.edu

CS 200 Primitives and Expressions Jim Williams, PhD CS Student Organization Fair Friday, September 13th 1-4PM CS Building Lobby | 1210 W. Dayton St Open to all students and majors Make connections with CS related organizations A great way to meet new people and get new experiences

Questions? [email protected] This Week Chap 1 Programs - 3 parts: Due Thursday Piazza: Don't Post Code Unless posted to "Instructors" which makes private. Team Lab tomorrow Feedback: Talk with TAs during office hours Lecture:

Programming Process (continued) Primitive Data Types and Expressions 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. Team Labs First meeting this week. 1350 and 1370 Computer Sciences a. 1st floor, around corner from elevators Meet Assistants a. Assigned small groups and pairs

Overview of Team Lab Content a. TA Discussion A Java Program public class Hello { public static void main(String[] args) { System.out.println("Welcome"); System.out.print(" to "); System.out.print("CS 200!"); }

} Tracing Code 1. How does the current line affect: a. input? b. memory? c. output? 2. What is the next line to be executed?

Java Visualizer Demo Edit-Compile-Run Cycle In Java Visualizer: Typing code in webpage (Editor) Clicking Visualize Execution (Compiler) Clicking Forward (Virtual Machine) Edit Code

(Return to Editor) Edit-Compile-Run Cycle From the command-line: notepad (Editor) javac (Compiler) java (Virtual Machine)

Demo Command Prompt/Terminal Edit-Compile-Run Cycle Users Edito r (Virtual) Machine

Compiler Hello.class (byte code) Hello.java Computer Programmer

Files Various Errors Naming/Saving Syntax/Compile time Edito r

Runtime & Logic (Virtual) Machine Compiler Hello.class Hello.java

Computer Programmer Files TopHat What is the command-line command to compile a Java program named Cat.java?

TopHat After compiling Cat.java what is the name of the file created? Programming Process What are inputs, outputs and their relationship? Solve a small example by hand. Design an algorithm in pseudocode

e.g., structured English Convert to Java. Edit-Compile-Run Cycle Tips for Solving Programming Problems Problem Estimate the cost to carpet a floor. What are inputs, outputs and their relationship?

Solve a small example by hand. Design an algorithm in pseudocode (e.g. structured English). Convert to Java. TopHat Order the steps in the Programming Process Expensive Software Bug On June 4, 1996 an unmanned Ariane 5 rocket exploded

just forty seconds after its first lift-o. The destroyed rocket . The destroyed rocket and its cargo were valued at $500 million. It turned out that the cause of the failure was a software error. A 64 bit oating point number was converted to a 16 bit signed integer. The number was larger than 32,767, the largest integer storeable in a 16 bit signed integer, and therefore the conversion was not accurate. https://en.wikipedia.org/wiki/Ariane_5#Notable_launches Infinite Numbers

Numbers are infinitely large, small, and precise. How can a computer with finite memory do calculations? 2 systems: integer math (exact) floating point math (approximate) Memory Terms 0000000001000001

bit nibble (4 bits) byte (8 bits) 2 bytes (16) Possible Permutations Number of bits 1 Possible Permutations 0 1

Number of Possible 2 Permutations or 21 2 3 n

00 01 10 11 000 001 010 011

2*2 or 22 2*2*2 or 23 100 101 110 111

2n Integer Data Types Examples: -192, 0, 42, 2000000000 byte short int long Number of Bytes

TopHat Order primitive integer data types by size in bytes. Floating Point Data Types Examples: 3.14159, 2.0, -5.2, 24901.0327483 float double

Number of Bytes 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 Literals Data literally typed into a program. 2 2.3 12l

12L 2.4F int double long long

float true boolean 'A' "A" char String

Variable Declaration & Assignment int j; j 5 j = 5; k 4 int 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 What are the values in a, b & c? int a = 2; int b = 1; int c = a + b; b = c; a = b; a: 2

b: 1 c: 3 a: 3 b: 1 c: 3 a: 2 b: 1 c: 2 Swap

int a = 5; int b = 3; Order the following to swap values in a & b. a = c; c = b; b = a; Google "swapping values without third variable" for many creative ways. Widening Conversion byte

short int long float double

char implicit cast Compiler converts automatically In some cases, may lose some precision (e.g., int to float). https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html Widening Primitive Conversion implicit cast (compiler does automatically)

float f = 23; double d = f; char ch = 'a'; short s = ch; //Both are 2 bytes will this work? Narrowing Primitive Conversion Programmer must explicitly cast. May do truncation and lose significant info. int n = (int)65.2; char c = (char)n;

//Similar example to $500 Million Error short s = (short)1424987.5; TopHat Assuming a Java expression, what is the value and data type of this result? double result = 9 / 2 * (2.0 + 2); What should the results be? double result1 = 4 / 8 * 2.0;

double result2 = 2.0 * 4 / 8; double result3 = 2.0 * (4 / 8); Tutorial: Precedence, Associativity & Order of Evaluatio n Integer vs Floating Point Division Meaning depends on data type of operands. integer divide floating point divide

1/2 vs 1 / 2.0 integer remainder (modulus operator) 5%2 Different data type operands are implicitly converted to "wider" by compiler. Operator Precedence & Associativity

What are the values of a, b & c? int a = 1; int b = 2; int c = a = b = 3; TH: What are the values of d & e? int d = 4; int e = d / (d = 2); TH: What should the result be?

1 + 2 + "3" + 4 + 5 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? 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 Magic Numbers (bad practice) Numbers with unexplained meaning: public class H { public static void main(String

[]args) { double s = 71 / 39.3701; } } Name Variables and Constants Constants (final variables) can help with readability. public class Height { public static void main(String []args) { final double INCHES_IN_METER =

39.3701; double heightInInches = 71; double heightInMeters = heightInInches / 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.

API Application Programmer Interface Example: Search for "Java 8 Math" Call class/static methods with the class name Calling Class/static Methods Call class/static methods with the class name. double numInts = Math.pow( 2, 32); double root = Math.sqrt( 16);

int num1 = 16; int num2 = 3; double result = num2 + Math.sqrt( num1); API vs Code API:What Code: How TopHat: Method Name

Select the method name. public static int add3(int a) { return a + 3; } TopHat: Method Parameter Select the method parameter. public static int add3(int a) { return a + 3; }

TopHat: Return Data Type Select the return data type. public static int add3(int a) { return a + 3; } TopHat: Method Definition Select the add3 method definition. public class Example {

public static int add3(int a) { return a + 3; } public static void main(String[] args) { int num = 1; int num2 = Example.add3( num); } } TopHat: Method Call

Select the add3 method call. public class Example { public static int add3(int a) { return a + 3; } public static void main(String[] args) { int num = 1; int num2 = Example.add3( num); } }

TopHat: Method Argument What is the add3 argument? public class Example { public static int add3(int a) { return a + 3; } public static void main(String[] args) { int num = 1; int num2 = Example.add3( num);

} } TopHat: Method Value What is the value of the method? public class Example { public static int add3(int a) { return a + 3; } public static void main(String[] args) {

int num = 1; int num2 = Example.add3( num); } } TopHat: What prints out? static void calc(int num) { num = 3; } public static void main(String []args) {

int n = 5; calc( n); System.out.println( n); } 3 5 35 error

Which is called first: calc or println? static int calc(int num) { num -= 33; return num; } public static void main(String []args) { int n = 55; System.out.println( calc( n)); }

calc println error Testing Methods Methods written to run test cases to help validate and debug your code.

Recently Viewed Presentations

  • Advocacy against Substance Use and Abuse

    Advocacy against Substance Use and Abuse

    Advocacy against Substance Use and Abuse Module E: Lesson 4 Grade 11 Active, Healthy Lifestyles General Signs of Alcohol or Other Drug Use General loss of energy and motivation Declining grades, dropping classes Skipping or being late for class Preoccupation...
  • Ecology Population Ecology Populations 3. A population is

    Ecology Population Ecology Populations 3. A population is

    Populations. 3. A . population. is a group of individuals of the same species living in an area. These iguanas live in trees over a River in the North East corner of Costa Rica.
  • Liquids and Solids - University of California, Santa Barbara

    Liquids and Solids - University of California, Santa Barbara

    a. The density of the solid is greater that that of the liquid. b. Sublimation occurs if starting with a solid at a constant temperature of 17°C the pressure is decreased until a phase change occurs. c. Condensation occurs if...
  • Island of the Blue Dolphins

    Island of the Blue Dolphins

    scurrying move hurriedly with short quick steps lair a wild animal's resting place, especially one that is well hidden mussel any of a number of bivalve mollusks with a brown or purplish-black shell headland a narrow piece of land that...
  • What is globalization? Getting started with global issues.

    What is globalization? Getting started with global issues.

    What is globalization? Getting started with global issues.
  • Unit 6 - PC\|MAC

    Unit 6 - PC\|MAC

    By Vincent Giovagnoli, Courtney Beck, & Ayla Odins. Chapter 23. In this chapter we use the t test to find our true mean of the data given. With the t test we find out if our null hypothesis is retained...
  • U.S. History EOCT test

    U.S. History EOCT test

    A proposal for a law that originates with state citizens and bypasses the legislature and gets on the ballot by the petition process. Referendum. A process where legislatures place a proposed law on the ballot to allow citizens to vote...
  • Class 16: History of 17th C

    Class 16: History of 17th C

    Strengthened the trade and postal system throughout Mongolian controlled Asia; Silk Route. By 14th C, Han Chinese in the south of China were rebelling, leading to over-throw of Mongolians in China and the establishment of the Ming Dynasty ... Mogul...