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

Class notes

Java programing complete Notes

 14 views  0 purchase
  • Course
  • Institution
  • Book

Part of O'Reilly's definitive set of Java documentation, Learning Java introduces the basics of Java, the object-oriented programming language for networked applications from Sun Microsystems. This book provides a broad survey of the Java 2 Standard Edition and contains everything necessary to get ...

[Show more]

Preview 4 out of 699  pages

  • June 18, 2024
  • 699
  • 2023/2024
  • Class notes
  • Mr. rahul sharma
  • All classes
  • Secondary school
  • 5
avatar-seller
Introduction to Programming Using Java
Version 5.0, December 2006

(Version 5.0.2, with minor corrections, November 2007)




David J. Eck
Hobart and William Smith Colleges

,ii




c 1996–2007, David J. Eck

David J. Eck (eck@hws.edu)
Department of Mathematics and Computer Science
Hobart and William Smith Colleges
Geneva, NY 14456

This book can be distributed in unmodified form with no restrictions.
Modified versions can be made and distributed provided they are distributed
under the same license as the original. More specifically: This work is
licensed under the Creative Commons Attribution-Share Alike 2.5 License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-
sa/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th
Floor, San Francisco, California, 94105, USA.

The web site for this book is: http://math.hws.edu/javanotes

,Contents

Preface xiii

1 The Mental Landscape 1
1.1 Machine Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Asynchronous Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 The Java Virtual Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Building Blocks of Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5 Object-oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.6 The Modern User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.7 The Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Quiz on Chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2 Names and Things 19
2.1 The Basic Java Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2 Variables and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.2.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2.2 Types and Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.2.3 Variables in Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.3 Objects and Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.3.1 Built-in Subroutines and Functions . . . . . . . . . . . . . . . . . . . . . . 29
2.3.2 Operations on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.3.3 Introduction to Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.4 Text Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.4.1 A First Text Input Example . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.4.2 Text Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.4.3 TextIO Input Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.4.4 Formatted Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.4.5 Introduction to File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.5 Details of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.5.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.5.2 Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.5.3 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.5.4 Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.5.5 Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.5.6 Assignment Operators and Type-Casts . . . . . . . . . . . . . . . . . . . . 48
2.5.7 Type Conversion of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.5.8 Precedence Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.6 Programming Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

iii

, iv CONTENTS


2.6.1 Java Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.6.2 Command Line Environment . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.6.3 IDEs and Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.6.4 The Problem of Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Exercises for Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Quiz on Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

3 Control 61
3.1 Blocks, Loops, and Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.1.1 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.1.2 The Basic While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
3.1.3 The Basic If Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.2 Algorithm Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
3.2.1 Pseudocode and Stepwise Refinement . . . . . . . . . . . . . . . . . . . . 66
3.2.2 The 3N+1 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.2.3 Coding, Testing, Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 72
3.3 while and do..while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
3.3.1 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
3.3.2 The do..while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.3.3 break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
3.4 The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.4.1 For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
3.4.2 Example: Counting Divisors . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3.4.3 Nested for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.4.4 Enums and for-each Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.5 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.5.1 The Dangling else Problem . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.5.2 The if...else if Construction . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.5.3 If Statement Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.5.4 The Empty Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.6 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.6.1 The Basic switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.6.2 Menus and switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.6.3 Enums in switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . 98
3.6.4 Definite Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
3.7 Exceptions and try..catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.7.1 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.7.2 try..catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
3.7.3 Exceptions in TextIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.8 GUI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Exercises for Chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Quiz on Chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

4 Subroutines 117
4.1 Black Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
4.2 Static Subroutines and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
4.2.1 Subroutine Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
4.2.2 Calling Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

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 sanketray2010. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

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

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

77858 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
$2.99
  • (0)
  Add to cart