Here is the program so far. It reads the data from input file. Updates client we
ID: 3560665 • Letter: H
Question
Here is the program so far. It reads the data from input file. Updates client weights and displays the current data on the console. Writes the data and statistics to a output file.
import java.io.*;
import java.util.Scanner;
public class Assign7 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in); // For keyboard input
Scanner input = null; // For file input
PrintStream output = null; // For file output
String[] clientNames = null;
double[] clientWeights = null;
boolean filesOK = false;
try {
input = new Scanner(new File("Clients.txt"));
output = new PrintStream(new File("ClientOut.txt"));
filesOK = true;
}
catch (FileNotFoundException e) {
System.out.println(e);
}
if (filesOK) {
// Create the correct number of elements in the clientNames array
// Note the input.nextInt() used to acquire the size of the array.
clientNames = new String[input.nextInt()];
// Create the correct number of elements in the clientWeights array
clientWeights = new double[clientNames.length];
readClientData(input, clientNames, clientWeights);
input.close();
updateWeights(console, clientNames, clientWeights);
outputStatistics(output, clientNames, clientWeights);
output.close();
}
}
/**
* This method reads from the input file starting on the second line, the clients'
* ID, their weight, and their name. The ID is used as an index into the names and
* weights arrays.
*
* @param in Scanner to the file being read.
* @param names is an array to hold each client's name.
* @param weights is an array to hold each client's weight.
*/
public static void readClientData(Scanner in, String[] names, double[] weights) {
}
/**
* This method allows the user to change the current weights of each client.
*
* @param console used for keyboard input
* @param names is an array to hold each client's name.
* @param weights is an array to hold each client's weight.
*/
public static void updateWeights(Scanner console, String[] names, double[] weights) {
}
/**
* This method writes to a file statistic regarding each client. All the client's ID,
* Name, and Weight are listed in a table followed by statistics on how many clients
* there are, the Average weight, the highest, and lowest weight.
*
* @param output file to which the statistics are written.
* @param names is an array to hold each client's name.
* @param weights is an array to hold each client's weight.
*/
public static void outputStatistics(PrintStream output, String[] names,
double[] weights) {
}
// The following methods are helper methods for outputStatistics:
/**
* This method returns the highest weight found in the array.
*
* @param weights is an array to hold each client's weight.
* @return highest weight found in the array.
*/
public static double getHighest(double[] weights) {
}
/**
* This method returns the lowest weight found in the array.
*
* @param weights is an array to hold each client's weight.
* @return lowest weight found in the array.
*/
public static double getLowest(double[] weights) {
}
/**
* This method returns the average of all the weights found in the array.
*
* @param weights is an array to hold each client's weight.
* @return average weight found in the array.
*/
public static double getAverage(double[] weights) {
}
}
Here is the input file text:
6
2 152 Lea Dea
3 243 Sam Spam
0 148 Ziggy Zip
1 206 Sally Solaride
4 104 Mia Dea
5 125 Tom Tom
Here is an example of the finished output:
Statistics on our clients:
Total number of clients: 6
Average weight: 161.83
Highest weight: 222.00
Lowest weight: 101.00
ID. NAME WEIGHT
0. Ziggy Zip 165.00
1. Sally Solaride 206.00
2. Lea Dea 152.00
3. Sam Spam 222.00
4. Mia Dea 101.00
5. Tom Tom 125.00
Explanation / Answer
import java.io.*;
import java.util.Scanner;
public class Assign7 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in); // For keyboard input
Scanner input = null; // For file input
PrintStream output = null; // For file output
String[] clientNames = null;
double[] clientWeights = null;
boolean filesOK = false;
try {
input = new Scanner(new File("Clients.txt"));
output = new PrintStream(new File("ClientOut.txt"));
filesOK = true;
}
catch (FileNotFoundException e) {
System.out.println(e);
}
if (filesOK) {
// Create the correct number of elements in the clientNames array
// Note the input.nextInt() used to acquire the size of the array.
clientNames = new String[input.nextInt()];
// Create the correct number of elements in the clientWeights array
clientWeights = new double[clientNames.length];
readClientData(input, clientNames, clientWeights);
input.close();
updateWeights(console, clientNames, clientWeights);
outputStatistics(output, clientNames, clientWeights);
output.close();
}
}
/**
* This method reads from the input file starting on the second line, the clients'
* ID, their weight, and their name. The ID is used as an index into the names and
* weights arrays.
*
* @param in Scanner to the file being read.
* @param names is an array to hold each client's name.
* @param weights is an array to hold each client's weight.
*/
public static void readClientData(Scanner in, String[] names, double[] weights) {
for (int i = 0; i < names.length; i++) {
int id = in.nextInt();
weights[id] = in.nextDouble();
names[id] = in.nextLine().trim();
}
}
/**
* This method allows the user to change the current weights of each client.
*
* @param console used for keyboard input
* @param names is an array to hold each client's name.
* @param weights is an array to hold each client's weight.
*/
public static void updateWeights(Scanner console, String[] names, double[] weights) {
for (int i = 0; i < names.length; i++) {
System.out.println("ID=" + i + " Name=" + names[i] + " Weight=" + weights[i]);
System.out.print("Would you want to change the weight of id " + i + "? (y/n) ");
String ans = console.next().toLowerCase();
if (ans.startsWith("y")) {
System.out.print("Enter new weight for id " + i + ": ");
weights[i] = console.nextDouble();
}
}
}
/**
* This method writes to a file statistic regarding each client. All the client's ID,
* Name, and Weight are listed in a table followed by statistics on how many clients
* there are, the Average weight, the highest, and lowest weight.
*
* @param output file to which the statistics are written.
* @param names is an array to hold each client's name.
* @param weights is an array to hold each client's weight.
*/
public static void outputStatistics(PrintStream output, String[] names,
double[] weights) {
output.println("Total number of clients: " + names.length);
output.printf("Average weight: %.2f%n", getAverage(weights));
output.printf("Highest weight: %.2f%n", getHighest(weights));
output.printf("Lowest weight: %.2f%n", getLowest(weights));
output.println("ID. NAME WEIGHT");
for (int i = 0; i < names.length; i++) {
output.printf("%d. %s %.2f%n", i, names[i], weights[i]);
}
}
// The following methods are helper methods for outputStatistics:
/**
* This method returns the highest weight found in the array.
*
* @param weights is an array to hold each client's weight.
* @return highest weight found in the array.
*/
public static double getHighest(double[] weights) {
double h = weights[0];
for (int i = 0; i < weights.length; i++) {
if (weights[i] > h)
h = weights[i];
}
return h;
}
/**
* This method returns the lowest weight found in the array.
*
* @param weights is an array to hold each client's weight.
* @return lowest weight found in the array.
*/
public static double getLowest(double[] weights) {
double l = weights[0];
for (int i = 0; i < weights.length; i++) {
if (weights[i] < l)
l = weights[i];
}
return l;
}
/**
* This method returns the average of all the weights found in the array.
*
* @param weights is an array to hold each client's weight.
* @return average weight found in the array.
*/
public static double getAverage(double[] weights) {
double sum = 0;
for (int i = 0; i < weights.length; i++) {
sum += weights[i];
}
return sum / weights.length;
}
}