CS Week 5. Jim Williams, PhD

Similar documents
CS Week 2. Jim Williams

Week 6 CS 302. Jim Williams, PhD

CS 200 Using Objects. Jim Williams, PhD

Getting started with Java

Introduction to Programming Using Java (98-388)

Midterm Examination (MTA)

Welcome to the Using Objects lab!

PROGRAMMING FUNDAMENTALS

CS 302 Week 9. Jim Williams

Chapter 4: Control Structures I

CS Week 11. Jim Williams, PhD

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Zheng-Liang Lu Java Programming 45 / 79

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Welcome to the Using Objects lab!

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

CSC Java Programming, Fall Java Data Types and Control Constructs

CIS 110: Introduction to Computer Programming

CS111: PROGRAMMING LANGUAGE II

Question: Total Points: Score:

AP Computer Science A Unit 2. Exercises

JAVA Programming Fundamentals

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Introduction to Java

1 Short Answer (10 Points Each)

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Fall 2017 CISC124 9/16/2017

CS 200 Arrays, Loops and Methods Jim Williams, PhD

CS302: Self Check Quiz 2

Welcome to the Primitives and Expressions Lab!

BM214E Object Oriented Programming Lecture 4

Assignment 2.4: Loops

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

4 Programming Fundamentals. Introduction to Programming 1 1

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

In this chapter, you will:

CS 200 Objects and ArrayList Jim Williams, PhD

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS 200 File Input and Output Jim Williams, PhD

Term 1 Unit 1 Week 1 Worksheet: Output Solution

CS180. Exam 1 Review

Java Basic Programming Constructs

Java Identifiers, Data Types & Variables

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Operators and Expressions

Object-Oriented Programming

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CS Week 14. Jim Williams, PhD

CS 177 Week 15 Recitation Slides. Review

CS 211: Methods, Memory, Equality

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Lec 3. Compilers, Debugging, Hello World, and Variables

Practice Midterm 1 Answer Key

Language Fundamentals Summary

Introduction to Java & Fundamental Data Types

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

AP Computer Science Unit 1. Programs

AP COMPUTER SCIENCE A

Chapter 2: Basic Elements of Java

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

Software Practice 1 Basic Grammar

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Decisions in Java IF Statements

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Department of Computer Science Purdue University, West Lafayette

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Important Java terminology

COMP Primitive and Class Types. Yi Hong May 14, 2015

CS 106 Introduction to Computer Science I

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CS 1301 Ch 8, Part A

Fundamentals of Programming Data Types & Methods

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

AP CS Unit 3: Control Structures Notes

Lecture Set 4: More About Methods and More About Operators

CS11 Java. Fall Lecture 1

Course information. Petr Hnětynka 2/2 Zk/Z

3. Java - Language Constructs I

Lexical Structure (Chapter 3, JLS)

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Selected Questions from by Nageshwara Rao

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Full file at

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Lecture 1. Types, Expressions, & Variables

Computer Components. Software{ User Programs. Operating System. Hardware

H212 Introduction to Software Systems Honors

Topics. Chapter 5. Equality Operators

Transcription:

CS 200 - Week 5 Jim Williams, PhD

The Study Cycle Check Am I using study methods that are effective? Do I understand the material enough to teach it to others? http://students.lsu.edu/academicsuccess/studying/strategies/tests/studying

This Week Exam 1 Next Week Lab: Branches, Eclipse & Styling Lecture: Review and more detail on Primitives, Objects, Branches and Methods

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

What is print out? char choice = 'a'; switch (choice) { case 'a': System.out.print("a"); case 'b': System.out.print("b"); break; default: System.out.print("other"); break; a b ab other

Scanner Scanner input = new Scanner("1 \ntwo \n 2\n\n"); int a = input.nextint(); if ( input.hasnextint()) { int b = input.nextint(); else { input.nextline(); String line = input.nextline(); System.out.println("#" + line + "#");

Scanner String note = "Hmm\na \na\n3\n\nline note."; Scanner input = new Scanner( note); int num = 0; String str1 = input.nextline(); String str2 = input.next(); if ( input.hasnextint()) { num = input.nextint(); String str4 = input.nextline(); num: 0 str4: 3 num: 0 str4: line note. num: 3 str4: line note. num: 0 str4:

Memory Areas static Code Stack - local, parameters in Stack Frame Heap - instances/objects

Strings and memory class S { public static void main( String []args) { String name; new String( dog ); name = new String( happy );

Where is memory allocated? public static void main( String []args) { String name; name = new String("spot"); new String("fido"); name: heap new String("fido"): heap name: stack new String("fido"): heap name: heap new String("fido"): stack name: stack new String("fido"): stack

What is print out? public static void showfruit( String name) { name = new String("apple"); new String("banana"); public static void main(string[] args) { String name = "grape"; showfruit( name); System.out.println( name); grape apple banana

java.lang.string String stra = "This is a string"; //String literal int lengtha = stra.length(); //odd looking but works. int lengthb = "This is a string".length();

What is print out? String str = null; System.out.print( str.length() > 0); null true false error

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

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

Primitives and Wrapper classes Draw a picture of memory and describe each. public static void main( String []args) { int k; Integer m; k = 2; //example of? m = 3; //example of? k = m; //example of?

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

char - Which will compile? int intvalue = 0; char achar = 'B'; intvalue = achar; // 1 achar = intvalue + 1; // 2 achar = '\u0041'; // 3 1, 2, 3 1, 3 1 2, 3

What is print out? static void one() { System.out.print( "one "); static void two(int num) { System.out.print("two "); one(); System.out.print("two "); public static void main( String []args) { two( 3); System.out.print("main "); two one two main main two one two two two one main one two two main

What prints out? static void print( double value) { System.out.println( "print(double)"); static int print( int value) { System.out.print( "print(int)"); return 1; public static void main( String [] args) { print( 10); print(double) print(int) invalid overloading error

What prints out? static void print( double val1, double val2) { System.out.println( "print(double)"); public static void main( String [] args) { double value = 10.0; print( value, 20); static void print( int val1, int val2) { System.out.println( "print(int)"); print(double) print(int) invalid overloading error

What prints out? static void print( double val1, double val2) { System.out.println( "void print"); static int print( double num1, double num2) { System.out.print( "int print"); return 1; public static void main( String [] args) { print( 10.0, 20.0); void print int print invalid overloading other error

What prints out? static void print( double val1, double val2) { System.out.println( "print(double)"); static int something( int val1, int val2) { System.out.print( "something(int)"); return 1; public static void main( String [] args) { print( 10, 20); print(double) something(int) invalid overloading error

Integer overflow int bignum = Integer.MIN_VALUE; bignum -= 1; System.out.println( bignum < 0? "negative" : "non-negative");

Comparing Floats double num1 = 10.3; double num2 = 2.2; double num3 = num1 - num2; if ( num3 == 8.1) { System.out.println( "equal"); else { System.out.println( "not equal"); equal not equal error

Short Circuit evaluation boolean flag = true; if ( flag (flag = false)) { System.out.print( "if: " + flag); else { System.out.print( "else: " + flag); if: true if: false else: true else: false

Compound and unary operators int i = 5; i += 2; i -= -4; i %= 2; System.out.print( i); 0 1 2 3

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 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 A B C @

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

What do a and b possibly have in common? public static void main(string []args) { ma(); mb(); static void ma() { int a = 10; static void mb() { int b; System.out.println( b); may be the exact same memory location looking at b will reveal a (unintentially) nothing Error

Is result true or false? Integer m = 5; Integer n = 5; boolean result = m == n; true false error Java Language Specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html