Docsity

Java OOP Course: Control Flow, Classes, Arrays, Inheritance, and More - Prof. David M. Mou, Study notes of Computer Science

Toggle sidebar
bg1
C
CM
MS
SC
C
1
13
31
1:
:
C
Ch
ha
ap
pt
te
er
r
2
28
8
F
Fi
in
na
al
l
R
Re
ev
vi
ie
ew
w:
:
W
Wh
ha
at
t
y
yo
ou
u
l
le
ea
ar
rn
ne
ed
d
t
th
hi
is
s
s
se
em
me
es
st
te
er
r
T
Th
he
e
“
“B
Bi
ig
g
P
Pi
ic
ct
tu
ur
re
e”
”
Object Oriented Programming: In this course we began an introduction to programming
from an object-oriented approach.
– Java was the primary vehicle we used for programming.
– This will continue in CMSC 132 where you will learn more of the finer points of object-
oriented design.
Major Topics:
Basic Elements: types, variables, constants, operators, I/O
Control Flow: if-else, switch, while, do-while, for
Classes:
• object instances and references
• instance and class data
• methods
Arrays: 1-dim, multi-dim, ragged arrays, programming
Inheritance: overriding, late-binding, interfaces
B
Ba
as
si
ic
c
E
El
le
em
me
en
nt
ts
s
(
(o
of
f
J
Ja
av
va
a)
)
Basic Elements:
– Basic file structure: File name, class structure, comments
– Identifiers: Naming rules and naming conventions.
– Primitive types:
• boolean
• Integral types: byte, (char), short, int, long
• Floating point types: float, double
– Strings: and common String operators/methods.
– Specifying constants: true, ‘x’, 12, 3124L, 3.14159e-10
– Operators: Their meaning and precedence rules.
Possible Questions:
– Are these valid identifier names? $_Pasta_Fazool_22
– Operator precedence: x * 2 + y --
– Is a cast needed? float y = 2.0;
pf3
pf4
pf5

Partial preview of the text

Download Java OOP Course: Control Flow, Classes, Arrays, Inheritance, and More - Prof. David M. Mou and more Study notes Computer Science in PDF only on Docsity!

CMCMSSCC 1 1331 1:: CChhaapptteerr 2 288

FiFinnaall RReevviieeww:: WhWhaatt yyoouu lleeaarrnneedd tthhiiss sseemmeesstteerr

Th Thee ““BBiigg PPiiccttuurree””

Object Oriented Programming: In this course we began an introduction to programming

from an object-oriented approach.

  • Java was the primary vehicle we used for programming.
  • This will continue in CMSC 132 where you will learn more of the finer points of object- oriented design.

Major Topics :

Basic Elements : types, variables, constants, operators, I/O Control Flow : if-else, switch, while, do-while, for Classes :

  • object instances and references
  • instance and class data
  • methods Arrays : 1-dim, multi-dim, ragged arrays, programming Inheritance : overriding, late-binding, interfaces

Ba Bassiicc EElleemmeennttss ((ooff JJaavvaa))

Basic Elements :

  • Basic file structure : File name, class structure, comments
  • Identifiers : Naming rules and naming conventions.
  • Primitive types :
    • boolean
    • Integral types : byte, (char), short, int, long
    • Floating point types : float, double
  • Strings : and common String operators/methods.
  • Specifying constants : true, ‘x’, 12, 3124L, 3.14159e-
  • Operators : Their meaning and precedence rules.

Possible Questions :

  • Are these valid identifier names? $Pasta_Fazool
  • Operator precedence : x * 2 + y --
  • Is a cast needed? float y = 2.0;

CoConnttrrooll FFllooww

Control flow :

  • Conditionals : if-else, switch
  • Loops : while, do-while, for
  • Jumps : break and continue

Possible Questions :

  • Trace the loop : for ( int i = 4; i < 10; i += 3 ) { … }
  • Combining loops and conditionals : Is a string a palindrome?
  • Combining loops with arrays : Insert an item into a sorted array.
  • Nested conditionals : Given x, y, and z, return the smallest.
  • Nested loops : Selection, Insertion, Bubble Sort.

Avoid Pitfalls :

  • Don’t forget breaks in switch statements.
  • Be sure you understand the problem before coding. (If not, ask)
  • Test on a example before and after coding.

Cl Claasssseess

Class Objects and References :

  • Creating object instances with new
  • The null reference
  • Java’s class library
  • The heap and garbage collection

Basic Class Concepts :

  • Instance (non-static) and class (static) variables
  • Access modifiers (public, private, protected, default (package))
  • Constructors (initializers)

Methods :

  • Static and non-static
  • Arguments, call-return, and local variables
  • Return and memory leaks

Heap

AArrrraayyss

Arrays :

  • Creating, indexing
  • Arrays of primitives and object references
  • 2-dimensional arrays and ragged arrays (array of arrays)

Possible Questions :

  • Array manipulation :
    • Inserting/Removal/Searching in partially filled arrays
    • Appending/Merging arrays
    • Shifting, rotating, permuting elements in an array
  • 2-dim array manipulation :
    • Appending arrays
    • Transposing arrays (inverting rows and columns)

Avoid Pitfalls :

  • Allocation : Remember to explicitly allocate array/element storage
  • Simplify : Break complex operations into smaller/simpler pieces
  • Inside-Out : Design nested loops from the inside-out

Bob

Alice

p: n:

x: 4 f (^) b:

main

call stack

InInhheerriittaannccee

Inheritance :

  • Terminology : base class (superclass), derived class (subclass), super (parent class)
  • Method overriding (vs. overloading), late-binding, and final
  • Abstract methods and abstract classes (derived class implements) and polymorphism
  • up- and down-casting , getClass and instanceof
  • Multiple inheritance and interfaces

Possible Questions :

  • Be the compiler : We give code. Which method is called?
  • Design : We give the specs, you design the classes and methods.

Avoid Pitfalls :

  • Overriding only occurs when prototypes are identical. Start at the declared class, and go to the derived class if overriding applies.

Mi Misscceellllaanneeoouuss

Miscellaneous Topics :

  • Javadoc : @tags and their usage: @author, @param, @returns, …
  • Java Class Library : Math, DecimalFormat, ArrayList, …
  • Packages :
    • Organizes Java files into groups (directories)
    • Packages may be subdivided into subpackages
    • You have access to names in your own package or packages you import.
  • Exceptions :
    • try-catch blocks and exception propagation

Possible Questions :

  • Know common methods for String and ArrayList. For others know the basic concepts (Stack: push, pop, peek) but not details.
  • Package : What is accessible with/without importing?
  • Exception : Design/trace try-catch blocks.