CS 200 - Week 2 Jim Williams, PhD.

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

Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Hello, world! Dissect HelloWorld.java Compile it Run it.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
The switch statement: an N-way selection statement.
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.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
The Java Programming Language
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
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.
 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.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
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.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
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,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Elementary Programming
Introduction to Computer Science / Procedural – 67130
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
Programming Language Concepts (CIS 635)
Comp Sci 200 Programming I Jim Williams, PhD.
CS Programming I Jim Williams, PhD.
First Programs CSE 1310 – Introduction to Computers and Programming
CS Week 2 Jim Williams, PhD.
CS 302 Week 15 Jim Williams, PhD.
Comp Sci 302 Introduction to Programming
CS 200 Using Objects Jim Williams, PhD.
CS Week 6 Jim Williams, PhD.
An Introduction to Java – Part I, language basics
Chapter 2 Edited by JJ Shepherd
Week 6 CS 302 Jim Williams, PhD.
Building Java Programs
IFS410 Advanced Analysis and Design
Fundamentals 2.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
CS 200 Primitives and Expressions
CS 200 Primitives and Expressions
CS 200 Methods, Using Objects
elementary programming
Building Java Programs
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Primitive Types and Expressions
Building Java Programs
Unit 3: Variables in Java
Building Java Programs
CS Programming I Jim Williams, PhD.
CS Week 3 Jim Williams, PhD.
Ben Stanley for gAlpha gALPHA free, four-week venture-creation workshop designed to help entrepreneurially-minded students and technologists create high-growth.
CS Programming I Jim Williams, PhD.
Building Java Programs
Presentation transcript:

CS 200 - Week 2 Jim Williams, PhD

Society of Women Engineers "Not just for women and not just for engineers; all students are welcome. We are especially looking for more computer science students!" - Emile Wille (Marketing Officer for SWE)

This Week Team Lab: Tuesday or Wednesday Program 1: Due Thursday Lecture: Primitive Data Types and Expressions

Team Labs First meeting this week. 1350 cs and 1370 cs Meet TAs & LAs 1st floor, around corner from elevators Meet TAs & LAs Work in small groups and pairs 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.

Learning to Trace with Java Visualizer Java Visualizer available via our course website or: https://cscircles.cemc.uwaterloo.ca/java_visualize/ import java.util.Scanner; public class RectangleArea { public static void main(String [] args) { Scanner input = new Scanner(System.in); int height = 0; int width = 0; System.out.print("Enter height: "); height = input.nextInt(); System.out.print("Enter width: "); width = input.nextInt(); int area = height * width; System.out.println("A rectangle with height " + height + " and width " + width + " has an area of: " + area); input.close(); return; //optional at end of method, unless returning value }

Programming Process Users Files Programmer Editor (Virtual) Compiler Machine Compiler Hello.java Hello.class Computer Files Programmer

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

Edit-Compile-Run Cycle From the command-line: notepad javac java

Programming Process Question Cat.txt Cat.java Cat.class Cat 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?

Learning Programming Lots of little steps that quickly build Not mastering little steps creates bigger steps Goes beyond Remembering and Understanding to Applying, Analyzing, Evaluating and Creating. Goal: Have basic fluency in Java and be able to solve small problems using it.

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 h = 4 + 10 / 3; int i = 10.0 / 3; int j = (int) 10.0 / 3; double k = (int) 10.0 / 3.0

Operator Precedence & Associativity

Constant public class World { public static void main(String []args) { int size = 3; final int MAX_SIZE = 10; }

String class A reference (class), not a primitive data type. Frequently used final String TITLE_PREFIX = "Welcome to "; int week = 2; String welcome = "Week " + week; System.out.println( TITLE_PREFIX + welcome);

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.

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

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 Lots of existing code 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);

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

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

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

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

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.

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.

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