Fall 2017 CISC124 9/16/2017

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

Computational Expression

Getting started with Java

Full file at

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

3. Java - Language Constructs I

Data Types, Literals, Operators

Software Practice 1 Basic Grammar

Program Fundamentals

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

Datatypes, Variables, and Operations

PROGRAMMING FUNDAMENTALS

Introduction to Programming Using Java (98-388)

Java Fall 2018 Margaret Reid-Miller

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Lesson 2A Data. Data Types, Variables, Constants, Naming Rules, Limits. A Lesson in Java Programming

Java Classes & Primitive Types

Java Classes & Primitive Types

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

CEN 414 Java Programming

Fall 2017 CISC124 10/1/2017

Basics of Java Programming

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

VARIABLES AND TYPES CITS1001

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

An Introduction to Processing

A Fast Review of C Essentials Part I

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.

Fall 2017 CISC/CMPE320 9/27/2017

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Chapter 2 Elementary Programming

Fall 2017 CISC124 9/27/2017

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

Zheng-Liang Lu Java Programming 45 / 79

3. Java - Language Constructs I

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Java Basic Datatypees

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

CS260 Intro to Java & Android 03.Java Language Basics

Winter 2019 CISC101 1/17/2019

Elementary Programming

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

Values and Variables 1 / 30

Identifiers and Variables

Computer Science II (20082) Week 1: Review and Inheritance

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Values, Variables, Types & Arithmetic Expressions. Agenda

Software and Programming 1

Advanced Computer Programming

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Programming Language Basics

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Chapter 2 Primitive Data Types and Operations. Objectives

1.00/1.001 Tutorial 1

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

Object-Oriented Programming

Programming with Java

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

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

Java Classes: Math, Integer A C S L E C T U R E 8

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

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

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Introduction to Programming (Java) 2/12

Week 6: Review. Java is Case Sensitive

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

printf( Please enter another number: ); scanf( %d, &num2);

C++ Modern and Lucid C++ for Professional Programmers

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Java Bytecode (binary file)

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Visual C# Instructor s Manual Table of Contents

CS11 Java. Fall Lecture 1

Introduction to Computer Science and Object-Oriented Programming

Chapter 2 ELEMENTARY PROGRAMMING

Review for Test 1 (Chapter 1-5)

Reserved Words and Identifiers

CS 302: Introduction to Programming

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Java Identifiers, Data Types & Variables

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

CMPT 125: Lecture 3 Data and Expressions

Java Notes. 10th ICSE. Saravanan Ganesh

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

13 th Windsor Regional Secondary School Computer Programming Competition

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

COMP 110 Introduction to Programming. What did we discuss?

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.

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Chapter 2: Basic Elements of C++

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

BLM2031 Structured Programming. Zeyneb KURT

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

Transcription:

CISC124 Labs start this week in JEFF 155: Meet your TA. Check out the course web site, if you have not already done so. Watch lecture videos if you need to review anything we have already done. Problems with Eclipse/JDK installation? Work on Exercises 1 to 3. Work on Assignment 1. Ask questions!!! Today Continue Java Syntax: Primitive types. Array declaration. Literals. Variable name syntax rules. Variable declaration. Constant attributes. Type Casting. Fall 2017 CISC124 - Prof. McLeod 1 Fall 2017 CISC124 - Prof. McLeod 2 Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1. In Eclipse, create a project for this program or decide to add the new file to an existing project. 2. In Windows explorer, copy & paste the file to the src folder within the project chosen above. 3. In Eclipse, choose the project in the Package Explorer at the left and press <F5> to refresh the contents listed the new file should show up in the listing. 4. Or, just paste the file into the project in Eclipse. Java primitive types: char byte short int long float double boolean Primitive Types in Java Fall 2017 CISC124 - Prof. McLeod 3 Fall 2017 CISC124 - Prof. McLeod 4 Primitive Types? What is a primitive type anyways? Everything else in Java is an Object. A variable declared as one of the types shown on the previous slide is not an Object. Why does Java have primitive types? Object construction often involves the data abstraction of several primitive type attributes. Fall 2017 CISC124 - Prof. McLeod 5 Integer Primitive Types byte, short, int, long For byte, from -128 to 127, inclusive (1 byte). For short, from -32768 to 32767, inclusive (2 bytes). For int, from -2147483648 to 2147483647, inclusive (4 bytes). For long, from -9223372036854775808 to 9223372036854775807, inclusive (8 bytes). A byte is 8 bits, where a bit is either 1 or 0. Fall 2017 CISC124 - Prof. McLeod 6 Prof. Alan McLeod 1

Aside - Number Ranges Where do these min and max numbers come from? Memory limitations and the system used by Java (two s complement) to store numbers determines the actual numbers. The Wrapper classes can be used to provide the values - for example: Integer.MAX_VALUE // returns the value 2147483647 Real Primitive Types Also called Floating Point Types: float, double For float, (4 bytes) roughly ±1.4 x 10-38 to ±3.4 x 10 38 to 7 significant digits. For double, (8 bytes) roughly ±4.9 x 10-308 to ±1.7 x 10 308 to 15 significant digits. More on these topics later! Fall 2017 CISC124 - Prof. McLeod 7 Fall 2017 CISC124 - Prof. McLeod 8 char Character Primitive Type Boolean Primitive Type boolean is either true or false. From '\u0000' to '\uffff' inclusive, that is, from 0 to 65535 (base 10) or 0 to ffff (base 16, or hexadecimal ). A variable of the char type represents a Unicode character. Can also be represented as 'a' or '8', etc. Fall 2017 CISC124 - Prof. McLeod 9 Fall 2017 CISC124 - Prof. McLeod 10 String Objects String s are not primitive data types, but are Objects. A String is declared in the same way as a primitive type using the keyword: String. String mystring = Hello Class! ; Or you can instantiate: String mystring = new String( Hello Class! ); Declaration in Java: Array Declaration int[] data = new int[10]; or int[] data; data = new int[10]; new is always involved with the instantiation of an Object. Arrays are Objects in Java. Fall 2017 CISC124 - Prof. McLeod 11 Fall 2017 CISC124 - Prof. McLeod 12 Prof. Alan McLeod 2

Arrays vs. Python Lists A Python list can contain any mix of types, and items can be added at any time the list will grow to accept them. On the other hand, a Java array can only hold items of all the same type (even if they are objects). The size of a Java array must be declared and is then fixed. Arrays vs. Python Lists, Cont. In Python you used the slice operator: [ ] or [ : ] to get a single item or a range of items from a list. In Java you can only get at a single element at a time. For example: int[] anarray = {1, 2, 3, 4}; // Array literal! anarray[2] = 10; // Changes third element to 10 Fall 2017 CISC124 - Prof. McLeod 13 Fall 2017 CISC124 - Prof. McLeod 14 Array Declaration, Cont. An array literal is just a set of same-type values separated by commas in a set of { }. Only useful for small arrays To get the size of an array, use the.length attribute. For example anarray.length would provide 4 for the array on the previous slide. Array indices range from 0 to.length 1. Array Declaration, Cont. Cannot dynamically size an array in Java (nothing like malloc() in Java) In Java, you cannot use pointers to access array elements, only the indices. (You can never obtain a memory address in Java!) The first array element is at index zero. In Java you will get an immediate error and your program will halt if you try to go beyond the bounds of your array. Fall 2017 CISC124 - Prof. McLeod 15 Fall 2017 CISC124 - Prof. McLeod 16 A Common Runtime Error public static void main(string[] args) { int[] nums = {10, 20, 4, 2, 6, 7, 8, 12}; int i = 0; while (i <= nums.length) { System.out.println(nums[i]); i++; } // end while } // end main Why would this fail? A Common Runtime Error, Cont. Console output: 10 20 4 Exception in thread "main" java.lang.arrayindexoutofboundsexception: 8 at HelloWorld.main(HelloWorld.java:11) 2 6 7 8 12 What s in the error message? Why does it appear before the error happens? Fall 2017 CISC124 - Prof. McLeod 17 Fall 2017 CISC124 - Prof. McLeod 18 Prof. Alan McLeod 3

Integers, for example: Literal Values 12-142 0 333444891 If you write these kinds of numbers into your program, Java will assume them to be of type int, and store them accordingly. If you want them to be of type long, then you must append a L to the number: 43L 9999983475L -22233487L Binary, Octal and Hex Literals Use the prefix 0b (or 0B) in front of the numbers to get a binary literal. Octal just use 0 Hex use 0x You can also use the underscore _ in-between digits, to aid in visualizing the number. System.out.println(), by default displays the numbers in base 10. (Use printf to show the numbers in another codex or base. See part 2 of Exercise 1.) Fall 2017 CISC124 - Prof. McLeod 19 Fall 2017 CISC124 - Prof. McLeod 20 Binary, Octal and Hex Literals, Cont. Summary examples of other literals: binary: 0b111010111 binary with underscores: 0b111_010_111 octal: 07321023 hex: 0xab10fc Literal Values - Cont. Real or Floating Point numbers, for example: 4.5-1.0 3.457E-10-3.4E45 These literals will be assumed to be of the double type. If you want them to be stored as float types, append an F : 3.456F 5.678E-10F -321.0F Fall 2017 CISC124 - Prof. McLeod 21 Fall 2017 CISC124 - Prof. McLeod 22 Literal Values - Cont. char literals: A 5 \u0032 boolean literals: true false String literals: Hello there! 456.7 West of North Legal Variable Names Java names may contain any number of letters, numbers and underscore ( _ ) characters, but they must begin with a letter. Standard Java Naming Conventions: Names beginning with lowercase letters are variables or methods. Names beginning with uppercase letters are class names. Successive words within a name are capitalized ( CamelCase ). Names in all capital letters are constants. Fall 2017 CISC124 - Prof. McLeod 23 Fall 2017 CISC124 - Prof. McLeod 24 Prof. Alan McLeod 4

Variable Declaration Carried out inside a method. Syntax: type variable_name [= value]; Variable Declaration - Cont. Java may prevent you from using variables that are not initialized. So, it is often good practice to initialize your variables before use, for example: int numdaysinyear = 365; double avgnumdaysinyear = 365.25; String greetingline = Hello there! ; long counter = 0; Fall 2017 CISC124 - Prof. McLeod 25 Fall 2017 CISC124 - Prof. McLeod 26 Syntax: Constant Attribute Declaration [private public] [static] final type ATTRIBUTE_NAME = literal_value; The Java keyword, final can be used to make sure a variable value is no longer variable. Usually these are declared public static. The value must be assigned this part is no longer optional. Constants, Cont. Java will not allow your program to change a constant s value once it has been declared. For example: final int NUM_DAYS_IN_YEAR = 365; final double MM_PER_INCH = 25.4; Note that constant names are all in upper-case, by convention. You can also declare constants inside a method. Fall 2017 CISC124 - Prof. McLeod 27 Fall 2017 CISC124 - Prof. McLeod 28 Type Casting When a value of one type is stored into a variable of another type. Casting of primitive types in one direction is automatic, you do not have to deliberately or explicitly cast: A value to the left can be assigned to a variable to the right without explicit casting: byte > short > int > long > float > double Fall 2017 CISC124 - Prof. McLeod 29 Type Casting - Cont. For example in the statement: double myvar = 3; the number 3 is automatically cast to a double (3.0) before it is stored in the variable myvar. However, if you tried the following: int anothervar = 345.892; the compiler would protest loudly because a double cannot be stored in an int variable without loss of precision. Wrong direction! Fall 2017 CISC124 - Prof. McLeod 30 Prof. Alan McLeod 5

Casting Operator If you really want to cast in the other direction, then you must make an explicit cast. For example: int anothervar = (int)345.892; is legal. The (int) part of the statement casts the double to an int. The variable anothervar would hold the value 345 Note how numbers are truncated, not rounded! Fall 2017 CISC124 - Prof. McLeod 31 Aside Casting Objects We will find (later) that you can cast objects using the casting operator too. Objects must have an inheritance relationship in order for the cast to succeed. For example: Integer aval = new Integer(45); Number anum = (Number)aVal; The Integer class extends the Number class. More about this later Fall 2017 CISC124 - Prof. McLeod 32 Prof. Alan McLeod 6