Programming Exercises

2.1 The following program has several errors. Modify it so that it will compile and run without errors. (See Section 4.6 on page 129 for compiling and running code from packages.)
import java.util.*;

package com.acme;

public class Exercise1 {
    int counter;

    void main(String[] args) {
        Exercise1 instance = new Exercise1();
        instance.go();
    }

    public void go() {
        int sum;
        int i = 0;
        while (i<100) {
            if (i == 0) sum = 100;
            sum = sum + i;
            i++;
        }
        System.out.println(sum);
    }
}
2.2 The following program has several errors. Modify it so that it will compile and run without errors.
 // Filename: Temperature.java PUBLIC CLASS temperature { ...

Get Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.