CS 200 Primitives and Expressions

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc : tell your younger brother: makeTea(1 milk, 0 sugar);
Hello, world! Dissect HelloWorld.java Compile it Run it.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
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.
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.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
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
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
Java Programming: From Problem Analysis to Program Design, 4e
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.
IDENTIFIERS CSC 111.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Week 6 CS 302 Jim Williams, PhD.
Building Java Programs
IFS410 Advanced Analysis and Design
Chapter 2: Java Fundamentals
CS 200 Primitives and Expressions
Building Java Programs Chapter 2
CS 200 Methods, Using Objects
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
Primitive Types and Expressions
Building Java Programs
Building Java Programs
CS Week 2 Jim Williams, PhD.
Chapter 2: Java Fundamentals cont’d
Building Java Programs
CS Week 3 Jim Williams, PhD.
Building Java Programs
Ben Stanley for gAlpha gALPHA free, four-week venture-creation workshop designed to help entrepreneurially-minded students and technologists create high-growth.
Building Java Programs
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CS 200 Primitives and Expressions Jim Williams, PhD

This Week Chap 1 Programs - 3 parts (P1) Due Thursday Team Lab: 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

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.

Effort Influences Intelligence "...effort and difficulty, that's when their neurons are making new connections, stronger connections. That's when they're getting smarter." - Carol Dweck https://www.ted.com/talks/carol_dweck_the_ power_of_believing_that_you_can_improve

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;

What are the values in a, b & c? int a = 2; int b = 1; int c = a + b; a = b; a = c;

Swap values in a & b int a = 5, b = 3; Write code to swap values in a & b. a = b; b = a; c = b; b = c; c = a; a = c; Google "swapping values without third variable" for many creative ways.

Primitive Data Types int whole numbers (4 bytes) double floating point numbers (8 bytes) char single character (2 bytes) boolean true or false value (>= 1 bit) less commonly used: float (4), long (8), short (2), byte (1)

Primitive Data Type Conversion Which work? double a = 5; double b = 6.0; int c = 3.0F; float d = b; c = d; d = c; char e = 'A'; int f = e; short s = f; int i = 10.0 / 3; int j = (int) (10.0 / 3); double k = (int) (10.0 / 3.0)

Expression Evaluation Operators evaluated following rules of precedence and associativity Sub-expressions are evaluated left-to-right

Operator Precedence & Associativity

4 + 3 * 2 5 - 4 - 3 int a = 1, b = 2; int c = a = b = 3; int d = 4; d / (d = 2)

What is output? int a = 3; a = 3 + 2 * 6 / 2 - 1; 8 14 other int a = 3; a = 3 + 2 * 6 / 2 - 1; System.out.print( a);

What is output? int a = 3; int b = 2; a = a - b * (a = b = 1) + 2; System.out.print( a);

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

Variable Names public class H { public static void main(String []args) { double height = 76; height = height / 39.3701; }

Use Constants public class H { public static void main(String []args) { final double INCHES_IN_METER = 39.3701; double heightInInches = 76; double heightInMeters = heightInInches / INCHES_IN_METER; }

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? Retrieval practice importance of committing to an answer

Java Visualizer Implement Equation Create and call a 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

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.

Return to Converter double degreesCelsius; double degreesFahrenheit = 100.0; degreesCelsius = (degreesFahrenheit - 32.0) * 5.0 / 9.0;