Computer Science 15-100, Fall 2009
Class Notes: Loops (An Introduction)
Loops
class MyCode {
public static void main(String[] args) {
int counter;
for (counter=1; counter<=5; counter++) {
System.out.println(counter);
}
}
}
class MyCode {
public static void main(String[] args) {
int counter;
int n = 10;
for (counter=1; counter<=n; counter++) {
System.out.println(counter);
}
}
}
class MyCode {
public static void main(String[] args) {
int counter;
int n = 10;
int sum = 0;
for (counter=1; counter<=n; counter++) {
sum = sum + counter;
}
System.out.println(sum);
}
}
class MyCode {
public static void main(String[] args) {
int counter;
int n = 10;
int sum = 0;
for (counter=1; counter<=n; counter++) {
if (counter % 2 != 0)
sum = sum + counter;
}
System.out.println(sum);
}
}
class MyCode {
public static void main(String[] args) {
int counter;
int n = 10;
int sum = 0;
for (counter=1; counter<=n; counter+=2) {
sum = sum + counter;
}
System.out.println(sum);
}
}
class MyCode {
public static void main(String[] args) {
int i; // i for index, rather than counter
String s = "Carpe diem";
for (i=0; i<s.length(); i++) {
char c = s.charAt(i);
System.out.println(c);
}
}
}
class MyCode {
public static void main(String[] args) {
int i;
String s = "Carpe diem";
for (i=s.length()-1; i>=0; i--) {
char c = s.charAt(i);
System.out.println(c);
}
}
}
class MyCode {
public static void main(String[] args) {
int x = 35;
int y = 5;
int quotient = 0;
while (x >= y) {
x -= y;
quotient++;
}
System.out.println(quotient);
}
}
class MyCode {
public static void main(String[] args) {
String s1 = "hi";
String s2 = "bonjour";
while (s1.length() < s2.length()) {
s1 = s1 + "hi";
}
System.out.println(s1);
System.out.println(s2);
}
}
class IsPrimeAndNthPrimeExamples {
//////////////////////////////////////////
/// isPrime
//////////////////////////////////////////
// Returns true if n is a prime number, and false otherwise.
// Do not worry about efficiency (yet). We'll make this faster soon...
public static boolean isPrime(int n) {
int counter;
if (n < 2) return false;
for (counter=2; counter<n; counter++) {
if (n % counter == 0)
return false;
}
return true;
}
public static void testIsPrime() {
System.out.print("Testing isPrime()... ");
assert(isPrime(2));
assert(isPrime(3));
assert(!isPrime(4));
assert(isPrime(5));
assert(!isPrime(6));
assert(isPrime(8179));
assert(!isPrime(8211));
assert(!isPrime(-3));
assert(!isPrime(0));
assert(!isPrime(1));
System.out.println("Passed all tests!");
}
//////////////////////////////////////////
/// nthPrime
//////////////////////////////////////////
// Returns the nth prime number, or -1 if n is non-positive.
// Do not worry about efficiency (yet). We'll make this faster soon...
public static int nthPrime(int n) {
int counter = 1;
int numberOfPrimes = 0;
if (n < 1) return -1;
while (numberOfPrimes < n) {
counter++;
if (isPrime(counter))
numberOfPrimes++;
}
return counter;
}
public static void testNthPrime() {
System.out.print("Testing nthPrime()... ");
assert(nthPrime(1) == 2);
assert(nthPrime(2) == 3);
assert(nthPrime(3) == 5);
assert(nthPrime(4) == 7);
assert(nthPrime(5) == 11);
assert(nthPrime(6) == 13);
assert(nthPrime(-5) == -1);
assert(nthPrime(0) == -1);
System.out.println("Passed all tests!");
}
//////////////////////////////////////////
/// main
//////////////////////////////////////////
public static void main(String[] args) {
testIsPrime();
testNthPrime();
}
}
class MyCode {
public static void main(String[] args) {
int x, y;
for (x=1; x<=4; x++) {
for (y=1; y<=4; y++)
System.out.print("(x=" + x + ",y=" + y + ") ");
System.out.println();
}
}
}
Output:
(x=1,y=1) (x=1,y=2) (x=1,y=3) (x=1,y=4) (x=2,y=1) (x=2,y=2) (x=2,y=3) (x=2,y=4) (x=3,y=1) (x=3,y=2) (x=3,y=3) (x=3,y=4) (x=4,y=1) (x=4,y=2) (x=4,y=3) (x=4,y=4)
import java.awt.*;
import javax.swing.*;
class MyCode extends JComponentWithEvents {
private int rows = 5;
private int cols = 7;
public void keyPressed(char key) {
if (key == SPACE) {
// Increase rows and cols when user hits SPACE bar
rows++;
cols++;
}
else if (key == 'r') {
// Reset to some small # of rows and cols
rows = 3;
cols = 2;
}
}
public void paint(Graphics2D page) {
int row, col;
for (row=0; row<rows; row++)
for (col=0; col<cols; col++)
paintCell(page, row, col);
}
// Helper method to paint a single cell in the 2d grid
public void paintCell(Graphics2D page, int row, int col) {
int width = getWidth();
int height = getHeight();
int left = col * width / cols;
int top = row * height / rows;
int right = (col+1) * width / cols;
int bottom = (row+1) * height / rows;
// paint the grid's outline
page.setColor(Color.black);
page.drawRect(left, top, right-left, bottom-top);
// fill the cell
page.setColor(Color.blue);
page.fillOval(left, top, right-left, bottom-top);
// label the cell
page.setColor(Color.black);
page.setFont(new Font("Arial", Font.BOLD, 14));
String label = row + "," + col;
drawCenteredString(page, label, left, top, right-left, bottom-top);
}
public static void main(String[] args) { launch(500, 400); }
}
Output:
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem