Question

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

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);
  
  
}
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Problem 1. Implement the Comparableinterface of a class ShoppingItem. Each ShoppingItemcontains a titleand a weightin pounds....
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
  • Design a program that inputs a list of up to 50 real values from a data...

    Design a program that inputs a list of up to 50 real values from a data file, Problem1InputFile.txt, and print the count and smallest value to the screen. Hint: an array is not really needed, but you can use one. Here is one sample input file for Problem1InputFile.txt (note that there is one real value per line and there is an extra blank line at the end): 10.5 2.1 2.0 -1.7 -0.5 Sample output: Count:       5 Smallest: -1.7 Give pseudocode...

  • Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials are...

    Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials are stored in a linked list of Term objects (two classes: Polynomial and Term). Term objects have two data fields: coefficient and exponent, both positive integers. Polynomial will have some methods that create linked list functionality, but not all list operations are needed; include only those used by this program. Do not use a separate linked list class. The list will be non-circular. Make a...

  • Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry...

    Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry in the dictionary is a pair: (word, meaning). Word is a one-word string, meaning can be a string of one or more words (it’s your choice of implementation, you can restrict the meaning to one-word strings). The dictionary is case-insensitive. It means “Book”, “BOOK”, “book” are all the same . Your dictionary application must provide its operations through the following menu (make sure that...

  • This assignment was locked Mar 24 at 11:59pm. For this lab you will implement a phone...

    This assignment was locked Mar 24 at 11:59pm. For this lab you will implement a phone book using a linked list to keep people's names and phone numbers organized in alphabetical order. 1. Define a structure to hold the contact information including: name, phone number, a pointer to the next node in the list. Example: struct ContactNode { string name; string phoneNumber; ContactNode *next; } 2. Define a class containing the structure and the appropriate methods to update and retrieve...

  • Create a C program to implement a grade book. Must follow these guidelines: 1. Use #define...

    Create a C program to implement a grade book. Must follow these guidelines: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct { char name[MAX_SIZE1]; int scores[MAX_SIZE2]; } 3. The program accepts from the comand line an integer n (<= 30) as the number of students in the class. You may assume that the input will always be valid. 4. Dynamically allocate memory for an array of...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag...

    ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag collection using a linked list and, in order to focus on the linked list implementation details, we will implement the collection to store only one type of object of your choice (i.e., not generic). You can use the object you created for program #2 IMPORTANT: You may not use the LinkedList class from the java library. Instead, you must implement your own linked list...

  • Prof. Tassos Dimitriou Homework 3 Deadline: Monday, April 1, 2019, IN CLASS Problem 3 [10 points ...

    problem3 Prof. Tassos Dimitriou Homework 3 Deadline: Monday, April 1, 2019, IN CLASS Problem 3 [10 points a) (5 points) Construct a circuit that takes as input a 3-bit number X - X2XiXo and increments it by one. Le. if the input is 101 the output should be 110. Use only half adders. b) Construct a circuit that takes as input a 3-bit number X-xx,xo and decrements it by one. 1. (5 points) Show the truth table of the circuit....

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

  • Part 1 The purpose of this part of the assignment is to give you practice in...

    Part 1 The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class. The class will have the following data members: A string for the name of the author A string for the book title A long integer for the ISBN The class will have the following member functions (details about each one are...

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
ADVERTISEMENT