Académique Documents
Professionnel Documents
Culture Documents
VARIABLES
CLASS is like a frame. Different Objects can use the same building frame as a reference.
When defining a class we define the DATA part and the Behaviour (METHODS) part
We begin with the DATA
METHODS
Methods are Public because
Method is followed by the information on the DATA (data type and nameOfThe Method() - the ()
signifies that Im not providing any data to this method ) it RETURNS. Think of data like a
function which only returns value when you actually put data in it.
INSTANTIATION
OBJECT IS:
PREVIOUSLY DEFINED CLASS NAME_NAME OF THE OBJECT WE WANT = CALLING THE
CONSTRUCTOR(new ASIGNMENT OPERATOR)_PREVIOUSLY DEFINED CLASS
NAME(data, data, data);
NAME OF THE OBJECT WE WANT - will be the instance of the previously defined class named
in some new way
THE POINT THE POINT THE POINT THE POINT THE POINT THE POINT THE POINT
OF CLASSES CONSTRUCTORS GETTERS AND SETTERS
Arithmetic
int to double
NEVER GO FROM A WIDE DATA TYPE TO A NARROW DATA TYPE because you will use
precision or data along the way and Java will show an error message
Java Input and Output
printf
Input Scanner
Example1
Example2
Input strings
Example1
If you want to input a whole name you will need to use a different method
Example 2
LOGICAL OPERATORS
Important example
REPETITION
While loop - Count controlled
BREAK
EXAMPLE2 -nice
Calling a method
Parameter and Argument have to match in data type and in number (if you need one integer put
into the Argument it has to match the number of integers your Method handles)
Fahrenheit to Celsius converter
Method with 2 parameters but only ONE return statement GETS spit out
Example
Example
ArrayList
Store multiple values under the same name
Example without an array list
Accessing elements from the array list with the get method
Example2
Random numbers
Data members
A Public data member is accessible by any program that has access to the class.
Public means these data members can be set to any value for that data type.
Private means you can control what values ged assigned to that data member and private also
means you can change hours, minutes and seconds only within the class and NOT from the
main program.
Implementing Constructors
Constructors are used to initialize data members to values such as 0 or values that user
specifies when extentiating (the process of creating and initializing a class object )an object.
Constructor is a method which has the same name as a class.
Displaying class data (current state of data) with toString() - great for debugging purposes
Another example of displaying data (same code as the previous one just a small mod)
Inheritance
Base Class or Super Class
Manager class is a sub-class of the Super Class
We first create the super class
Manager class is extending the class from above but also inherits all the getters and setters and
the rest from the super class
OVERRIDING METHODS
Protected members - The class (Super class) and Manager class which extends it have direct
access to protected members
Object class - it is on the top of the hierarchy
Polymorphism
Sub classes of Superclasses can have methods of the same name performing different
operations
Abstract classes
To save a Super class from being extentiated is by defining it as abstract. Abstract class can be
inherited from.
When you create a graphic program and you want to create a shape, you actually want to
create a TYPE of shape and not just any shape. So extentiating a Shape class does not make a
lot of sense. We make a class abstract by:
Example
We can also make abstract methods. Abstract method tells the Manager class to inherit a
method from Super class but not necessarily the BODY of the method.
Interface class - Interface class is just a class which has method names only, but no method
definitions. When you inherit from an interface, you are saying - my class is going to have these
features and then in that inherited class (Manager) you are going to define how those features
work.
Interfaces - Interface is a contract stating that you are going to define a list of methods in the
interface and that any class that tries to use the interface (or implement) must define those
methods listed in the interface definition.
Example of interface
Example of polymorphism
Array
Array size must be declared at the beginning and its size, once declared can never change.
Array is very fast and is native to Java.
There are 2 ways for declaring an array:
1 - declare the array and specify the size
2 - declare the array with the elements you want to store in the array
1
If the compiler does not catch an error but the Java runtime catches it - it is called a runtime
error
Passing Arrays as function arguments
0 % 10 = 0
Do while loop - The statements inside the loop are going to be executed at least one time before
it can stop
Attention
If there is no good reason to use the do while loop, use the while loop
For Each loop - use them instead for loops whenever possible
For each loop differs from a for loop in that we do not provide a loop control variable that
controls how many times the loop runs. We supply the for each loop vith a variable and a
collection and each element of the collection is put into the variable until we run out of elements
of the collection. For each loop has to go through all the elements of the collection whereas For
loop can be stopped at some point.
Example2
Exception handling
Example
Read characters from one file and write them to a different file
The code