Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.

Slides:



Advertisements
Similar presentations
 Base “primitive” types: TypeDescriptionSize intThe integer type, with range -2,147,483, ,147,483,647 4 bytes byteThe type describing a single.
Advertisements

Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Chapter 3 Fundamental Data Types Goals: To understand integer and floating-point numbers To recognize the limitations of the int and double types and the.
Chapter 4  Fundamental Data Types 1 Chapter 4 Fundamental Data Types.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
How Create a C++ Program. #include using namespace std; void main() { cout
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 4: Fundamental Data Types. To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware.
Chapter 4 Numeric types & arithmetic Strings: reading & writing.
Chapter 3 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the int and double types.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 3 – Fundamental Data Types.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To recognize.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Fall 2006Slides adapted from Java Concepts companion slides1 Fundamental Data Types Advanced Programming ICOM 4015 Lecture 4 Reading: Java Concepts Chapter.
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 1 Chapter 2 Fundamental Data Types.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
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.
A final variable is a constant
Chapter 4 – Fundamental Data Types
Chapter Goals To understand integer and floating-point numbers
Lecture Notes – Basics (Ch1-6)
Multiple variables can be created in one declaration
Identifiers - symbolic names
Java Programming: From Problem Analysis to Program Design, 4e
IDENTIFIERS CSC 111.
Chapter 4 – Fundamental Data Types
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Fundamentals 2.
Chapter 2: Java Fundamentals
Principles of Computer Science I
Primitive Types and Expressions
4.3 Arithmetic Operators and Mathematical Functions
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Computer Science A 2: 6/2

Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor, libraries. A bit about software engineering – methods used in constructing programs. A bit about graphics Freeware book

Computer Science A What you have to do 6 * home work 3 * mini-project 1 * oral test based on mini-project All information available on

Java programs public class Hello{ public static void main(String[] args){ System.out.println(”Hello world”); } A program contains classes (here Hello )chapters classes contains methods (here main )paragraphs methods contains statements (here System.out… ) One class has the same name as the file ( Hello.java ) One method in that class is called main

Java Concepts Fundamental Data Types int (long + short + byte) double (and float) boolean char String

Variable declaration typeName variableName = value; or typeName variableName; Example: String greeting = "Hello, Dave!"; Purpose: To define a new variable of a particular type and optionally supply an initial value

Simple types typesvalues int double e100 char’a’ ’\n’ ’\’’ booleantrue false String”hello” ”” ”line\n”

Identifiers Identifier: name of a variable, method, or class Rules for identifiers in Java: –Can be made up of letters, digits, and the underscore (_) character –Cannot start with a digit –Cannot use other symbols such as ? or % –Spaces are not permitted inside identifiers –You cannot use reserved words –They are case sensitive By convention, variable names start with a lowercase letter By convention, class names start with an uppercase letter

Java: primitive types type size int The integer type, with range -2,147,483, ,147,483,647 4 byte The type describing a single byte, with range short The short integer type, with range long The long integer type, with range - 9,223,372,036,854,775, ,223,372,036,854,775,807 8 double The double-precision floating-point type, with a range of about ±10308 and about 15 significant decimal digits 8 float The single-precision floating-point type, with a range of about ±1038 and about 7 significant decimal digits 4 char The character type, representing code units in the Unicode encoding scheme 2 boolean The type with the two truth values false and true 1

Number Types: Floating-point Types Rounding errors occur when an exact conversion between numbers is not possible double f = 4.35; System.out.println(100 * f); // prints Java: Illegal to assign a floating-point expression to an integer variable double balance = 13.75; int dollars = balance; // Error Casts: used to convert a value to a different type int dollars = (int) balance; // OK Cast discards fractional part. Math.round converts a floating-point number to nearest integer long rounded = Math.round(balance); // if balance is 13.75, then rounded is set to 14

Syntax: type cast Syntax: (typeName) expression Example: (int) (balance * 100) (char) 65 Purpose: To convert an expression to a ‘smaller’ type double  long  int  char

Constants A final variable is a constant Once its value has been set, it cannot be changed Named constants make programs easier to read and maintain Convention: use all-uppercase names for constants final double QUARTER_VALUE = 0.25; final double DIME_VALUE = 0.1; final double NICKEL_VALUE = 0.05; final double PENNY_VALUE = 0.01; payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE + nickels * NICKEL_VALUE + pennies * PENNY_VALUE

Constants: static final If constant values are needed in several methods, declare them together with the instance fields of a class and tag them as static and final Give static final constants public access to enable other classes to use them public class Math {... public static final double E = ; public static final double PI = ; } double circumference = Math.PI * diameter;

Syntax: Constant Definition In a method: final typeName variableName = expression ; In a class: accessSpecifier static final typeName variableName = expression; Example: final double NICKEL_VALUE = 0.05; public static final double LITERS_PER_GALLON = 3.785; Purpose: To define a constant in a method or a class

Assignment Assignment is not the same as mathematical equality: items = items + 1; items++ is the same as items = items + 1 items-- subtracts 1 from items

Arithmetic Operations / is the division operator If both arguments are integers, the result is an integer. The remainder is discarded 7.0 / 4 yields / 4 yields 1 Get the remainder with % (pronounced "modulo") 7 % 4 is 3

Math class Math.sqrt(x) Square root Math.pow(x, y) Power y x Math.exp(x) exex Math.log(x) Natural log Math.sin(x), Math.cos(x), Math.tan(x) Sine, cosine, tangent Math.round(x) Closest integer to x Math.min(x, y), Math.max(x, y) Minimum, maximum

Using Math class

Syntax: Static Method Call ClassName. methodName(parameters) Example: Math.sqrt(4) Purpose: To invoke a static method (a method that does not operate on an object) and supply its parameters

Strings A string is a sequence of characters Strings are objects of the String class String constants: "Hello, World!" String variables: String message = "Hello, World!"; String length: int n = message.length(); Empty string: ""

Concatenation Use the + operator: String name = "Dave"; String message = "Hello, " + name; // message is "Hello, Dave" If one of the arguments of the + operator is a string, the other is converted to a string String a = "Agent"; int n = 7; String bond = a + n; // bond is Agent7

Converting between Strings and Numbers Convert to number: int n = Integer.parseInt(str); double x = Double.parseDouble(x); Convert to string: String str = "" + n; str = Integer.toString(n);

Substring String greeting = "Hello, World!"; String sub = greeting.substring(0, 5); // sub is "Hello" Supply start and “past the end” position First position is at 0 Substring length is “past the end” - start

Java API documentation