Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
11th Edition
ISBN: 9780134671710
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 20.6, Problem 20.6.2CP
Explanation of Solution
Comparable interface:
Comparable interface offers the sequence of single sorting which offers the compareTo() method to sort the data.
- It helps the compare the data of same classes.
- The Comparable interface is present in java.lang package.
Class to implement the Comparable interface is given below:
//Class A is implements the Comparable interface
public class A implements Comparable<A>
{
//Method definition
public int compareTo(A o)
{
//Return the value
return variable;
}
}
Explanation:
In the above code,
- Define the class with name of “A” which is implements the Comparable interface.
- This interface contains the compareTo() method to return the value.
“Yes”, two instances of the “A” class comparable.
Reason:
- Comparable interface helps the compare the data of same classes.
- It offers the compareTo() method to sort the data.
Class to implement the Comparator interface is given below:
/*Class B is implements the Comparator interface with type "B1"...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
In this project you will implement a Set class which represents a general collection of values. For this assignment, a set is generally defined as a list of values that is sorted and does not contain any duplicate values. More specifically, a set shall contain no pair of elements e1 and e2 such that e1.equals(e2) and no null elements. Requirements To ensure consistency among all implementations there are some requirements that all implementations must maintain. • Your implementation should reflect the definition of a set at all times. • For simplicity, your set will be used to store Integer objects. • An ArrayList object must be used to represent the set. • All methods that have an object parameter must be able to handle an input of null. • Methods such as Collections.sort that automatically sort a list may not be used. • The Set class shall reside in the default package. Recommendations There are deviations in program implementation that are acceptable and will not impact the overall…
In this project you will implement a Set class which represents a general collection of values. For this
assignment, a set is generally defined as a list of values that is sorted and does not contain any
duplicate values. More specifically, a set shall contain no pair of elements e1 and e2 such that
e1.equals(e2) and no null elements.
Requirements
To ensure consistency among all implementations there are some requirements that all implementations
must maintain.
• Your implementation should reflect the definition of a set at all times.
• For simplicity, your set will be used to store Integer objects.
• An ArrayList<Integer> object must be used to represent the set.
• All methods that have an object parameter must be able to handle an input of null.
• Methods such as Collections.sort that automatically sort a list may not be used.
• The Set class shall reside in the default package.
Recommendations
There are deviations in program implementation that are acceptable and will not…
In this project you will implement a Set class which represents a general collection of values. For this
assignment, a set is generally defined as a list of values that is sorted and does not contain any
duplicate values. More specifically, a set shall contain no pair of elements e1 and e2 such that
e1.equals(e2) and no null elements.
Requirements
To ensure consistency among all implementations there are some requirements that all implementations
must maintain.
• Your implementation should reflect the definition of a set at all times.
• For simplicity, your set will be used to store Integer objects.
• An ArrayList<Integer> object must be used to represent the set.
• All methods that have an object parameter must be able to handle an input of null.
• Methods such as Collections.sort that automatically sort a list may not be used.
• The Set class shall reside in the default package.
Recommendations
There are deviations in program implementation that are acceptable and will not…
Chapter 20 Solutions
Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
Ch. 20.2 - Prob. 20.2.1CPCh. 20.2 - Prob. 20.2.2CPCh. 20.2 - Prob. 20.2.3CPCh. 20.2 - Prob. 20.2.4CPCh. 20.2 - Prob. 20.2.5CPCh. 20.3 - Prob. 20.3.1CPCh. 20.3 - Prob. 20.3.2CPCh. 20.3 - Prob. 20.3.3CPCh. 20.3 - Prob. 20.3.4CPCh. 20.4 - Prob. 20.4.1CP
Ch. 20.4 - Prob. 20.4.2CPCh. 20.5 - Prob. 20.5.1CPCh. 20.5 - Suppose list1 is a list that contains the strings...Ch. 20.5 - Prob. 20.5.3CPCh. 20.5 - Prob. 20.5.4CPCh. 20.5 - Prob. 20.5.5CPCh. 20.6 - Prob. 20.6.1CPCh. 20.6 - Prob. 20.6.2CPCh. 20.6 - Write a lambda expression to create a comparator...Ch. 20.6 - Prob. 20.6.4CPCh. 20.6 - Write a statement that sorts an array of Point2D...Ch. 20.6 - Write a statement that sorts an ArrayList of...Ch. 20.6 - Write a statement that sorts a two-dimensional...Ch. 20.6 - Write a statement that sorts a two-dimensional...Ch. 20.7 - Are all the methods in the Collections class...Ch. 20.7 - Prob. 20.7.2CPCh. 20.7 - Show the output of the following code: import...Ch. 20.7 - Prob. 20.7.4CPCh. 20.7 - Prob. 20.7.5CPCh. 20.7 - Prob. 20.7.6CPCh. 20.8 - Prob. 20.8.1CPCh. 20.8 - Prob. 20.8.2CPCh. 20.8 - Prob. 20.8.3CPCh. 20.9 - How do you create an instance of Vector? How do...Ch. 20.9 - How do you create an instance of Stack? How do you...Ch. 20.9 - Prob. 20.9.3CPCh. 20.10 - Prob. 20.10.1CPCh. 20.10 - Prob. 20.10.2CPCh. 20.10 - Prob. 20.10.3CPCh. 20.11 - Can the EvaluateExpression program evaluate the...Ch. 20.11 - Prob. 20.11.2CPCh. 20.11 - If you enter an expression "4 + 5 5 5", the...Ch. 20 - (Display words in ascending alphabetical order)...Ch. 20 - (Store numbers in a linked list) Write a program...Ch. 20 - (Guessing the capitals) Rewrite Programming...Ch. 20 - (Sort points in a plane) Write a program that...Ch. 20 - (Combine colliding bouncing balls) The example in...Ch. 20 - (Game: lottery) Revise Programming Exercise 3.15...Ch. 20 - Prob. 20.9PECh. 20 - Prob. 20.10PECh. 20 - (Match grouping symbols) A Java program contains...Ch. 20 - Prob. 20.12PECh. 20 - Prob. 20.14PECh. 20 - Prob. 20.16PECh. 20 - (Directory size) Listing 18.10,...Ch. 20 - Prob. 20.20PECh. 20 - (Nonrecursive Tower of Hanoi) Implement the...Ch. 20 - Evaluate expression Modify Listing 20.12,...
Knowledge Booster
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
- Implement the Plates class buildMap function so that it populates the HashMap with the state abbreviations as keys and the counts of how many each appear in the file as values. Sometimes, the parking attendant will add special notation to help her remember something about a specific entry. There are just non alphabetic characters that she adds to the state - your program should ignore these characters so that an entry like NY* still counts toward the NY plate count. She is also very inconsistent with how she enters the plates. Sometimes she uses upper case, sometimes lowercase, and sometimes she even uses a mix. Be sure to account for this in your program. Only add information for plates in New England (Maine, New Hampshire, Vermont, Massachusetts, Rhode Island, and Connecticut). Plates.java import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class Plates { private Map<String, Integer> plateMap;…arrow_forwardNeed help making a java file that combines both linearSearch and binarySearch •Both search methods must use the Comparable<T> interface and the compareTo() method.•Your program must be able to handle different data types, i.e., use generics.•For binarySearch, if you decide to use a midpoint computation formula that is different fromthe textbook, explain that formula briefly as a comment within your code. //code from textbook //linearSearch public static <T> boolean linearSearch(T[] data, int min, int max, T target) { int index = min; boolean found = false; while (!found && index <= max) { found = data [index].equals(target); index++; } return found; } //binarySearch public static <T extends Comparable<T>> boolean binarySearch(T[] data, int min, int max, T target) { boolean found = false; int midpoint = (min + max)/2; if (data[midpoint].compareTo(target)==0)…arrow_forwardI am trying to practice Java Programming and would like to see and example of a program so I can better understand ArrayList using generics, Asymptotics, Searching, and Sorting. All variables should be inaccessible from other classes and must require an instance to be accessed through, unless specified otherwise. All methods should be accessible from everywhere and must require an instance to be accessed through, unless specified otherwise. Make sure to reuse code when applicable. I only one part of the full program I am writing from you. This is for practice so I can better understand some areas I am having trouble with. Thank you IceCream.Java This class will describe a certain kind of dessert that Bob can pick: ice cream. Write this class so Bob can have ice cream. This class will have to extend the Dessert class.Variables:• int scoops – the number of scoops of ice cream you get• boolean cone – represents if the ice cream has a coneConstructor(s):• A constructor that takes in…arrow_forward
- I am trying to practice Java Programming and would like to see and example of a program so I can better understand ArrayList using generics, Asymptotics, Searching, and Sorting. All variables should be inaccessible from other classes and must require an instance to be accessed through, unless specified otherwise. All methods should be accessible from everywhere and must require an instance to be accessed through, unless specified otherwise. Make sure to reuse code when applicable. I only one part of the full program I am writing from you. This is for practice so I can better understand some areas I am having trouble with. Thank you Cake.java This class will describe a certain kind of dessert that Bob can pick: cakes. This class extends the Dessert class.Variables:• String frosting – what the frosting of the cake isConstructor(s):• A constructor that takes in flavor, sweetness, and frosting of the cake (in this order).• A constructor that takes in the flavor and sets the sweetness to…arrow_forwardProblem 3: In classroom, we implemented MyStack by including an ArrayList as private data field of the class (using composition). In this problem, we will use another way to implement the stack class. Define a new MyStack class that extends ArrayList. Draw the UML diagram for the classes and then implement MyStack. Write a test program that prompts the user to enter five strings and displays them in reverse order. (1) Your UML diagram: (3)arrow_forwardDiscuss the limitations of the Comparable interface when it comes to sorting custom objects. When might you need to resort to using the Comparator interface instead?arrow_forward
- Declare and implement 5 classes: FloatArray, SortedArray,FrontArray, PositiveArray & NegativeArray.1- The FloatArray class stores a dynamic array of floats and itssize. It has:- A parameterized constructor that takes the array size.- An add method that adds a float at the end of the array.- Overloading for the insertion operator << to write the array to afile (ofstream)- Overloading for the extraction operator >> to read the arrayelements from the file (ifstream) and add them to the array.- A destructor to deallocate the array2- The SortedArray inherits from FloatArray. It has:- A parameterized constructor that takes the array size.- An add method that adds a float at the right place in the arraysuch that the array remains sorted with every add. Don’t add tothe array then sort but rather add in the right place.3- The FrontArray inherits from FloatArray. It has:- A parameterized constructor that takes the array size.- An add method that adds a float at the front of the…arrow_forwardDeclare and implement 5 classes: FloatArray, SortedArray,FrontArray, PositiveArray & NegativeArray.1- The FloatArray class stores a dynamic array of floats and itssize. It has:- A parameterized constructor that takes the array size.- An add method that adds a float at the end of the array.- Overloading for the insertion operator << to write the array to afile (ofstream)- Overloading for the extraction operator >> to read the arrayelements from the file (ifstream) and add them to the array.- A destructor to deallocate the array2- The SortedArray inherits from FloatArray. It has:- A parameterized constructor that takes the array size.- An add method that adds a float at the right place in the arraysuch that the array remains sorted with every add. Don’t add tothe array then sort but rather add in the right place.3- The FrontArray inherits from FloatArray. It has:- A parameterized constructor that takes the array size.- An add method that adds a float at the front of the…arrow_forwardPart 2: Sorting the WorkOrders via dates Another error that will still be showing is that there is not Comparable/compareTo() method setup on the WorkOrder class file. That is something you need to fix and code. Implement the use of the Comparable interface and add the compareTo() method to the WorkOrder class. The compareTo() method will take a little work here. We are going to compare via the date of the work order. The dates of the WorkOrder are saved in a MM-DD-YYYY format. There is a dash '-' in between each part of the date. You will need to split both the current object's date and the date sent through the compareTo() parameters. You will have three things to compare against. You first need to check the year. If the years are the same value then you need to go another step to check the months, otherwise you compare them with less than or greater than and return the corresponding value. If you have to check the months it would be the same for years. If the months are the same you…arrow_forward
- Declare and implement 5 classes: FloatArray, SortedArray, FrontArray, PositiveArray & NegativeArray. 1- The FloatArray class stores a dynamic array of floats and its size. It has:- A parameterized constructor that takes the array size. - An add method that adds a float at the end of the array.- Overloading for the insertion operator << to write the array to afile (ofstream)- Overloading for the extraction operator >> to read the array elements from the file (ifstream) and add them to the array.- A destructor to deallocate the array2- The SortedArray inherits from FloatArray. It has:- A parameterized constructor that takes the array size. - An add method that adds a float at the right place in the arraysuch that the array remains sorted with every add. Don’t add to the array then sort but rather add in the right place.3- The FrontArray inherits from FloatArray. It has:- A parameterized constructor that takes the array size. - An add method that adds a float at the front…arrow_forwardDeclare and implement 5 classes: FloatArray, SortedArray, FrontArray, PositiveArray & NegativeArray 1- The FloatArray class stores a dynamic array of floats and its size. It has:- A parameterized constructor that takes the array size. - An add method that adds a float at the end of the array.- Overloading for the insertion operator << to write the array to afile (ofstream)- Overloading for the extraction operator >> to read the array elements from the file (ifstream) and add them to the array.- A destructor to deallocate the array 2- The SortedArray inherits from FloatArray. It has:- A parameterized constructor that takes the array size. - An add method that adds a float at the right place in the arraysuch that the array remains sorted with every add. Don’t add to the array then sort but rather add in the right place. 3- The FrontArray inherits from FloatArray. It has:- A parameterized constructor that takes the array size. - An add method that adds a float at the front…arrow_forwardDevelop a class ResizingArrayQueueOfStrings that implements the queueabstraction with a fixed-size array, and then extend your implementation to use arrayresizing to remove the size restriction.Develop a class ResizingArrayQueueOfStrings that implements the queueabstraction with a fixed-size array, and then extend your implementation to use arrayresizing to remove the size restriction.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning