Question

In: Computer Science

I am trying to create a function in JAVA that takes in an ArrayList and sorts...

I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first.

It does not need to output the values in anyway, but it should return them so they can be output elsewhere.

Please try to use the stub class below.

The code for the main class is not necessary. I am only really asking for the formula code to sortByDistance. I do not want anything else.

public ArrayList sortByDistance(boolean increasing) {
       // can sort by distance increasing (true) or decreasing (false)
       return null;
   }

Solutions

Expert Solution

Here is the answer for your question in Java Programming Language.

Kindly upvote if you find the answer helpful.

NOTE : As you mentioned your function sortByDistance will take in ArrayList as parameter, you have to pass array list into the function. But I could see a boolean passed to the function. If you expect the function to sort according to the boolean value of increasing (i.e., if increasing is true,sorts in increasing order, if false, sorts in decreasing order)please find the below code.

Otherwise there is a buit-in function sort() that automatically sorts an arraylist arranging its elements in increasing order.

I will provide function implementation using sort() and without using sort(). Please comment below if you have any doubts.

####################################################################

CODE :

Using sort()


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

public class SortArrayList {
  
public static void main(String[] args){
//Have taken distances as double values
//Please change datatypes as per your requirements
ArrayList<Double> distances = new ArrayList<>();
//Adding distance values to arraylist
distances.add(90.5);
distances.add(88.6);
distances.add(190.6);
distances.add(23.6);
distances.add(48.6);
distances.add(289.6);
  
//Call method with increasing as true
SortArrayList obj = new SortArrayList();
obj.sortByDistance(distances, true);
System.out.println("After sorting in increasing order : ");
System.out.println(distances);
System.out.println("====================================");
  
obj.sortByDistance(distances, false);
System.out.println("After sorting in decreasing order : ");
System.out.println(distances);
System.out.println("====================================");
}
//As we have to take array list as parameter,had to change the function prototype
public ArrayList sortByDistance(ArrayList<Double> distances,boolean increasing) {
// can sort by distance increasing (true) or decreasing (false)
if(increasing){
Collections.sort(distances);
}else{
double temp = distances.get(0);
//Sorting in decreading order (if increasing is false)
//NOTE : We cannot use sort() for decreasing order
for(int i = 0;i<distances.size();i++){
for(int j = i+1;j<distances.size();j++){
if(distances.get(i) < distances.get(j)){
temp = distances.get(i);
distances.set(i, distances.get(j));
distances.set(j,temp);
}
}
}
}
return distances;
}
}

###############################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

#################################################################

Without using sort()


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

public class SortArrayList {
  
public static void main(String[] args){
//Have taken distances as double values
//Please change datatypes as per your requirements
ArrayList<Double> distances = new ArrayList<>();
//Adding distance values to arraylist
distances.add(90.5);
distances.add(88.6);
distances.add(190.6);
distances.add(23.6);
distances.add(48.6);
distances.add(289.6);
  
//Call method with increasing as true
SortArrayList obj = new SortArrayList();
obj.sortByDistance(distances, true);
System.out.println("After sorting in increasing order : ");
System.out.println(distances);
System.out.println("====================================");
  
obj.sortByDistance(distances, false);
System.out.println("After sorting in decreasing order : ");
System.out.println(distances);
System.out.println("====================================");
}
//As we have to take array list as parameter,had to change the function prototype
public ArrayList sortByDistance(ArrayList<Double> distances,boolean increasing) {
// can sort by distance increasing (true) or decreasing (false)
double temp = distances.get(0);
if(increasing){
for(int i = 0;i<distances.size();i++){
for(int j = i+1;j<distances.size();j++){
if(distances.get(i) > distances.get(j)){
temp = distances.get(i);
distances.set(i, distances.get(j));
distances.set(j,temp);
}
}
}
}else{
//Sorting in decreading order (if increasing is false)
//NOTE : We cannot use sort() for decreasing order
for(int i = 0;i<distances.size();i++){
for(int j = i+1;j<distances.size();j++){
if(distances.get(i) < distances.get(j)){
temp = distances.get(i);
distances.set(i, distances.get(j));
distances.set(j,temp);
}
}
}
}
return distances;
}
}

##########################################################################

SCREENSHOTS :

As the change is only with the function part while comparing to above screenshots, sharing the screenshots of function implementation.

#####################################################################

OUTPUT :

Any doubts regarding this can be explained with pleasure :)


Related Solutions

I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts it by the amount of "reviews" that a property has in increasing order. So the most reviews first. So each listing in the array would contain a different number of reviews, and they should be sorted based on that value. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters it by the requested price range that a property has. So if someone wants a property between the value of 10(min) and 20(max) it would show all members of the array that meet those conditions.. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below....
In basic C++ Create a function that takes in a vector, sorts it, then outputs to...
In basic C++ Create a function that takes in a vector, sorts it, then outputs to the console the result.
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked...
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked to calculate the sum of all fines in the policeOfficer class from the arraylist i created. I modified the issueParking ticket method which i bolded at the very end to add each issued Parking ticket to the arrayList, i think thats the right way? if not please let me know. What I dont understand how to do is access the fineAmountInCAD from the arrayList...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
I am trying to create a classified balance sheet and I am unsure what is involved...
I am trying to create a classified balance sheet and I am unsure what is involved when reporting the current assets, liabilities and owners equity?
In trying to apply my knowledge in the real world, I am trying to create a...
In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 25 and 70, in which I would deposit 20% of my income each year. The income starts at 80,000 with an annual growth rate of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT