Interview
Java Programming
Java programming basics
[Show more]
Preview 4 out of 268 pages
Uploaded on
August 10, 2022
Number of pages
268
Written in
2022/2023
Type
Interview
Company
Unknown
Person
Unknown
Institution
11th Grade
Course
Java Programming
Schooljaar
1
All documents for this subject (1)
$8.69
Added
Add to cart
Add to wishlist
100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached
Programming for Engineers *
Contents
1 Introduction to computers and programming 8
1.1 Hardware and software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2 How computers store data . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2.1 Decimal to binary conversion . . . . . . . . . . . . . . . . . . . . . 10
1.3 How a program works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4 Algorithm and flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4.1 Advantages of using Flowcharts . . . . . . . . . . . . . . . . . . . . 15
1.5 Self-review exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2 Introducing Java 18
2.1 Java as a programming language . . . . . . . . . . . . . . . . . . . . . . . 18
2.2 Installing the JDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3 Introduction to Java applications 20
3.1 Java language rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2 A first program in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3 Displaying a single line with multiple statement . . . . . . . . . . . . . . . 23
3.4 Displaying multiple lines with a single statement . . . . . . . . . . . . . . . 24
3.5 Formatted output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4 Introducing integer numbers 26
4.1 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
* Allmaterials used in the preparation of this manual were obtained from public sources freely available
on the Internet.
1
, 4.2 Decision making . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5 Self-review exercises 31
6 Introduction to classes and objects 34
6.1 Declaring a simple class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.2 Use of a parameter in a method . . . . . . . . . . . . . . . . . . . . . . . . 35
6.3 Instance variables, set and get methods . . . . . . . . . . . . . . . . . . . . 37
6.4 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
6.5 Floating-point numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
6.5.1 Internationalization with Java Locale . . . . . . . . . . . . . . . . . 43
6.6 Self-review exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
7 Control statements 46
7.1 Deciding with if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
7.1.1 if Single-selection statement . . . . . . . . . . . . . . . . . . . . . . 46
7.1.2 if . . . else Double selection statement . . . . . . . . . . . . . . . . . 46
7.1.3 Conditional operator (?:) . . . . . . . . . . . . . . . . . . . . . . . . 47
7.1.4 Nested if . . . else statement . . . . . . . . . . . . . . . . . . . . . . 47
7.2 while Repetition statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7.3 Compound assignment operators . . . . . . . . . . . . . . . . . . . . . . . 53
7.3.1 Counter-controlled repetition . . . . . . . . . . . . . . . . . . . . . 55
7.4 for Repetition statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
7.5 switch Multiple-selection statement . . . . . . . . . . . . . . . . . . . . . . 59
7.6 break and continue Statements . . . . . . . . . . . . . . . . . . . . . . . . 64
7.7 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
7.7.1 Conditional AND (&&) operator . . . . . . . . . . . . . . . . . . . 66
2
, 7.7.2 Conditional OR (| |) operator . . . . . . . . . . . . . . . . . . . . . 66
7.7.3 Logical negation (!) operator . . . . . . . . . . . . . . . . . . . . . . 67
7.7.4 Boolean logical AND (&) and boolean logical inclusive OR (|) op-
erators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.7.5 Boolean Logical Exclusive OR (∧) . . . . . . . . . . . . . . . . . . . 68
7.8 Self-review exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
8 Methods 75
8.1 Once again, about classes and objects . . . . . . . . . . . . . . . . . . . . . 75
8.2 Static fields and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.2.1 Static fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.2.2 Static methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.2.3 Class Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.3 Methods with multiple parameters . . . . . . . . . . . . . . . . . . . . . . 78
8.4 Argument promotion and casting . . . . . . . . . . . . . . . . . . . . . . . 81
8.5 Example: Random-number generation . . . . . . . . . . . . . . . . . . . . 82
8.5.1 Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
8.6 Scope of declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
8.7 Method overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
8.8 Self-review exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
9 Arrays 93
9.1 Declaring and creating arrays . . . . . . . . . . . . . . . . . . . . . . . . . 93
9.2 Examples using arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
9.2.1 Creating and initializing an array . . . . . . . . . . . . . . . . . . . 94
9.2.2 Using an array initializer . . . . . . . . . . . . . . . . . . . . . . . . 95
9.2.3 Calculating the values to store in an array . . . . . . . . . . . . . . 96
3
, 9.2.4 Summing the elements of an array . . . . . . . . . . . . . . . . . . . 97
9.2.5 Using the elements of an array as counters . . . . . . . . . . . . . . 97
9.2.6 Using arrays to analyse survey results . . . . . . . . . . . . . . . . . 98
9.3 Enhanced for statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
9.4 Passing arrays to methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
9.5 Multidimensional arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
9.5.1 Two-dimensional arrays with rows of different lengths . . . . . . . . 110
9.5.2 Examples: Multiplication of matrices . . . . . . . . . . . . . . . . . 111
9.6 Variable-length argument lists . . . . . . . . . . . . . . . . . . . . . . . . . 114
9.7 Using command-line arguments . . . . . . . . . . . . . . . . . . . . . . . . 116
9.8 Self-review exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
10 Classes and objects 120
10.1 Example: Time class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
10.2 this Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
10.3 Overloaded constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
10.4 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
10.5 Enumerations: enum Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
10.6 static Class members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
10.6.1 Static fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
10.6.2 Static methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
10.6.3 static Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
10.7 final Instance Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
10.8 Packages in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
10.9 Self-review exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
4