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

Similar documents
last time in cs recitations. computer commands. today s topics.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Basics of Java Programming

Object-Oriented Programming

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

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

Course Outline. Introduction to java

Lecture Set 4: More About Methods and More About Operators

Welcome (back) to CS1007!

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

AP Computer Science A

4 Programming Fundamentals. Introduction to Programming 1 1

CMPT 125: Lecture 3 Data and Expressions

Lecture Set 4: More About Methods and More About Operators

Full file at

Getting started with Java

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

CIS 110: Introduction to Computer Programming

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

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

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

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

Program Fundamentals

DATA TYPES AND EXPRESSIONS

Accelerating Information Technology Innovation

Chapter 2: Data and Expressions

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

An overview of Java, Data types and variables

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

3. Java - Language Constructs I

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Programming with Java

JAVA Programming Fundamentals

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

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

CS 106 Introduction to Computer Science I

MODULE 02: BASIC COMPUTATION IN JAVA

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Introduction to Programming Using Java (98-388)

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Chapter 2: Data and Expressions

Control Structures in Java if-else and switch

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

Visual C# Instructor s Manual Table of Contents

CSC 1214: Object-Oriented Programming

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Chapter 2: Data and Expressions

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CIS133J. Working with Numbers in Java

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

Midterms Save the Dates!

Datatypes, Variables, and Operations

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Variables and data types

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Java Notes. 10th ICSE. Saravanan Ganesh

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Java Programming Fundamentals. Visit for more.

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

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

Chapter 2: Basic Elements of C++

Operators in java Operator operands.

Important Java terminology

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

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

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Zheng-Liang Lu Java Programming 45 / 79

Java Bytecode (binary file)

Fundamentals of Programming

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Language Reference Manual simplicity

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Midterm Review Topics Java Basics and Structure Variables Branching Loops Methods Arrays. Midterm Review Kage Weiss Lab 001 TA, SPR 17

A variable is a name for a location in memory A variable must be declared

1.00 Lecture 4. Promotion

Chapter 3: Operators, Expressions and Type Conversion

Mr. Monroe s Guide to Mastering Java Syntax

Introduction to Java & Fundamental Data Types

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Ex: If you use a program to record sales, you will want to remember data:

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

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

1 Shyam sir JAVA Notes

QUIZ: What value is stored in a after this

Array. Prepared By - Rifat Shahriyar

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

Programming Lecture 3

Fundamental of Programming (C)

ECE 122 Engineering Problem Solving with Java

today cs3157-fall2002-sklar-lect05 1

Introduction to Java Applications

CSc Introduction to Computing

Transcription:

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

Java. Java is an object-oriented language: it is structured around objects and methods, where a method is an action or something you do with the object Java programs are divided into entities called classes some Java classes are native but you can also write classes yourself Java programs can run as applications or applets cis20.1-fall2007-sklar-leci.2 2

our first application. hello world typical first program in any language output only (no input) cis20.1-fall2007-sklar-leci.2 3

the application source code. file name = hello.java /*---------------------------------------------------------- 5-sep-2007/sklar, hello.java This class demonstrates output from a Java application. ----------------------------------------------------------*/ public class hello { public static void main ( String[] args ) { System.out.println( "hello world!\n" ); } // end of main() } // end of class hello() cis20.1-fall2007-sklar-leci.2 4

output. methods System.out.println( ) System.out.print( ) arguments those things inside the parenthesis ( ) one or more Strings, separated by + s escape sequences: \n, \t also called parameters example System.out.println( "The quick" + ", brown " + "fox" ); cis20.1-fall2007-sklar-leci.2 5

things to notice. Java is CASE sensitive punctuation is really important! whitespace doesn t matter for compilation BUT whitespace DOES matter for readability and your grade! file name is same as class name cis20.1-fall2007-sklar-leci.2 6

data types and storage. programs = objects + methods objects = data data must be stored all storage is numeric (0 s and 1 s) cis20.1-fall2007-sklar-leci.2 7

memory. think of the computer s memory as a bunch of boxes inside each box, there is a number you give each box a name defining a variable example: program code: int x; computer s memory: x cis20.1-fall2007-sklar-leci.2 8

variables. variables have: name type value naming rules: names may contain letters and/or numbers but cannot begin with a number names may also contain underscore ( ) and dollar sign ($) underscore is used frequently; dollar sign is not too common in Java can be of any length cannot use Java keywords Java is case-sensitive!! cis20.1-fall2007-sklar-leci.2 9

primitive data types. numeric byte 8 bits -128 = -2 7 127 = 2 7-1 short 16 bits -32,768 = -2 15 32,767 = -2 15-1 int 32 bits -2 31 2 31-1 long 64 bits -2 62 2 63-1 float 32 bits -3.4E+38, 7 sig dig 3.4E+38, 7 sig dig double 64 bits -1.7E+308, 15 sig dig 1.7E+308, 15 sig dig boolean boolean 1 bit character char 16 bits cis20.1-fall2007-sklar-leci.2 10

assignment. = is the assignment operator example: program code: int x; // declaration x = 19; // assignment or int x = 19; computer s memory: x 19 cis20.1-fall2007-sklar-leci.2 11

Strings. a String in Java is a special data type it s called a wrapper class (which we ll talk about in detail later) a String is essentially a group of chars it comes with a method called length() that lets you find out how many characters are in the string (i.e., how long it is) it comes with a number of other methods, which we ll talk about later a char has single quotes around it char c = A ; a String has double quotes around it String s = "hello world!"; in this case, the method s.length() returns 12 cis20.1-fall2007-sklar-leci.2 12

mathematical operators. + unary plus unary minus + addition subtraction multiplication / division % modulo example: int x, y; x = -5; y = x * 7; y = y + 3; x = x * -2; y = x / 19; what are x and y equal to? modulo means remainder after integer division cis20.1-fall2007-sklar-leci.2 13

coercion or type casting. remember from last time: data of type char is stored as a number which is really an index into the ASCII table a declaration like this: char y = A ; really stores a 65 (the ASCII value of A ) in a memory location that is labeled y you can do math on that 65 by coercing (aka type casting) the char to an int for example: char y = A ; // initialize variable y to store an A int x = (int)y; // initialize variable x to store 65 x = x + 1; // increment x (to 66) y = (char)x; // coerce x from an int to a char ( B ) cis20.1-fall2007-sklar-leci.2 14

increment and decrement operators. increment: ++ i++; is the same as: i = i + 1; decrement: i--; is the same as: i = i - 1; cis20.1-fall2007-sklar-leci.2 15

assignment operators. += i += 3; is the same as: i = i + 3; -= i -= 3; is the same as: i = i - 3; *= i *= 3; is the same as: i = i * 3; /= i /= 3; is the same as: i = i / 3; %= i %= 3; is the same as: i = i % 3; cis20.1-fall2007-sklar-leci.2 16

boolean expressions. boolean variables: true (1) or false (0) logical operators:! not && and or example: boolean x, y; x = true; y = false; System.out.println( "x && y = " + ( x && y )); // outputs fals System.out.println( "x y = " + ( x y )); // outputs true System.out.println( "x &&!y = " + ( x &&!y )); // outputs tr cis20.1-fall2007-sklar-leci.2 17

truth tables. a!a false true true false a b a && b true true true true false false false true false false false false a b a b true true true true false true false true true false false false cis20.1-fall2007-sklar-leci.2 18

relational operators. == equality!= inequality > greater than < less than >= greater than or equal to <= Less than or equal to example: int x, y; x = -5; y = 7; some truths: ( x < y ) true ( x == y ) false ( x >= y ) false cis20.1-fall2007-sklar-leci.2 19

the if branching statement. if ( x < y ) { x = y; } if ( x < y ) { x = y; } else { x = 91; } cis20.1-fall2007-sklar-leci.2 20

the if branching statement (1). there are four forms: (1) simple if if ( x < 0 ) { System.out.println( "x is negative\n" ); } // end if x < 0 (2) if/else if ( x < 0 ) { System.out.println( "x is negative\n" ); } // end if x < 0 else { System.out.println( "x is not negative\n" ); } // end else x >= 0 cis20.1-fall2007-sklar-leci.2 21

the if branching statement (2). (3) if/else if if ( x < 0 ) { System.out.println( "x is negative\n" ); } // end if x < 0 else if ( x > 0 ) { System.out.println( "x is positive\n" ); } // end if x > 0 else { System.out.println( "x is zero\n" ); } // end else x == 0 cis20.1-fall2007-sklar-leci.2 22

the if branching statement (3). (4) nested if you can nest any kind/number of if s if ( x < 0 ) { System.out.println( "x is negative\n" ); } // end if x < 0 else { if ( x > 0 ) { System.out.println( "x is positive\n" ); } // end if x > 0 else { System.out.println( "x is zero\n" ); } // end else x == 0 } // end else x >= 0 cis20.1-fall2007-sklar-leci.2 23

System.exit() (1) a method in class java.lang.system definition: public static void exit( int status ); terminates the currently running Java Virtual Machine the argument serves as a status code by convention, a nonzero status code indicates abnormal termination use at the end of a program to exit cleanly or to terminate in the middle cis20.1-fall2007-sklar-leci.2 24

System.exit() (2) import java.lang.*; public class ex_exit { public static void main ( String[] args ) { if ( args.length < 3 ) { System.out.println( "usage: java ex_exit <a> <b> <c>" ); System.exit( 1 ); // abnormal termination } //... rest of program goes here... System.exit( 0 ); // normal termination } // end of main() } // end of class ex_exit cis20.1-fall2007-sklar-leci.2 25