24
CIS 260: App Dev I

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

Embed Size (px)

Citation preview

Page 1: 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

CIS 260: App Dev I

Page 2: 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

2

Programs and Programming

ProgramA sequence of steps designed to accomplish a task

Program designA detailed _____ for implementing a program

ProgrammingThe process of implementing a program design

Application programA stand-alone computer program that is applied to a

real-world problem

Page 3: 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

3

The Java Programming Language

Programming languageThe ________ used to create valid program

statements Syntax

The symbols, words, and rules of a programming language

A simple Java programpublic class Welcome

{

public static void main( String[] args )

{

System.out.println( "Welcome to Java Programming“ );

}

}

Page 4: 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

4

The Java Syntax Tokens

Special symbols, word symbols, and __________ of a language

Special symbol One or more characters w/ special meaning Examples: +, -, *, /, <, …

Word symbols Reserved word (___________) Examples: int, static, return, true, …

Identifier Predefined or user-defined names of things Examples: print, totalCost, …

Page 5: 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

5

Data Types Data type

A classification of data according to legal values and legal operations on those values

Primitive data types in Java

primitive

character numeric logical

integral floating-point

char byte short int long float double boolean

Page 6: 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

6

Details on Selected Data Types

char Examples: ‘A’, ‘a’, ‘$’, ‘&’, ‘ ’ , … Unicode 65 is ‘A’ and Unicode 43 is ‘+’

int Non-decimal (whole number) values Range of values: -2147483648 to 2147483647 Examples: 24, -117, 34082, 0 , …

double Decimal values with up to 15 decimal places (double precision) Range of values: -1.7 x 10308 to 1.7 x 10308

Examples: 14.75 , -.00053 , -289038432.8993, -5.3E-4

boolean Logical values Examples: true, ___________

Page 7: 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

7

Arithmetic Operators Possible arithmetic operators for integral and floating-

point data types: +, -, *, /, % (remainder upon division)

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 _____

Page 8: 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

8

Order of Precedence *, /, and % have the same precedence + and – have the same precedence, but lower

than *, /, and % Operations with the same precedence are

performed from left to right ()’s can be used to override normal

precedence Examples

4 + 8 / 2 % 3 yields ____(4 + 8 / 2) % 3 yields ____

Page 9: 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

9

Expressions Integral expressions

All operands are integers or integer typesExample: (apples + oranges) * 2

Floating-point expressionsAll operands are floating-points or floating-point typesExample: totalCost * .05

Mixed expressionsOperands are of different typesExamples:

• 2*5/mpg yields ______ if mpg is 4.0 (double)• cost/2+(7-10.0) yields _______ if cost is 3 (int)

Page 10: 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

10

Type Casting Implicit type coercion

Occurs automatically with mixed expressions15/4.0 automatically becomes 15.0/4.0

Explicit type conversionAlso called type _________Converts a result to a desired data typeExamples

• (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 ______

Page 11: 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

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 (“”) 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, …

Page 12: 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

12

Parsing Numeric Strings In Java, input can only be received as a string

or character A string with only integers or decimal numbers

is called a ________ string (e.g. “78.3”, “.0038”, “17”)

To convert a numeric string to an actual number in Java Integer.parseInt(“17”) yields ____ Double.parseDouble(“78.3”) yields ____ Integer.parseInt(numInput) yields ____

Page 13: 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

13

Variables and Named Constants

How to store program data in main memory:Write a statement to ___________ memoryWrite a statement to put data in memory location

Data that may change during program execution are stored in a ___________.int hoursWorked; // allocates memoryhoursWorked = 45; // puts data inint overtimeHours = 5; // does both

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.

Page 14: 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

14

Assignment Statements _______ variables in Java (allocate memory):

double cost;String firstName, lastName;int i, j, k;

________ variables in Java (store in memory):cost = 19.95;firstName = “Richard”;i = i + 1; // get i, add 1, store in i

The “=“ is an _________ operator, not “equals”. It literally means “is assigned the value”.

Assign a new value to an existing variable:cost = materiaCost + laborCost;

Page 15: 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

15

Input: The Scanner Class Java is a pure OO language and uses its own

___________ classes, objects, and methods. The Scanner input stream class (new in 5.0):

static Scanner console = new Scanner( System.in ); console.nextInt() // gets next item as an integer console.nextDouble() // gets next item as a double console.next() // gets next item as a String console.nextLine() // gets everything to end of line

To read a single character: char aCharacter; aCharacter = console.next().charAt(0);

To convert string data to numeric data: double price; price = Double.parseDouble( console.nextDouble() );

Page 16: 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

16

Input: The JOptionPane Class

GUIs can be used for program input and output. Using GUIs requires an _________ statement. The following statement (must be the first

statement) imports the JOptionPane class: import javax.swing.JOptionPane;

This statement uses the showInputDialog method to get _________ input: name = JOptionPane.showInputDialog

( “Enter your name.” );

This statement uses the showMessageDialog method to display output: JOptionPane.showMessageDialog( null,

outputMessage );

Page 17: 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

17

Increment and Decrement The following type of statement is used a lot:

count = count + 1; It means “get the value in count, add 1 to it, assign that to

count” A shortcut in Java: count++; or ++count;

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.

Example:int a = 0, b = 0, c = 0;

c = 2 + (++a); // ___ will be stored in c

c = a + (b++); // ___ will be stored in c

c = a + (++b); // ___ will be stored in c

Page 18: 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

18

Using the String Class A String object usually consists of one or more

__________.String title = “War and Peace”;

The following shows an empty String and a null String:String code = “”; // has memory addressString inputValue = null; // no address

_______ sequences use the \ to create new lines, tabs, or special characters.“\”Bud\”\tSmith \n Rick\tAnkiel”

“Bud” Smith

Rick Ankiel

Page 19: 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

19

String Class Methods/Joining

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 How to join (____________) String objects:

String title = “War and Peace”;

double price = 14.95;

String message = “Title: ” + title + “\n”

+ “Price: ” + price; Note the code is written to enhance readability.

Page 20: 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

20

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. System.out.println(‘q’);// displays q System.out.println(“Joe”);// displays Joe Escape sequence \n is for a new _____, \t is

for a _____. Example

String name = “Joe”;

System.out.println(“My name is \n” + name + “.”);

Page 21: 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

21

Packages and import In Java, a package is a collection of related

________. A class is a section of Java code in a file that

contains methods and data definitions. A method is a set of instructions to accomplish a

specific _____. 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.

Page 22: 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

22

Java Application Programs Your Java application program must contain at

least one class and one of those classes must have a method called ______.

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;

Page 23: 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

23

Programming Style and Form

________ rules must be followed. For example,You must place a “;” at the end of each program

statement { } ‘s must always occur in pairs

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 text for inputProvide good explanation for input and output

Page 24: 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 Java Application Program

//A properly formatted Java program.

import java.util.Scanner;

public class IntegerNameHeight{ static Scanner console = new Scanner( System.in );

public static void main( String[] args ) { // preparation int num; double height; String name; // input System.out.print("Enter an integer: "); System.out.flush(); num = console.nextInt(); System.out.println(); System.out.print( "Enter the first name: “ ); System.out.flush(); name = console.next(); System.out.println();

System.out.print( "Enter the height: “ ); System.out.flush(); height = console.nextDouble();

// output System.out.println(); System.out.println( "num: " + num ); System.out.println( "Name: " + name ); System.out.println( "Height: " + height ); }}