100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Java Programming $8.69   Add to cart

Interview

Java Programming

 21 views  1 purchase
  • Course
  • Institution

Java programming basics

Preview 4 out of 268  pages

  • August 10, 2022
  • 268
  • 2022/2023
  • Interview
  • Unknown
  • Unknown
  • 1
avatar-seller
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

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller Morbius. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $8.69. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

84866 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$8.69  1x  sold
  • (0)
  Add to cart