Pular para o conteúdo

AP Computer Science A

AP · College BoardComputer Science A44 notas em 4 pastas, 228 KB

44 notas de revisão de College Board AP Computer Science A, criadas por cookie, em 4 seções que seguem os tópicos do curso.

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.

Ao adicioná-las, você coloca uma cópia nas suas anotações, em uma pasta própria com as pastas abaixo. Você pode alterá-las e transformá-las em cartões de estudo ou em um conjunto de questões. Ao baixar, você recebe um arquivo zip com arquivos Markdown, que podem ser abertos em qualquer aplicativo de anotações.

O que contém

  • Using objects and methods
    • Algorithms, compilers and errors5 KB
    • Variables and primitive types5 KB
    • Expressions and output5 KB
    • Casting, range and compound assignment5 KB
    • APIs, libraries and comments4 KB
    • Methods, parameters and class methods5 KB
    • The Math class4 KB
    • Classes, objects and constructors5 KB
    • Creating and combining Strings4 KB
    • String methods5 KB
  • Selection and iteration
    • Algorithms with selection and repetition4 KB
    • Boolean expressions and relational operators4 KB
    • if statements5 KB
    • Logical operators and equivalent expressions5 KB
    • Comparing object references4 KB
    • while loops4 KB
    • for loops5 KB
    • Standard algorithms with loops5 KB
    • String algorithms6 KB
    • Nested loops and run-time analysis5 KB
  • Class creation
    • Abstraction and program design5 KB
    • Reliability, legal and ethical impacts of computing4 KB
    • Anatomy of a class5 KB
    • Constructors and object state6 KB
    • Writing methods5 KB
    • Passing and returning object references6 KB
    • Class variables and methods5 KB
    • Scope, access and this6 KB
  • Data collections
    • Data collection, privacy and bias5 KB
    • Arrays5 KB
    • Array traversals6 KB
    • Array algorithms6 KB
    • Text files and Scanner6 KB
    • Wrapper classes5 KB
    • ArrayList methods6 KB
    • ArrayList traversals5 KB
    • ArrayList algorithms6 KB
    • 2D arrays4 KB
    • 2D array traversals6 KB
    • 2D array algorithms7 KB
    • Searching6 KB
    • Selection sort and insertion sort6 KB
    • Recursion7 KB
    • Merge sort5 KB

A primeira anotação

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

E mais 43 depois que você adicionar ou baixar.

Avaliações

Ainda não há avaliações escritas. Adicione estas notas às suas e você poderá ser a primeira pessoa a deixar uma avaliação.