Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Introduction to JAVA: COLOR.JAVA public class Color { private int red; private int green; private int blue; // Color constants public static final Color RED

Introduction to JAVA:

image text in transcribed

COLOR.JAVA

public class Color { private int red; private int green; private int blue;

// Color constants

public static final Color RED = new Color(255, 0, 0); public static final Color GREEN = new Color(0, 255, 0); public static final Color BLUE = new Color(0, 0, 255); public static final Color WHITE = new Color(255, 255, 255); public static final Color LIGHT_GRAY = new Color(192, 192, 192); public static final Color GRAY = new Color(128, 128, 128); public static final Color DARK_GRAY = new Color(64, 64, 64); public static final Color BLACK = new Color(0, 0, 0); public static final Color CYAN = new Color(0, 255, 255); public static final Color MAGENTA = new Color(255, 0, 255); public static final Color YELLOW = new Color(255, 255, 0); public static final Color PINK = new Color(255, 175, 175); public static final Color ORANGE = new Color(255, 200, 0); /** * Constructs a new Color object. * @param red the red value of the color (between 0 and 255) * @param green the green value of the color (between 0 and 255) * @param blue the blue value of the color (between 0 and 255) */ public Color(int red, int green, int blue) { this.red = red; this.green = green; this.blue = blue; }

/** * Gets the red value of this color. * @return the red value (between 0 and 255) */ public int getRed() { return red; }

/** * Gets the green value of this color. * @return the green value (between 0 and 255) */ public int getGreen() { return green; }

/** * Gets the blue value of this color. * @return the blue value (between 0 and 255) */ public int getBlue() { return blue; } }

THE TESTER:

/** * Tester for the Crayons class. * * */ public class CrayonsTester { public static void main(String[] args) { Color[] colors1 = { new Color(200, 100, 100), new Color(100, 0 ,255), Color.RED, Color.BLUE, Color.YELLOW }; Crayons box1 = new Crayons(colors1); System.out.println("Count: " + box1.numberOfCrayons()); System.out.println("Expected: 5"); System.out.println("Has more red: " + box1.moreRedThanBlue()); System.out.println("Expected: 3"); Color[] colors2 = { new Color(200, 100, 100), new Color(100, 0 ,255), Color.BLUE, Color.MAGENTA }; Crayons box2 = new Crayons(colors2); System.out.println("Count: " + box2.numberOfCrayons()); System.out.println("Expected: 4"); System.out.println("Has more red: " + box2.moreRedThanBlue()); System.out.println("Expected: 1"); Color[] colors3 = { new Color(200, 100, 100), new Color(100, 0 ,255), Color.BLUE, Color.MAGENTA, Color.WHITE, Color.PINK, Color.ORANGE, new Color(125, 125, 50), }; Crayons box3 = new Crayons(colors3); System.out.println("Has more red: " + box3.moreRedThanBlue()); System.out.println("Expected: 4"); Color[] colors4 = { new Color(200, 100, 100), new Color(100, 0 ,255), Color.GREEN, new Color(255, 0, 0), Color.PINK, Color.ORANGE, new Color(125, 125, 50), }; Crayons box4 = new Crayons(colors4); System.out.println("Has more red: " + box4.moreRedThanBlue()); System.out.println("Expected: 5"); } }
Write a Crayons class to model a box of crayons. There is no starter in Codecheck Provide the conststructor. The constructor takes an array of Color objects each representing the color of a crayon and initializes the instance variable Provide these methods for the Crayons class public int numberOfCrayons ) gets the number of crayons in the box of crayons. public int moreRedThanBlue ) gets the number of crayons that have more red than blue. The Color class has a method getRed0 to get the red value for the Color. It also has getGreenO and getBlue0 method. You will need to import the graphics package to get the Color class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions