Beruflich Dokumente
Kultur Dokumente
(continued)
Mr. Smith
AP Computer Science
A
Basic Java
Syntax and Semantics
Methods, Messages, and Signatures
Classes implement methods, and objects
are instances of classes.
Objects that share common behavior are
grouped into classes.
An object responds to a message only if its
class implements a corresponding method.
To correspond the method must have the
same name as the message.
Messages are sometimes accompanied by
parameters and sometimes not:
pen.move(); // No parameter expected
pen.moveDown(8); // One parameter expected
Java Concepts 2.3 (Objects, Classes, and Methods), 2.4 (Method Parameters and return values)
Basic Java
Syntax and Semantics
Some methods return a value and
others do not.
To use a method successfully we
must know:
What type of value it returns
It’s name (identifier)
The number and type of the parameters
it expects
This information is called the
method’s signature.
Java Concepts 2.3 (Objects, Classes, and Methods), 2.4 (Method Parameters and return values)
Basic Java
Syntax and Semantics
Programming Protocols: Use
camelCase
When forming a compound variable
name, programmers usually capitalize
the first letter of each word except the
first.
(For example: taxableIncome)
All the words in a program’s name
typically begin with a capital letter
(ComputeEmployeePayroll).
Constant names usually are all
uppercase (TAX_RATE). Java Concepts 2.1 (Types and Variables)
Comments
Comments are explanatory
sentences inserted in a program in
such a matter that the compiler
ignores them.
There are two styles for indicating
comments:
End of line comments:
These include all of the text following
a double slash (//) on any given line; in
other words, this style is best for just
one line of comments
Multiline comments:
These include all of the text between
Java Concepts 1.6 (Compiling a Simple Program)
absNum = Math.abs(-30);
powerNum = Math.pow(-3, 3);
randomNum = Math.random();
Results:
absNum = 30 //Absolute value of -30
powerNum = -27 //-3 to the 2nd power
randomNum = ????? //Random number between 0
and 1
Java Concepts 4.4 (Arithmetic Operations and Mathematical Functions)
Standard
Classes and Methods
Random Numbers and Simulation
The Random class of the Java library implements
a random number generator.
To generate random numbers, you construct an
object of the Random class and then apply one of
the following methods:
nextInt(n) – returns a random integer between 0
(inclusive) and n (exclusive)
nextDouble() – returns a random floating point
number between 0 (inclusive) and 1 (exclusive)
For example, if a contestant on “Deal or No Deal”
is randomly selecting their briefcase from the 26
initial briefcases:
import java.util.Random; //Include this at top of
program
Random generator = new Random();
int briefcaseNum = 1 + generator.nextInt(26);
Java Concepts 6.5 (Random Numbers and Simulation)
Classwork/Homework
DiceRoller program (similar to Yahtzee):
Write a program to:
Allow the person to initially roll five dice.
Each die has six faces representing numbers from
1 to 6.
Print the results of each die to the console.
After the first roll, the person should input the
number of dice (between 0 and 5) that they want
to roll again.
If they enter 0, then stop.
Else, roll that many dice and print the results of
each die to the console again.
Repeat the prompt one more time to see if the
person wants to roll some of the dice again (for a
maximum of 3 rolls).
Note: You may want to use the Scanner class to input
data, the Random class to generate a random number,
a for loop for rolling the dice, an if statement for
checking the input and a break command to exit the
Java Concepts 6.5 (Random Numbers and Simulation)
Control Statements
While and if-else are called control
statements.
while (some condition) if (some condition)
{ {
do stuff; do stuff 1;
} }
else
{
do stuff 2;
}
if (condition)
{ statement1;
statement2;
}
else
{ statement3;
statement4;
} Java Concepts 5.1 (The if Statement)
The if and
if-else Statements
Relational Operators
The complete list of relational operators available for use
in Java.
int x;
if ( x > 10 )
if ( x = 10 ) Incorrect
if ( x >< 10 ) Incorrect
if ( x == 10 )
if ( x >= 10 )
if ( x != 10 )
OPTION 1:
int num;
boolean done = false;
while ( ! done)
{
System.out.print(“enter a value, 99 to end “);
num = in.nextInt();
if (num == 99)
{
done = true;
}
else
{
// rest of code to do something goes here
}
}
OPTION 2:
int num;
while (true)
{
System.out.print(“enter a value, 99 to end “);
num = in.nextInt();
if (num == 99)
{
break;
}
// rest of code to do something goes here
}