Java. Refer to attachment. import java.util.*; public class PoD { public static void main( String [] args ) { Scanner in = new Scanner( System.in ); FruitBasket fruitBasket = new FruitBasket(); while(in.hasNextLine()) { String line = in.nextLine(); String[] fruitDetails = line.split(" "); Fruit nextFruit = new Fruit(fruitDetails[0],fruitDetails[1], Double.parseDouble(fruitDetails[2])); fruitBasket.addFruit(nextFruit); } System.out.println("--- BEFORE SORT ---"); System.out.println(fruitBasket); Collections.sort(fruitBasket.basket); System.out.println("--- AFTER SORT ---"); System.out.println(fruitBasket); in.close(); System.out.print("END OF OUTPUT"); } }   import java.util.*; public class FruitBasket { //attributes protected ArrayList basket = new ArrayList(); //constructor public FruitBasket(){} //Setters public void addFruit(Fruit fruitToAdd) { this.basket.add(fruitToAdd); } public String toString() { String basketContents = "FRUIT BASKET:\n"; for (Fruit fruit: basket) { basketContents += fruit.toString()+"\n"; } return basketContents; } }   import java.util.*; public class Fruit implements Comparable { //attributes protected String name = null; protected String colour = null; protected double weight = 0; //constructor public Fruit(String name, String colour, double weight) { this.name = name; this.colour = colour; this.weight = weight; } //Getters public String getName(){return name;} public String getColour(){return colour;} public double getWeight(){return weight;} //Setters public void setName(String name){this.name = name;} public void setColour(String colour){this.colour = colour;} public void setWeight(double weight){this.weight = weight;} public String toString() { String fruitDetails = name+" (colour: "+colour+", weight: "+weight+")"; return fruitDetails; } // Write the compareTo method here! }     Input: apple red 1.111 apple red 1.1432 apple red 7.11888 apple green 8.00001 apple red 1.989 apple green 2.222 apple red 8.0

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

Java. Refer to attachment.

import java.util.*;

public class PoD
{

public static void main( String [] args ) {

Scanner in = new Scanner( System.in );

FruitBasket fruitBasket = new FruitBasket();

while(in.hasNextLine())
{
String line = in.nextLine();
String[] fruitDetails = line.split(" ");

Fruit nextFruit = new Fruit(fruitDetails[0],fruitDetails[1], Double.parseDouble(fruitDetails[2]));
fruitBasket.addFruit(nextFruit);
}

System.out.println("--- BEFORE SORT ---");
System.out.println(fruitBasket);

Collections.sort(fruitBasket.basket);

System.out.println("--- AFTER SORT ---");
System.out.println(fruitBasket);

in.close();
System.out.print("END OF OUTPUT");
}
}

 

import java.util.*;

public class FruitBasket
{
//attributes
protected ArrayList<Fruit> basket = new ArrayList<Fruit>();

//constructor
public FruitBasket(){}

//Setters
public void addFruit(Fruit fruitToAdd)
{
this.basket.add(fruitToAdd);
}

public String toString()
{
String basketContents = "FRUIT BASKET:\n";
for (Fruit fruit: basket)
{
basketContents += fruit.toString()+"\n";
}

return basketContents;
}
}

 

import java.util.*;


public class Fruit implements Comparable<Fruit>
{
//attributes
protected String name = null;
protected String colour = null;
protected double weight = 0;

//constructor
public Fruit(String name, String colour, double weight)
{
this.name = name;
this.colour = colour;
this.weight = weight;
}

//Getters
public String getName(){return name;}
public String getColour(){return colour;}
public double getWeight(){return weight;}

//Setters
public void setName(String name){this.name = name;}
public void setColour(String colour){this.colour = colour;}
public void setWeight(double weight){this.weight = weight;}

public String toString()
{
String fruitDetails = name+" (colour: "+colour+", weight: "+weight+")";
return fruitDetails;
}

// Write the compareTo method here!

}

 

 

Input:

apple red 1.111

apple red 1.1432

apple red 7.11888

apple green 8.00001

apple red 1.989

apple green 2.222

apple red 8.0

10
11
AFTER SORT
---
12
FRUIT BASKET:
13 apple (colour: green, weight: 8.00001)
14 apple (colour: red, weight: 8.0)
15 apple (colour: red, weight: 7.11888)
16 apple (colour: green, weight: 2.222)
17 apple (colour: red, weight: 1.989)
18 apple (colour: red, weight: 1.1432)
19 apple (colour: red, weight: 1.111)
20
21
END OF OUTPUT
THE CORRECT OUTPUT OF THE TEST CASE
6 apple (colour: green, weight: 8.00001)
7 apple (colour: red, weight: 1.989)
8 apple (colour: green, weight: 2.222)
9 apple (colour: red, weight: 8.0)
10
11
AFTER SORT
---
12
FRUIT BASKET:
13 apple (colour: red, weight: 1.111)
14 apple (colour: red, weight: 1.1432)
15 apple (colour: red, weight: 1.989)
16 apple (colour: green, weight: 2.222)
17 apple (colour: red, weight: 7.11888)
18 apple (colour: red, weight: 8.0)
19 apple (colour: green, weight: 8.00001)
20
Transcribed Image Text:10 11 AFTER SORT --- 12 FRUIT BASKET: 13 apple (colour: green, weight: 8.00001) 14 apple (colour: red, weight: 8.0) 15 apple (colour: red, weight: 7.11888) 16 apple (colour: green, weight: 2.222) 17 apple (colour: red, weight: 1.989) 18 apple (colour: red, weight: 1.1432) 19 apple (colour: red, weight: 1.111) 20 21 END OF OUTPUT THE CORRECT OUTPUT OF THE TEST CASE 6 apple (colour: green, weight: 8.00001) 7 apple (colour: red, weight: 1.989) 8 apple (colour: green, weight: 2.222) 9 apple (colour: red, weight: 8.0) 10 11 AFTER SORT --- 12 FRUIT BASKET: 13 apple (colour: red, weight: 1.111) 14 apple (colour: red, weight: 1.1432) 15 apple (colour: red, weight: 1.989) 16 apple (colour: green, weight: 2.222) 17 apple (colour: red, weight: 7.11888) 18 apple (colour: red, weight: 8.0) 19 apple (colour: green, weight: 8.00001) 20
You will implement a comparable interface within Frult objects. Pretend that all fruit costs a toonie, no matter the size. To get more for your money, you will want to buy the bigger fruit.
Details
Input
Input for this problem will be a fruit name, followed by a colour, and then the weight. Input has been handled for you in POD.java.
Processing & Output
Compare the weight of the object with the weight of the Frult object taken as input.
The value you return from the compareTo() method will be as follows:
• If the weight of the fruits is equal, you will return 0.
• If the current fruit object is greater, return 1.
• Otherwise (i.e. the other fruit object is greater), you will return -1.
This will allow us to sort the fruit objects by weight. This sort is handled in the main method of PoD.java. All output from that method is handled for you.
Transcribed Image Text:You will implement a comparable interface within Frult objects. Pretend that all fruit costs a toonie, no matter the size. To get more for your money, you will want to buy the bigger fruit. Details Input Input for this problem will be a fruit name, followed by a colour, and then the weight. Input has been handled for you in POD.java. Processing & Output Compare the weight of the object with the weight of the Frult object taken as input. The value you return from the compareTo() method will be as follows: • If the weight of the fruits is equal, you will return 0. • If the current fruit object is greater, return 1. • Otherwise (i.e. the other fruit object is greater), you will return -1. This will allow us to sort the fruit objects by weight. This sort is handled in the main method of PoD.java. All output from that method is handled for you.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Unreferenced Objects
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