Configuring the URL context

I am working on configuring a website where the client wants us to configure URL’s like example.com/newyork and example.com/england. We are using Java SpringBoot environment.

Please advise how should I approach this problem? Is this something that can be configured in spring boot/spring-mvc or is an apache configuration.

Recursive mergesort only sorts half of an array

I am attempting to implement a recursive merge sort algorithm to sort a simple array of integers but I am getting weird values for the indexes in the second half of my array. The first half seems to sort fine which is confusing given that its implemented recursively. The array of random integers is initialized in my main method.

public class MergeSort {

public static int Rounds = 1;
public static void MergeSort(Comparable[] ToSort, Comparable[] temp, int first, int last) {
    if(first < last) {
        int mid = (first + last) / 2;

        //Test Block
        System.out.print("For Round " + Rounds + ":n");
        System.out.print("first = " + first + "   mid = " + mid + "   last = " + last + "n");
        Rounds++;
        System.out.print("Array in Round " + (Rounds - 1) + " = {");
        for(int i = 0; i <= ToSort.length - 1; i++) {
            System.out.print(ToSort[i]);
            if(i < ToSort.length - 1)
                System.out.print(", ");
            else {
                System.out.print("}nn");
            }
        }

        MergeSort(ToSort, temp, first, mid);
        MergeSort(ToSort, temp, mid + 1, last);
        Merge(ToSort, temp, first, mid + 1, last);
    }

}

public static void Merge(Comparable[] ToSort, Comparable[] temp, int first, int mid, int last) {
    int beginHalf1 = first;
    int endHalf1 = mid - 1;
    int beginHalf2 = mid;
    int endHalf2 = last;
    int index = first;
    int Elements = (last - first) + 1;

    while(beginHalf1 <= endHalf1 && beginHalf2 <= endHalf2) {
        if(ToSort[beginHalf1].compareTo(ToSort[beginHalf2]) < 0) temp[index++] = ToSort[beginHalf1++];
        else temp[index++] = ToSort[beginHalf2++];
    }

    while(beginHalf1 <= endHalf1) temp[index++] = ToSort[beginHalf1++];
    while(beginHalf2 <= endHalf2) temp[index++] = ToSort[beginHalf2++];
    for(int i = 0; i < Elements; i++, last--) ToSort[last] = temp[last];

}

}

This produces the following output:

UNSORTED ARRAY = {15, 9, 12, 19, 49, 43, 57, 70, 78, 87}
For Round 1:
first = 0 mid = 4 last = 9
Array in Round 1 = {15, 9, 12, 19, 49, 43, 57, 70, 78, 87}

For Round 2:
first = 0 mid = 2 last = 4
Array in Round 2 = {15, 9, 12, 19, 49, 43, 57, 70, 78, 87}

For Round 3:
first = 0 mid = 1 last = 2
Array in Round 3 = {15, 9, 12, 19, 49, 43, 57, 70, 78, 87}

For Round 4:
first = 0 mid = 0 last = 1
Array in Round 4 = {15, 9, 12, 19, 49, 43, 57, 70, 78, 87}

For Round 5:
first = 3 mid = 3 last = 4
Array in Round 5 = {9, 12, 15, 19, 49, 43, 57, 70, 78, 87}

For Round 6:
first = 5 mid = 7 last = 9
Array in Round 6 = {9, 12, 15, 19, 49, 43, 57, 70, 78, 87}

For Round 7:
first = 5 mid = 6 last = 7
Array in Round 7 = {9, 12, 15, 19, 49, 43, 57, 70, 78, 87}

For Round 8:
first = 5 mid = 5 last = 6
Array in Round 8 = {9, 12, 15, 19, 49, 43, 57, 70, 78, 87}

For Round 9:
first = 8 mid = 8 last = 9
Array in Round 9 = {9, 12, 15, 19, 49, 43, 57, 70, 78, 87}

Implement a generic Java method using an O(n^2 ) sort and a comparator

Implement the following generic Java method using an O(n^2) sort and a comparator:

public static <E> void aSort(E[] list, Comparator<? super E> comparator)

Write test program that creates a list of at least 5 elements of the class type created in problem 3
above, calls the above method to sort the list, then outputs the sorted list via calls to toString.

Unsorted: 
A Circle with the radius of: [1]
A Circle with the radius of: [15]
A Circle with the radius of: [10]
A Circle with the radius of: [12]
A Circle with the radius of: [100]

I don’t know why it’s printing this backwards but it is supposed to sort in ascending order

Sorted: 
A Circle with the radius of: [100]
A Circle with the radius of: [12]
A Circle with the radius of: [10]
A Circle with the radius of: [15]
A Circle with the radius of: [1]

This is what I got so far

public static void main(String[] args) {

    Circle c1 = new Circle();
    Circle c2 = new Circle(15);
    Circle c3 = new Circle(10);
    Circle c4 = new Circle(12);
    Circle c5 = new Circle(100);

    Circle w[] = new Circle[5];
    w[0] = c1;
    w[1] = c2;
    w[2] = c3;
    w[3] = c4;
    w[4] = c5;

    CompareCircle cc = new CompareCircle();
    System.out.println("Unsorted: ");
    for (Circle go : w) {
        System.out.println(go.toString());
    }

    bubbleSort(w, new CompareCircle());
    System.out.println();
    System.out.println("Sorted: ");
    for (int i = 0; i < w.length; i++) {
        System.out.println(w[i].toString());

    }
   public static <E> void bubbleSort(E[] list, Comparator<? super E> comparator) {
    boolean needNextPass = true;

    for (int k = 1; k < list.length && needNextPass; k++) {
        // Array may be sorted and next pass not needed
        needNextPass = false;
        for (int i = 0; i < list.length - k; i++) {
            if (comparator.compare(list[i], list[i + 1]) > 0) {
                // Swap list[i] with list[i + 1]
                E temp = list[i];
                list[i] = list[i + 1];
                list[i + 1] = temp;

                needNextPass = true; // Next pass still needed
            }
        }
    }

}

}

This is my Circle class

import java.io.Serializable;
import java.util.Comparator;

public class Circle implements Serializable {

private int radius = 1;

public Circle() {
}

public Circle(int radius) {
    setRadius(radius);

}

public void setRadius(int v) {
    if (v > 0) {
        this.radius = v;
    }

}

public int getRadius() {
    return this.radius;

}

@Override
public String toString() {
    return "A Circle with the radius of: [" + radius + "]";
}


}

This is my CompareCircle class

import java.util.Comparator;

public class CompareCircle implements Comparator<Circle> {

@Override
public int compare(Circle o1, Circle o2) {
    int radius1 = o1.getRadius();
    int radius2 = o2.getRadius();

    if (radius1 < radius2){
        return radius2;
    }
    if (radius1 == radius2){
        return radius1;
    }
    else
        return radius1;
 }

 }

Flood Fill does not fill in eveything

I am making a painting app for android. I have a problem with the flood fill.
Here is the code.

public class floodTest {


public void floodFill(Bitmap  image, Point node, int targetColor,
                      int replacementColor) {
    int width = image.getWidth();
    int height = image.getHeight();
    int target = targetColor;
    int replacement = replacementColor;
    if (target != replacement) {
        Queue<Point> queue = new LinkedList<Point>();
        do {
            int x = node.x;
            int y = node.y;
            while (x > 0 && image.getPixel(x - 1, y) == target) {
                x--;
            }
            boolean spanUp = false;
            boolean spanDown = false;
            while (x < width && image.getPixel(x, y) == target) {
                image.setPixel(x, y, replacement);
                if (!spanUp && y > 0 && image.getPixel(x, y - 1) == target) {
                    queue.add(new Point(x, y - 1));
                    spanUp = true;
                } else if (spanUp && y > 0
                        && image.getPixel(x, y - 1) != target) {
                    spanUp = false;
                }
                if (!spanDown && y < height - 1
                        && image.getPixel(x, y + 1) == target) {
                    queue.add(new Point(x, y + 1));
                    spanDown = true;
                } else if (spanDown && y < height - 1
                        && image.getPixel(x, y + 1) != target) {
                    spanDown = false;
                }
                x++;
            }
        } while ((node = queue.poll()) != null);
    }
}
}

Here is when it gets called:

   @Override
public boolean onTouchEvent(MotionEvent event){
    switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
           int x = (int)event.getX();
           int y  = (int)event.getY();
             floodTest testyFlood = new floodTest();
            testyFlood.floodFill(sizedBitmap,new Point(x,y),sizedBitmap.getPixel(x,y), Color.rgb(floodfill.redrgb,floodfill.greenrgb,floodfill.bluergb));
            invalidate();

    }
    return true;
}

The problem I have is it doesn’t fill in everything in black part.

enter image description here

The circled part should be all the way filled in.
How could I do this?

The Sonar way to define a internal constant

I read into the question The Sonar way to define a constant that you shouldn’t mess up with the sonar default settings, however, I do have a case that I think it’s quite common and I’m struggling to swallow these sonar defaults.

I do have a repository class with some private constants that I need to make the repository annotations easier to read:

@Repository
public interface DepartamentoRepository extends JpaRepository<Departamento, Long> {

   Page<Departamento> findAll(Pageable pageable);

   // @formatter:off
   static final String FIND_ALL_SEM_EMPREGADOS =
      "SELECT d " +
      "FROM   Departamento d " +
      "WHERE  d.id NOT IN ( " +
                "SELECT e.departamento.id " +
                "FROM   Empregado e " +
                "WHERE  e.departamento.id = d.id " +
      ")";
    // @formatter:on

   @Query(FIND_ALL_SEM_EMPREGADOS)
   List<Departamento> findAllSemEmpregados();
}

I know that I could simply remove this constant and use it straight into the annotation but I don’t like the way it looks at the end. My class has more than 40 constants – this one here is just a simplification for sake of space.

So, this is that bad?

Java Convert Long to Currency, Why is there 1 cent difference and What’s the safe way to convert such long to currency?

Following is my code to convert long (cents) to dollar, however, there is 1 cent difference.

My expected answer is: $123,456,789,123,456.47, but the output is $123,456,789,123,456.48

public static void main(String[] args) {
    long l = 12345678912345647L;    
    double d = l / 100.00;
    NumberFormat formatter = NumberFormat.getCurrencyInstance();
    System.out.println(formatter.format(d));
}

Adding two numbers from the database in the Spring Service layer

I have an entity called User which holds three fields :

ID No1

1 5

2 4

Then I have a UserRepository which extends JpaRepository,thus including crud operations.
I want to be able to take value 5 and 4 and add them together.
This logic should be applied in the Service layer.
So I have a AddService interface and AddServiceImpl which implements that interface.

public interface AddService{
    public void addNumbers(user1, user2);
}

public class AddServiceImpl implements AddService{
     public void addNumbers(user1, user2){
            List<User> user1=userRepository.findOne(1);
            List<User> user2=userRepository.findOne(2);
     // how do I take those specific values that are on the field No?
 }
}

How do I implement this Service so that it adds those numbers ?
I need to inject the repository and save a list with the data from user1 and user2 and then add them ? But how do I do that ?
Thank you

How to query a table to see what range a certain value belongs to , java

I have a table in the db ,used as lookup to see what score a certain value has.
The table

 5     1

10     2

15    3

If given an input, example 6, how do I query the table in Java and see that 6 is in between 5 and 10..so its score is 2.
if it’s 12. It’s between 10 and 15 and the score is 3.

Or maybe instead of a table in the database, I could save the lookup values in a class ?
Is there a more efficient way to do this ?

Java Convert Long to Currency, Why is there 1 cent difference?

Following is my code to convert long (cents) to dollar, however, there is 1 cent difference.

My expected answer is: $123,456,789,123,456.47, but the output is $123,456,789,123,456.48

public static void main(String[] args) {
    long l = 12345678912345647L;    
    double d = l / 100.00;
    NumberFormat formatter = NumberFormat.getCurrencyInstance();
    System.out.println(formatter.format(d));
}

Java HTTP sample code hangs

I create a simple SWT app with a single button. When the button is clicked, the following code gets executed but it just hangs. I know the URL is ok! Any idea of what could be causing this?

HttpGet httpGetRequest = new HttpGet(URL_UUID);

// Execute HTTP request
HttpResponse httpResponse = httpClient.execute(httpGetRequest);

System.out.println("----------------------------------------");
System.out.println(httpResponse.getStatusLine());
System.out.println("----------------------------------------");