Przejdź do treści

AP Computer Science A

AP · College BoardComputer Science A44 notatki w 4 folderach, 228 KB

44 notatki do nauki dla College Board AP Computer Science A, utworzonych przez cookie, w 4 sekcjach zgodnych z tematami kursu.

Notes for AP Computer Science A (College Board), in folders for the four units of the course framework in order: Using Objects and Methods; Selection and Iteration; Class Creation; Data Collections. Each note covers one topic with short Java examples, traced by hand where that helps. Delete any note you have already mastered once the notes are yours.

Co zawiera

Pierwsza notatka

Using objects and methods / Algorithms, compilers and errors

Algorithms and sequencing

An algorithm is a step-by-step process for completing a task or solving a problem. Everyday processes are algorithms: a recipe, directions to a friend's house, the steps for making a phone call. An algorithm can be written in ordinary language or drawn as a diagram such as a flowchart, and writing one out before any code is a good habit because it shows which steps the program needs and in what order.

Sequencing is the ordering of the steps. The steps of a process are carried out one at a time, and the order matters, because changing it can change the result. Consider this algorithm for making a sandwich with cheese and butter:

  1. Take two slices of bread.
  2. Spread butter on one slice.
  3. Place cheese on the buttered slice.
  4. Place the second slice on top.

If step 4 came before step 3 the cheese would have nowhere to go. A program is an algorithm written in a language a computer can follow, and a Java program runs its statements in order from top to bottom unless a statement changes the flow of control.

int width = 4;
int height = 3;
int area = width * height;
System.out.println(area);

This prints 12. Swapping the third and fourth lines would not compile or would print the wrong thing, because area would be used before it has a value.

Writing, compiling and running Java

Java source code is plain text and can be written in any text editor. Most programmers use an integrated development environment (IDE), which bundles an editor with tools to compile and run the code and to show errors as they are found.

Java is a compiled language. A compiler reads the source code and checks it against the rules of the language. If it finds a mistake it reports an error, and the program cannot run until the error is fixed. If the code passes, the compiler translates it into a form the Java runtime can execute. So there are two distinct stages, compile time and run time, and the kind of error a program has depends on the stage at which it shows up.

A compiler checks only some errors. It knows the grammar of Java and the types of variables, so it can tell that a semicolon is missing or that a number is being stored in a String variable. It cannot know what you meant the program to do.

Types of error

Syntax errors

A syntax error is a mistake where the rules of the language are not followed. The compiler detects these, so the program will not run until they are corrected. Typical causes are a missing semicolon, a missing or unmatched brace or bracket, a misspelled keyword, a variable used without being declared, and a type that does not fit.

int count = 5
System.out.println(count);

The first line has no semicolon, so the compiler rejects the program. The next example has a type error, which is also caught at compile time:

int total = "seven";

The literal "seven" is a String and cannot be stored in an int variable.

Logic errors

A logic error is a flaw in the algorithm or the code that makes the program do the wrong thing. The compiler cannot see it, because the code follows all the rules of Java and runs to the end. You find logic errors by testing the program with chosen data and comparing the output with what you expected.

int a = 6;
int b = 10;
double average = a + b / 2;
System.out.println(average);

The programmer wanted the average of 6 and 10, which is 8. Because division happens before addition, the expression is 6+(10/2)=116 + (10 / 2) = 11, and the value stored is 11.0. The code compiles and runs, and the answer is simply wrong. The repair is parentheses: (a + b) / 2, which gives 16 / 2 = 8, and then 8 is widened to 8.0 when stored in the double.

Testing works best with a spread of inputs, including values at the edges of what the program should handle, because a logic error often appears only for particular values.

Run-time errors and exceptions

A run-time error is a mistake that occurs while the program is executing, and it usually makes the program stop abnormally. The compiler accepted the code, but something happened during the run that the program could not carry on through.

An exception is a kind of run-time error caused by an unexpected problem that the compiler did not detect, and it interrupts the normal flow of execution. Several exceptions come up in Java programs, each covered in the note for the feature that causes it:

ExceptionTypical cause
ArithmeticExceptioninteger division or remainder by zero
NullPointerExceptioncalling a method on a reference that is null
StringIndexOutOfBoundsExceptionan invalid index into a String
ArrayIndexOutOfBoundsExceptionan invalid index into an array
IndexOutOfBoundsExceptionan invalid index into an ArrayList
ConcurrentModificationExceptionchanging the size of an ArrayList during an enhanced for loop
InputMismatchExceptionreading the wrong type with a Scanner

For example:

int students = 0;
int perTeam = 20 / students;

This compiles, and then throws an ArithmeticException when the division is reached, and the statements after it never run.

Telling the three apart

The clearest test is to ask when the problem shows itself and what the program does.

ErrorFoundWhat happens
Syntaxby the compiler, before the runthe program does not run
Logicby testing the outputthe program runs and gives wrong results
Run-timeduring executionthe program stops with an exception

I jeszcze 43: otwórz dowolne z nich z powyższej listy.

Opinie

Nie ma jeszcze pisemnych opinii. Dodaj te notatki do swoich, a możesz być pierwszą osobą, która doda opinię.