Download Program Design & Discrete Mathematics

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Syntax and Semantics

Syntax


The formal rules governing how valid instructions
are written in a programming language
Semantics

The set of rules that determines the meaning of
instructions written in a programming language
Data Types

Data is stored internally in memory, externally
on disk or tape or input from an input device

Data type determines how the data is
represented in the computer and the kinds of
processing the computer can perform on it.
Data Types

Standard or built-in data types
Used frequently and provided by Java
 integer number, real number, characters and
boolean


User-defined data types

These are referred to as classes
The char Data Type


Describes data consisting of one alphanumeric
character – a letter, a digit or a special symbol
Examples of values of type char
‘A’
‘’
‘a’
‘8’
‘+’ ‘?’
‘‘
 Each character is enclosed in single quotes


How does Java represent the single quote?
‘’’ – Not valid , syntax error
 Java provides an escape sequence - the sequence of the
two characters \’ is treated as a single character – ‘\’’
 How would you represent the backslash character?


Class
Classes and objects
A description of behavior of a group of objects
with similar properties and behaviors
 A pattern for an object
 Contains fields or data values and methods.
(Method: A subprogram that defines one aspect of
the behavior of the class)


Object
An entity or thing that is relevant in the context of a
problem
 An instance of a class

Classes and Objects

How do we create an object from a class?
Instantiation
Use of an operator called new, takes the class name
and returns an object of the class type.
 Object that is returned is an instance of the class

Class Declaration
ImportDeclaration;...
Modifiers
private
.
.
.
public
Modifiers... ClassIdentifier
{
ClassDeclaration...
class DoNothing
}
{
}
The String Class



A string is a sequence of characters enclosed in
double quotes.
In Java, a string is an object, an instance of the
class String.
Examples



“Introduction to” “Program” “ Design”
A string must be typed entirely on one line
Quotes are not considered parts of the string
The String Class




“amount” – character string made up of letters
a, m, o, u, n, and t.
“12345” – character string made up of
characters 1, 2, 3, 4, and 5 in that order
12345 is an integer quantity that can be used in
calculations.
“” – Empty strings contains no characters, not
even spaces
The String Class

Java provides operations for
Joining strings
 Comparing strings
 Copying portions of strings
 Changing the case of letters in strings
 Converting strings to numbers

Declarations – defining terms

A statement that associates a name (an identifier)
with a description of an element in a Java
program.
- elements : field, a method, a class or a package sot
that the programmer can refer to that item by name.
int minAB;
 Compiler picks a location in memory to be associated
with identifier
Declarations


In Java, the identifier must be declared before it
is used.
Allows compiler to verify that the use of the
identifier is consistent with what it is declared to
be.


Java is strongly typed. A variable can only contain a
value of the type or class specified in its declaration
Fields can be variable or constant
Variables
char myChar;
Variable Identifier
myChar
(memory location 111001010101)
VARIABLE
?
VALUE
(char)
DATA TYPE


Variables
A variable is a location in memory, referenced by an
identifier or name, that contains a data value that can
be changed
Variable declaration
Modifiers TypeName Identifier, Identifier...;

TypeName – Name of class or type such as char or String
char letter, middleInitial, ch;
OR
char letter;
char middleInitial;
char ch;
Variables
String firstName;// person first name
String lastName; //person’s last name
String title;
//person’s title
char middileInitial;
// person’s initial
Constants

Something whose value never changes


Use of the actual value of a constant is the use of a
literal value.


‘A’, ‘@’, “Hello World” are constants
Literal value - any constant value written in a program
Alternative to a literal value is the named constant
which is introduced in a declaration statement

A named constant - a location in memory, referenced by an
identifier, that contains a data value that cannot be changed
Constant Declaration
Modifiers final TypeName Identifier = LiteralValue;
final modifier tells Java compiler that this value is the last and
only value that this field will have.
Examples
final String LINE_OF_STARS = “*******”;
final char BLANK = ‘ ‘;
final String MESSAGE = “Error Condition”;

(Programming convention: Named constant in uppercase, to distinguish between
it and variables)
Matters of Style
– Capitalization of Identifiers

Variables and methods begin with a lowercase letter and
capitalize each successive English word.


middleInitial
hours
Class names begin with an upper case letter but are
capitalized the same as variable names thereafter.


lenghtInYards
PayRollFrame
String
MyDataType
Identifiers representing named constants are all upper
case with underscores used to separate the English
words

BOOK_TITLE
OVERTIME
MAX_LENGTH
Executable Statements

Provide ways of acting or performing
operations on data

Assignment statement

String expressions

Initialization of Fields

Outputting data to the screen
Assignment statement
A statement that stores the value of an expression
into a variable.
variable = expression;
variable is set equal to the value of the expression
variable gets the value of the expression
Expression


An arrangement of identifiers, literals and operators that
can be evaluated to compute a value of a given type
Assignment Statement

Only one variable can be on the left-hand side
of an assignment statement.



NOT like the math = (e.g. x + y = z + 4)
The expression on the RHS is evaluated and
the result is then stored into the single variable
on the left of the operator.
The value assigned to a variable must be of the
same type as the variable.
Declaration & Assignment Example
String firstName, middleName, lastName;
String title;
char middleInitial, myChar;
firstName = “Patryce”;
middleName = firstName;
middleName = “”;
title = “Mrs.”;
myChar = ‘D’;
middleInitial = myChar;
middleInitial = “D”;
myChar = firstName;
lastName= ;
“Allen” = lastName;
String Expressions


Concatenation – A special string operation that uses the
+ operator
Concatenating two strings results in a new string
containing characters from both strings
String courseCode, phrase1,phrase2;
phrase1 = “CS”;
phrase2 = “51Q”;
courseCode = phrase1 + phrase2;
courseCode now stores string “CS51Q”
String Expressions
String literals that are too long to fit on one line can be
broken into smaller string literals and concatenated.
longSentence = “ this is a long sentence ” +
“which cannot fit on one line“ +
“. Here is the last segment.”;
 To extend an existing string:
String courseCode;
courseCode = “CS”;
courseCode = courseCode + “51Q”;

String Expressions
Concatenation only works with values of type
String
 Java converts built-in types to its equivalent
string if one attempts to concatenate it to a
String.
String result;
result = “The square of 12 is “ + 144;
“The square of 12 is 144” is assigned to the variable
result

Fields

Java does not distinguish between the declaration of a
variable and a constant.


final keyword is used to make the necessary distinction and
the field is given an initial value when declared.
Generally, fields can be initialized following this
template
Modifier TypeName Identifier = Expression, Identifier = Expression;
Output – System.out

System.out is an object that represents an output
device.

By default, the output device is the screen.

Messages to these objects state what should be
printed to the screen

Messages sent by applying methods print and println
System.out
Method print is invoked by placing the method name
next to the object with a dot in between.
 The “something” to be printed is called a parameter is
placed within the braces.
System.out.print(“Computer” + “ “ + “Science”);

Computer Science
System.out.print(“Computer”);
System.out.print(“ “);
System.out.print(“Science”):
System.out


print – successive messages printed next to each
other
println – used to go to a new line after a string
has been printed
Comments



Statements entered by the programmer that are
ignored by the compiler
Useful for documenting program
Two forms



/* This statement is ignored */
//Everything from the double forward slash to the //end of the line is
ignored.
Use comments at the beginning of programs, after
defining variables, to explain methods, to keep track of
starting and ending of blocks of code.