0% found this document useful (0 votes)
2 views

Java

The document provides an overview of Java programming concepts, including variable types, operators, loops, methods, and object-oriented programming principles. It covers essential topics such as primitive and reference variables, ArrayLists, and classes, along with programming tips for writing clean code. Additionally, it discusses the distinction between static and instance methods in Java.

Uploaded by

Angel Ciobotaru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java

The document provides an overview of Java programming concepts, including variable types, operators, loops, methods, and object-oriented programming principles. It covers essential topics such as primitive and reference variables, ArrayLists, and classes, along with programming tips for writing clean code. Additionally, it discusses the distinction between static and instance methods in Java.

Uploaded by

Angel Ciobotaru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

JAVA

1
2
Types of variables
 String - text
 int - whole numbers
 double - floating-point numbers
 boolean - true or false

int value = Integer.valueOf(scanner.nextLine());

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

(double) in fata la un int o face double

8
while

9
for

10
New method

11
Method Parameters

12
Return method

13
ArrayList

Add it to the program:


- import java.util.ArrayList;
Creating a new list
- ArrayList<Type> list = new ArrayList<>()
{ ArrayList<Integer>/ArrayList<Double> }

Adding to the list

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

An Object refers to an independent entity that contains both


data (instance variables) and behavior (methods). Objects may
come in lots of different forms:
-some may describe problem-domain concepts, others are used
to coordinate the interaction that happens between objects.

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

Can’t have two constructors with the exact same parameters

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.

In the second example we must use publicString toString() {


return this.name
} in order for sout(luke) to work as intended.

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

Objects in an Instance Variable List

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.

Methods in Java can be divided into two groups, based on


whether they have the static modifier or not. Methods without
the static modifier are instance methods. Methods with the static
modifier are class methods.

Instance methods are methods that are associated with an object,


can process the objects variables and can call the object's other
methods. Instance methods specifically CAN use the this
modifier, which refers to the variables associated with the
specific object, that is calling the instance method. Class
methods can't use the this modifier, meaning that they can only
access the variables they are given as parameters or that they
create themselves.

In reality class methods can also access class variable, among


other things. However, these things are outside the scope of this
course.

55
Built-in sorting algorithms in Java (sort)

56

You might also like