View
219
Download
0
Embed Size (px)
Citation preview
8/10/2019 Presentacin 1 - Java 1
1/51
Java Basics I
Christian Rodrguez Bustos
Object Oriented Programming
Java Basics
8/10/2019 Presentacin 1 - Java 1
2/51
Last Class
Exercise
Why Java?
Java programsstructure
Java basicapplications
Agenda
Java Basics
8/10/2019 Presentacin 1 - Java 1
3/51
1. Do the Eclipse HelloWord!!or NetBeans HelloWord!!
2. Modify the Multidimensional array use example code in
order to:
print the main diagonal of the next two multidimensional
arrays
Last Class Exercise
Java Basics
1 2 3
4 5 6
7 8 9
a b c d
e f g h
i j k l
m n o p
Numbers
array
Letters
array
http://www.youtube.com/watch?v=UGmhks4K13ghttp://www.youtube.com/watch?v=NzG_1yrTl64http://www.youtube.com/watch?v=NzG_1yrTl64http://www.youtube.com/watch?v=NzG_1yrTl64http://www.youtube.com/watch?v=NzG_1yrTl64http://www.youtube.com/watch?v=UGmhks4K13ghttp://www.youtube.com/watch?v=UGmhks4K13g8/10/2019 Presentacin 1 - Java 1
4/51
Java Basics
Char Array
Int Array
8/10/2019 Presentacin 1 - Java 1
5/51
Method for
printing int
arrays
Method for
printing
chararrays
8/10/2019 Presentacin 1 - Java 1
6/51
Java advantagesJava acronyms
Why Java?
Java Basics
8/10/2019 Presentacin 1 - Java 1
7/51
Java Is
ArchitectureNeutral
Java ProvidesOne-StopShopping
Java Is Object-Oriented from
the Ground Up
Practice MakesPerfect
Java Is an Open
Standard
Java Is Free!
Java advantages
Java Basics
8/10/2019 Presentacin 1 - Java 1
8/51
Java Is Architecture Neutral
Java Basics
Java compilerfor Windows
Java compilerfor Linux
Java compilerfor Solaris
C++ compilerfor Windows
C++ compilerfor Linux
C++ compilerfor Solaris
Platform
independentC++ source
code
Platform
independentJava source
code
Platform independent bytecode
(.class file)
Windows version Linux version Solaris version
8/10/2019 Presentacin 1 - Java 1
9/51
8/10/2019 Presentacin 1 - Java 1
10/51
Java Provides One-Stop Shopping
Java Basics
Java language provides an extensive set of application
programming interfaces APIs)
java.io: Used for file system access
java.sql: The JDBC API, used for communicating with relationaldatabases in a vendor-independent fashion
java.awt: The Abstract Windowing Toolkit, used for GUI development
javax.swing: Swing components, also used for GUI development
And there are manymore
8/10/2019 Presentacin 1 - Java 1
11/51
Java Is Object-Oriented from the Ground Up
Java Basics
Primitive or simple data types are still just single pieces of information
Object-oriented objectsare complex types that have multiple pieces of
information and specific properties(or attributes) and behaviors(methods).
8/10/2019 Presentacin 1 - Java 1
12/51
All data, with the exception of a few
primitive types are objects.
All of the GUI building blocks windows,
buttons, text input fields, scroll bars, lists,menus, and so on are objects.
All functions are associated with objects
and are known as methods there can be
no free-floating functions as there are inC/C++.
Java Is Object-Oriented from the Ground Up
Java Basics
8/10/2019 Presentacin 1 - Java 1
13/51
Java taken the best features ofC++,Eiffel, Ada, and Smalltalk
Added some capabilities andfeatures not found in those
languages.
Features that had proven to bemost troublesome in those
earlier languages wereeliminated.
Practice Makes Perfect
Java Basics
8/10/2019 Presentacin 1 - Java 1
14/51
Java Is an Open Standard
Java Basics
8/10/2019 Presentacin 1 - Java 1
15/51
Java Is Free!
Java Basics
8/10/2019 Presentacin 1 - Java 1
16/51
Java Is
ArchitectureNeutral
Java ProvidesOne-StopShopping
Java Is Object-Oriented from
the Ground Up
Practice MakesPerfect
Java Is an Open
Standard
Java Is Free!
Java advantages
Java Basics
8/10/2019 Presentacin 1 - Java 1
17/51
JDK: Java Developer Kit:Develop and execution
JRE: Java RuntimeEnvironment: Execution
JVM: Java Virtual Machine
Java acronyms
Java Basics
8/10/2019 Presentacin 1 - Java 1
18/51
Java EE
Enterprise Edition
Java SE
Standard Edition
Java ME
Micro Edition
Java acronyms
Java Basics
8/10/2019 Presentacin 1 - Java 1
19/51
File Structure
Classes
Methods
Java programs structure
Java Basics
8/10/2019 Presentacin 1 - Java 1
20/51
A source code file holds oneclass definition
Java Basics
Same name
Put a class in a source file !!!
8/10/2019 Presentacin 1 - Java 1
21/51
A class holds one or more methods
Java Basics
Put methods in a class !!!
Method 1
Method 2
Method 3
8/10/2019 Presentacin 1 - Java 1
22/51
A method holds statements
Java Basics
Put statements in a method !!!
Method 1
Statements
Method 2
Statements
8/10/2019 Presentacin 1 - Java 1
23/51
Source
file
ClassMethod
Method
File structure
Java Basics
Put a class in a source file !!!
Put methods in a class !!!
Put statements in a method !!!
8/10/2019 Presentacin 1 - Java 1
24/51
Class definition
Java Basics
Define who can
access the class:
publicmeans
everyone canaccess it
This is a
class Class name
Closing
brace
Opening
brace
8/10/2019 Presentacin 1 - Java 1
25/51
Should be nouns, in mixedcase with the first letter of
each internal word
capitalized.
Try to keep your class namessimple and descriptive.
Use whole words, avoid
acronyms and abbreviations.
Java is case sensitive.
Class names Should be nouns
Java Basics
Good Examples:
class SoccerPlayer{}
class Person{}
Bad Examples
class XYZ{}
class PERSON{}
class soccerplayer{}
8/10/2019 Presentacin 1 - Java 1
26/51
Methods definition
Java Basics
Define who can
access the class:
publicmeans
everyone canaccess it
Return
type
Closing
brace
Opening
brace
Method
name
Parameters
to the
method
We will see
this later
8/10/2019 Presentacin 1 - Java 1
27/51
Good Examples:
private static void play(int coinValue) {}
public static void moveToRight(int steps) {}
public static void getDirection() {}
Bad Examples
public static void person() {}
public static void PLAY() {}
public static void soccerplayer() {}
Methods names Should be verbs
Java Basics
Should be verbs (behaviors), in mixed
case with the first letter lowercase,
with the first letter of each internal
word capitalized.
Th i h d h
8/10/2019 Presentacin 1 - Java 1
28/51
Is not necessarya
main method in a
class
The mainmethod is where your program start to run
Java Basics
8/10/2019 Presentacin 1 - Java 1
29/51
Comments, operators and precedenceSystem.out and System.in
Java basic applications
C d b l f d
8/10/2019 Presentacin 1 - Java 1
30/51
Comments improve readability of source code
Java Basics
A good source code do not
required comments
Most of the times ;)
What is the best comment in source code you have ever encountered
S lf l d C d d
http://stackoverflow.com/questions/184618/what-is-the-best-comment-in-source-code-you-have-ever-encountered/http://stackoverflow.com/questions/184618/what-is-the-best-comment-in-source-code-you-have-ever-encountered/http://stackoverflow.com/questions/184618/what-is-the-best-comment-in-source-code-you-have-ever-encountered/http://stackoverflow.com/questions/184618/what-is-the-best-comment-in-source-code-you-have-ever-encountered/8/10/2019 Presentacin 1 - Java 1
31/51
Self explanatory code vs Commented code
Java Basics
Self explanatory
Commented code
P d f i h i
8/10/2019 Presentacin 1 - Java 1
32/51
Precedence of arithmetic operators
Java Basics
P d f i h i
8/10/2019 Presentacin 1 - Java 1
33/51
Precedence of arithmetic operators
Java Basics
??????
P d f ith ti t
8/10/2019 Presentacin 1 - Java 1
34/51
Precedence of arithmetic operators
Java Basics
S stem o t i th " t d d" j t t t
8/10/2019 Presentacin 1 - Java 1
35/51
This stream is already open and ready to accept output
data.
Print an object
System.out.print(Object object);
Print an object using a specific format
System.out.printf(String format, Object object);
Print an object in a new line
System.out.println(Object object);
System.out is the "standard" java output stream
Java Basics
System out i th " t d d" j t t t
8/10/2019 Presentacin 1 - Java 1
36/51
System.out.print();Print without moving cursor to
the next line
System.out is the "standard" java output stream
Java Basics
System.out.println();
Print moving cursor to the next
line
System out is the "standard" java output stream
8/10/2019 Presentacin 1 - Java 1
37/51
System.out is the "standard" java output stream
Java Basics
System out is the "standard" java output stream
8/10/2019 Presentacin 1 - Java 1
38/51
System.out.printf();
Print without moving cursor to the next line
System.out is the standard java output stream
Java Basics
Printing formats: Numbers with zeros
8/10/2019 Presentacin 1 - Java 1
39/51
Printing formats: Numbers with zeros
Java Basics
This is the format
Printing formats: Letters
8/10/2019 Presentacin 1 - Java 1
40/51
Printing formats: Letters
Java Basics
This is the format
Printing formats: Dates
8/10/2019 Presentacin 1 - Java 1
41/51
Printing formats: Dates
Java Basics
This is the format
Remember
to importthe Date
Class
Printing formats: Floats
8/10/2019 Presentacin 1 - Java 1
42/51
Printing formats: Floats
Java Basics
This is the format
Printing formats resources
8/10/2019 Presentacin 1 - Java 1
43/51
See more formats on
http://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htm
Printing formats resources
Java Basics
Escape sequences
http://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htmhttp://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htmhttp://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htmhttp://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htmhttp://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htm8/10/2019 Presentacin 1 - Java 1
44/51
Escape sequences
Java Basics
Escape sequences: new line and tab examples
8/10/2019 Presentacin 1 - Java 1
45/51
Escape sequences: new line and tab examples
Java Basics
Escape sequences: slash and double quote examples
8/10/2019 Presentacin 1 - Java 1
46/51
Escape sequences: slash and double quote examples
Java Basics
System in is used to read user inputs
8/10/2019 Presentacin 1 - Java 1
47/51
System.inand Scannerclass allow us to read values
typed by the user
System.in is used to read user inputs
Java Basics
First we need to import
theScanner class
at the
beginning of our source
code file
System in: reading strings example
8/10/2019 Presentacin 1 - Java 1
48/51
System.in: reading strings example
Java Basics
Creating the
scanner
Reading
an integer
System in: reading strings example
8/10/2019 Presentacin 1 - Java 1
49/51
System.in: reading strings example
Java Basics
Creating the
scanner
Reading
a String
System in: reading posibilities
8/10/2019 Presentacin 1 - Java 1
50/51
System.in: reading posibilities
Java Basics
References
8/10/2019 Presentacin 1 - Java 1
51/51
[Barker] J. Barker, Beginning Java Objects: From Concepts To Code,Second Edition, Apress, 2005.
[Deitel] H.M. Deitel and P.J. Deitel,Java How to Program: Early Objects
Version, Prentice Hall, 2009.
[Sierra] K. Sierra and B. Bates, Head First Java, 2nd Edition, O'Reilly Media,
2005.
Code Conventions for the Java Programming Language, available at
http://java.sun.com/docs/codeconv/CodeConventions.pdf
References
http://java.sun.com/docs/codeconv/CodeConventions.pdfhttp://java.sun.com/docs/codeconv/CodeConventions.pdfhttp://java.sun.com/docs/codeconv/CodeConventions.pdf