













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














JAVA Test 1 Notes Ch Algorithms In an informal sense, you can think of an algorithm as simply a procedure for solving a problem. To meet its more formal definition, an algorithm must be: Clearly and unambiguously defined. Effective, in the sense that its steps are executable. Finite, in the sense that it terminates after a bounded number of steps. Chapter 2
2. variables n1, n
e. Drawing Geometrical Objects i. Constructors
iii. indentifiers should make their purpose obvious to the reader iv. identifiers should adhere to standard conventions. Variable names, for example, should begin with a lowercase letter f. You must declare a variable before you can use it i. type name = value;
iii. control statements
d. Cascading if statement if ( condition 1 ) { statements 1 } else if ( condition 2 ) { statements 2
... more else / if conditions... } else { statements else }
d. Java evaluates the statements in a case clause until it reaches the break statement which should appear at end of each clause e. if none of the values in the case clauses match the expression Java evaluates the statements in the default clause switch ( expression ) { case v 1 : statements to be executed if expression = v 1 break; case v 2 : statements to be executed if expression = v 2 break;
... more case clauses if needed... default: statements to be executed if no values match break; }
c. the advantage of the for statement is that everything you need to know to understand how many times the loop will run is explicitly included in the header line d. for statement ex i. for (int i = 1; i <= 10; i++)
c. The individual elements of each output line are easier to display if you call print instead of println. The print method is similar to println but doesn’t return the cursor position to the beginning of the next line in the way that println does. Using print therefore makes it possible to string several output values together on the same line. d. The n th^ output line contains n values before the equal sign but only n – 1 plus signs. Your program therefore cannot print a plus sign on each cycle of the inner loop but must instead skip one cycle. public class TriangleTable extends ConsoleProgram { public void run() { for (int n = 1; n <= MAX_VALUE; n++) { int total = 0; for (int i = 1; i <= n; i++) { if (i > 1) print(" + "); print(i); total += i; } println(" = " + total); } } / Private constants / private static final int MAX_VALUE = 10; }
14. Simple Graphical Animation a. The while and for statements make it possible to implement simple graphical animation. The basic strategy is to create a set of graphical objects and then execute the following loop: for (int i = 0; i < N_STEPS; i++) { update the graphical objects by a small amount pause(PAUSE_TIME); }
println("This program adds four numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int n3 = readInt("Enter n3: "); int n4 = readInt("Enter n4: "); int total = n1 + n2 + n3 + n4; println("The total is " + total + "."); } } can be written as public class AddNIntegers extends ConsoleProgram { public void run() { println("This program adds " + N + " numbers."); int total = 0; for (int i = 0; i < N; i++) { int value = readInt("? "); total += value; } println("The total is " + total + "."); } private static final int N = 100; }
This is a multiple-choice exam scheduled during regular lecture time. It consists of approximately 30 questions that need to be answered in 50 minutes. This is a closed notes and books exam. Calculators and other electronic devices are not allowed during exam. Bring a pencil and paper!
This exam covers the text chapters AND everything I have said in class thus far including: o Chapters 1 o Algorithm and problem solving o Chapter 2 o Examples, examples, examples - Big picture. o Understanding idioms and patterns so that you can learn the material. o basic acm graphic shapes and how to use them (declare/create a shape, call methods on it to make it display, change color, etc.) o recangle, oval, line label o readInt(), readDouble(), readLine(), print(), println() etc. o extends GraphicsProgram, DialogProgram, COnsoleProgram o run() method o Chapter 3 o Expressions o primative data types o declaring constants and variables o operators and precedence... Evaluating expressions o int division, floating-point division, mixed division (and other operations) o type conversions, "upgrading" o assignment statement (especially conpared to == equality!) o increment and decrement o Boolean expressions o relational operators o logical operators o Chapter 4 o Statements o Simple, compound (blocks) i.e. { }, and control statements o Iterative vs Conditional o repeat-N-times pattern (for loop) o while loop o (please do NOT use an if statement to break out of a while loop!) o infinite loop o sumation o nested loops o for loops o nested for loops o if statement o series of statements with if statements o imbedded if statements o if-else statements o cascading and embedded if-else o NEVER use the ?: operator in this class. It is too confusing. o switch statement o cases are independant o expression must eval to int or char o cases must be int/char constants