READING ASSIGNMENT IntroductionToJava ClassesVariablesDataTypesOperators
READING ASSIGNMENT IntroductionToJava ClassesVariablesDataTypesOperators
OBJECT ORIENTED PROGRAMMING (CONT’D) OBJECT ORIENTED PROGRAMMING (CONT’D) OBJECT ORIENTED PROGRAMMING (CONT’D)
You can traverse to the lower objects using dot notation Objects can also have several properties Properties
To traverse to the Hand you will…
Person.Body.Arm.Hand Properties Head.Size = Large
Size = Medium
Person Head.Color = Green
Color = Red
Body Shape = Round Head.Shape = Square
Hand is a property Head
Head
of Arm
Arm
Traverse: These properties can be read and sometimes Read Person.Head.Color
changed
To travel through, taking
Hand the right path
Green
unit
CREATING A JAVA APPLICATION (CONT’D) CREATING A JAVA APPLICATION (CONT’D) CREATING A JAVA APPLICATION (CONT’D)
A project is a group of programming source files (code) and Now give your project a Make sure your name has no spaces, special characters (e.g.
name and browse to a $^&*@-/), and it must begin
the settings with which you build, run, and debug those
location to save it (your with a letter (not a number)
source files
folder) When you look at the IDE you are seeing:
Once your have selected this, a dialog box will appear to Then click Finish Files window: shows a directory-based view of your projects
determine the type of project you wish to create (a template) where you can open and edit your project configuration files
You will see this
For now we will pick screen Runtime window: displays information during project
Java Java runtime Navigator Window
Application Navigator window: displays information about the java
source files in the currently opened project
Then click Next >
Source Editor: is a full-featured text editor that is integrated
with the GUI Builder, Projects window, compiler, and
debugger
Main source
RUNNING A JAVA APPLICATION anywhere in the code,
errors will instead be
code editor generated and
displayed in the
Runtime runtime window
window
The “Hello World” program is traditionally the first program learned in The first lines of code (and all code shaded this way) is a Java comment
Computer Science because it gives the basics of any computer program (more on comments later)
If we look at the various code statements we can get an understanding of
what’s going on
Add to the code already
“// TODO code application logic here”
Add a new line here…
“HELLO WORLD” IN JAVA (CONT’D) CLASS NAMES… “HELLO WORLD” IN JAVA (CONT’D)
This line is the class
definition for the By convention, all class names in Java begin with a
class “Main” capital letter and have a capital letter for every word
Every program in Java consists of at least one class definition that is in the name
defined by ‘you’ the programmer For example:
These classes are therefore defined as “User-defined classes” (similar SampleClassNameDefined
to the concept of user-defined procedures and functions discussed in
earlier units) This name is called an identifier
The keyword public states that this class is public to the application Identifiers can contain letters, numbers, underscore The next lines are a collection of comments and
The keyword class defines this line as a class definition (_), and dollar signs ($) but cannot begin with a something called a constructor
The word Main is the user-defined name for this class, notice the number (public Main) which we will cover in later units
color coding of keywords, all Java keywords are always typed in Identifier names are case sensitive, A1 is different
lowercase letters this year
from a1
For now, we will just accept these lines and move on…
This line of code is part of every Java application COMMENTS AND GOOD
Java applications (like all applications) begin execution at main This line instructs the computer to perform an action PROGRAMMING STYLE
The brackets following this line define it as a method In this case, to print – println() – a string of text
A method is a function for the class
(contained within the quotations)
COMMENTS AND STYLE COMMENTS AND STYLE (CONT’D) COMMENTS AND STYLE (CONT’D)
Comments are important in Java just as in any programming Method Style is important, good programming standards must be adhered to for
language two… your Java assignments, and are normal professional standards
There is two ways we will use to comment in Java (there is also a The /* Using comments after curly brackets { is an example of this to help
third we will not be discussing) programmers track which bracket matches up with which
opens the
Method The following standards are expected for your Java applications and
comment,
one… applets you will be writing (some will become more clear as we cover
the */
these topics):
closes it
1. Each of your program files must begin with
anywhere in your code a comment
Comments are ignored by the compiler block like the
They are used for the benefit of the programmer following:
COMMENTS AND STYLE (CONT’D) COMMENTS AND STYLE (CONT’D) COMMENTS AND STYLE (CONT’D)
2. Insert a prologue comment at the beginning of each method 7. Use meaningful but reasonable variable names 9. Avoid the use of literal constants ("magic numbers") in your
(function) after the method declaration, which contains: Start in lower case and capitalize only the first letter of program
Parts of the prologue may
each new word, for example: Generally use constant (final) identifiers rather than
be omitted if appropriate,
e.g. if the method has no
literal constants in your program
int a; // Very bad, too short, not meaningful
parameters.
int averageMark; // Good
int average_of_all_the_marks_in_the_list; // Bad, too long and wordy
Acceptable exceptions are strings that appear only
once in output titles and headings, and small
3. Use blank lines to separate blocks of code and declarations to fundamental values, for example:
improve readability 8. Additional documentation of variables should appear in a
4. Provide a comment for each major loop and other major control comment following the variable declaration (note that this sum = 0;
count = count + 1;
// Literal constant 0 is OK
// Literal constant 1 is OK
structures requires one variable declaration per line), for example: price = total * 1.07; // No create a named constant like:
final double PST_RATE = 1.07;
5. Comment blocks of code to describe why you wrote the code this double maxFlow; // in river channel [m3/s]
lastDigit = accountNumber % 10; // Literal constant 10 is OK
if(codeLetter == 'R') // No create a named constant like:
way, not what each line does int swapCount; // 0 swaps needed if data is sorted final char REFUND_CODE = 'R';
6. Avoid declaring variables "as you go"
In order to have the user involved in our programs we need to What’s happening in this line is somewhat complex, but let’s
get inputs and provide outputs examine some of the fundamentals happening…
We will be looking at graphics and GUIs this year, but later in Any code word typed in Java that starts with a capital letter and
then uses camel casing is a class
the course
This means the class has properties (things about it) we can
So our inputs/output will be more basic to start
access, and methods (actions that perform functions) we can use
The most basic output already seen We can recognize a class (“System”) and the action we have
(System.out.println()) provides simple outputs
used (“println”) to produce the action of making text appear
on the screen
System.out is the standard output object – the screen
White-space (space bar) is ignored by Java in your code lines Produces this output:
except if it is contained within quotations indicating that it is part of a Hello to the World
COMPUTER SCIENCE
Java Variables
Java is a strongly typed language, meaning A strongly typed language makes for:
that we have to watch our syntax
More compiler errors
For example…
Fewer bugs (programmers must think)
char firstLetter = 'A'; Faster code when finished
Students let us examine ...
JAVA NAMING RULES Char secondLetter = 'B'; Variables identifier names have to
follow naming rules similar to other
This will compile with an error, you cannot use a
capital and it will not fix it for you programming languages
Operators Constants
Operators are used in the same way as VB ( +, -, *, / ) Constants can also be used in Java (and should
with precedence order
be used)
1. Unary minus –x 5. Division x / y
They define a variable value that doesn’t change
2. Addition x + y 6. Modulus x % y
However, in Java, the term final is used
3. Subtraction x - y instead of const (which is used in Java’s
Students let us examine ...
4. Multiplication x * y parent language C++)
USING JAVA VARIABLES
For example…
Integer division automatically truncates the
decimal point final float PI = 3.14;
Increment & Decrement Operators Short Hand Operators Using Variables (cont’d)
Java has two somewhat special operators As well, the basic math operations can use another short-hand public class VariableExample
notation to assign values to variables in a quicker (less-typing) {
The increment ++ and decrement -- way, for example… public static void main(String args[])
operators, for example… int x = 10, y = 7; {
int x = 6, y = 15, z = 0;
int moreCount = 0; x += y; //same as x = x + y char ch1 = 'L', ch2 = 'a', ch3 = 'r',