0

How do I approach this?

Write a Java code that reads the length of an integer array and its elements from the user then perform the following tasks: Write a method LargestGap() that finds the length of the largest gap in the input array. Write a method range() that finds the difference between the largest and smallest element of the array. Write a method numberOfDistinctElement() that returns the number of distinct elements in the input array. Write a method distinctElements() that returns an array with all the distinct elements in the first array. Sample run Enter the length of the array: 7 Enter the elements of the array: 1 3 5 2 5 7 3 The largest gap in the array is 4. The range of the array is 6. The number of distinct elements is 5. The array of distinct elements is [1, 3, 5, 2, 7] Note 1: You should submit one file with a .java extension. If the submitted file is not a .java file you will receive 0. Note 2: If the submitted file has any form of error (syntax, logical, runtime) error, your grade will be 0. Make sure to run your code before submitting it. Note 3: Grading is following the grading rubric used for projects. Note 4: You can read the elements of the array either all in one line or one by one.

3rd May 2021, 5:48 PM
J.C
9 Respostas
+ 5
BTW... I absolutely hate the Java language compared to C# or Kotlin. Java lacks the expressiveness of the other languages, making for a clunky experience in general.
4th May 2021, 1:43 PM
David Carroll
David Carroll - avatar
+ 4
Joshcle It might help if you start by talking / thinking through the logic at a high level. Don't worry about the actual code. Explain it in conversational style like you're explaining it to someone who knows nothing about programming. This ability is far more important than the coding part. Once you've mapped out a plan (in English), then coding the solution becomes much easier. You'll have a roadmap to follow and can explore / research options as you go through each step of the process. With enough practice, questions like this become effortless and quickly implemented within a few minutes, from start to finish. If you post an attempt to share your thought process of what you think needs to be done for each part of the problem, we can help you from there. Again, conversation style, not code. 😉
3rd May 2021, 11:56 PM
David Carroll
David Carroll - avatar
+ 4
Basically... those help with keeping my code clean and simplified, as well as, easier to read and maintain in the main method. Otherwise, the code gets quite messy in main().
4th May 2021, 1:39 PM
David Carroll
David Carroll - avatar
+ 3
Joshcle Nice attempt. It'll be easier for me to just share my version of the code. You should keep the original copy of your code for your own future reference. Build a new version of your code based on any guidance you get from my version. You'll want to revisit your original code at some point later to understand the differences in your approach over time. Doing this will help reinforce your growth and thought processes. https://code.sololearn.com/ca23a193A11a Use the following input to test when running my code: -------- 7 1 3 5 2 5 7 3 ---- Best of luck with everything.
4th May 2021, 8:36 AM
David Carroll
David Carroll - avatar
+ 2
here is my code https://code.sololearn.com/cA14A1243A10 How can I shorten my main method?
4th May 2021, 5:40 AM
J.C
+ 1
@David Carroll What do these lines of code provide? private static void print(String template, int value) { System.out.println(String.format(template, value)); } private static void print(String template, int[] value) { System.out.print(template); System.out.println(Arrays.toString(value)); }
4th May 2021, 1:26 PM
J.C
0
Read the length, create an array and read the elements in a loop, then call the other methods with that array and output the return value as specified. For the methods you need simple loops, math functions min and max. And for unique values, you should find a method in the standard library
3rd May 2021, 7:02 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
David Carroll I couldn't agree more. Java was one of the first languages I got in touch with and I liked it, because it seemed better and easier to me than the others I knew (VB, C, C++, JS, Dr. Scheme/LISP). Now I hope I never have to use it again professionally. It lacks so many features that improve expressiveness. I would also include Python and maybe Typescript in your list. Although I'd prefer C# for medium and large projects. I thought those print methods to be inspired by python
4th May 2021, 3:21 PM
Benjamin Jürgens
Benjamin Jürgens - avatar