I need to create a sorting method for an array of tunes objects. Each tune objec
ID: 3649065 • Letter: I
Question
I need to create a sorting method for an array of tunes objects.Each tune object has 3 private variables: String artist, String title, and int year.
I need to create the following sorting methods:
public void sortTunesByYear() : rearranges the tunes in the array to be in increasing order by year.
public void sortTunesByTitle() : rearranges the tunes in the array to be in alphabetical order by title
public String toString() : returns a String representation which first displays the number of books in the library, then uses the toString function of each tune object in its array to display each tune it contains.
I've seen similar programs using List or something like that, but I would prefer something that doesn't use List since I'm not familiar with that.
Thank you
Explanation / Answer
//below is the code to sort item object based on either price or Item name. //It doesn't involve list concept //hope it helps import java.util.*; public class Items { // * A String instance variable to hold the item name private String itemName; // * A double instance variable to hold the price private double itemPrice; // * A constructor that takes a String and double to initialize the instance variables public Items (String itemName, double itemPrice) { this.itemPrice = itemPrice; this.itemName = itemName; } public Items() { } // * A get and set method for each instance variable public String getName() { return itemName; } public double getPrice() { return itemPrice; } public void setItemName(String someItem) { itemName = someItem; } public void setItemPrice(double somePrice) { itemPrice = somePrice; } public String toString() { return "Item: " + itemName + " price:" + itemPrice; } static final Comparator NAME_ORDER = new Comparator() { public int compare(Items e1, Items e2) { return e1.getName().compareTo(e2.getName()); } }; static final Comparator PRICE_ORDER = new Comparator() { public int compare(Items e1, Items e2) { return Double.compare(e1.getPrice(), e2.getPrice()); } }; } Tester Class import javax.swing.*; import java.util.*; import java.text.*;//Imported for NumberFormat // Once you have this class created, you write a second class named CoffeeDriver. This class has the following methods: public class CoffeeDriver { // sortName – this method sorts the array of items by item name and then displays the name and price of all items on the screen public static void sortName(Items arr[]) { Arrays.sort(arr, Items.NAME_ORDER); String out=""; for(Items item : arr){ out+= item.toString()+" "; } JOptionPane.showMessageDialog(null, out); } // sorts the array by price and displays the list public static void sortPrice(Items arr[]){ Arrays.sort(arr, Items.PRICE_ORDER); String out=""; for(Items item : arr){ out+= item.toString()+" "; } JOptionPane.showMessageDialog(null, out); } // main - It creates an array of Item objects using the data above to set each Item's information. public static void main(String []args) { String userSorted; int x; Items arr[] = new Items[5]; arr[0] = new Items(); arr[0].setItemName("Coffee"); arr[0].setItemPrice(1.00); arr[1] = new Items(); arr[1].setItemName("Water"); arr[1].setItemPrice(2.00); arr[2] = new Items(); arr[2].setItemName("Milk"); arr[2].setItemPrice(1.50); arr[3] = new Items(); arr[3].setItemName("Bagel"); arr[3].setItemPrice(1.25); arr[4] = new Items(); arr[4].setItemName("Donut"); arr[4].setItemPrice(0.75); int selectSort; JOptionPane.showMessageDialog(null, "Welcome to The Coffee Shop!"); userSorted = JOptionPane.showInputDialog(null, "Please indicate how you would like to sort the menu. To sort by price, press 1. To sort by name, press 2. To exit the program, press 3."); selectSort = Integer.parseInt(userSorted); // insert do while loop here if (selectSort == 1) sortPrice(arr); if (selectSort == 2) sortName(arr); if (selectSort == 3) JOptionPane.showMessageDialog(null, "Thank you for using Wings Coffee Shop. Have a great day!"); System.exit(0); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.