CISC124 Labs start this week in JEFF 155: Meet your TA.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
Summer 2007CISC121 - Prof. McLeod1 CISC121 to: “Introduction to Computing Science I” Prof. McLeod GOO550 TA’s are Ana and Krista.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
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?
COMP Primitive and Class Types Yi Hong May 14, 2015.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
Spring 2006CISC101 - Prof. McLeod1 Today! Build “HelloWorld” yourself in BlueJ and Eclipse. Look at all the Java keywords. Primitive Types.
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.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Fundamentals 2.
Chapter # 2 Part 2 Programs And data
Chapter 2 Variables.
Chapter 2 Basic Computation
Chapter 4 Assignment Statement
Crash course in the Java Programming Language
Lecture 5: Some more Java!
Yanal Alahmad Java Workshop Yanal Alahmad
Documentation Need to have documentation in all programs
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Multiple variables can be created in one declaration
Variables and Primative Types
Chapter 3 Assignment Statement
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.
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
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
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
Expressions and Assignment
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.
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Introduction to Primitives
Introduction to Primitives
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Java Programming Language
CMPE212 – Reminders Course Web Site:
Java Basics Data Types in Java.
Primitive Types and Expressions
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Unit 3: Variables in Java
Chapter 2 Variables.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Variables in C Topics Naming Variables Declaring Variables
CISC101 Reminders Assignment 3 due today.
Presentation transcript:

CISC124 Labs start this week in JEFF 155: Meet your TA. Fall 2017 CISC124 8/28/2018 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!!! Fall 2017 CISC124 - Prof. McLeod Prof. Alan McLeod

Today Continue Java Syntax: Primitive types. Array declaration. Literals. Variable name syntax rules. Variable declaration. Constant attributes. Type Casting. Fall 2017 CISC124 - Prof. McLeod

Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! In Eclipse, create a project for this program or decide to add the new file to an existing project. In Windows explorer, copy & paste the file to the src folder within the project chosen above. 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. Or, just paste the file into the project in Eclipse. Fall 2017 CISC124 - Prof. McLeod

Primitive Types in Java Java primitive types: char byte short int long float double boolean Fall 2017 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 2017 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 2017 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. The Wrapper classes can be used to provide the values - for example: Integer.MAX_VALUE // returns the value 2147483647 More on these topics later! Fall 2017 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 2017 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 2017 CISC124 - Prof. McLeod

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

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!”); Fall 2017 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 2017 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 2017 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 2017 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 2017 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 2017 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 2017 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 2017 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 2017 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 2017 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 2017 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 2017 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 2017 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 2017 CISC124 - Prof. McLeod

type variable_name [= value]; Variable Declaration Carried out inside a method. Syntax: type variable_name [= value]; Fall 2017 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 2017 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 2017 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 2017 CISC124 - Prof. McLeod

byte > short > int > long > float > double 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

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

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

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