Question

Please use Java Problem 1. Implement the Comparable interface of a class ShoppingItem. Each ShoppingItem contains...

Please use Java

Problem 1. Implement the Comparable interface of a class ShoppingItem. Each ShoppingItem contains a title and a weight in pounds. Design a tester class, and sorted a list of input ShoppingItem by their weight. The output should show the title and weight.

For testing, please use these inputs:

  • Fitness equipment, 150 pounds
  • TV screen, 50 pounds
  • Apple watch, 0.1 pound
  • Book, 0.5 pound
  • Bag, 2 pound
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

class ShoppingItem implements Comparable{
private String title;
private double quantity;

   public ShoppingItem(String aTitle, double aQuantity) {
   super();
   title = aTitle;
   quantity = aQuantity;
}

   public String getTitle() {
       return title;
   }

   public void setTitle(String aTitle) {
       title = aTitle;
   }

   public double getQuantity() {
       return quantity;
   }

   public void setQuantity(double aQuantity) {
       quantity = aQuantity;
   }

   @Override
   public String toString() {
       return title + " , "+ quantity+" pounds";
   }

   @Override
   public int compareTo(Object aO) {
       //sorting based on the weight
       ShoppingItem item=(ShoppingItem)aO;
       return new Double(this.quantity).compareTo(new Double(item.getQuantity()));
   }
  
}
public class TestShoppingItem {
public static void main(String[] args) {
   ArrayList<ShoppingItem>list= new ArrayList<ShoppingItem>();
   list.add(new ShoppingItem("Fitness equipment",150));
   list.add(new ShoppingItem("Tv screen",50));
   list.add(new ShoppingItem("Apple watch",0.1));
   list.add(new ShoppingItem("Book",0.5));
   list.add(new ShoppingItem("Bag ",2));
   Collections.sort(list);
   for(ShoppingItem i:list)
       System.out.println(i);
  
  
}
}

Add a comment
Know the answer?
Add Answer to:
Please use Java Problem 1. Implement the Comparable interface of a class ShoppingItem. Each ShoppingItem contains...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1. Implement the Comparableinterface of a class ShoppingItem. Each ShoppingItemcontains a titleand a weightin pounds....

    Problem 1. Implement the Comparableinterface of a class ShoppingItem. Each ShoppingItemcontains a titleand a weightin pounds. Design a tester class, and sorted a list of input ShoppingItemby their weight. The output should show the titleand the weight. For testing, please use these inputs: Fitness equipment, 150 pounds TV screen, 50 pounds Apple watch, 0.1 pound Book, 0.5 pound Bag,  2 pound

  • JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other);...

    JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other); } A Film class is defined as public class Film {      private String title;      private int yearOfRelease;           public Film(String title, int yearOfRelease) {           super();           this.title = title;           this.yearOfRelease = yearOfRelease;      }           public void display()      { System.out.println("Title " + title +". Release" + yearOfRelease);      } } Rewrite the Film class so that it...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • In java please Create an immutable class named Card implementing the Comparable interface that will represent...

    In java please Create an immutable class named Card implementing the Comparable interface that will represent a card, have a suit: diamonds - lowest, clubs, hearts, spades - highest) and a rank ( 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A). The class must have a constructor provided with a suit and rank, and the following methods: toString(), getSuit(), getRank(), print(). Create a static method that will randomly generate a card. A card is less...

  • -please use java 3. Interface: Implement a program that creates an Interfacecalled Vehiclewith the following abstract...

    -please use java 3. Interface: Implement a program that creates an Interfacecalled Vehiclewith the following abstract methods(getters): getMake(), getModel(), getYear(), and creates a Classcalled Carthatimplements the Vehicleinterface. The Carclass also contains instance data that represents the make, model, and year of the carand the constructor to initialize these values.Create a driver class called CarTest, whose main method instantiates a Carobject with the following information: Chevrolet, Impala, 2019, and test the getter methods you implement. 4. Abstract Class: Implement the above...

  • please write code in java and comment. thanks. the program is about interface . Implement the...

    please write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are...

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

  • JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees....

    JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare ();...

  • I need some help doing this Java project. 1.Implement the MySort class with the following operations....

    I need some help doing this Java project. 1.Implement the MySort class with the following operations. a.bubbleSort(MyArrayList arraylist) – conduct bubble sort on the elements contained in a MyArrayList object. b.selectionSort(MyArrayList arraylist) – conduct selection sort on the elements contained in a MyArrayList object. 2.Implement the MySearch class with the following operations. a.binarySearch(MyArrayList arraylist, Comparable target) – conduct binary search on sorted elements contained in a MyArrayList object. b.linearSearchSorted (MyArrayList arraylist, Comparable target) – conduct linear search on sorted elements...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT