CS 302 - Week 2 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
String Escape Sequences
Review Review Review. Concepts Comments: definition, example, different types of comments Class: definition, example Object: definition, example Data.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
Chapter 2 Variables.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
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.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Fundamentals 2.
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Basic Computation
Building Java Programs
Yanal Alahmad Java Workshop Yanal Alahmad
Documentation Need to have documentation in all programs
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
CS 200 Branches Jim Williams, PhD.
CS Week 4 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.
CS Week 7 Jim Williams, PhD.
Computers & Programming Languages
CS Week 3 Jim Williams, PhD.
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Week 6 CS 302 Jim Williams, PhD.
Chapter 2 Variables.
IFS410 Advanced Analysis and Design
Fundamentals 2.
CS 200 More Primitives, Objects, Branches and Methods
CS Week 3 Jim Williams.
Chapter 2: Java Fundamentals
CS 200 Primitives and Expressions
CS Week 4 Jim Williams, PhD.
CS 200 Primitives and Expressions
CS 200 Methods, Using Objects
Control Structure Chapter 3.
Java Programming Review 1
CS2011 Introduction to Programming I Elementary Programming
Primitive Types and Expressions
Unit 3: Variables in Java
CS Week 2 Jim Williams, PhD.
Chapter 2 Variables.
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.
Control Structure.
CS302 Week 6 Jim Williams.
Presentation transcript:

CS 302 - Week 2 Jim Williams, PhD

This Week Team Labs Lecture: Scanner, data types, conditionals

Team Labs First meeting this week. 1350 cs and 1370 cs Meet TAs & LAs, expect an Ice Breaker, be assigned a partner and do the lab together. Pair programming

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. Import to switch roles (who has the keyboard). Provide respectful, honest and friendly feedback

P1 Available P1 - Individual work Style and Commenting Discuss concepts, don’t share assignment code Style and Commenting

Piazza notes Don't post your code publicly (working or not). You can post code from the book if you cite it. From section 2.3.1, ... Review the other posts (search for common terms) before posting as your question may already have been answered. Refer to these posts along with your question and say how yours is different.

Read In Values Recall: import java.util.Scanner; Scanner input = new Scanner( System.in); int age = input.nextInt(); String name = input.nextLine();

What are values of variables? name: Minsub\n age: 22\nCS major: name: Minsub\n22\CS age: name: Minsub age: 22 String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();

What are values of variables? name: Minsub\n22\CS age: major: name: Minsub age: 22 name: Minsub major: CS String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();

What are values of variables? score1: 20 score2: 25 title: scores title: score1: 20 25 score2: scores String note = "20 25\nscores"; Scanner input = new Scanner( note); int score1 = input.nextInt(); int score2 = input.nextInt(); String title = input.nextLine();

Questions (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

Eclipse IDE Opening project, copying in files Style and Commenting Guides Strings I/O Calling methods Compiler & Runtime Errors Scanner reading a number

Review (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

Number Systems Decimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary 0, 1

Decimal 100 10 1 3 0 2 = 302

Binary 8 4 2 1 1 1 1 0 = 14

Binary 8 4 2 1 0 1 0 1 = 5

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)

Hexadecimal (group bits by nibble) 0000 = 0 0001 = 1 0010 = 2 0011 = 3 0100 = 4 0101 = 5 0110 = 6 0111 = 7 1000 = 8 1001 = 9 1010 = A 1011 = B 1100 = C 1101 = D 1110 = E 1111 = F

What character is this? 0000 0000 0100 0001 @ A Unicode: B C Unicode: 0x003E 62 > 0x003F 63 ? 0x0040 64 @ 0x0041 65 A 0x0042 66 B 0x0043 67 C http://www.ssec.wisc.edu/~tomw/java/unicode.html

Color 183, 1, 1 B70101 Red, Green, Blue (RGB) 183, 1, 1 B70101 Red, Green, Blue (RGB) https://umark.wisc.edu/brand/web/colors.php

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, long, short, byte

boolean operators (comparison) == != < <= > >= int n = 6; boolean result = n != 5; result: true result: false

Primitive vs Reference Data Types Store value Reference Store a reference to place in memory to find value

java.util.Random Random randGen; //Declare reference variable randGen = new Random(); //create instance // randGen.setSeed( 123); //set state int valueA = randGen.nextInt( 5); //get a value int valueB = randGen.nextInt( 5); int valueC = randGen.nextInt( 5);

Calling String methods answer:true answer:false String strA = "This is a string"; char aChar = strA.charAt( 3); boolean answer; answer = aChar == 'i';

java.lang.String String strA = "This is a string"; //String literal int lengthA = strA.length(); int lengthB = "This is a string".length();

Values of a1 and a2? String str1 = "Hello"; a1: true a2: true a1: false a2: false String str1 = "Hello"; String str2 = new String( "Hello"); boolean a1 = str1 == str2; boolean a2 = str1.equals(str2);

Values of a1 and a2? char ch1 = 'H'; String str = "Hello"; a1: true a2: true a1: false a2: false char ch1 = 'H'; String str = "Hello"; char ch2 = str.charAt(0); boolean a1 = ch1 == ch2; boolean a2 = ch1.equals(ch2);

Calling java.lang.Math methods double max( double a,double b) float max( float a, float b) int max(int a, int b) double numInts = Math.pow( 2, 32); double root = Math.sqrt( 16); int age1 = 5; int age2 = 25; double big = Math.max( age1, age2);

Are these equivalent? boolean tired = true; if ( tired) { Same result in all cases Different result sometimes Error boolean tired = true; if ( tired) { //take break tired = false; } if ( !tired) { //keep working boolean tired = true; if ( tired) { //take break tired = false; } else { //keep working }

Floating Down a River http://www.thehiltonorlando.com/discover/pools-and-lazy-river/

Side Trip, maybe multiple side trips boolean tired = true; if ( tired) { System.out.println(“take break”); }

One side of the Island or the other boolean sunny = false; if ( sunny) { System.out.print(“sunny”); } else { System.out.print(“not sunny”); } System.out.println( “ and back together”);

Equivalent? Yes No char chr = //any valid char out = 'W'; if ( chr == 'A') { out = 'X'; } else if ( chr == 'B') { out = 'Y'; } else { out = 'Z'; } char chr = //any valid char out = 'W'; if ( chr == 'A') { out = 'X'; } if ( chr == 'B') { out = 'Y'; } if ( chr != 'A' || chr != 'B') { out = 'Z'; } Yes No

Dangling Else - What is out? int value = 0; String out = "abc"; if ( value >= 1 ) if ( value <= 10) out = "1 to 10"; else out = "else"; out: abc out: 1 to 10 out: else out: abcelse

What is the value of msg? boolean flag = true; String msg = "before if"; if ( flag = false); { msg = "" + flag; } before if true false