int s = 1; int n = 1; while (s < 10) { s = s + n; n++; }
int s = 1; int n = 1; do { s = s + n; n++; } while ( s < 2*n );
class A { public A() { n = 0; } public A(int a) { n = a; } public void f() { n++; } public void g() { f(); n = 2 * n; f(); } public void h() { return n; } public void k() { System.out.println(n); } private int n; }
public boolean isPrime(int n)that returns true if n is a prime number and false otherwise.
import ccj.*; public class mystery1 extends GraphicsApplet { public void run() { Point[] pp = new Point[6]; for ( int i = 0 ; i < 6 ; i++ ) { double t = i * (2*Math.PI/6); pp[i] = new Point(5*Math.cos(t),5*Math.sin(t)); } for ( int i = 0 ; i < n ; i++ ) new Line(pp[i], pp[(i+1)%6]).draw(); } }
import ccj.*; public class mystery2 extends GraphicsApplet { public void run() { Point[] pp = new Point[8]; for ( int i = 0 ; i < 8 ; i++ ) { double t = i * (2*Math.PI/8); pp[i] = new Point(5*Math.cos(t),5*Math.sin(t)); } new Circle(new Point(0,0),5).draw(); for ( int i = 0 ; i < 8 ; i++ ) new Line(pp[i], new Point(0,0)).draw(); } }
import java.applet.*; import java.awt.*; public class OneColorDraw extends Applet { private int last_x = 0; private int last_y = 0; private Button clear_button; private Color current_color = Color.black; public void init() { clear_button = new Button("Clear"); clear_button.setForeground(Color.black); clear_button.setBackground(Color.lightGray); this.add(clear_button); } public boolean mouseDrag(Event event, int x, int y) { Graphics g = this.getGraphics(); g.setColor(current_color); g.drawLine(last_x, last_y, x, y); last_x = x; last_y = y; return true; } // (continued on the next page -->)
public boolean action(Event event, Object arg) { if (event.target == clear_button) { Graphics g = this.getGraphics(); Rectangle r = this.bounds(); g.setColor(this.getBackground()); g.fillRect(r.x, r.y, r.width, r.height); return true; } else return super.action(event, arg); } }
import ccj.*; public class PuzzlePiece { private Point ul; int label; /** * Initialize the label to 0 and the corner to (0,0). */ public PuzzlePiece() { } /** * Initialize the label to l and the corner to (0,0). * @param l the label for the piece */ public PuzzlePiece(int l) { } /** * Initialize the label to l and the corner to (x,y). * @param l the label for the piece * @param x the x-coordinate of the corner. * @param y the y-coordinate of the corner. */ public PuzzlePiece(int l, double x, double y) { } // (continued on the next page -->)
/** * Move the piece by dx units in the x direction and dy units in the y * direction. * @param dx change in x-coordinate * @param dy change in y-coordinate */ public void move(int dx, int dy) { } /** * Draw the puzzle piece. The label is drawn, surrounded by * a square whose upper left corner is the point stored in * PuzzlePiece. */ public void draw() { Point ll = new Point(ul.getX(), ul.getY()+2); Point lr = new Point(ul.getX()+2, ul.getY()+2); Point ur = new Point(ul.getX()+2, ul.getY()); new Line( ul, ll).draw(); new Line( ul, ur).draw(); new Line( ll, lr).draw(); new Line( ur, lr).draw(); Point mp = new Point(ul.getX()+1, ul.getY()+1); Message m = new Message(mp, "" + label); m.draw(); } }
$ java Count sample.txtcounts the number of lines in the file sample.txt.