Professional Documents
Culture Documents
This
beginners. It covers the fundamentals of programming, roughly the same material that is covered in a beginning programming course in a university or in a high school AP Computer Science course. For maximum benefit, go though these notes interactively, thinking about and answering the question at the bottom of each page. There are about 20 pages per chapter. If you spend about 3 minutes per page each chapter will take about 60 minutes, or longer if you copy and run some of the programs. If you are a beginning programmer, plan on spending more than a month with this.
Here is very nice site (unrelated to this site) that allows you to practice Java programming on line: These notes assume that you have the Java http://javabat.com. version 5.0 or later from Oracle, Inc. at Another nice site (unrelated to this http://www.oracle.com/technetwork/java/index.ht site) that allows you to run Java ml and a text editor such as Notepad. Compiling programs directly in your browser is: and running programs is done from the command http://ideone.com. line interface. You may use more sophisticated environments, as well. An alternate site, with faster response time, that contains this material is: Mirror Site Best viewed at 1024 x 768 or higher. The audio works best with Windows Media Player.
Quiz
Flash Cards.
Chapter 2 Analog and Binary Signals Chapter 3 Computer Memory Chapter 4 The Processor
Part 3: Data
Chapter 8 Primitive Data Chapter 9A Chapter 9B Chapter 9C Chapter 10 Chapter
Variables and the Assignment Statement Expressions and Arithmetic Operators Object Data
Quiz
Quiz
Quiz
Exercises
Quiz
Exercises
11
Quiz
Exercises
Quiz
Exercises
Review Exercises
Review Exercises
Quiz
Exercises
Review Exercises
Sentinel-controlled Loops
Review Exercises
Result-controlled Loops
Quiz
Random
Quiz
Review Exercises
Review Exercises
Review Exercises
Review Exercises
Formatted Output
Quiz
Exercises
Quiz
Object References
Quiz
Method Parameters
Review
Quiz
Exercises
Quiz Review
Exercises
Defining your own Classes Class Design Example (miles per gallon) Class Design Example (checking account) Encapsulation and Visibility Modifiers
Exercises
Review Exercises
Review
Quiz
Exercises
Quiz
Review Exercises
Quiz
Exercises
Applet Examples
Quiz
Exercises
Quiz
Exercises
Quiz
Boolean Expressions and ShortQuiz circuit Operators Truth Tables and De Morgan's Laws The for Statement
Review
Quiz
Exercises
Exercises Exercises
Quiz
43 Chapter 44
Quiz
Exercises
Part 9: Arrays
Chapter 46 Chapter 47 Chapter 48 Chapter 49A Chapter 49B Chapter 49C Chapter 49D
Introduction to Arrays
Quiz
Exercises
Quiz Review
Exercises
Arrays as Parameters
Exercises
Methods that Change Arrays Arrays of Objects and Linear Search Two-dimensional Arrays
Quiz
Quiz
Exercises
Quiz
Exercises
StringBuffers
Quiz
Exercises
Quiz
Quiz
Quiz
Interfaces
Quiz
Exercises
Quiz
Exercises
Quiz
Quiz
Quiz
Quiz
Quiz
Exercises
Quiz
Exercises
GUI Applications
Quiz
Exercises
Quiz
Exercises
Exercises
Quiz
Exercises
Recursion in Java
Quiz
Exercises
Examples of Recursion
Quiz
Exercises
More Recursion
Quiz
Exercises
Exercises
Exercises
Quiz
Exercises
Quiz
Exercises
Quiz
Quiz
Exercises
Quiz
Exercises
Quiz
Exercises
Chapter 86 Chapter 87
Quiz
Exercises
Quiz
Exercises
Appendix
Part 1 Pages, Paragraphs, and Fonts. Part 2 Lists, Links, and URLs. Part 3 Applets, Images, and Tables. Appendix B Installing Java Appendix C Editing the PATH variable
o o o
Computer Systems
A computer is a complex system consisting of both hardware and software components. This chapter discusses these components. Chapter Topics:
..
Everyone knows that computers are "digital" and that they use "binary" numbers. You probably have some idea of what these terms mean. But we need to be clear about their precise meaning and to be clear about why computers are digital. Chapter Topics:
Study these topics carefully if you want to know what is really going on with computers.
QUESTION 1:
Why is a digital wristwatch called "digital"?
Bits and Bytes Kilobytes, megabytes, gigabytes Main memory and addresses Secondary memory Files
Recall from chapter one that the major hardware components of a computer system are:
QUESTION 1:
(Educated Guess: ) If you bought a new computer, about how much main memory would you expect it to have: 200 bytes, 2 kilobytes, 2 megabytes, or 2 gigabytes?
Chapter Topics:
Machine operations and machine language Example of machine language Different types of processor chips High level programming languages Language translators (compilers) Language interpreters
From previous chapters you know that the major hardware components of a computer system are:
This chapter examines the "brain" of the computer system the processor. Then it shows how the programs you write are translated into instructions for the processor.
QUESTION 1:
What component of a computer system is directly responsible for running a program?
Hello World program Java Bytecodes Java compiler and Java virtual machine Using Notepad to create a source program Running a Java program
The previous chapters have discussed computer hardware, computer software, bits and bytes, machine language, language translation, language interpretation, and virtual machines. This chapter assumes that you know all that and are ready to look at Java.
These notes are written for Java 2 Standard Edition version 5.0 or higher. Many features were added to this edition and previous versions will not work as well with these notes. If you are installing Java for the first time on your computer, install Java 2 Standard Edition version 6.0 or higher. See Appendix B for information on how to do this.
QUESTION 1:
Can the processor of a computer system directly execute source programs written in Java?
Small example programs Names for source files and class files Syntax errors Bugs The Edit, Compile, and Run cycle Matching braces Neat indenting
The previous chapter discussed the mechanics of creating and running a Java program. This chapter is an overview of Java using the example program from that chapter.
QUESTION 1:
(Review:) What role in Java programming does each of the following files play?
How to copy, paste, compile, and run programs Starting Notepad and the command prompt Copying text to the clipboard Pasting text from the clipboard to Notepad Saving the text as a source file Running a program
These notes show many example programs. You need to run them and play with them. The most effective way to learn programming is to fiddle around with example programs. You don't have to type in the programs to run them. If you can see the program in a browser window, then you can easily copy it to Notepad (or other editor) and then save the text to a file. Once that is done, you can compile and run the program as described in the previous chapter. If this is perfectly clear skip this chapter. If not, this chapter walks you through these steps. Super Easy Way to Run Java: The web site http://ideone.com/ lets you copy-andpaste Java code into a web page text box, then compile and run it completly within your browser. This is ideal for playing with the example programs in many of these chapters.
QUESTION 1:
How to copy, paste, compile, and run programs Starting Notepad and the command prompt Copying text to the clipboard Pasting text from the clipboard to Notepad Saving the text as a source file Running a program
These notes show many example programs. You need to run them and play with them. The most effective way to learn programming is to fiddle around with example programs. You don't have to type in the programs to run them. If you can see the program in a browser window, then you can easily copy it to Notepad (or other editor) and then save the text to a file. Once that is done, you can compile and run the program as described in the previous chapter. If this is perfectly clear skip this chapter. If not, this chapter walks you through these steps. Super Easy Way to Run Java: The web site http://ideone.com/ lets you copy-andpaste Java code into a web page text box, then compile and run it completly within your browser. This is ideal for playing with the example programs in many of these chapters.
QUESTION 1:
Is it sometimes beneficial to type in a program rather than just copying it?
In all but the smallest programs, an executing program is constantly working with values. These values are kept in little sections of main memory called variables. Chapter Topics:
QUESTION 1:
Do you imagine that a variable can change its value?
Chapter Topics:
Review of Expressions Arithmetic Operators Integer operators Floating point operators Mixed Floating point and Integer Expressions Constants
QUESTION 1:
(Review: ) Is the following correct?
13 * 6 -
Classes Objects Reference variables String objects Running methods of objects Methods that create String objects Type wrappers Packages
Chapter 25 and following discuss objects in greater detail than this chapter.
QUESTION 1:
(Review: ) What are the two divisions of data in Java?
Input and Output Streams The standard I/O streams Scanner class Exceptions Converting character data to type int
Note: Some books show input done using classes written by the book's author. To use them, you must install a package from a CD or from a web site. This is OK. You can use these notes (with standard Java methods) along with your text without any interference.
QUESTION 1:
When a computer program does an input operation, in which direction does the data flow?
From an outside device into the program? From the program out to some device?
Java includes the primitive types float and double, which hold floating point numbers. When converted to characters, floating point numbers include a decimal point followed by a decimal fraction. For example, 3.14159 and -0.718802. Chapter Topics:
Converting strings to double Keyboard input of floating point numbers The Math class Square root Trig functions PI Not a Number (NaN)
All of the familiar mathematical functions such as sine, log, and square root are available to your program in the Java Math class. These functions usually use arguments of type double, and usually return values of type double.
QUESTION 1:
Which uses more bits: a Java float or a a Java double ?
9/26/98, 08/01/99, 12/09/99, 01/12/06, 07/09/08
This chapter looks at how computer programs make decisions using the if statement. This statement is one of the fundamental building blocks of programming. Chapter Topics:
Two-way Decisions The if statement Outline of a two-way decision Blocks of statements Boolean Expressions Relational Operators Example Programs
QUESTION 1:
You are driving in your car and it starts to rain. The rain falls on your windshield and makes it hard to see. Should your windshield wipers be on or off?
Single-branch if statements Boolean expressions that use arithmetic Picking the right relational symbol Several if statements in a program
QUESTION 1:
You are walking through the shopping mall and are a little bit hungry. Pleasant aromas tempt you as you pass by a cookie store. What will you do?
Logical Operators AND Operator How to check that a number is in range Boolean Expressions OR Operator Comparison between AND and OR NOT Operator
Questions involving boolean expressions have been prominent on past AP Computer Science Tests.
QUESTION 1:
You have decided to bake some cookies (much cheaper than buying them at the Mall). An interesting cookie recipe calls for 4 cups of flour and 2 cups of sugar. You look in your pantry and find 3 cups of flour and 2 cups of sugar. Can you bake cookies with what you have?
CHAPTER 15 Loops
Chapter Topics:
The while statement Syntax and semantics of the while statement The loop body Three parts of a loop that must be coordinated Counting loops and the loop control variable
Most modern machines work by endlessly repeating the same motions. The engine in your car cycles through the same motions over and over as it burns gasoline to provide power. Electric motors are similar. They convert electric power into circular motion. Because of their circular motions, these machines keep going as long as you want. Computer programs use cycles, also. In programming, a cycle is called a loop. When a program has a loop in it, some statements are done over and over as long as is needed to get the work done. Most computer programs execute many millions of program statements each time they are used. Usually the same statements are executed many times. This chapter discusses several kinds of programming loops.
QUESTION 1:
Think of some machines (mechanical or other) that use cycles.
Counting upward by two's The while condition as a gatekeeper Iteration Decrementing the loop control variable Infinite loops Basing a value on the loop control variable
This chapter gives you more practice with counting loops. There are other types of loops, but counting loops are by far the most common and well worth further study.
QUESTION 1:
(Review) What three parts of a loop must be coordinated for it to work properly?
Adding up even and odd integers Computing N factorial Dropping a brick Making a rectangle of stars
This chapter shows how counting loops and if statements are used together to implement the logic of a program. These examples give you practice in building programs out of fundamental pieces.
QUESTION 1:
With only paper and pencil, would you like to add up all the integers from one to one thousand?
Sentinel-controlled loops Example programs: o Adding up a list of integers o evaluating polynomials Using an if-else statement inside the loop body Matching up if's with else's Using a String value as a sentinel
The previous chapters have discussed loops in general, and counting loops in particular. The same statements that are used to build counting loops are used to build other kinds of loops. This chapter looks at a kind of loop called a sentinel controlled loop.
QUESTION 1:
Say that you want a program that adds up any number of integers entered from the keyboard. Will a loop be used in this program?
Result controlled loops Example programs: o Savings account interest o Newton's method Nested Loops Improving an estimate by iteration Precision of double precision variables
QUESTION 1:
You put $1000 in the bank at 5% annual compound interest. How much will you have at the end of one year?
Programs frequently need random numbers. A card game uses random numbers to create a shuffled deck of cards. A dice game uses random numbers to simulate a throw. Many activities in the real world (such as traffic on a highway system) appear to be random, and a program that models them must use random numbers. This chapter discusses the Random class that comes with Java. Random numbers, probability, and statistics are important for testing, for modeling the world, and for general understanding of the universe. Computer science degree programs usually require a course in these subjects. The chapter discusses:
Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords
QUESTION 1:
What number is most likely outcome for a throw of a fair die?
QUESTION 1:
What is a file? (Try to think of a one sentence definition.)
Input file redirection Problems with blank lines Redirecting both input and output Numeric input from a text file Problems with illegal characters in a number The trim() method
The previous chapter discussed text files and writing to text files. This chapter discusses reading from text files.
QUESTION 1:
Where has the data for our previous programs come from?
Created: 03/19/2006
PrintStream
This chapter discusses how to use the
use the PrintStream class for output to files. This chapter is optional. Except for chapter 24, disk I/O is not much used again until it is taken up in more detail in chapter 80. However, most interesting programs do some disk I/O, and reading this chapter may enable you to write some of them. Chapter Topics:
Using Scanner with disk files. I/O Exceptions hasNext Methods of Scanner Class PrintStream print() and println() Methods of PrintStream
QUESTION 1:
In our past programs, what input stream has been used with
Scanner ?
The programs in this chapter read and write disk files using
Scanner and
QUESTION 1:
(Not again!) What are the three types of loops?
created 07/10/2006, revised 04/17/2010
you can ask for only two places right of the decimal point:
value = 13.34
This chapter is optional. Future chapters do not depend on it, so you can safely skip it. However, your programs will produce better looking output if you use number formatting. Chapter Topics:
Default Locale Formatting numeric output DecimalFormat Changing the Locale Format patterns o 0 format character o . format character o , format character o # format character Formats for positive and negative numbers
There is much more to formatting numbers than this chapter discusses. Sometime in the future you may wish to study the detailed documentation available on line. Depending on their location and language, people use different formats for numbers, dates, times, and currency. How to customize a program for the location and language where it will be used is called internationalization. This chapter only briefly discusses this.
QUESTION 1:
How would you prefer to see this number displayed:
123456789
CHAPTER 25 Objects
Chapter Topics:
What is an Object? What is a Class? Characteristics of Objects Static Methods Constructors Cookie Cutters Dot Notation
Until now, we have not been doing much with objects. This chapter discusses objects and classes. Forget programming for a while. Think about the Real World and the things that are in it. What things are objects? What things are not objects? This is actually a difficult problem which has occupied philosophers for thousands of years. Don't be too worried if it is not immediately clear to you.
QUESTION 1:
Look around you. List four objects and list four non-objects.
Objects
Non-objects
Primitive data types, and objects Object references and object reference variables Copying object references The == operator and object references The equals() method for strings String literals
You might wish to study this carefully, as a pre-emptive strike against confusion.
QUESTION 1:
(Review:) In Java, a piece of data either is of a _________________ data type or is an ________________ data type.
The Point class Class descriptions The toString() method Changing the data in an object vs constructing a new object The equals() method The == operator (again) Aliases
You have seen some of the material in this chapter before. But, don't rush through it. The purpose of this chapter is to review topics that you may still be somewhat unclear about.
QUESTION 1:
In geometry, what is a two dimensional point