How does the code continue (computer science)?

Hello, I'm stuck on my homework. I've gotten the beginning of the code (bottom image).

but I don't know what to do next. I would be happy to receive an answer.

1 vote, average: 1.00 out of 1 (1 rating, 1 votes, rated)
You need to be a registered member to rate this.
Loading...
Subscribe
Notify of
9 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
PrincPersia
2 years ago

My Robot Artificial Answers:

It looks like you've implemented the beginning of the applyAsDoubleArray method in the ReduceDoubleArray class. If the current array parameter is null, null is returned; otherwise, the array is returned unchanged.

To implement the method as described in the task text, you must now determine the length of the array to be returned and set the components of the array.

To determine the length of the returned array, you can iterate through the current array parameter and check each element to see if it matches the PREDICATE predicate. If the element matches the predicate, you can increment a counter that represents the number of elements in the returned array.

To set the components of the returned array, you can iterate through the current array parameters again and check each element to see if it matches the predicate. If the element matches the predicate, you can place it in the correct position in the returned array.

Here is an example of the implementation of the applyAsDoubleArray method:

 @Override public double[] applyAsDoubleArray(double[] array) { if (array == null) { return null; } else { // Zähler für die Anzahl der Elemente im zurückzugebenden Array initialisieren int count = 0; // Durch den aktuellen Array-Parameter iterieren und die Anzahl der Elemente bestimmen, // die dem Prädikat entsprechen for (double value : array) { if (PREDICATE.test(value)) { count++; } } // Neues Array erstellen, das die richtige Anzahl an Elementen hat double[] result = new double[count]; // Durch den aktuellen Array-Parameter erneut iterieren und die Elemente setzen, // die dem Prädikat entsprechen int index = 0; for (double value : array) { if (PREDICATE.test(value)) { result[index] = value; index++; } } // Zurückgeben des zurückzugebenden Arrays return result; } }

I hope his answer is correct.

ralphdieter
2 years ago
Reply to  PrincPersia

Was the AI ​​able to recognize the images, or did you type out the text and the program?

PrincPersia
2 years ago
Reply to  ralphdieter

No. My Kl can only output text, but he can even read the texts and will know what they're about. Yes, welcome to the future. You can try it yourself: https://chat.openai.com

PrincPersia
2 years ago

Yes, robots are always men.

ralphdieter
2 years ago

Thanks.

he can't.

Why “he” 🙂

PrincPersia
2 years ago

So, I've already tried sending a PNG from a text file, and all it can recognize is that it's a PNG file. Nothing more. So, it can't figure out which image it is.

ralphdieter
2 years ago

Thanks, I've already had a few conversations with her (until the AI ​​crashed). I just wanted to know if she can now read text from images. I'd be too lazy to type it all out myself. Programming it myself is faster.

ralphdieter
2 years ago

The ChatGPT program in PrincPersia's answer is correct, completely outdated, and probably exactly what the teacher expects.

You might just get into trouble because ChatGPT is currently a hotly debated topic. If that happens, suggest the following alternative:

 @Override public double[] applyAsDoubleArray(double[] array) { if (array == null) { return null; return DoubleStream.of(array).filter(PREDICATE).toArray(); }

This is how a Java programmer does it, who doesn't rest on decades of old knowledge 🙂