0
Write a static method called weave that that takes two integers as parameters and that returns the result of weaving their digits together to form a single integer. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to-the-last digit of x followed by the second-to-the-last digit of y. And so on.
Hi, I’m a beginner in coding So please help me out 😅
13 Antworten
+ 3
I know that I’m suppose to make it by myself but it is so difficult to me , i want just a hint to start writing it
+ 2
public class Main {
public static void main(String[] args) {
System.out.println(weave(128, 394));
System.out.println(weave(2384, 12));
}
public static int weave(int x, int y) {
int answer = 0;
int maxValue = Math.max(x, y);
int lenNum = String.valueOf(maxValue).length();
for (int i = 0; i < lenNum; i++) {
int xLast = (x % 10) * 10;
int yLast = y % 10;
answer += (xLast + yLast) * (int)(Math.pow(100, i));
x /= 10;
y /= 10;
}
return answer;
}
}
+ 1
I gotta be honest, you are the first to write the longest question text I ever seen :D
Now, I have to tell you this just to be straight, since this is your assignment it is you who will be writing the code, not other people.
That said, can you pass here an example for the <x> and <y> and the resulting "weaved" number, I'm not getting how it supposed to be.
+ 1
I can explain it , For example, consider weaving 128 with 394. The last pair of digits in the result should be 84 (because the original numbers end in 8 and 4). The second-to-the-last pair of digits in the result should be 29 (because the second-to-the-last digits of the original numbers are 2 and 9). The third-to-the-last pair of digits in the result should be 13 (because the third-to-the-last digits of the original numbers are 1 and 3). Thus:
weave(128, 394);
should return 132984. Notice that the order of the arguments is important. The call weave(394, 128) would return 319248.
If one of the numbers has more digits than the other, you should imagine that leading zeros are used to make the numbers have equal length. For example, weave(2384, 12) should return 20308142 (as if it were a call on weave(2384, 0012)). Similarly, weave(9, 318) should return 30198 (as if it were a call on weave(009, 318)).
0
Thank you frank but it’s a little bit complicated for me 😅if you can explain it i will be thankful 👍🏻
0
It seems that it has one error🤔
0
What type of an error?
0
I apply this program in an app and i got that(compiler.java:1: error: class Main is public, should be declared in a file named Main.java
public class Main{
^
1 error)
0
Should i made a file first??
0
Yes, The name of the file should be same as the class name because class Main has public modifier.
0
Yeah yeah that’s right thank you 💛
0
@Frank can i ask you sth ?
0
Why did you use the Math.max method ?
I search it , it is using to know which is the biggest number right?