CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.

Slides:



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

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 2: Basic Elements of C++
CMT Programming Software Applications
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 28, 2005.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2: Basic Elements of C++
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
COMP 14 Introduction to Programming Miguel A. Otaduy May 14, 2004.
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Basic Elements of C++ Chapter 2.
Chapter 2-3: Basic Elements of Java. Chapter Objectives Discover how to use arithmetic operators. Examine how a program evaluates arithmetic expressions.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 2: Using Data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Basic Elements of C++.
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.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
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[]
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
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.
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of 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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Introduction to Computer Science / Procedural – 67130
2.5 Another Java Application: Adding Integers
Basic Elements of C++.
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
elementary programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chapter 2 Primitive Data Types and Operations
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

CIS 260: App Dev I

2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing a program n Programming  The process of implementing a program design n Application program  A stand-alone computer program that is applied to a real-world problem plan

3 The Java Programming Language n Programming language  The ________ used to create valid program statements n Syntax  The symbols, words, and rules of a programming language n A simple Java program public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java Programming"); } syntax

4 The Java Syntax n Tokens  Special symbols, word symbols, and __________ of a language n Special symbol  One or more characters w/ special meaning  Examples: +, -, *, /, <, … n Word symbols  Reserved word (___________)  Examples: int, static, return, true, … n Identifier  Predefined or user-defined names of things  Examples: print, totalCost, … identifiers keyword

5 Data Types n Data type  A classification of data according to legal values and legal operations on those values n Primitive data types in Java primitive characternumericlogical integralfloating-point charbyteshortintlongfloatdoubleboolean

6 Details on Selected Data Types n char  Examples: ‘A’, ‘a’, ‘$’, ‘&’, ‘ ’, …  Unicode 65 is ‘A’ and Unicode 43 is ‘+’ n int  Non-decimal (whole number) values  Range of values: to  Examples: 24, -117, 34082, 0, … n double  Decimal values with up to 15 decimal places (double precision)  Range of values: -1.7 x to 1.7 x  Examples: 14.75, , , -5.3E-4 n boolean  Logical values  Examples: true, ___________ false

7 Arithmetic Operators n Possible arithmetic operators for integral and floating- point data types:  +, -, *, /, % n Examples  8+7 yields 15  6-15 yields -9  6*8 yields 48  6*8.0 yields 48.0  15/4 yields _____  15/4.0 yields 3.75  15%7 yields 1  15.2%7 yields _____

8 Order of Precedence *, /, and % have the same precedence + and – have the same precedence, but lower than *, /, and % n Operations with the same precedence are performed from left to right () ’s can be used to override normal precedence n Examples  / 2 % 3 yields ____  (4 + 8 / 2) % 3 yields ____ 5 2

9 Expressions n Integral expressions  All operands are integers or integer types  Example: (apples + oranges) * 2 n Floating-point expressions  All operands are floating-points or floating-point types  Example: totalCost *.05 n Mixed expressions  Operands are of different types  Examples: 2*5/mpg yields ______ if mpg is 4.0 ( double ) cost/2+(7-10.0) yields _______ if cost is 3 ( int )

10 Type Casting n Implicit type coercion  Occurs automatically with mixed expressions  15/4.0 automatically becomes 15.0/4.0 n Explicit type conversion  Also called type _________  Converts a result to a desired data type  Examples (double) 15/3 yields ______ (int) (16/3.0)+2*8%5 yields ______ ( int) 16/number + 7 yields _____ if number is 2.0 (char) 65 yields ______ casting 6 15 ‘A’ 5.0

11 The class String A string is a sequence of 0 or more characters enclosed in double ________ (e.g., “Joe” ) In Java, a String is not a primitive data type A String with no characters is called a _____ string ( “” ) n The length of a String is its number of ___________ The position of a character in a String starts with 0 for the first, 1 for the second, … quotes null characters

12 Parsing Numeric Strings n In Java, input can only be received as a string or character n A string with only integers or decimal numbers is called a ________ string (e.g. “78.3”, “.0038”, “17”) n To convert a numeric string to an actual number in Java  Integer.parseInt(“17”) yields ____  Double.parseDouble(“78.3”) yields ____  Integer.parseInt(numInput) yields ____ numeric ???

13 Variables and Named Constants n How to store program data in main memory:  Write a statement to ___________ memory  Write a statement to put data in memory location n Data that may change during program execution are stored in a ___________.  int hoursWorked; // allocates memory  hoursWorked = 45; // puts data in  int overtimeHours = 5; // does both n Data that should not change during program execution are stored in a named ___________.  final double PAY_RATE = 7.50; Variables and constants are just __________ locations. allocate variable constant memory

14 Assignment Statements n _______ variables in Java (allocate memory):  double cost;  String firstName, lastName;  int i, j, k; n ________ variables in Java (store in memory):  cost = 19.95;  firstName = “Richard”;  i = i + 1; // get i, add 1, store in i n The “=“ is an _________ operator, not “equals”. It literally means “is assigned the value”. n Assign a new value to an existing variable:  cost = materiaCost + laborCost; Declare Assign assignment

15 Input (Read) Statement in Java n Java is a pure OO language and uses its own special classes, objects, and methods. The two Java statements below use the BufferedReader and ___________________ classes and the ___ object of the System class to create an object called keyboard, which uses a predefined method called readLine() to store characters in a variable called name : BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); name = keyboard.readLine(); n To convert string data to numeric data:  price = Double.parseDouble(keyboard.readLine()); InputStreamReader in

16 Increment and Decrement n The following type of statement is used a lot:  count = count + 1;  It means “get the value in count, add 1 to add, assign it to count”  A shortcut in Java: count++; or ++count; n Increment and decrement operators have prefix and _______ forms.  The prefix form is evaluated before the expression is evaluated.  The postfix form is evaluated ______ the expression is evaluated. n Example: int a=0, b=0, c; c = 2 + (++a); // ___ will be stored in c c = a + (b++); // ___ will be stored in c c = a + (++b); // ___ will be stored in c postfix after 3 1 3

17 Using the String Class n A String object usually consists of one or more __________. String title = “War and Peace”; n The following shows an empty String and a null String:  String code = “”;  String inputValue = null; n _______ sequences use the \ to create new lines, tabs, or special characters. “\”Bud\”\tSmith \n Rick\tAnkiel”  “Bud”Smith RickAnkiel characters Escape

18 String Class Methods/Joining n A method of a class is called using the object name, a ____, and the method name (with arguments). String choice=“X”; if (choice.equals(“x”)) // returns false n How to join (____________) String objects: String title = “War and Peace”; double price = 14.95; String message = “Title: ” + title + “\n” + “Price: ” + price; n Note the code is written to enhance readability. dot concatenate

19 Output In Java, the standard output object is __________ with methods print and _______. print leaves the cursor at the end of the current line while println moves it to the next line. n System.out.println(‘q’);// displays q n System.out.println(“Joe”);// displays Joe Escape sequence \n is for a new _____, \t is for a _____. n Example String name = “Joe”; System.out.println(“My name is \n” + name + “.”); System.outprintln line tab

20 Packages and import n In Java, a package is a collection of related ________. n A class is a section of Java code in a file that contains methods and data definitions. n A method is a set of instructions to accomplish a specific _____. n The package _________ contains classes for program input and output. To make all classes in java.io available in your program you need the statement ________________ at the very beginning. classes task java.io import java.io

21 Java Application Programs n Your Java application program must contain at least one class and one of those classes must have a method called ______. n The method main has two parts:  The heading: public static void main(String [] args) –public means main is accessible to other classes –static means main isn’t directly related to objects –void means main will not return data  The body Enclosed in { } ’s Contains declaration statements: int myAge, yourAge; Contains executable statements: myAge = 50; main

22 Programming Style and Form n ________ rules must be followed. For example,  You must place a “ ; ” at the end of each program statement  { } ‘s must always occur in pairs n Form and style:  Write just one statement per line  Indent lines for readability (as shown in examples)  Add important _____________ using // and /* … */ Always begin a program with comments for the program name, the programmer, the date, and the program purpose  Naming variables ( hourlyWage ), constants ( TAX_RATE )  Provide prompt lines for input  Provide good explanation for output Syntax documentation

A Java Application Program /* * Conversion.java * Created by Malik & Nair * 9/3/04 * Convert inches to centimeters */ import java.io.*; public class Conversion { static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); static final double CONVERSION = 2.54; static final int INCHES_PER_FOOT = 12; public static void main (String[] args) throws IOException { //declare variables int feet, inches, totalInches; double centimeter; // get input from user System.out.println("Enter feet: "); feet = Integer.parseInt(keyboard.readLine()); System.out.println(); System.out.println("Enter inches: "); inches = Integer.parseInt(keyboard.readLine()); // process data totalInches = INCHES_PER_FOOT * feet + inches; centimeter = totalInches * CONVERSION; // display output to user System.out.println("\nThe numbers you entered are " + feet + " for feet " + "and " + inches + " for inches. "); System.out.println("\nThe total number of inches = " + totalInches); System.out.println("The number of centimeters = " + centimeter); }