Question
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print(Enter a value for r, g, b: );
import java.util.Scanner;
public class Main {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a value for r, g, b: "); Color c = new Color(sc.nextInt(), sc.nextInt(), sc.nextInt());
System.out.println(c); System.out.print(" Do you want to change a color? (y) "); String option = sc.next(); if(option.charAt(0) == 'y') { System.out.print(" What color do you want to change? (r/g/b) "); String color = sc.next(); System.out.print(" What value do you want? (0-255) "); int val = sc.nextInt(); if(color.charAt(0) == 'r') { c.setRed(val); } else if(color.charAt(0) == 'g') { c.setGreen(val); } else if(color.charAt(0) == 'b') { c.setBlue(val); } else { System.out.println(" Not a valid response. Exiting program."); } } else if(option.charAt(0) == 'n') { System.out.println("No changes made."); } else { System.out.println(" That's not a valid response. Exiting program."); }
System.out.println(c); } }
class Color { // instance variables private int red; private int green; private int blue;
// constructors public Color() { this(0, 0, 0); }
public Color(int red, int green, int blue) { setRed(red); setGreen(green); setBlue(blue); } // get/accessor methods public int getRed() { return red; }
public int getGreen() { return green; }
public int getBlue() { return blue; }
public int[] getColor() { int[] colors = {red, green, blue}; return colors; } // set/mutator methods public void setRed(int red) { checkColor(red); this.red = red; }
public void setGreen(int green) { checkColor(green); this.green = green; }
public void setBlue(int blue) { checkColor(blue); this.blue = blue; } // other methods public void checkColor(int color) { if(color 255) { System.out.println("Color must be in the interval 0-255"); System.exit(1); } }
public String toString() { return "R: " + red + " G: " + green + " B: " + blue; } }
a. Using Scanner, ask a user to enter how many Color objects they want created. b. Create an array to hold the objects. c. Create the amount of objects wanted by using the default constructor (sets all values to 0 ). Add them to your array. d. In the Color class, create another method that sets red, green and blue values all at once. The method name and parameters should be setColors(int r, int b, int g). - HINT: We want to make sure that colors are within the correct parameters [0-255], so which methods can you call when setting r, g, and b that you already have in the color class that does this check for you as well as set the value? Please look at my solution from last lab if you haven't already. e. In the Color class, create another method that sets the values of a color object using the method you created in 1d to random values. Use Math.random0 for choosing values for red, green, and blue. All values should be randomly chosen from an interval of [0-255]. This method should be called setRandom0 and shouldn't take any parameters. f. In the main method, change the values of your color objects using the method you created in 1e. g. Display the array by creating a static method that displays each color object on a separate line. h. Create another method for the Color class that sets all values of red, green and blue back to 0 . This method should be called setDefault) and not take any parameters. Use this method on your array of Color objects. Display your array. HINT: Use the setColors method again. \begin{tabular}{l} \hline \multicolumn{1}{|c|}{ Color } \\ \hline -red: int \\ -green: int \\ -blue: int \\ \hline +Color() \\ +Color(red:int, green:int, blue:int) \\ + getRed():int \\ +getGreen():int \\ +getBlue():int \\ +setRed(red:int) \\ +setGreen(green:int) \\ +setBlue(blue:int) \\ + checkColor(color:int):boolean \\ +getColors():int \\ + setColors(red:int, green:int, blue:int) \\ + setDefault \\ + setRandom() \\ + toString():String \end{tabular}Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started