Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.

Slides:



Advertisements
Similar presentations
What Is Java? The name Java is a trademark of Sun Microsystems
Advertisements

Basic Java Constructs and Data Types – Nuts and Bolts
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world.
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
CSci 1130 Intro to Programming in Java
COMP 14 Introduction to Programming
Introduction to Programming G51PRG University of Nottingham Revision 1
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CMT Programming Software Applications
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2: Introduction to C++.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Hello, world! Dissect HelloWorld.java Compile it Run it.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Chapter 2: Java Fundamentals
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Copyright Curt Hill Variables What are they? Why do we need them?
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
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.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
Chapter 2 Basic Computation
Chapter 2: Introduction to C++
Yanal Alahmad Java Workshop Yanal Alahmad
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Chapter 2.
Introduction to Programming in Java
2.1 Parts of a C++ Program.
Chapter 2 Edited by JJ Shepherd
An overview of Java, Data types and variables
Chapter 2: Basic Elements of Java
Review for Exam 1 Spring 2007 CS 101/CS 101-E.
Chapter 2: Introduction to C++.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Building Java Programs
In this class, we will cover:
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Building Java Programs
Presentation transcript:

Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1

Simple Java Program public class HelloWorld { public static void main (String[] args) { //our code will all go here… System.out.println("Hello, World!"); } Each word of this should make sense by the semester's end! For now it is boilerplate code—just the template we'll use to write code. file: HelloWorld.java

Whitespace Whitespace means the 'blank' characters: space, tab, newline characters. White Space is (almost) irrelevant in Java. Java does not care about indentation However, indentation is highly recommended! Java uses curly braces, { and } to replace indentation Follow the indentation pattern you had for Python, for readability The computer will only care about { }, however

Good Whitespace Example public class GoodSpacing { public static void main (String[] args) { int x = 5; int y = 12; System.out.println("x+y = "+ (x+y)); } } indentation levels for each block: class, method definitions, control structures…

Types! Java is strongly typed : every expression has a specific type that is known at compile time. Java has two kinds of types: primitive types (containing literal values) reference types (containing objects of some class). These are complex types. Variable types must be declared before use

Type declaration public class GoodSpacing { public static void main (String[] args) { int x = 5; int y = 12; System.out.println("x+y = "+ (x+y)); } } x is declared to be of type integer args is declared to be a list of strings main is declared to return void (nothing) the println method returns void

Primitive Types boolean: truth values: true, false char: a character in single-quotes: 'a' 'H' '\n' '5’ integers: byte, short, int, long floating point: float, double void is technically not a type, but is used to represent when a method doesn’t return a value

Specific Integer Types Java provides integral numbers of different 'widths' (different numbers of bits), and different ranges of values: byte(8 bits)-128to127 short(16 bits)-32768to32767 int(32 bits) to long(64 bits)(-2 63 )to( ) char(16 bits)0 to (all positive) (a character is actually stored in memory as an integer) You don’t have to memorize any of these ranges, just be aware they exist

Floating Point Numbers Approximating the real #s: called floating point numbers. internal binary representation: like scientific notation. S: sign bit.E: bias-adjusted exponent. M: adjusted fractional value. value = (-1) S * 2 E * M Also representable: infinity (+/−), NaN ("not a number") float: 32-bit representation.(1 sign, 8 exp, 23 frac) double: 64-bit representation. (1 sign, 11 exp, 52 frac) signexponentfraction

Reference Types strings: String language-defined: System, Math user-defined: Person, Address, Animal arrays: int[], String[], Person[], double[][] int[] myArray = {1, 5, 7}; int[] otherArray = new int[]; Person jill = new Person(); new is a keyword that we can use to create all reference types arrays and strings are special in Java; other ways to create without new

Creating Variables Variables must be declared and initialized before use. Declaration: creates the variable. It includes a type and a name. The variable can only hold values of that type. int x; char c; double[][] grid; Person p; Initialization: assign an expr. of the variable's type to it. x=7+8; c='M'; grid={{0,1},{2,3}}; p= new Person(); Both: we can declare and instantiate all at once: int x = 5; char c = 'S'; Person p = new Person();

Casting a cast is a conversion from one type to another. We cast things by placing the type in parentheses in front of it: (int) 3.14 One use: forcing floating-point division. int x=3, y=4; double z = ((double)x)/y; System.out.println(z);

Strings Can be declared as String cat = “Fluffy”; String cat = new String(“Fluffy”); Can be added with + operator (concatenation) “Hello” + “ “ + “world” Can mix types String cat = “Hi” ‘ ‘ + false; int number = 5 + 3;

Java Comments There are two main styles of comments in Java: Single-Line: from // to the end of the line. Multi-Line: all content between /* and */, whether it spans multiple lines or part of one line. JavaDoc: convention of commenting style that helps generate code documentation/API. More on this later.

Expressions, Statements Expression: a representation of a calculation that can be evaluated to result in a single value. There is no indication what to do with the value. Statement: a command, or instruction, for the computer to perform some action. Statements often contain expressions.

Basic Expressions literals (all our primitive types) operation expressions: >= == !=(relational ops) + - * / %(math ops) & | && || !(boolean ops) parenthesized expressions ( expr ) variables (can store primitive types or reference types) method calls that return a value (are not void)

Programming TRAP Only use == and != for primitive types We will learn how to compare reference types later

Expression Examples Legal: x++(3>x) && (! true) x%2==1(x y>z4 && false x=37(x+y)

Basic Statements Declaration: announce that a variable exists. Assignment: store an expression's result into a variable. method invocations (may be stand-alone) blocks: multiple statements in { }'s control-flow: if-else, for, while, … (next lecture)

Statement Examples int x;// declare x x = 15;// assignment to x int y = 7;// decl./assign. of y x = y+((3*x)−5);// assign. with operators x++;// increment (x = x+1) System.out.println(x);// method invocation if (true):{//if statement int x = 50; int y = 3; x = x + y; }

Print Statement println is a method of the PrintStream class, that can print anything on a single line: System.out.println(“Hello World!); System.out is the standard buffer for printing to the screen

User Input Create a Scanner object and get the next value: Scanner scan = new Scanner(System.in); int num = scan.nextInt(); String string = scan.nextLine(); System.in is the standard buffer for keyboard input

Questions?