- Java and JSP
||11-21-2012 06:40 PM
Clueless on an Array project
We were given this project today, which is due Friday, and I am completely clueless where to even start with this. Any help would be appreciated.
Code, execute and debug the application below using a one-dimensional array.
1. Write an application which inputs and stores six floating point values in an array, each between 1.0 and 1000.0, inclusive.
2. Display in table form, the index and the value of each element in the array.
3. Calculate and display the minimum, maximum and average of the array elements.
4. Allow the user to specify the number of values to be entered.
5. Calculate and display the average after eliminating any duplicates in the array. Count and display the number of duplicates removed."
||11-21-2012 08:35 PM
Can't do too much by way of code since this is an assignment, so you'll need to work through that and post back any specific issues.
- That is a matter of instantiating an array of
new float. See here for some additional information about arrays: http://docs.oracle.com/javase/tutori...ts/arrays.html. Also, to control input I'd suggest that a method could be added to provide the verification; this is only a requirement if you actually accept input. Please note that it will be somewhat tricky as you are using a float/double type which is primitive, so you cannot keep adding to an existing one without using member properties on objects. For that you would need to use Float/Double class wrapper types.
- This depends on exactly what the display is. If its a command line application, you simply use a for loop. Arrays are always sequential, so you can loop from
for (int i = 0; i < yourarray.length; ++i). Each entry represents one line, so you need to make sure you add the linefeed (either manually with a print and \n, or using println). Since you only have one thing to display per record, using println is sufficient. The i in the loop represents the offset index.
- Average: For this I would use new methods. Ignoring other packages, to do this you need to take all the items and sum them up, then divide by the number in the collection. Simple as that, return a float or double.
- Min/Max: Without writing a custom sort and without using additional packages, you can use
Arrays.sort(yourArray) to sort them ascending. The first item is the smallest, the last item is the largest. Also makes sense to write methods for these as you will likely want to work on a copy of the collection (and using a primitive type provided as an argument will be a copy).
- This tells me you should actually write a method to deal with forcing entries between 1.0 and 1000.0. Methods are good since it doesn't matter what you interface with. Input in command line will use the Scanner or Console class or swing text area or input dialogs. Scanner is easy to use; I can't find a decent one for cli entry on the tutorials for oracle, but here's a (lengthy) tutorial here: http://www.java-made-easy.com/java-scanner.html
- Start by sorting an array and creating the resulting array (of the same size). Use a a loop and check to see if it exists in the new array. I won't suggest a collection; I have suggested Arrays.sort already so I'd suggest that Arrays.binarySearch would also be useful.
Something like that should work. If you need to track the actual duplicates, you can add to the else clause there and push them into an array of their own. If you just need the count, it will be the difference between the mysortedarray length, and the iAdd value.
int iAdd = 0;
for (int i = 0; i < mysortedarray.length; ++i)
if (Arrays.binarySearch(myResultingArray, mysortedarray[i]) < 0)
myResultingArray[iAdd++] = mysortedarray[i];
Hope that gives you some ideas to start with.
|All times are GMT +1. The time now is 01:26 PM.
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.