Type the program's output ColorSorter.java Color.java import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class ColorSorter { Input public static void main (String[] args) { Scanner scnr = new Scanner (System.in); final int NUM_ELEMENTS = 5; ArrayList colors = new ArrayList(); 234 253 99 132 45 154 int i; 132 45 10 short red; short green; short blue; 132 231 201 3 45 174 for (i = 0; i < NUM_ELEMENTS; ++1) { red = (short) (scnr.nextShort () ) ; green = (short)scnr.nextShort(); blue = (short) scnr.nextShort (); colors.add (new Color(red, green, blue)); Output Collections.sort (colors); for (i = 0; i < NUM_ELEMENTS; ++i) { System.out.printin(colors.get(i));

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

public class ColorSorter {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      final int NUM_ELEMENTS = 5;
      ArrayList<Color> colors = new ArrayList<Color>();
      int i;
      short red;
      short green;
      short blue;

      for (i = 0; i < NUM_ELEMENTS; ++i) {
         red = (short)(scnr.nextShort());
         green = (short)scnr.nextShort();
         blue = (short)scnr.nextShort();
         colors.add(new Color(red, green, blue));
      }

      Collections.sort(colors);

      for (i = 0; i < NUM_ELEMENTS; ++i) {
         System.out.println(colors.get(i));
      }
   }
}

 

 

other :

public class Color implements Comparable<Color> {
   private short red;
   private short green;
   private short blue;

   Color(short red, short green, short blue) {
      this.red = red;
      this.green = green;
      this.blue = blue;
   }

   @Override
   public int compareTo(Color otherColor) {
      if (red < otherColor.red) {
         return 1;
      }
      else if (red > otherColor.red) {
         return -1;
      }
      else if (green < otherColor.green) {
         return 1;
      }
      else if (green > otherColor.green) {
         return -1;
      }

else if (blue < otherColor.blue) {
         return 1;
      }
      else if (blue > otherColor.blue) {
         return -1;
      }
      else {
         return 0;
      }
   }

   @Override
   public String toString() {
      return String.format("(%d, %d, %d)", red, green, blue);
   }
   }

 

 

Type the program's output
ColorSorter.java
Color.java
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class ColorSorter {
Input
public static void main (String[] args) {
Scanner scnr = new Scanner (System.in);
final int NUM_ELEMENTS = 5;
ArrayList<Color> colors = new ArrayList<Color> ();
234 253 99
132 45 154
int i;
132 45 10
short red;
short green;
132 231 201
short blue;
3 45 174
for (i = 0; i < NUM ELEMENTS; ++i) {
red = (short) (scnr.nextShort () );
green = (short) scnr.nextShort ();
blue = (short) scnr.nextShort () ;
colors.add (new Color (red, green, blue));
Output
}
Collections.sort (colors);
for (i = 0; i < NUM_ELEMENTS; ++i) {
System.out.println (colors.get (i));
}
}
Transcribed Image Text:Type the program's output ColorSorter.java Color.java import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class ColorSorter { Input public static void main (String[] args) { Scanner scnr = new Scanner (System.in); final int NUM_ELEMENTS = 5; ArrayList<Color> colors = new ArrayList<Color> (); 234 253 99 132 45 154 int i; 132 45 10 short red; short green; 132 231 201 short blue; 3 45 174 for (i = 0; i < NUM ELEMENTS; ++i) { red = (short) (scnr.nextShort () ); green = (short) scnr.nextShort (); blue = (short) scnr.nextShort () ; colors.add (new Color (red, green, blue)); Output } Collections.sort (colors); for (i = 0; i < NUM_ELEMENTS; ++i) { System.out.println (colors.get (i)); } }
public class Color implements Comparable<Color> {
private short red;
private short green;
private short blue;
Color (short red, short green, short blue) {
this.red = red;
this.green = green;
this.blue = blue;
Input
@override
public int compareTo (Color otherColor) {
if (red < otherColor.red) {
return 1;
234 253 99
132 45 154
else if (red > otherColor.red) {
132 45 10
return -1;
132 231 201
}
else if (green < otherColor.green) {
return 1;
3 45 174
Output
else if (green > otherColor.green) {
return -1;
}
else if (blue < otherColor.blue) {
return 1;
}
else if (blue > otherColor.blue) {
return -1;
else (
return 0;
@Override
public String toString () {
return String.format (" (šd, d, id)", red, green, blue);
Transcribed Image Text:public class Color implements Comparable<Color> { private short red; private short green; private short blue; Color (short red, short green, short blue) { this.red = red; this.green = green; this.blue = blue; Input @override public int compareTo (Color otherColor) { if (red < otherColor.red) { return 1; 234 253 99 132 45 154 else if (red > otherColor.red) { 132 45 10 return -1; 132 231 201 } else if (green < otherColor.green) { return 1; 3 45 174 Output else if (green > otherColor.green) { return -1; } else if (blue < otherColor.blue) { return 1; } else if (blue > otherColor.blue) { return -1; else ( return 0; @Override public String toString () { return String.format (" (šd, d, id)", red, green, blue);
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education