Create an application that provides a solution for problem 20.8 In addition to requirements specified in the description. The class must satisfy the following Default Constructor Two argument constructor for data used to initialize "first" and "second" "toString" method for displaying the "first" and "second" data elements Creates a "Comparator" object (to be used by the sort method; i.e. create an instance of a class that implements the interface [must override "compare" and "equals" methods]) The application must create an ArrayList of 5 Pair objects and then display the contents in sorted order largest to smallest, based on the method used to compare "Pair" objects. The class only supports types that extend Number. Assume the "Pair" objects contain x, y coordinates. Therefore calculate the distance from a 0,0 coordinate to determine the sorting order (i.e., ascending, smallest distance to largest distance).
Please include the problem from 20.8 in the coding, I can't figure out how to incorporate the array list into the former problem.
20.8 (Generic Class Pair) Write a generic class Pair which has two type parameters--F and S--each representing the type of the first and second element of the pair, respectively. Add get and set methods for the first and second elements of the pair. [Hint: The class header should be public class Pair<F, S>.]
Hi. I have answered this question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Pair.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;
public class Pair<F extends Number, S extends Number> {
private F first;
private S second;
public Pair() {
first = null;
second = null;
}
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
public F getFirst() {
return first;
}
public void setFirst(F first) {
this.first = first;
}
public S getSecond() {
return second;
}
public void setSecond(S second) {
this.second = second;
}
@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
@Override
public boolean equals(Object other) {
if (other instanceof Pair) {
Pair p = (Pair) other;
if (first.equals(p.first) && second.equals(p.second)) {
//equal object
return true;
}
}
//not equal
return false;
}
public static void main(String[] args) {
// creating a Comparator object to sort Pair of two Integers
Comparator<Pair<Integer, Integer>> comp = new Comparator<Pair<Integer, Integer>>() {
@Override
public int compare(Pair<Integer, Integer> p1,
Pair<Integer, Integer> p2) {
// finding distance between origin and points specified by pairs
// using formula sqrt(x^2 + y^2)
double dist1 = Math.sqrt(p1.getFirst() * p1.getFirst()
+ p1.getSecond() * p1.getSecond());
double dist2 = Math.sqrt(p2.getFirst() * p2.getFirst()
+ p2.getSecond() * p2.getSecond());
// checking if first pair has less distance from origin than
// second
if (dist1 < dist2) {
return -1; // p1 should come first
}
if (dist1 > dist2) {
return 1; // p2 should come first
}
return 0; // equal pair
}
};
// creating an array list of Pair objects
ArrayList<Pair<Integer, Integer>> pairList = new ArrayList<Pair<Integer, Integer>>();
// using a Random object, creating 5 random coordinates Pair objects,
// adding to array list
Random random = new Random();
for (int i = 0; i < 5; i++) {
Pair<Integer, Integer> p = new Pair<Integer, Integer>(
random.nextInt(10), random.nextInt(10));
pairList.add(p);
}
// displaying before sorting
System.out.println("List before sorting: ");
System.out.println(pairList);
// sorting and displaying after sorting
Collections.sort(pairList, comp);
System.out.println("List after sorting: ");
System.out.println(pairList);
}
}
/*OUTPUT*/
List before sorting:
[(8, 6), (6, 8), (3, 1), (2, 4), (7, 0)]
List after sorting:
[(3, 1), (2, 4), (7, 0), (8, 6), (6, 8)]
Create an application that provides a solution for problem 20.8 In addition to requirements specified in...
Please Do It With Java. Make it copy paste friendly so i can run and test it. Thnak You. Write programs for challenges 1 and 2 and 6 at the end of the chapter on Generics. 1) Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which...
I need help coding this with Python. Create a Point class that consists of an ordered pair (x, y) representing a point's location on the x and y axes. The constructor allow the x and y values to be passed in and should default missing values to 0. You must override the __str__ method to print the point as normal (i.e., "(2, 5)"). Also, create a Line class that consists of a pair of Point objects p and q. A...
Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...
In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList) a) Create a DateTime class 1. Add day, month, year, hours, minutes as attributes 2. Add a constructor and a toString() method 3. Implement the Comparable interface, and add a CompareTo() method 4. Add methods to get and set all attributes. b) Add to MyLinkedList class the following methods: 1. Insert a Node to a particular position in the List 2. Insert a Node...
Create a Java application, that support the following: Create an Employee class, which holds following information: Employee First Name Employee Last Name Employee Phone Number Employee Address Employee id Employee Title Employee salary Create an application that uses the Employee class Constructors –Getters and setters A minimum of 3 constructors including default constructor equals() method Helper methods toString() method Create an array of 5 Employee objects Use a Scanner to read in 5 employee information Change 2 employees information (3-items...
JAVA. Create an application that uses a "PriorityQueue" to perform the following Uses the constructor that receives a "Comparator" object as an argument Stores 5 "Time1" objects using the "Time1" class shown in Fig. 8.1 on page 331. The class must be modified to implement the "Comparator" interface Displays the "Universal Time" in priority order Note: To determine the ordering when implementing the "Comparator" interface, convert the time into seconds (i.e., hours * 3600 + minutes * 60 + seconds),...
Answer the following: 1. Create a Class Car with the following instance variables: model (String), Brand (String), maxspeed (double) Note: use encapsulation (all variables are private). (3 Marks) 2. Create a non-default constructor for Class Car which takes 3 parameters. (2 Marks) 3. Create a get and set methods for instance variable model variable only. (2 Marks) 4. Create PrintInfo() method which prints all three instance variables. (2 Marks) 5. Create a subclass GasCar with instance variable TankSize (double).. (2...
Project 2 Tasks: Create a Coin.java class that includes the following: Takes in a coin name as part of the constructor and stores it in a private string Has a method that returns the coins name Has an abstract getvalue method Create four children classes of Coin.java with the names Penny, Nickle, Dime, and Quarter that includes the following: A constructor that passes the coins name to the parent A private variable that defines the...
java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...
Create an Item class, which is the abstract super class of all Items. Item class includes an attribute, description of type String for every item. [for eg. Book] A constructor to initialize its data member of Item class. Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details. Declare a constant RATE with value 0.25 Declare a method called calculateExtraCharge(), which returns a double value. Create the...