Vous êtes sur la page 1sur 80

Programming Java for Beginners

Infinite skills - notes

Java files have to begin with a capital letter like HelloWorld.java


Every java program is IN ITSELF A CLASS
CLASS name in the file has to be the same as the FILE NAME ITSELF
Above public static void main is the so called Global area

Numeric Data types

String is a CLASS and not a built in type

VARIABLES

Variables should begin with lowercase letters


save the Upper case letters for Classes

you can comment with


//comment
or
/* blah
blah comment */

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.

VARIABLES are sometimes called MEMBERS and FIELDS ?!! WTF

CONSTRUCTOR is just another type of METHOD, CONSTRUCTOR METHOD - is always


PUBLIC and always has the SAME NAME as the Class

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

To print out a result


Assigning DATA to a Class OBJECT

THE POINT THE POINT THE POINT THE POINT THE POINT THE POINT THE POINT
OF CLASSES CONSTRUCTORS GETTERS AND SETTERS

Arithmetic

CONSTANT = FINAL in JAVA (variable which cant be changed)

Special Math functions with MATH Class

int to double

but it does not work backwards

char to num returns an ASCI value

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

Uniform distribution of TAB spaces

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

A SIMPLE QUESTION AND ANSWER PROGRAM

DECISION MAKING WITH COMPARISON OPERATORS = RELATIONAL OPERATORS


==

LOGICAL OPERATORS

nested if-else statement is always WORSE choice than if-else-if

Important example

REPETITION
While loop - Count controlled

While loop - Condition controlled

While loop - Results controlled

For loop - runs a specified number of times

AVOID THE OPERATION WITH VARIABLE BELOW!

AVOID THE OPERATION WITH VARIABLE BELOW!

CONTINUE and BREAK statements - USE SPARINGLY


CONTINUE

BREAK

EXAMPLE - very nice

EXAMPLE2 -nice

METHOD = function in other language


Method is a named set of functions which can be called as a named operation. It takes input
and spits out output
Defining a method
see also here the definition of VOID method

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

PREDICATE Methods return True or False based on a condition

Example

VOID Method does not return a value

but check out this Void method cool trick

PASS BY VALUE RULE


Arguments value never changes the initial variables value permanently. It just passes the
variables value COPY to the method. The proof for this is below.

ArrayList
Store multiple values under the same name
Example without an array list

Example with an array list

Initializing an array list

size() function returns us the number of ITEMS in the array list


add(5) adds the value 5 to the array list

Accessing elements from the array list with the get method

Passing Array lists as method arguments


Example1

Example2

Random numbers

Searching elements in the string


indexOf ()

To check if the item is in the list at all - we do that with


contains () is a boolean method that returns true if the array contains the element and returns
false otherwise

Searching for a minimal value

Sorting an Array list

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

Another example of displaying data (same code as the previous one just a small mod)

Set and Get methods

Example of working with classes

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

How that looks like in the main program

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

Implementing the shape 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

Accessing Array elements

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

Two dimensional array

But in practice it is better to define it this way

Initializing two dimensional arrays


Example

The Switch statement example

Switch statement trick or the drop through

Do while loop - The statements inside the loop are going to be executed at least one time before
it can stop

Attention

Difference between while and do while loop

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

Writing data to a file

Example

Reading data from a file

Reading data from a file 2

Appending (adding) data to an existing file

Writing characters sentences to a file

Read characters from one file and write them to a different file

The code

Writing a To-Do list program

To-Do list first part

To-Do list second part - showlist

To-Do list third part - additem

To-Do list 4th part - removeitem

Vous aimerez peut-être aussi