Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CIS 260: App Dev I
Programs and Programming
Program
A sequence of steps designed to accomplish a task
Program design
A detailed _____ for implementing a program
Programming
The process of implementing a program design
Application program
A stand-alone computer program that is applied to a
real-world problem
2
The Java Programming Language
Programming language
The ________ used to create valid program
statements
Syntax
The symbols, words, and rules of a programming
language
A simple Java program
public class Welcome
{
public static void main( String[] args )
{
System.out.println( "Welcome to Java Programming“ );
}
}
3
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, …
4
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
integral
char
byte
short
int
logical
floating-point
long
float
double
boolean
5
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, ___________
6
Arithmetic Operators
Possible arithmetic operators for integral and floatingpoint 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 _____
7
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 ____
8
Expressions
Integral expressions
All operands are integers or integer types
Example: (apples + oranges) * 2
Floating-point expressions
All operands are floating-points or floating-point types
Example: totalCost * .05
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)
9
Type Casting
Implicit type coercion
Occurs automatically with mixed expressions
15/4.0 automatically becomes 15.0/4.0
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 ______
10
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, …
11
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 ____
12
Variables and Named Constants
How to store program data in main memory:
Write a statement to ___________ memory
Write a statement to put data in memory location
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
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.
13
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;
14
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() ); 15
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 );
16
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
c =
c =
c =
a
2
a
a
=
+
+
+
0, b =
(++a);
(b++);
(++b);
0,
//
//
//
c =
___
___
___
0;
will be stored in c
will be stored in c
will be stored in c
17
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 address
String inputValue = null; // no address
_______ sequences use the \ to create new
lines, tabs, or special characters.
“\”Bud\”\tSmith \n Rick\tAnkiel”
“Bud” Smith
Rick
Ankiel
18
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.
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.
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 + “.”);
20
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.
21
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;
22
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 input
23
Provide good explanation for input and output
//A properly formatted Java program.
import java.util.Scanner;
A Java Application Program
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 );
}
}