import java.awt.*; import java.awt.geom.*; /** * A circle that can be manipulated and that draws itself on a canvas. */ public class SimpleCircle { private int xPosition; private int yPosition; private String color; private Circle testCircle; private boolean isSunny; /** * Create a new circle at default position with default color. */ public SimpleCircle() { xPosition = 20; yPosition = 60; color = "blue"; draw(); } //Print out how many quarters, dimes, nickles, pennies //To throw at the greedy customer /* * Strategy: * Divide cents by 25, 10, 5, 1 * Divide by 25 -> number quarters * Modulus cents by 25 -> remainder of change * Divide by 10 -> number of dimes * Modulus cents by 10 -> remainder of change * .... */ public void printChange(int cents) { System.out.println(); System.out.println(); System.out.println(); System.out.print(cents/25 + " quarters "); cents %= 25; System.out.print(cents/10 + " dimes "); cents %= 10; System.out.print(cents/5 + " nickles "); cents -= 5*(cents/5); System.out.println("You get the idea"); } public void doTest(){ boolean checkValue=true; if (4<5) { checkValue = false; } System.out.println(checkValue); if (checkValue) { System.out.println("HI!"); } } public boolean isOK(boolean quick, boolean cheap, boolean correct) { int counter=0; if (quick) { counter++; } if (cheap) { counter++; } if (correct) { counter++; } return (counter==2); } public void printGrade(double grade){ String letterGrade="F"; if(grade >= 90.0) { letterGrade = "A"; } else { if (grade >= 80.0) { letterGrade = "B"; } else { if (grade >=70.0) { letterGrade="C"; } } } System.out.println(letterGrade); } /** * Move the circle horizontally by 'distance' pixels. */ public void moveHorizontal(int distance) { erase(); xPosition += distance; draw(); } /** * Move the circle vertically by 'distance' pixels. */ public void moveVertical(int distance) { erase(); yPosition += distance; draw(); } /** * Silly example method public void moveVertical(int distance, String newColor) { erase(); yPosition += distance; changeColor(newColor); draw(); } */ /** * Change the color. Valid colors are "red", "yellow", "blue", "green", * "magenta" and "black". */ public void changeColor(String newColor) { erase(); color = newColor; draw(); } /** * Make the circle dance around */ public void doCircleDance() { moveVertical(80); moveHorizontal(-80); moveVertical(-80); } private void yoyo3(int speed) { moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); moveVertical(-speed); } public void yoyo4(int speed){ if (speed != 5){ yoyo3(speed); yoyo3(speed); yoyo3(speed); } else{ System.out.println("Ha! You Loose!"); } } // How far is the circle from (0,0)? public double calcOriginDistance() { //get x & y position, square them, and take the square root double distance; distance = xPosition*xPosition + yPosition*yPosition; distance = Math.sqrt(distance); return distance; } //Print out the circle’s distance from the origin public void printOriginDistance() { System.out.println((int)calcOriginDistance()); } /* * Draw the circle with current specifications on screen. */ private void draw() { Canvas canvas = Canvas.getCanvas(); Ellipse2D theEllipse = new Ellipse2D.Double(xPosition, yPosition, 20, 20); canvas.draw(this, color, theEllipse); canvas.wait(40); } /* * Erase the circle on screen. */ private void erase() { Canvas canvas = Canvas.getCanvas(); canvas.erase(this); } }