Java Method Needs Condensing - java

How can I condense this method to look more clean? It works perfect how it is, just looks extremely ugly, has no elegance. I'm sure there's a better way to go about doing this I just can't find it.
public class ItemProfitComparator implements Comparator<MenuItem>{
public int compare(MenuItem menuVar1, MenuItem menuVar2)
{
if( ((menuVar1.getTotalSales() - (menuVar1.getNumOrders()*menuVar1.getWholesaleCost())) - (menuVar2.getTotalSales() - (menuVar2.getNumOrders()*menuVar2.getWholesaleCost()))) > 0){
return (int)Math.ceil(((menuVar1.getTotalSales() - (menuVar1.getNumOrders()*menuVar1.getWholesaleCost())) - (menuVar2.getTotalSales() - (menuVar2.getNumOrders()*menuVar2.getWholesaleCost()))));
}else if(((menuVar1.getTotalSales() - (menuVar1.getNumOrders()*menuVar1.getWholesaleCost())) - (menuVar2.getTotalSales() - (menuVar2.getNumOrders()*menuVar2.getWholesaleCost()))) < 0){
return (int)Math.floor(((menuVar1.getTotalSales() - (menuVar1.getNumOrders()*menuVar1.getWholesaleCost())) - (menuVar2.getTotalSales() - (menuVar2.getNumOrders()*menuVar2.getWholesaleCost()))));
}else{
return 0;
}
}
}

You can use a variable to store the result of your calculation:
public class ItemProfitComparator implements Comparator<MenuItem> {
public int compare(MenuItem menuVar1, MenuItem menuVar2) {
double res = menuVar1.getTotalSales() - (menuVar1.getNumOrders() * menuVar1.getWholesaleCost())) - (menuVar2.getTotalSales() - (menuVar2.getNumOrders() * menuVar2.getWholesaleCost()));
if (res > 0) {
return (int) Math.ceil(res);
} else if (res < 0) {
return (int) Math.floor(res);
} else {
return 0;
}
}
}
However, for code review, there is a separate site.

As others point out you can improve this by using local variables.
Also, your use of floor and ceil is unnecessary. All that matters is the sign of the answer. To compare two double values you can do
if (a > b) {
return 1;
} else if (a < b) {
return -1;
} else {
return 0;
}
However, you are much better of just doing
return Double.compare(a, b);
Note that Java 8 introduced Comparator.comparingDouble(...) for exactly this sort of thing.

A comparator is much easier to write using a lambda:
Comparator<MenuItem> profitComparator = Comparator.comparing(
(menuItem) -> menuItem.getTotalSales() - (menuItem.getNumOrders() * menuItem.getWholesaleCost())
);

Related

How to get rid of multiple if statements in java?

Is there a better way to write this constructor which has multiple if statements and multiple arguments? I'm a noob to programming so any leads would be helpful.
public Latency(final double full, final double cpuOne, final double cpuTwo, final double cpuThree, final double cpuFour) {
if (full > 10.0 || (full <= 0.0)) {
throw new IllegalArgumentException("Must check the values");
}
this.full = full;
if (cpuOne == 0 && cpuTwo == 0 && cpuThree == 0 && cpuFour == 0) {
throw new IllegalArgumentException("not all can be zero");
} else {
if (cpuOne == 0.5) {
this.cpuOne = full;
} else {
this.cpuOne = cpuOne;
}
if (cpuTwo == 0.5) {
this.cpuTwo = full;
} else {
this.cpuTwo = cpuTwo;
}
if (cpuThree == 0.5) {
this.cpuThree = full;
} else {
this.cpuThree = cpuThree;
}
if (cpuFour == 0.5) {
this.cpuFour = full;
} else {
this.cpuFour = cpuFour;
}
}
}
I think this code doesn't need much of context as it is pretty straight forward.
I found out that we can't use switch statements for type double. How to optimize this?
There are a number of possible ways of refactoring the code that you've written, and there are pros and cons of each one. Here are some ideas.
Idea One - use the conditional operator
You could replace the else block with code that looks like this. This is just effectively a shorter way of writing each of the inner if/else blocks. Many people find this kind of form more readable than a bunch of verbose if/else blocks, but it takes some time to get used to it.
this.cpuOne = cpuOne == 0.5 ? full : cpuOne;
this.cpuTwo = cpuTwo == 0.5 ? full : cpuTwo;
this.cpuThree = cpuThree == 0.5 ? full : cpuThree;
this.cpuFour = cpuFour == 0.5 ? full : cpuFour;
Idea Two - move common functionality to its own method
You could have a method something like this
private static double changeHalfToFull(double value, double full) {
if (value == 0.5) {
return full;
} else {
return value;
}
}
then call it within your constructor, something like this.
this.cpuOne = changeHalfToFull(cpuOne);
this.cpuTwo = changeHalfToFull(cpuTwo);
this.cpuThree = changeHalfToFull(cpuThree);
this.cpuFour = changeHalfToFull(cpuFour);
This has the advantage that the key logic is expressed only once, so it's less error prone than repeating code over and over.
Idea Three - use arrays
You could use an array of four elements in the field that stores these values. You could also use an array for the constructor parameter. This has a huge advantage - it indicates that the four CPU values are somehow all "the same". In other words, there's nothing special about cpuOne compared to cpuTwo, for example. That kind of messaging within your code has real value to someone trying to understand this.
public Latency(final double full, final double[] cpuValues) {
// validation conditions go here ...
this.cpuValues = new double[4];
for (int index = 0; index <= 3; index++) {
if (cpuValues[index] == 0.5) {
this.cpuValues[index] = full;
} else {
this.cpuValues[index] = cpuValues[index];
}
}
}
Or a combination
You could use some combination of all these ideas. For example, you might have something like this, which combines all three of the above ideas.
public Latency(final double full, final double[] cpuValues) {
// validation conditions go here ...
this.cpuValues = new double[4];
for (int index = 0; index <= 3; index++) {
this.cpuValues[index] = changeHalfToFull(cpuValues[index]);
}
}
private static double changeHalfToFull(double value, double full) {
return value == 0.5 ? full : value;
}
There are obviously other possibilities. There is no single correct answer to this question. You need to choose what you're comfortable with, and what makes sense in the larger context of your project.
DRY - Don't Repeat Yourself
Each if is essentially the same. Put it in a separate method and call the method once for each cpu* variable.
public class Latency {
private double full;
private double cpuOne;
private double cpuTwo;
private double cpuThree;
private double cpuFour;
public Latency(final double full,
final double cpuOne,
final double cpuTwo,
final double cpuThree,
final double cpuFour) {
if (full > 10.0 || (full <= 0.0)) {
throw new IllegalArgumentException("Must check the values");
}
this.full = full;
if (cpuOne == 0 && cpuTwo == 0 && cpuThree == 0 && cpuFour == 0) {
throw new IllegalArgumentException("not all can be zero");
}
else {
this.cpuOne = initCpu(cpuOne);
this.cpuTwo = initCpu(cpuTwo);
this.cpuThree = initCpu(cpuThree);
this.cpuFour = initCpu(cpuFour);
}
}
private double initCpu(double cpu) {
return cpu == 0.5 ? full : cpu;
}
public static void main(String[] arg) {
new Latency(9.99, 8.0, 7.0, 6.0, 0.5);
}
}

Convert a complex method into Lambda Expression

i want to write my code below, with new stuff. I want to use Java8 stream and functional programming.
private static void algoritmoSolC(List<Storage> freeSpaces, Double dimPacket, Double nPackets,
int storageIndex) {
if (nPackets == 0)
return;
List<Storage> list = new ArrayList(freeSpaces) {
public Object get(int index) {
if (index < 0) {
index = Math.abs(index);
} else if (index >= size()) {
index = index % size();
}
return super.get(index);
}
};
for (int i = 0; i < nPackets; i++) {
Storage storage = list.get(storageIndex);
if (storage.getFreeSpace() > dimPacket) {
storage.setFreeSpace(storage.getFreeSpace() - dimPacket);
++storageIndex;
} else {
++storageIndex;
++nPackets;
}
}
}
I think if I convert code in functional programming, I spent less time for result.
Can anyone help me to convert this snippet of code?
Thanks in advance
Didnt really tested it but it could go about this:
IntStream
.range(storageIndex,Integer.MAX_VALUE)
.mapToObj(i-> freeSpaces.get(Math.abs(i) % freeSpaces.size()))
.filter(storage -> storage.getFreeSpace() > dimPacket)
.limit(nPackets)
.forEach(storage.setFreeSpace(storage.getFreeSpace() - dimPacket))
Looking at this it is really surprisingly more elegant thant your code :-)

Error: missing return statement (once more)

public static int salariDepart1( int duradaPeriode, int horesEfectives, float preuHora, int bonificHoresExtres) {
int salary;
if(duradaPeriode<0||horesEfectives<0||preuHora<0||bonificHoresExtres<0){
return (int) -1;
}
else if(horesEfectives<(duradaPeriode-(duradaPeriode*0.75))){
if(preuHora<6){
salary = (int) (preuHora * horesEfectives);
return (int) Math.round(salary);
}
else{
salary = (int) (horesEfectives * (preuHora-(preuHora*0.10)));
return (int) Math.round(salary);
}
}
else if(horesEfectives>(duradaPeriode+(duradaPeriode*0.20))){
bonificHoresExtres = (int) (preuHora+(preuHora*0.03));
if(bonificHoresExtres>200){
return (int) -2;
}
else if(bonificHoresExtres<200){
salary = (int) (horesEfectives * (preuHora+(preuHora*0.03)));
return (int) Math.round(salary);
}
else{
salary = (int) (horesEfectives * preuHora);
return (int) Math.round(salary);
}
}
}
public static void main(String[] args){
System.out.println(salariDepart1(200,120,8,5));
System.out.println(salariDepart1(190,100,7,10));
System.out.println(salariDepart1(180,90,7,20));
}
}
This error keeps popping up, and I dont know why. Help is appreciated. :)
The reason gets clearer if you indent the code consistently and clearly:
public static int salariDepart1(int duradaPeriode, int horesEfectives, float preuHora, int bonificHoresExtres) {
int salary;
if (duradaPeriode < 0 || horesEfectives < 0 || preuHora < 0 || bonificHoresExtres < 0) {
return (int) - 1;
} else if (horesEfectives < (duradaPeriode - (duradaPeriode * 0.75))) {
if (preuHora < 6) {
salary = (int)(preuHora * horesEfectives);
return (int) Math.round(salary);
} else {
salary = (int)(horesEfectives * (preuHora - (preuHora * 0.10)));
return (int) Math.round(salary);
}
} else if (horesEfectives > (duradaPeriode + (duradaPeriode * 0.20))) {
bonificHoresExtres = (int)(preuHora + (preuHora * 0.03));
if (bonificHoresExtres > 200) {
return (int) - 2;
} else if (bonificHoresExtres < 200) {
salary = (int)(horesEfectives * (preuHora + (preuHora * 0.03)));
return (int) Math.round(salary);
} else {
salary = (int)(horesEfectives * preuHora);
return (int) Math.round(salary);
}
}
}
What if duradaPeriode is >= 0 but neither horesEfectives < (duradaPeriode - (duradaPeriode * 0.75)) nor horesEfectives > (duradaPeriode + (duradaPeriode * 0.20)) is true? That's the path where the code doesn't return a value. So there's a path through the code that doesn't have a return.
Even if the conditions were all mutually-exclusive, the compiler can't always know that they are If they really are (which I don't think is the case above), make the final else if just an else. If they aren't, well, that's your problem, you need a returnin that case.
There is no else path for
else if(horesEfectives>(duradaPeriode+(duradaPeriode*0.20))){
So if that condition is not satisfied the compiler needs a return statement, which is missing.
Generally one should avoid to have many return statements, instead you better should introduce a returnValue variable which you inizialize at the beginning. This improves the abiliyt to debug your code.
int retVal = -1;
and assign the retVal in each of the conditions,
eg:
..
retVal = Math.round(salary);
..
finally return the value as last line
return retVal;
Set a default value that is returned when none of the conditional return statements are used.
So adding
return -1
for example at the end of the function will solve this
Not all code paths in your logic will encounter a return statement. Hence, you're missing one.
Your structure looks essentially like this:
if (something) {
if (something) {
return;
} else {
return;
}
} else if (something) {
if (something) {
return;
} else {
return;
}
}
Your "inner" conditionals are fine, but look at the "outer" one:
if (something) {
// every path in here returns
} else if (something) {
// and every path in here returns
}
What if neither of the "outer" conditions is true? No return statement is ever encountered.
Add an else to the "outer" conditional with a return or add a return after the entire conditional block. Every possible logical path through the method must return something.

trying to break out of for loop but keeps going back into it and performing recursive call

I just discovered the project euler website, I have done challenges 1 and 2 and have just started number 3 in java... here is my code so far:
import java.util.ArrayList;
public class IntegerFactorise {
private static int value = 13195;
private static ArrayList<Integer> primeFactors = new ArrayList<Integer>();
private static int maxPrime = 0;
/**
* Check whether a give number is prime or not
* return boolean
*/
public static boolean isPrimeNumber(double num) {
for(int i = 2; i < num; i++) {
if(num % i == 0) {
return false;
}
}
return true;
}
/*Multiply all of the prime factors in the list of prime factors*/
public static int multiplyPrimeFactors() {
int ans = 1;
for(Integer i : primeFactors) {
ans *= i;
}
return ans;
}
/*Find the maximum prime number in the list of prime numbers*/
public static void findMaxPrime() {
int max = 0;
for(Integer i : primeFactors) {
if(i > max) {
max = i;
}
}
maxPrime = max;;
}
/**
* Find all of the prime factors for a number given the first
* prime factor
*/
public static boolean findPrimeFactors(int num) {
for(int i = 2; i <= num; i++) {
if(isPrimeNumber(i) && num % i == 0 && i == num) {
//could not possibly go further
primeFactors.add(num);
break;
}
else if(isPrimeNumber(i) && num % i == 0) {
primeFactors.add(i);
findPrimeFactors(num / i);
}
}
int sumOfPrimes = multiplyPrimeFactors();
if(sumOfPrimes == value) {
return true;
}
else {
return false;
}
}
/*start here*/
public static void main(String[] args) {
boolean found = false;
for(int i = 2; i < value; i++) {
if(isPrimeNumber(i) && value % i == 0) {
primeFactors.add(i);
found = findPrimeFactors(value / i);
if(found == true) {
findMaxPrime();
System.out.println(maxPrime);
break;
}
}
}
}
}
I am not using the large number they ask me to use yet, I am testing my code with some smaller numbers, with 13195 (their example) i get down to 29 in this bit of my code:
else if(isPrimeNumber(i) && num % i == 0) {
primeFactors.add(i);
findPrimeFactors(num / i);
}
}
int sumOfPrimes = multiplyPrimeFactors();
if(sumOfPrimes == value) {
return true;
}
It gets to the break statement then finally the check and then the return statement.
I am expecting the program to go back to the main method after my return statement, but it jumps up to:
findPrimeFactors(num / i);
and tries to finish the iteration...I guess my understanding is a flawed here, could someone explain to me why it is behaving like this? I can't wait to finish it of :) I'll find a more efficient way of doing it after I know I can get this inefficient one working.
You are using recursion, which means that a function will call itself.
So, if we trace what your function calls are when you call return, we will have something like that:
IntegerFactorise.main()
|-> IntegerFactorise.findPrimeFactors(2639)
|-> IntegerFactorise.findPrimeFactors(377)
|-> IntegerFactorise.findPrimeFactors(29) -> return true;
So, when you return in the last findPrimeFactors(), you will only return from this call, not from all the stack of calls, and the execution of the previous findPrimeFactors() will continue just after the point where you called findPrimeFactors().
If you want to return from all the stack of calls, you have to modify your code to do something like that:
else if(isPrimeNumber(i) && num % i == 0) {
primeFactors.add(i);
return findPrimeFactors(num / i);
}
So that when the last findPrimeFactors() returns, all the previous findPrimeFactors() which called it will return too.
I think the problem is that you are ignoring the return value from your recursive call to findPrimeFactors().
Let's walk through this. We start with the initial call to findPrimeFactors that happens in main. We then enter the for loop as it's the first thing in that method. Now let's say at some point we get into the else statement and thus recursively call frindPrimeFactors(num / i). This will suspend the looping, but as this recursive call starts to run you enter the for loop again (remember, the previous loop is merely paused and not finished looping yet). This time around you encounter the break, which allows this recursive call to finish out, returning true of false. When that happens you are now back to the original loop. At this point the original loop continues even if the recursive call returned true. So, you might try something like this:
if (findPrimeFactors(num / i))
return true;
I'm assuming that you need to continue looping if the recursive call returned false. If you should always finish looping upon return (whether true or false) then try this:
return findPrimeFactors(num / i);

GOTO/Continue in Java Dynamic Programming

I have the following piece of code in Java implementing dynamic programming recursiverelatio:
public double routeCost() throws Exception {
double cost = Double.MAX_VALUE;
for (int l=i; l<=j; l++) {
if (! (customers.get(l) instanceof VehicleCustomer) )
continue;
double value = F(l,j) + (customers.get(l).distanceFrom(depot));
if (value < cost)
cost = value;
}
return cost;
}
private double F(int l, int m) {
//=========================== FIRST CASE ===========================
if (l==i && m==i) {
//System.out.println(i+","+j+","+l+","+m);
return firstCase();
}
//=========================== SECOND CASE ===========================
if (l==i && (i<m && m<=j) ) {
//System.out.println(i+","+j+","+l+","+m);
//analyses the possibility of performing all the soubtours based at heicle customert_i
return secondCase(i,m);
}
//=========================== GENERAL CASE ===========================
else {
System.out.println(i+","+j+","+l+","+m);
assert (customers.get(l) instanceof VehicleCustomer);
assert ( (i<l && l<=j) && (l<=m && m<=j) );
return Math.min(thirdCaseFirstTerm(l,m), thirdCaseSecondTerm(l,m));
}
}
private double firstCase() {
mainRoute.add(depot);
mainRoute.add(customers.get(i));
return depot.distanceFrom(customers.get(i));
}
private double secondCase(int i,int m) {
double caseValue = Double.MAX_VALUE;
int k = i;
while (k<m) {
double totalDemand=0;
for (int u=k+1; ( (u<=m) && (totalDemand<=truckCapacity) ); u++)
totalDemand += customers.get(u).getDemand();
double cost = F(i,k) + thita(i,k+1,m);
if (cost <= caseValue)
caseValue = cost;
k++;
}
return caseValue;
}
private double thirdCaseFirstTerm(int l, int m) {
double caseValue = Double.MAX_VALUE;
int k = i;
while (k<m) {
double totalDemand=0;
for (int u=k+1; ( (u<=m) && (totalDemand<=truckCapacity) ); u++)
totalDemand += customers.get(u).getDemand();
double cost = F(l,k) + thita(l,k+1,m);
if (cost <= caseValue)
caseValue = cost;
k++;
}
return caseValue;
}
private double thirdCaseSecondTerm(int l,int m) {
double caseValue = Double.MAX_VALUE;
int k = i;
for (Customer cust : customers) {
int h = customers.indexOf(cust);
if ( (!(cust instanceof VehicleCustomer)) || (h >=l)) {
continue;
}
double totalDemand=0;
for (int u=k+2; ( (u<=m) && (totalDemand<=truckCapacity) ); u++)
totalDemand += customers.get(u).getDemand();
double cost = F(h,k) + customers.get(h).distanceFrom(customers.get(l)) + thita(l,k+2,m);
if (cost < caseValue)
caseValue = cost;
}
return caseValue;
}
Method F(int,int) is invoked from the for loop in method routeCost().
I want to find a way to enforce that whenever the assertion assert (customers.get(l) instanceof VehicleCustomer);
` is not true, instead of going down to the return statement, I want to infrom the for loop from the routeCost() to continue to the next iteration. But F() has to return a value!
I know that what I'm trying to do violates almost every rule of object orientation, but I really need that.
You could throw an Exception in F() and catch it in routeCost().
This approach is much better than using assertions. They are rarely used in practice, and there's a good reason for this: exceptions are much more flexible and better suited for detecting errors, invalid input etc.
PS: When I say "rarely used", I base this statement on the fact that I saw hundreds of thousands of lines of Java code in the past years and I rarely came accross code that uses assertions.
You can return a special value like Double.NaN which you can check for with Double.isNaN(d)
You could make F() return a Double (instead of double) and return null in the case where your assert fails. Then have your outer for loop do a null check on the returned value before adding it, etc.
Why not replace the asserts with if statements? When the if-statements are true then calculate the value, otherwise return the MAX_VALUE of double. When F returns MAX_VALUE the cost will not be updated.
if (customers.get(l) instanceof VehicleCustomer) {
if ( (i<l && l<=j) && (l<=m && m<=j) ) {
return Math.min(thirdCaseFirstTerm(l,m), thirdCaseSecondTerm(l,m));
}
}
return Double.MAX_VALUE;
Use asserts during development to weed out things that should never happen in private methods. (Asserts can be switched off in production)
Throw an exception when something unexpected happens (e.g. a client of your class passes in invalid data).
However, from your question it seems you expect to get instances that are not VehicleCustomer, so asserts and exceptions are not the right approach here.
Peter Lawrey's and Jeff's answers will also work.

Categories