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();
Related
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.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
Loop
how to loop this without using array? I'm clearly confuse in this in java module I don't know the solution ;-; I'm new to java pls helppp mee
Try this.
for (int i = 5; i <= 25; i += 5)
System.out.println(i / 2);
Or
for (int i = 1; i <= 5; i++)
System.out.println(i * 5 / 2);
output:
2
5
7
10
12
Think for yourself why this produces the correct result.
public class Main {
public static void main(String[] args) {
int outputNum = 0;
for(int i = 0; i < 5; i++){
if (i % 2 == 0){
outputNum += 2;
}
else {
outputNum += 3;
}
System.out.print(outputNum + (i == 4 ? "" : " "));
}
}
}
This does exactly what you need.
Explanation:
In the loop if the i variable is even we add 2 to the output number (since you need to add 2 then 3 then 2...) and if i is uneven then we add three. At the end of the for loop we print the number without the newline using System.out.print and we separate them by a space if we aren't printing the last number using a ternary operator (i == 4 ? "" : " ") which returns a space if i is not equal to 4 and an empty string if it is, we do this to avoid having a space on the end of our output, so we get this 2 5 7 10 12 instead of this 2 5 7 10 12
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
In Java, the following code:
long x = 123;
String s = "abc" + x;
takes a significantly more runtime than:
long x = 123;
String s = "abc" + String.valueOf(x);
I got to know this through leetcode. I was trying to solve the following problem: https://leetcode.com/problems/fraction-to-recurring-decimal/
Here is the exact code for my solution:
public String fractionToDecimal(int numerator, int denominator) {
long n = numerator, d = denominator;
boolean isNegative = (n * d < 0);
if(n < 0) n = -n;
if(d < 0) d = -d;
long q = n / d;
long r = n % d;
if(r == 0) return (isNegative ? "-" : "") + q;
StringBuilder sb = new StringBuilder();
if(isNegative) sb.append('-');
sb.append(q).append('.');
Map<Long, Integer> found = new HashMap<>();
int index = sb.length();
while(r > 0 && !found.containsKey(r)){
found.put(r, index++);
n = r * 10;
q = n / d;
r = n % d;
sb.append(q);
}
if(r > 0) {
sb.insert(found.get(r), "(");
sb.append(')');
}
return sb.toString();
}
When I click on Submit it takes as long as 7 milliseconds to complete.
But if I literally just change line no. 8 from + q to + String.valueOf(q) the runtime plummets down to just 1 millisecond. Please feel free to copy paste the code on leetcode to try it out there and see this change in runtime for yourself, if necessary.
This is highly confusing to me. Why is this happening? As per my understanding, in both the cases, compiler first converts the long to a String, and then concatenates those two Strings together, right? So, under the hood, isn't concatenating a String and a long exactly the same as concatenating two Strings together? Then why does one take more time to run than the other? Any insight would be highly appreciated. TIA.
Note: this answer was written before the question was changed. It used to include the expressions shown below.
"abc" + 123 is a constant expression - the concatenation is done at compile-time so "abc123" ends up in the constant pool.
"abc" + String.valueOf(123) is not a constant expression. The concatenation happens at execution time, which is obviously rather more expensive than just using the compile-time concatenation result.
So I'd expect the result to be the opposite of what you've actually reported in the question.
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
// a program tells the maximum value we can store in (unsigned integer) a given bits .( 256 for 8 bits)
int counter=0;
int last= 0b11111111;
for(int first=0b00000000;first<=last;counter++)
{
first=first + 1;//adding 1(binary addition)
}
System.out.println("for "+ variable "bits u can store "+counter values");
//variable here 8.
//(1.how to get it from user? 2.how to convert it into binary 0b00000000?)
//how to do this without 0b ,actually in previous version of java
//a program in which if you give 8 bit(in case of unsigned) then it give u maximum values u can store in it, not by using ((2*n)-1).
//code is not only for java 8
// sorry i do not have java 8 i hope the above code will compile without error
thank you in advance
Try this:
long result = 1 << numBits;
If numBits is greater than the size of long, use a double instead (and cast the "1" and "numBits" to double).
how about this
int last = 0;
for(int i = 0; i < bitNum; i++){
last = (last << 1) + 1
}
Scanner s = new Scanner(System.in);
int variable = s.nextInt();
int counter = 0;
long last = (1 << variable) - 1;// = 0b'111....111
for(int first = 0; i <= last; counter++){
first = first + 1;
}
System.out.println("for "+ variable +"bits u can store " + counter + "values");
this code is same with your code, but this code dont use 0b.
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.