Fall 2018 CISC124 2/15/2019 CISC124 TA names and emails will be added to the course web site by the end of the week. Labs start next week in JEFF 155:

Slides:



Advertisements
Similar presentations
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Copyright Curt Hill Variables What are they? Why do we need them?
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
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.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Spring 2006CISC101 - Prof. McLeod1 Today! Build “HelloWorld” yourself in BlueJ and Eclipse. Look at all the Java keywords. Primitive Types.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
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.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
Fundamentals 2.
Chapter # 2 Part 2 Programs And data
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Crash course in the Java Programming Language
Elementary Programming
Lecture 5: Some more Java!
Yanal Alahmad Java Workshop Yanal Alahmad
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Multiple variables can be created in one declaration
Variables and Primative Types
Selenium WebDriver Web Test Tool Training
University of Central Florida COP 3330 Object Oriented Programming
CISC124 Labs start this week in JEFF 155: Meet your TA.
CISC/CMPE320 - Prof. McLeod
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CISC101 Reminders Quiz 2 this week.
IDENTIFIERS CSC 111.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Winter 2018 CISC101 11/29/2018 CISC101 Reminders
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Building Java Programs
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Chapter 2 Variables.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Chapter 2: Java Fundamentals
CISC124 TA names and s have been added to the course web site.
CISC124 Labs start this week in JEFF 155.
Chapter # 2 Part 2 Programs And data
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
Chapter 2: Java Fundamentals
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 2 due today.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CMPE212 – Reminders Course Web Site:
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Chapter 2 Variables.
Variables in C Topics Naming Variables Declaring Variables
CISC101 Reminders Assignment 3 due today.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Fall 2018 CISC124 2/15/2019 CISC124 TA names and emails will be added to the course web site by the end of the week. Labs start next 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!!! Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

Reminder Please fill out the lab section survey. So far, 192 out of 233 done. Survey closes tomorrow. Fall 2018 CISC124 - Prof. McLeod

Today Continue Java Syntax: Primitive types String declaration Array declaration Literal values The var type in Java 10 Constants Type casting (if we have time) Fall 2018 CISC124 - Prof. McLeod

But First, Discussion What are the advantages/disadvantages of the use of a declarative language with static typing? Fall 2018 CISC124 - Prof. McLeod

Primitive Types in Java Java primitive types: char byte short int long float double boolean Fall 2018 CISC124 - Prof. McLeod

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 2018 CISC124 - Prof. McLeod

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 2018 CISC124 - Prof. McLeod

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. (More on this topic later.) The Wrapper classes can be used to provide the values - for example: Integer.MAX_VALUE // returns the value 2147483647 More on Wrapper classes later too! Fall 2018 CISC124 - Prof. McLeod

Real Primitive Types Also called “Floating Point” Types: float, double For float, (4 bytes) roughly ±1.4 x 10-38 to ±3.4 x 1038 to 7 significant digits. For double, (8 bytes) roughly ±4.9 x 10-308 to ±1.7 x 10308 to 15 significant digits. Fall 2018 CISC124 - Prof. McLeod

Character Primitive Type 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 2018 CISC124 - Prof. McLeod

Boolean Primitive Type boolean is either true or false. Fall 2018 CISC124 - Prof. McLeod

String Objects String’s are not primitive data types, but are Objects. A String can be declared in the same way as a primitive type using the keyword: String. String example = "I am a string!"; Is the same as the following instantiation: String example = new String("I am a string!"); Fall 2018 CISC124 - Prof. McLeod

Array Declaration Declaration in Java: new is always involved with the instantiation of an Object. Arrays are Objects in Java. int[] data = new int[10]; or int[] data; data = new int[10]; Fall 2018 CISC124 - Prof. McLeod

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. Fall 2018 CISC124 - Prof. McLeod

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 2018 CISC124 - Prof. McLeod

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. Fall 2018 CISC124 - Prof. McLeod

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 2018 CISC124 - Prof. McLeod

Array Declaration Declaration in Java: new is always involved with the instantiation of an Object. Arrays are Objects in Java. int[] data = new int[10]; or int[] data; data = new int[10]; Fall 2018 CISC124 - Prof. McLeod

A Common Runtime Error Why would this fail? 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? Fall 2018 CISC124 - Prof. McLeod

A Common Runtime Error, Cont. Console output: What’s in the error message? Why does it appear before the error happens? 10 20 4 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at HelloWorld.main(HelloWorld.java:11) -2 6 7 8 12 Fall 2018 CISC124 - Prof. McLeod

Literal Values Integers, for example: 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 Fall 2018 CISC124 - Prof. McLeod

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 2018 CISC124 - Prof. McLeod

Binary, Octal and Hex Literals, Cont. Summary examples of other literals: binary: 0b111010111 binary with underscores: 0b111_010_111 octal: 07321023 hex: 0xab10fc Fall 2018 CISC124 - Prof. McLeod

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 2018 CISC124 - Prof. McLeod

Literal Values - Cont. char literals: boolean literals: ‘A’ ‘5’ ‘\u0032’ boolean literals: true false String literals: “Hello there!” “456.7” “West of North” Fall 2018 CISC124 - Prof. McLeod

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 2018 CISC124 - Prof. McLeod

Local Variable Declaration Carried out inside a method. Syntax: type variable_name [= value]; Fall 2018 CISC124 - Prof. McLeod

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 2018 CISC124 - Prof. McLeod

Java 10 var Type var is not a Java keyword instead it is called a “reserved type name”. This allows code like: var aNum = 10; aNum will be of type int. The type for the variable is inferred from the type of the literal value used to initialize the variable. Fall 2018 CISC124 - Prof. McLeod

Java 10 var Type, Cont. This uses “type inference”, which will only work if the type can be figured out from the same line of code as the declaration. So, variables have to be initialized using a literal or the result of some expression or method call. They can only be used for local variables, and: You can also use them in for each loops (later), for the type of the collection element produced by iterating through the collection. Fall 2018 CISC124 - Prof. McLeod

Java 10 var Type, Cont. What’s the point? var can reduce repetition in longer declarations. For example: var collect = new ArrayList<String>(); is easier than: ArrayList<String> collect = new ArrayList<String>(); and does not contain so much redundancy. Fall 2018 CISC124 - Prof. McLeod

Java 10 var Type, Cont. Suppose you have a series of local variable declarations that don’t line up very nicely: int aNum = 45; ArrayList<Integer> nums = new ArrayList<Integer>(); String aString = "Hello Class!"; You could write this as: var aNum = 45; var nums = new ArrayList<Integer>(); var aString = "Hello Class!"; Fall 2018 CISC124 - Prof. McLeod

Constant Attribute Declaration Syntax: [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. Fall 2018 CISC124 - Prof. McLeod

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 2018 CISC124 - Prof. McLeod