Java
Java
1
2
Types of variables
String - text
int - whole numbers
double - floating-point numbers
boolean - true or false
if
3
else
else if
Comparision operators
4
Odd or even (modulo %)
1%2 ==> 1 deci este odd
2%2 ==> 0 deci este even
5%2 ==> 1 deci este odd
9%3 ==> 0 deci este even
Ca un numar sa fie divizibil de exemplu cu 3 modulul lui
trebuie sa fie egal cu zero
Astfel ca sa aratam ca un numar e divizibil cu 3 avem:
(number % 3 == 0 )
Logical Operators
5
&& - and
|| - or
! – not
An expression consisting of two expressions combined using the
and-operator is true, if and only if both of the combined
expressions evaluate to true.
An expression consisting of two expressions combined using the
or-operator is true if either one, or both, of the combined
expressions evaluate to true.
Logical operators are not used for changing the boolean value from
true to false, or false to true.
&& example:
|| example:
6
! example:
7
Transform a integer to a double
Int sum = 0;
Int numbers = 0;
Double average = 0;
Average = (double) sum / (double) numbers
8
while
9
for
10
New method
11
Method Parameters
12
Return method
13
ArrayList
14
Getting values from the list:
15
Indexing
16
Loops with arrays (get, for )
.get () incepe de la 0
17
.remove
18
.contains
19
Recap for ArrayList
20
Array ( incepe de la zero pana la -1 numaru max)
Creating an array
21
Size of an array and iterating
22
Array recap
23
Strings
Splitting a string
24
.contains
.length
25
Usefull stuff:
.charAt()
26
.toLowerCase()
.trim()
Sanitized Strings
makes a string insensitive to upper/lower cases plus spaces
27
Sum of elements
28
Average of numbers
29
Classes
30
Defining a Constructor
31
32
Returning a value From a Method
33
34
35
toString-method
36
Reading from a file
import java.util.Scanner;
import java.nio.file.Paths;
37
Reading from a file then adding it to an
ArrayList
38
Reading Data of a Specific Format From a File (CSV type)
39
Reading objects from a file
40
OOP (object-oriented programming) - a program is built from small and
distinct objects that work together
41
42
Class
A class defines the types of objects that can be created from it. It
contains instance variables describing the object's data, a
constructor or constructors used to create it, and methods that
define its behavior. A rectangle class is detailed below which
defines the functionality of a rectangle.
43
Constructor Overloading
44
Calling Your Constructor
45
Method Overloading
46
Primitive and reference variables
Variables in Java are classified into primitive and reference variables. From the
programmer's perspective, a primitive variable's information is stored as the
value of that variable, whereas a reference variable holds a reference to
information related to that variable. reference variables are practically always
objects in Java. Let's take a look at both of these types with the help of two
examples.
47
Primitive Variables
Java has eight different primitive variables. These are: boolean (a truth
value: either true or false),
byte (a byte containing 8 bits, between the values -128 and 127),
char (a 16-bit value representing a single character),
short (a 16-bit value that represents a small integer, between the values -
32768 and 32767),
int (a 32-bit value that represents a medium-sized integer, between the
values -231 and 231-1),
long (a 64-bit value that represents a large integer, between values -
263 and 263-1),
float (a floating-point number that uses 32 bits), and
double (a floating-point number that uses 64 bits).
48
Comparing objects
49
Object as a method's return value
50
Clearing an Object's List
We'll next add a removeEveryoneOnRide method to the amusement park
ride, which removes each and every person currently on the ride.
The list method .clear is very handy here.
51
52
Retrieving a Specific Object from a List
53
Programming tips
In the larger example above, we were following the advice given here.
Proceed with small steps
o Try to separate the program into several sub-problems and work
on only one sub-problem at a time
o Always test that the program code is advancing in the right
direction, in other words: test that the solution to the sub-problem
is correct
o Recognize the conditions that require the program to work
differently. In the example above, we needed a different
functionality to test whether a word had been already entered
before.
Write as "clean" code as possible
o Indent your code
o Use descriptive method and variable names
o Don't make your methods too long, not even the main method
o Do only one thing inside one method
o Remove all copy-paste code
o Replace the "bad" and unclean parts of your code with clean code
If needed, take a step back and assess the program as a whole. If it
doesn't work, it might be a good idea to return into a previous state
where the code still worked. As a corollary, we might say that a
program that's broken is rarely fixed by adding more code to it.
Programmers follow these conventions so that programming can be made
easier. Following them also makes it easier to read programs, to keep them
up, and to edit them in teams.
54
Static or not
At the start of the course, all of our methods included the static
modifier, but when we started using objects, use of the static
modifier was banned.
55
Built-in sorting algorithms in Java (sort)
56