



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Chapter 6āObjects and Classes The Art and Science of An Introduction
C H A P T E R 6 To beautify life is to give it an object. āJosĆ© MartĆ, On Oscar Wilde, 1888
Returns a random int between low and high , inclusive.
Returns a random int from 0 to n - 1.
Returns a random double d in the range low! d < high.
Returns a random double d in the range 0! d < 1.
Returns a random boolean value, which is true 50 percent of the time.
Returns a random boolean, which is true with probability p, where 0! p! 1.
Returns a random color.
public class name extends superclass { class body }
This comment describes the class as a whole. The class header defines Student as a direct subclass of Object_. This comment describes the constructor. The constructor sets the instance variables. page 1 of 4 skip code_
/ public int getID() { return this.studentID; } // end method getID /*
These methods retrieve the value of an instance variable and are called getters and ID number are fixed, there are. Because the student name no corresponding setters. This method changes the value of an instance variable and is called a setter. page 2 of 4 skip code /**
/ public double getCredits() { return this.credits; } // end method getCredits /*
Names for getter methods usually begin with the prefix get_. The only exception is for getter methods that return a_ boolean , in which case the name typically begins with is_. page 3 of 4 skip code_
The tells Java how to display toString method a value of this class. All of your classes should override toString_. Classes often export named constants. These declarations define the instance variables that maintain the internal state of the class. All instance variables used in the text are private. page 4 of 4 skip code_
Student chosenOne = new Student("Harry Potter", 123456); Student topStudent = new Student("Hermione Granger", 314159);
topStudent.setCredits(97); chosenOne.getName();
ListenerExample
mouseClicked( e ) mousePressed( e ) mouseReleased( e ) mouseMoved( e ) mouseDragged( e ) Called when the user clicks the mouse Called when the mouse button is pressed Called when the mouse button is released Called when the user moves the mouse Called when the mouse is dragged with the button down
/** Raises a GOval, the sun / public class Sunrise extends GraphicsProgram { public GOval sun; public Sunrise() { this.start(); sun = new GOval(this.getWidth()/2 - 100, this.getHeight(), 200,200); sun.setFilled(true); sun.setColor(Color.YELLOW); this.add(sun); this.addMouseListeners(); } / executed when mouse button is clicked */ public void mouseClicked(MouseEvent e) { sun.move(0,-5); } }
/** Raises a GOval, the sun / public class Sunrise extends GraphicsProgram { public GOval sun; public Sunrise() { this.start(); sun = new GOval(this.getWidth()/2 - 100, this.getHeight(), 200,200); sun.setFilled(true); sun.setColor(Color.YELLOW); this.add(sun); this.addMouseListeners(); } / executed when mouse button is clicked */ public void mouseClicked(MouseEvent e) { sun.move(0,-5); } }
/** Raises a GOval, the sun / public class Sunrise extends GraphicsProgram { public GOval sun; public Sunrise() { this.start(); sun = new GOval(this.getWidth()/2 - 100, this.getHeight(), 200,200); sun.setFilled(true); sun.setColor(Color.YELLOW); this.add(sun); this.addMouseListeners(); } / executed when mouse button is clicked */ public void mouseClicked(MouseEvent e) { sun.move(0,-5); } }
import acm.program.; import acm.graphics.; import java.awt.event.; /*
page 1 of 2 /* 0-parameter constructor / public SunriseImage() { this.start(); / Create sun in its initial position / this.sun = new GImage("HappySun.jpg"); this.sun.setBounds(this.getWidth()/2 - SUN_SIZE/2, this.getHeight(), SUN_SIZE, SUN_SIZE); this.add(sun); / Add the mouse listener / this.addMouseListeners(); } / Each mouse click causes the GImage sun to move up */ public void mouseClicked(MouseEvent e) { sun.move(0,MOVE_UP); } }
page 2 of 2