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

  • Value Investing Versus Evolution: Where Mutants Thrive

    Value Investing Versus Evolution: Where Mutants Thrive

    smartwomansecurities October 31, 2007 Financial Metrics: Part 1 MORGEN PECK EQUITY RESEARCH ANALYST FIDELITY INVESTMENTS These materials are made for educational purposes and should not be distributed.
  • Transportation Experiences Among Returning Citizens

    Transportation Experiences Among Returning Citizens

    "I start my day running to drop my urine (drug testing). Then I go to see my children, show up for my training program, look for a job, go to a meeting (Alcoholics Anonymous) and show up at my part-time...
  • Diapositiva 1 - asl.bergamo.it

    Diapositiva 1 - asl.bergamo.it

    Lo scompenso cardiaco Affrontiamolo insieme * Quasi il 90 per cento dei pazienti scompensati presenta almeno un valore di pressione misurato nell' arco degli ultimi 12 mesi e quasi un quarto presenta da 7 a 12 misure nell' ultimo anno.
  • Documentary Photography  The history of the genre Larry

    Documentary Photography The history of the genre Larry

    Diane Arbus. Nan Goldin. LARRY CLARK. Lawrence Donald "Larry" Clark (born January 19, 1943) is an American film director, photographer, writer and film producer who is best known for the movie Kids and his photography book Tulsa. His most common...
  • Portfolio Management 3-228-07 Albert Lee Chun Construction of

    Portfolio Management 3-228-07 Albert Lee Chun Construction of

    * Two-Fund Theorem A B Interesting Fact: Any two efficient portfolios will generate the entire efficient frontier! Every point on the efficient frontier is a linear combination of any two efficient portfolios A and B. * Now imagine a risky...
  • Welcome to Maastricht University School of Business and

    Welcome to Maastricht University School of Business and

    What is EleUM? Stands for Electronic Learning Environment University Maastricht (Access to) Online-platform for students, professors, and all other UM employees. Provides (educational) information about programmes, courses, deadlines, timetables and examinations
  • Hi there! VotesforSchools is working alongside the Government

    Hi there! VotesforSchools is working alongside the Government

    VotesforSchools. is working alongside the Government this week to find out your views on how social media could be made safer. If you want more lessons like this . you can find out more about VotesforSchools. here. or head to...
  • Balanced Literacy Approach - Nancy Broz

    Balanced Literacy Approach - Nancy Broz

    Balanced Literacy Approach Cohen and Cowen ... Syntactic Cues Semantic Cues Reading Cues Graphophonic cues - alphabetic knowledge of letters and sounds Semantic cues - cues from the context of the sentence Syntactic cues - decodig words from grammar or...