CHAPTER 4 - // assumes java.util.Random and java.awt.Color...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Java Software Solutions, 4e Lewis and Loftus Chapter 4 Exercise Solutions 4.1 Write a method called randomInRange that accepts two integer parameters representing a range. The method should return a random integer in the specified range (inclusive). Return zero if the first parameter is greater than the second. // assumes java.util.Random is imported public int randomInRange (int first, int second) { int result = 0; Random generator = new Random(); if (first <= second) { int range = second – first + 1; result = generator.nextInt(range) + first; } return result; } 4.2 Write a method called randomColor that creates and returns a Color object that represents a random color. Recall that a Color object can be defined by three integer values between 0 and 255 representing the contributions of red, green, and blue (its RGB value). final int MAX = 256;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // assumes java.util.Random and java.awt.Color are imported public Color randomColor () { Random generator = new Random(); int randRed = generator.nextInt(MAX); int randGreen = generator.nextInt(MAX); int randBlue = generator.nextInt(MAX); return new Color(randRed, randGreen, randBlue); } 4.3 Draw a UML class diagram that shows the relationships among the classes used in the PushCounter program. Java Software Solutions, 4e Lewis and Loftus PushCounter - APPLET_WIDTH : int - APPLET_HEIGHT : int - pushes : int JApplet JLabel JButton ButtonListener 4.4 Draw a UML class diagram that shows the relationships among the classes used in the Fahrenheit program. Fahrenheit + main (args : String) : void FahrenheitGUI - WIDTH : int - HEIGHT : int + display() : void 1 1 JFrame JPanel JLabel JTextField...
View Full Document

This note was uploaded on 05/17/2011 for the course COP 3530 taught by Professor Davis during the Spring '08 term at University of Florida.

Page1 / 2

CHAPTER 4 - // assumes java.util.Random and java.awt.Color...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online