Please help me code the following in: JAVA Please use many COMMENTS and read the
ID: 3719408 • Letter: P
Question
Please help me code the following in: JAVA
Please use many COMMENTS and read the task THOROUGHLY!
Full points will be awarded, thanks in advance!
ColorException Extends RuntimeException As programs execute, they may encounter exceptional situations in which they could either try to recover or simply terminate due to the situation at hand. We're going to make a specific type of exception for our SimpleColor (and ColorWithAlpha) class, in the event the user tries to set any channel to anything outside the range [0,255], inclusive. Note that this class will be only a handful of lines, and demonstrates how inheritance can quickly produce fully functioning classes in a short amount of time. 1. Create a new project for this lab Define a new class called ColorException Build a default, no-arg constructor that contains only one line Overload your constructor with a second constructor that takes a String "msg" as input. Then, 2. a. This class should include "extends RuntimeException" 3. a. super( "An error occurred in Color"); 4. the only line of code in this function will be: a. super(msg); 5 In your old SimpleColor class (and in your new ColorWithAlpha class below), in the setter functions (setRed, setGreen, setBlue, etc.), throw a new ColorException if the user tries to set a value outside of [0,255], inclusive. To test your exception, make a main in ColorException and include the line: 6. a. throw new ColorException( "A test in main");Explanation / Answer
import java.util.*;
class SimpleColor
{
private int r,g,b;
public int getR()
{
return r;
}
public void setR(int r)
{
if(r<0 || r>255)
throw new ColorException();
this.r = r;
}
public int getG()
{
return g;
}
public void setG(int g)
{
if(g<0 || g>255)
throw new ColorException();
this.g = g;
}
public int getB()
{
return b;
}
public void setB(int b)
{
if(b<0 || b>255)
throw new ColorException();
this.b = b;
}
public void setColor(int a,int b,int c)
{
setR(a);
setG(b);
setB(c);
}
public SimpleColor(){}
public SimpleColor(int r,int g,int b)
{
setR(r);
setG(g);
setB(b);
}
public SimpleColor(SimpleColor b)
{
this(b.r,b.g,b.b);
}
}
class ColorException extends RuntimeException
{
public ColorException()
{
super("An error occurred in Color");
}
public ColorException(String msg)
{
super(msg);
}
}
class Test
{
public static void main (String[] args)
{
try
{
//SimpleColor sc = new SimpleColor(256,134,200);
throw new ColorException("Test in main");
}
catch(ColorException ex)
{
System.out.println(ex.toString());
}
}
}
Output:
ColorException: An error occurred in Color
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.