Cyclomatic complexity value [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
int a, b, c;
a = 1;
b = 2;
if (a > b) {
c = 23;
} else {
c = 25;
}
while (b < c) {
b = b + 1;
}
System.print.outline(“answer=“ + b);
return 0;
What's the cyclomatic complexity value for the code?
I tried calculating it and got 3 and 4, it changes as I tried different flow graph that connect to different nodes.

Related

java: jdk1.8 StringBuilder::newCapacity? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
private int newCapacity(int minCapacity) {
// overflow-conscious code
int newCapacity = (value.length << 1) + 2;
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
? hugeCapacity(minCapacity)
: newCapacity;
}
Why +2 when getting new capacity?
Is it for speed? Space for time?
I think it ensures the corner case of very small strings.

how to stop the code when the code has executed a certain amount of times? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Name: runSequence4
Input: Intake feed, int n
Output: none
Action: Takes in an intake object and calls the method give(). It first passes the number 1 into
give and then alternates between -1 and 1. It does this n number of times.
How do I stop the loop when the code executes it "N" times?
This is what i have so far
public void runSequence4(Intake feed, int n){
for(int x = 1; ???? ; x = (x * -1)){
feed.give(x);
}
}
Just add a variable i to store the current iteration and test whether it has exceeded n
public void runSequence4(Intake feed, int n){
for(int x = 1, i = 0; i < n; x = (x * -1), i++){
feed.give(x);
}
}

Finding the sum of the two greatest numbers of 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
With Java, how would one find the greatest two numbers of a set of 3 numbers without using if conditionals.
For example given the 3 numbers {2,3,5}
int a = 2;
int b = 3;
int c = 5;
int total;
total would be replaced with the value of c+b = 8
List<Integer> data = Arrays.asList(23,6,13);
Collections.sort(data);
Collections.reverse(data)
data = data.sublist(0,2);
System.out.println(data);
One line:
int biggest = (a > b ? a : b) > c ? (a > b ? a : b) : c;
Two lines:
int firstStep = (a > b ? a : b);
int biggest = firstStep > c ? firstStep : c;
Java 8:
int max = Arrays.stream(numbers).max().getAsInt();
int sec_max = Arrays.stream(numbers).filter(i -> i != max).max().getAsInt();

How to put values from ArrayList in certain ranges? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is it possible to put values from my ArrayList into a specific ranges?
My ArrayList contains doubles:
0.4987873968412172
0.49949149542775684
0.4995227496071881
0.4999386804321369
0.500520813392628
0.5005912302221284
0.5006590304457654
0.5009476185730563
0.5013192031470984
And I want to divide them into (for example) two ranges to get an output:
Numbers from 0.498 to 0.499
0.4987873968412172
0.49949149542775684
0.4995227496071881
0.4999386804321369
Numbers from 0.500 to 0.501
0.500520813392628
0.5005912302221284
0.5006590304457654
0.5009476185730563
0.5013192031470984
List<Integer> range1 = new ArrayList<>();
List<Integer> range2 = new ArrayList<>();
for(Integer i: myArrayList)
if(i < 0.5) range1.add(i);
else range2.add(i);
Yes, use if and else
if (number > 0.498 && number < 0.499) {
firstArrayList.add(number);
}
else
{
secondArrayList.add(number);
}
And if you have 3 (or more) ranges use else if
if (number > 0.498 && number < 0.499) {
firstArrayList.add(number);
}
else if (number > 0.500 && number < 0.501)
{
secondArrayList.add(number);
}
else
{
thirdArrayList.add(number);
}

Error Incompatible types in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is my code but it gives me this error
class MultiplyDivide {
public static void main (String args[]) {
int i = 5;
int j = 10;
System.out.println("5 is " + i);
System.out.println("10 is " + j);
int k = i/j;
System.out.println("5/10 is " + k);
k = i * j;
System.out.println("5 * 10 is " + k);
}
}
Hi,
Actually the code that you posted should not give you an error.
It will return an output like:
5 is 5
10 is 10
5/10 is 0
5 * 10 is 50
One point that can be an error from your point of view is the 5/10 equals zero. But it is a correct java behavior because you are dividing integer by integer and assign a result to an integer.
If you want to get a double-type result you need to do something like:
double k = i * 1.0 / j;
Hope this resolves your issue.

Categories