// APS101, Winter 2009: Lecture 17 (Feb. 23) // // Assignment 2 has been posted. Please start on it early. // You will have a tutorial on A2 next Tuesday (Mar. 3). // Also, there will be TA office hours for A2 (time and location TBA). // Check the FAQ page for updates and answers to common questions. // // Review: so far, we've looked at basic concepts of Object-Oriented programming. /** * 4 basic types of statements: * * 1. Variable declaration * int x; * JFrame j; * * 2. Variable assignment * x = 45; * j = new JFrame(); * * 3. return * return 24.2; * return 34 + 23 / 2; * return j; * return "Monday"; * * 4. method calls * j.setVisible(true); * "hello".substring(0, 3) */ // now, on to more complex things... // if-statements: // // if (boolean-expression) { // then-part; // } int i = 10, j = 20, max; i j max if (i < j) { max =j; } i = 30 if (i < j) { max =j; } max // if(boolean-expression) { // then-part; // } else { // else-part; // } i = 10; j = 20; max = 0; i j max if (i < j) { max = j; } else { max = i; } max // if(boolean-expression) { // then-part; // } // } else if (another boolean-expression) { // else if-part; // } else { // else-part; // } i=10; j=20; max=0; i j max if (i < j) { max = j; } else if (i > j) { max = i; } else { // you can leave it blank - means "do nothing"! } max int numEnrolled = 0; int maxEnrolled = 5; // If there is room in the course, then enrol a student. // enrol a student: numEnrolled++; // there is room in the course: numEnrolled < maxEnrolled // if(there is room in the course) { // enrol a student // } if(numEnrolled < maxEnrolled) { numEnrolled++; } numEnrolled if(numEnrolled < maxEnrolled) { numEnrolled++; } numEnrolled if(numEnrolled < maxEnrolled) { numEnrolled++; } if(numEnrolled < maxEnrolled) { numEnrolled++; } if(numEnrolled < maxEnrolled) { numEnrolled++; } if(numEnrolled < maxEnrolled) { numEnrolled++; } numEnrolled System.out.println("test"); // displays "test" on the screen //out is a static object in "System" class if(numEnrolled < maxEnrolled) { numEnrolled++; System.out.println("You are enrolled."); } if(numEnrolled < maxEnrolled) { numEnrolled++; System.out.println("You are enrolled."); } else { System.out.println("The course is full."); } // what if maxEnrolled is 50, or say 10000000? // we need to use a loop! for now we will use "while" loops // next week, we'll look at "for" loops // while loops: // // while (boolean termination condition) { // do something; // } // (the loop will keep going while the termination condition is true; // in other words, the loop will stop when the termination condition // becomes false) int numEnrolled = 20; int maxEnrolled = 10; while (numEnrolled < maxEnrolled){ numEnrolled++; System.out.println("Enrolled. You are student no:" + numEnrolled); } numEnrolled = 0; maxEnrolled = 10; while (numEnrolled < maxEnrolled){ numEnrolled++; System.out.println("Enrolled. You are student no:" + numEnrolled); } numEnrolled = 0; maxEnrolled = 10; while (numEnrolled < maxEnrolled){ numEnrolled++; System.out.println("Enrolled. You are student no:" + numEnrolled); if (numEnrolled == maxEnrolled) { System.out.println("The course is full."); } }