Array Data - Write two methods that read data from the console and store the dat
ID: 3547574 • Letter: A
Question
Array Data - Write two methods that read data from the console and store the data in an array:
a. The method:
int readData(int [ ] x)
reads a list of at most 100 integers into the array x. A sentinel 999 terminates the list. The method returns the size of the list.
9. Largest and Smallest
Design a method that determines the largest and smallest values stored in an integer
array, x. Your method should return these values in an array of length two. Use the
following algorithm:
Initialize variables currentBig and currentSmall to the larger and smaller values
of x[0] and x[1]. Process the rest of the list, two elements at a time. Compare the
larger of the two elements to currentBig , and replace currentBig if necessary.
Compare the smaller of the two elements to currentSmall, and replace currentSmall
if necessary. Test your method in a program and include a method that reads a list,
terminated by 999, into an array.
Explanation / Answer
Public class ReadInputDisplay{
public static ArrayList readData(String fileName) {
String line = "";
ArrayList data = new ArrayList();//consider using ArrayList<int>
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
while((line = br.readLine()) != null) {
data.add(line);
System.out.println(data.size());
}
}
catch(FileNotFoundException fN) {
fN.printStackTrace();
}
catch(IOException e) {
System.out.println(e);
}
return data;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.