Ben Stanley for gAlpha gALPHA free, four-week venture-creation workshop designed to help entrepreneurially-minded students and technologists create high-growth.

Slides:



Advertisements
Similar presentations
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
Advertisements

Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Information and Computer Sciences University of Hawaii, Manoa
Elementary Programming
Building Java Programs
Introduction to Computer Science / Procedural – 67130
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
Primitive Data, Variables, Loops (Maybe)
Comp Sci 200 Programming I Jim Williams, PhD.
CS Programming I Jim Williams, PhD.
CS Week 2 Jim Williams, PhD.
Comp Sci 302 Introduction to Programming
CS 200 Using Objects Jim Williams, PhD.
CS Week 6 Jim Williams, PhD.
Introduction to C++ Programming
An Introduction to Java – Part I, language basics
Chapter 2 Edited by JJ Shepherd
Week 6 CS 302 Jim Williams, PhD.
Building Java Programs
Building Java Programs
CS 200 Loops Jim Williams, PhD.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
CS 200 Primitives and Expressions
Building Java Programs Chapter 2
CS 200 Primitives and Expressions
CS 200 Methods, Using Objects
Building Java Programs
CS2011 Introduction to Programming I Elementary Programming
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSC 1051 – Data Structures and Algorithms I
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
CS Week 2 Jim Williams, PhD.
CS Programming I Jim Williams, PhD.
Building Java Programs
CS Week 3 Jim Williams, PhD.
Building Java Programs
CS Programming I Jim Williams, PhD.
Building Java Programs
Presentation transcript:

Ben Stanley for gAlpha gALPHA free, four-week venture-creation workshop designed to help entrepreneurially-minded students and technologists create high-growth startups Partnership UW - Computer Science department WARF (Wisconsin Alumni Research Foundation) Extension of the UW-CS NEST program

CS 200 Primitives and Expressions Jim Williams, PhD

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, Process, Expressions and Methods

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

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.

Edit-Compile-Run Cycle Users Editor (Virtual) Machine Compiler Hello.java Hello.class Computer Files Programmer

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

Demo In team lab 1

File name For the class named Cat, what is the name of the file that the compiler creates and is provided to the Java Virtual Machine to run? Cat.txt Cat.java Cat.class Cat Cat.class

Compile on Command Line For the class named Cat, what command would compile the class? notepad Cat.java javac Cat.java java Cat.java java Cat javac Cat.java

Edit-Compile-Run Cycle Users Editor (Virtual) Machine Compiler Hello.java Hello.class Computer Files Programmer

Various Errors Naming/Saving Syntax/Compile time Runtime & Logic Files Editor (Virtual) Machine Compiler Hello.java Hello.class Computer Files Programmer

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

Infinite Numbers How do we represent and do calculations on a finite machine? 1 -1 1.0 1.5 3000000000 3.1415926

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

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

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

Primitive Data Types How many can you list?

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

Today Primitives & Expressions Process Methods

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

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

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

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

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

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)?

Evaluating Java Expressions Apply Precedence and Associativity rules by Parenthesizing expressions. Evaluate expressions Left-to-Right.

Precedence Evaluate higher precedence operators first. 1 + 2 * 3 Effectively: (1 + (2 * 3))

What is the result? 2 * 3 + 1 * 4 + 2 Effectively: ((2 * 3) + (1 * 4) )+ 2

Associativity If same precedence, which first? 2 + 3 + 4 "A" + 2 + 3 Since + associativity is left-to-right, effectively: ((2 + 3) + 4) (("A" + 2) + 3)

What is result? 5 - 4 - 3

Operator Precedence & Associativity

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

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

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 -17.778"); System.out.println( fahrenheitToCelsius( 98.6) + " expect 37"); System.out.println( fahrenheitToCelsius( -20) + " expect -28.8889"); //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 - 32.0) * (5.0 / 9); return degreesCelsius; //the value return as a result of the method

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

Java Visualizer Implement Equation Create and call a method

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

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.

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);

mPrint - which is Call, Definition? mPrint call then mPrint definition static void mPrint() { System.out.println("my print"); } public static void main(String []args) { mPrint(); B is the correct answer

Defining Methods public class M { //method definition static void mPrint() { System.out.println("my print"); } public static void main(String []args) { mPrint(); // method call. B is the correct answer

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).

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);

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.

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.

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

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

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

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