You are on page 1of 8

CMPP269 Module 1 -5 Chapter 1-5 Review

Java Language Specification – technical definition of the Java programming language’s syntax and semantics

API – Application Programmers Interface – library – predefined classes and interfaces for developing Java programs
JDK – Java Development Kit
JRE – Java Runtime Environment
JVM – Java Virtual Machine
IDE – Integrated Development Environment – e.g. BlueJ, Eclipse, NetBeans

Java is case sensitive.

Identifiers – program defined name that represents some element of a program, eg variables, class or method names

Naming Conventions – classes, variables, constants

Syntax errors

Runtime errors

Logic errors

Variable Data Types

• Primitive
numeric: integer – byte, short, int, long
decimal – float, double
character: char
(represented internally by numbers, Java uses Unicode, Appendix B)
boolean: boolean

• Reference Type (object – has both attributes and methods)


String creates a string object in memory, variable holds address of object
Integer, Boolean, Long, Double, Character, etc – Wrapper classes
• Constants remember to use keyword final

Scope of a variable – the part of a program where a variable can be referenced

Integer Division

Literals – default for integer literals is int


default for decimal literals is double

Type Casting – widening a type, narrowing a type

Operators (for operator precedence See Appendix C)

• Assignment operators: =
• Arithmetic operators: +, -, *, /, %
• Augmented assignment operators: +=, -=, *=, /=, %=
• Concatenation operator: +
• Cast operators: (typecasting) (int), (long), (double), etc.
• Logical operators: &&, ||, !
• Comparison operators: ==, !=, >, <, >=, <=
• Increment & decrement operators: ++, --, +=, -=
( prefix : ++num, postfix: num++)
CMPP269 Module 1 -5 Chapter 1-5 Review

Decision Structures / Branches:

• if Statement

if (BooleanExpression)
{
statement
statement
Etc.
}

• if – else statement

if (BooleanExpression)
{
statement;
statement;
Etc.
}
else
{
statement;
statement;
Etc.
}

• nested if statements

if (BooleanExpression)
{
if (BooleanExpression2)
{
statement;
statement;
Etc.
}
else
{
statement;
Etc.
}
}
else
{
statement;
Etc.
}
CMPP269 Module 1 -5 Chapter 1-5 Review

• if – else – if

if (BooleanExpression1)
{
statement
statement
Etc.
}
else if (BooleanExpression2)
{
statement;
statement;
Etc.
}
else
{
statement;
statement;
Etc.
}

• switch statement

switch (testexpression)
{
case value_1:
statement;
break;

case value_2:
statement;
break;

default:
statement;
break;
}

• conditional operator e. g. y = x > 0 ? 10: 20;

BooleanExpression ? Value1: Value 2


CMPP269 Module 1 -5 Chapter 1-5 Review

Loops

• while

while (BooleanExpression)
{
statement;
statement;
Etc. (something must happen that makes to BooleanExpression false to exit the loop
}

• do – while

do
{
statement;
statement;
Etc. (something must happen that makes to BooleanExpression false to exit the loop
}
while (BooleanExpression);

• for

for (Initialization; Test; Update)


{
statement;
statement;
Etc. (the Update must eventually make the Test false to exit the loop
}

• nested loops
• sentinel values
• pretest loops: while, for
• post test loops: do-while
CMPP269 Module 1 -5 Chapter 1-5 Review

Deciding on which control structure to use:

1. Does the problem (or portion of the problem) seem to make a decision and then execute a group of instructions
only once? If yes, it is probably a type of branch.

2. Does the problem (or portion of the whole problem) seem to require that a series of instructions are run
multiple times depending on some condition? If so, it is probably a loop.

Decision Structures / Branches


1. Is the condition, that decides which instructions are run, based on a single integer or character value? If so,
consider using a switch.

2. Does the problem seem to be related to one value falling into a range of different values which controls the
instructions that are to be run? If so, consider using an if-else if construct.

3. Does the problem seem to do one thing if the condition is true and another thing if the condition is false? If
so look at a simple if-else construct.

4. Does the problem seem to do something if a condition is true and nothing if the condition is false? If so look
at a simple if construct.

Loops

1. Does the problem decide when to terminate based on some special value input from the user or by
detecting some special condition (e.g. the end of a file if you are file processing)? If yes, then it is best to
solve the problem with a while or do-while construct.

//priming read
while (amount !=0)
{
//body of instructions to be executed over and over again
// secondary read
}

2. Does the problem seem to be based on the value of a variable going up in a very specific increment (eg by 1
or by 10, etc.) and the calculations within the loop are based on that value ? If so, a for loop will yield the
best solution.

for (i=1 ; i <= 10 ; i++)


{
// code to be executed, typically you would use the loop variable (i) in here
}
CMPP269 Module 1 -5 Chapter 1-5 Review

Escape Characters

\b – backspace
\n – new line
\t – tab
\r – carriage return
\f – formfeed
\\ - to put a \ in a string
\” - to put a ” in a string

Printing Output

System.out.print(String) – print a String

System.out.println(String) – prints a String and goes to the next line

Use concatenation operator + to join multiple part into one output string.

Formatting Output

System.out.printf (FormatString, ArgumentList)


stringName = String.Format(FormatString,ArgumentList)

Arguments in the ArgumentList are separated by ,


Every argument needs its own format specifier in the FormatString

FormatString Syntax: %[flags][width][.precision]conversion


e.g. %-10s formats a String in a width of 10 and left-justified
%+10.2f formats a floating point number in a field width of 10 with 2 positions after the decimal and both +
and – signs printed
%6d format a decimal integer in a field width of 6

Flags:

- left justify
+ outputs a + or – for numerical values
0 forces numerical value to be zero padded
, grouping separator for number >1000
space will display a minus sign if number is negative or a space if it is positive.

Conversion Characters:

d decimal-integer
f floating point
c character
C uppercase the letter
s String
S Uppercase all the letters in a string
n newline – use %n instead of \n for greater platform compatibility
CMPP269 Module 1 -5 Chapter 1-5 Review

Static vs instance methods – difference and how to call

void methods, value returning methods

Java Class Class Name Methods


Library

java.lang String char charAt(int index)


int compareTo(String anotherString)
int compareToIgnoreCase(String str)
String concat (String anotherString)
boolean contains(CharSequence s)
boolean endsWith(String suffix)
boolean equals(String anotherString)
boolean equalsIgnoreCase(String anotherString)
int indexOf(int ch)
int indexOf(int ch, int fromIndex )
int indexOf(String str)
int indexOf(String str, int fromIndex )
int lastIndexOf(int ch)
int lastIndexOf (int ch, int fromIndex )
int lastIndexOf (String str)
int lastIndexOf (String str, int fromIndex )
int length()
boolean startsWith(String suffix)
String substring (int beginIndex)
String substring (int beginIndex, int endIndex )
String toLowerCase()
String toUpperCase()
String trim()

static String format(String format, Object... args)

System static long currentTimeMillis()

Math static double abs(double a)


static float abs(float a)
static int abs(int a)
static long abs(long a)
static double acos(double a)
static double asin(double a)
static double atan(double a)
static double ceil(double a)
static double cos(double a)
static double exp(double a)
static double floor(double a)
static double log(double a)
static double log10(double a)
static double max(double a,double b)
static float max(float a,float b)
static int max(int a, int b)
static long max(long a, long b)
CMPP269 Module 1 -5 Chapter 1-5 Review

Java Class Class Name Methods


Library

java.lang Math static double min(double a,double b)


static float min(float a,float b)
static int min(int a, int b)
static long min(long a, long b)
static double pow(double a, double b)
static double random()
static double rint(double a)
static long round(double a)
static int round(float a)
static double sin(double a)
static double sqrt(double a)
static double tan(double a)
static double toDegrees(double angrad)
static double toRadians(double angdeg)

Byte static byte parseByte(String s)

Character static boolean isDigit(char ch)


static boolean isLetter(char ch)
static boolean isLetterOrDigit(char ch)
static boolean isLowerCase(char ch)
static boolean isUpperCase(char ch)
static char toLowerCase(char ch)
static char toUpperCase(char ch)

Double static double parseDouble(String s)

Float static float parseFloat(String s)

Integer static int parseInt(String s)

Long static long parseLong(String s)

Short static short parseShort(String s)

java.util Scanner String next()


boolean nextBoolean()
double nextDouble()
float nextFloat()
int nextInt()
long nextLong()
short nextShort()
byte nextByte()
String nextLine()

Random int nextInt(int bound)

You might also like