Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class RGB { private int R,G,B; public RGB() { this(0,0,0); } public RGB(int R,int G,int B) { this.R=R; this.G=G; this.B=B; } public int[] getColor() {

image text in transcribed

class RGB { private int R,G,B; public RGB() { this(0,0,0); } public RGB(int R,int G,int B) { this.R=R; this.G=G; this.B=B; } public int[] getColor() { return new int[]{R,G,B}; } public void setColor(int[] c) { R=c[0]; G=c[1]; B=c[2]; } public String toString() { return "("+R+","+G+","+B+")"; } public boolean equals(RGB r) { int[] c=r.getColor(); return (this.R==c[0]) && (this.G==c[1]) && (this.B==c[2]); } public RGB invert() { return new RGB(255-R,255-G,255-B); } }

=================================================================

public class Shape { //Attributes private int numOfSides; private String color; private boolean striped; //Constructors public Shape(int s, String c, boolean str) { numOfSides = s; color = c; striped = str; } public Shape() { numOfSides = 4; color = "red"; striped = false; } //get methods public int getNumOfSides() { return numOfSides; } public String getColor() { return color; } public boolean getStriped() { return striped; } //set methods public void setNumOfSides(int n) { numOfSides = n; } public void setColor(String c) { color = c; } public void setStriped(boolean b) { striped = b; } //Methods public String toString() { return "numOfSides: " + numOfSides + " color: " + color + " striped: " + striped; } public boolean equals(Shape s) { if (this.numOfSides == s.getNumOfSides() && this.color.equals(s.getColor()) && this.striped == s.getStriped()) return true; return false; } }

============================================================

public class Triangle extends Shape { private double side1; private double side2; private double side3; public Triangle(int s, String c, boolean str, double side1, double side2, double side3) { super(s, c, str); this.side1 = side1; this.side2 = side2; this.side3 = side3; setNumOfSides(3); } public Triangle(double side1, double side2, double side3) { super(); this.side1 = side1; this.side2 = side2; this.side3 = side3; setNumOfSides(3); } public Triangle() { super(); this.side1 = 1.0; this.side2 = 1.0; this.side3 = 1.0; setNumOfSides(3); } public double getSide1() { return side1; } public void setSide1(double side1) { this.side1 = side1; } public double getSide2() { return side2; } public void setSide2(double side2) { this.side2 = side2; } public double getSide3() { return side3; } public void setSide3(double side3) { this.side3 = side3; } public double perimeter() { return side1 + side2 + side3; } public double area() { double s = (side1 + side2 + side3) / 2; return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); } @Override public String toString() { return super.toString()+" "+ "Triangle"+" "+ "\tside1 = "+side1+" "+ "\tside2 = "+side2+" "+ "\tside3 = "+side3+" "+ "\tArea = " +area()+" "+ "\tPerimeter = "+perimeter()+" "; } public boolean equals(Shape s) { if (s != null && s instanceof Triangle) { Triangle aTriangle = (Triangle) s; return super.equals(s) && side1 == aTriangle.side1 && side2 == aTriangle.side2 && side3 == aTriangle.side3; } return false; } } 

============================================================

import java.util.*; public class TriangleTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String color; boolean flag = false; System.out.print("Enter a color: "); color = sc.nextLine(); System.out.print("Is the triangle stiped (yes or no): "); flag = sc.nextLine().equalsIgnoreCase("yes"); double side1 = 0, side2 = 0, side3 = 0; boolean validSides = false; for (int attempt = 1; attempt  

2. Using the RGB class on blackboard we are going to update the Shape class. First read through the RGB.java file. Make sure you understand what each line is doing. How should we use RGB to modify the Shape class? Now update the color attribute in Shape to be an RGB object. You also need to modify the triangle and test classes. 3. Add an exception for checking if RGB values are outside [0,255). 4. Add an exception for checking if the sides of a triangle are valid. 5. Practice throwing and handling the exceptions. 2. Using the RGB class on blackboard we are going to update the Shape class. First read through the RGB.java file. Make sure you understand what each line is doing. How should we use RGB to modify the Shape class? Now update the color attribute in Shape to be an RGB object. You also need to modify the triangle and test classes. 3. Add an exception for checking if RGB values are outside [0,255). 4. Add an exception for checking if the sides of a triangle are valid. 5. Practice throwing and handling the exceptions

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

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions