Week 1 Topics - Franklin University

Download Report

Transcript Week 1 Topics - Franklin University

Week 2
Introduction to Computer Science
and Object-Oriented Programming
COMP 111
George Basham
Week 2 Topics
2.1.1 Types
2.1.2 Variables
2.1.3 The Assignment Operator
2.1.4 Objects, Classes, and
Methods
2.1.5 Method Parameters and
Return Values
2.1.1 Types
•
•
•
•
In Java, every value has a type
Examples:
“Hello World” has the type String
The object System.out has the type
PrintStream
• The number 13 has the type int (integer)
2.1.1 Types Cont.
• Java has separate types for integers and
floating-point numbers
• When a floating-point number is divided or
multiplied by 10, only the position of the
decimal point changes; it “floats”
• Use the type double to represent a
floating-point number such as 1.3 or
-0.33333
2.1.1 Types Cont.
• Use an integer (int) for quantities that can
never have a fractional part
• Advantage of an int is that they take less
storage space, are processed faster, and
don’t cause rounding errors
• There are several other number types
(less commonly used than int or double)
that we cover in Week 5
2.1.1 Types Cont.
• Do not use commas when writing numbers
in Java. For example, 13,000 must be
written as 13000
• To write numbers in exponential format,
use En. For example 1.3 x 10 to the -4
power (.00013) is written as 1.3E-4.
• In Java numbers are not objects and
number types are not classes
• Number types are called primitive types
2.1.1 Types Cont.
• You can combine numbers with operators
such as + or • To multiple two numbers use the *
operator, to divide use /
• * and / have higher precedence than + or –
• If x = 2 and y = 3, then x + y * 2 = 8
• Use parentheses to change the
precedence: (x + y) * 2 = 10
2.1.2 Variables
• Use variables to store values that you
want to use at a later time (a variable is an
alias for a memory location)
• int luckyNbr = 13;
• System.out.println(luckyNbr);
• Above is the same as
System.out.println(13);
2.1.2 Variables Cont.
• An identifier is the name of a variable,
method or class, and the following are firm
rules that Java enforces:
• Identifiers can be made up of letters, digits
and the underscore character, but cannot
start with a digit. 1greeting is not legal.
• You cannot use other symbols such as ?
or %. hello! is not legal.
2.1.2 Variables Cont.
• Spaces are not permitted inside identifiers.
lucky Nbr is not a legal identifier.
• You cannot use Java reserved words such
as public
• Identifiers are case sensitive, thus,
greeting, GREETING and Greeting are
different
2.1.2 Variables Cont.
• There are some conventions that you
should follow also (even though will
compile if you violate):
• Variable and method names should start
with a lowercase letter, such as
luckyNbr
• Class names should start with an
Uppercase letter. Examples: Employee,
Point, PrintQueue
2.1.2 Types Variables Cont.
• When deciding on a name for a variable,
you should make a choice that describes
the purpose of the variable. For example,
the variable name greeting is a better
identifier than the name g.
2.1.3 The Assignment Operator
• Use the assignment operator (=) to
initialize the value or change the value of a
variable
• int luckyNbr = 13;
• To change the value of the variable,
simply assign the new value:
• luckyNbr = 12;
2.1.3 The Assignment Operator Cont.
• It is an error to use a variable that is not
initialized
• int luckyNbr;
• System.out.println(luckyNbr);
// NO!
• We will cover this topic later on in the
course, but note the assignment operator
is NOT used to test for equality
2.1.4 Objects, Classes, and Methods
• A class is a model of an entity needed to
address a problem (domain class)
• Examples: Employee, Encryptor,
PrintQueue, Point,
PurchaseOrder
• A class defines the attributes (data) and
the actions (methods) of the entity
• The attributes (data) of a class are nouns:
firstName, message, xCoordinate
2.1.4 Objects, Classes, and Methods Cont.
• The actions (methods) of a class are
verbs: setName, getXcoord,
encryptMessage
• Methods are routines (called functions in
some languages) that contain logic
• A method is a sequence of instructions
that accesses the data of an instance of a
class
2.1.4 Objects, Classes, and Methods Cont.
• An object is a specific instance of a class
• Think of a class as a mold, and an object
as the manufactured widget
• A method may or may not receive input
• The format for calling a method is:
objectVariable.methodName(inputs)
2.1.4 Objects, Classes, and Methods Cont.
• String greeting = “Hello
World!”;
• The class is type String, the object is
pointed to by the variable greeting
• int n = greeting.length();
• length is a method defined by the String
class
• The value of n is 12
2.1.3 Objects, Classes, and Methods Cont.
• Note that the method is called on the
object variable: greeting.length()
• The String class length method does not
receive input
• System.out.println(“Hello
World!”);
• The println (print line) method does
receive input
2.1.4 Objects, Classes, and Methods Cont.
• String river = “Mississippi”;
• String bigRiver =
river.toUpperCase();
• bigRiver will contain value
“MISSISSIPPI”
• System.out.length(); // NO!
• The PrintStream class (which System.out
belongs to) does not contain a length
method
2.1.4 Objects, Classes, and Methods Cont.
• Every object belongs to a class
• The class defines the methods for an
object
• The methods form the public interface for
the class
• A class also defines a private
implementation, describing the data and
the instructions in the methods, these are
hidden from the programmer (black box)
2.1.5 Methods Parameters and Return
Values
• A parameter is an input to a method
• System.out.println(greeting);
• The string greeting is a parameter to
the println method
• The implicit parameter of a method call is
the object on which the method is invoked
• The System.out PrintStream object is an
implicit parameter of println
2.1.5 Methods Parameters and Return
Values Cont.
• The return value of a method is a result
that the method has computed for use by
the code that called it
• int n = greetings.length();
• You can also use the return value as a
parameter of another method
• System.out.println(greeting.len
gth());
2.1.5 Methods Parameters and Return
Values Cont.
• Not all methods return a value, such as
println
• String newRiver =
river.replace(“issipp”, “our”);
• The above method call has one implicit
parameter (“Mississippi”, the value of
river), two explicit parameters, and the
return value “Missouri”
2.1.5 Methods Parameters and Return
Values Cont.
• When a method is defined in a class, the
definition specifies the types of the explicit
parameters and the return value
• public int length()
• public String replace(String
target, String replacement)
• A method name is overloaded if a class
has more than one method with the same
name but different parameter types
2.1.5 Methods Parameters and Return
Values Cont.
• public void
output)
• public void
• public void
output)
• public void
println(String
println(int output)
println(double
println()
Reference: Big Java 4th Edition by Cay
Horstmann
2.1.1 Types (section 2.1 in Big Java)
2.1.2 Variables (section 2.2 in Big
Java)
2.1.3 The Assignment Operator
(section (section 2.3 in Big Java)
2.1.4 Objects, Classes, and Methods
(section 2.4 in Big Java)
2.1.5 Method Parameters and Return
Values (section 2.5 in Big Java)