Please find the Java code below. Find inline comments for better understanding.
****Note: Please place the "donations_data.txt" file under src/ directory otherwise you will get "File not found" exception.
CODE
/////////////////////////////////////////////////////////
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Solution {
public static final int MAX_SIZE = 50; // represents the maximum number of entries
public static int[] idnumbers = new int[MAX_SIZE]; // represents the array of ID numbers
public static int[] donations = new int[MAX_SIZE]; // represents the array of donations
/*
* readData() function reads IDs and Donation amounts from a text file and store it in the respective arrays
* Returns the numbers of records found in text file
*/
public static int readData() throws IOException {
int numberRecords = 0; // keeps track of the number of records in the text file
try {
File f = new File("src/donations_data.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = ""; // stores each line as a string
// for loop to read the text file line by line
while ((readLine = b.readLine()) != null) {
// We need to split each line based on space " " to get the ID and donation
String[] data = readLine.split(" ");
idnumbers[numberRecords] = Integer.parseInt(data[0]); // store the ID in idnumbers array
donations[numberRecords] = Integer.parseInt(data[1]); // store the donation in donations array
numberRecords ++;
}
} catch (IOException e) {
e.printStackTrace();
}
return numberRecords;
}
/*
* sortIds() methods is used to sort the IDs in ascending order based on IDs
* Arguments: Number of donations.
* The data is sorted using insertion sort algorithm.
*/
public static void sortIDs(int numberRecords) {
for (int i=1; i<numberRecords; i++) {
// lets store the current element
int currentId = idnumbers[i];
int currentDonation = donations[i];
// j stores the index of the previous element
int j = i-1;
/* Move elements of idnumbers[0 to i-1] that are greater than current to one position ahead
* of their current position. This step will make sure that the array elements upto index i
* will always be sorted.
*/
while (j>=0 && idnumbers[j] > currentId) {
idnumbers[j+1] = idnumbers[j];
donations[j+1] = donations[j];
j = j-1;
}
// now we need to currentId to its correct index
idnumbers[j+1] = currentId;
// we also need to move currentDonation to keep consistency
donations[j+1] = currentDonation;
}
}
/*
* sortDonations() methods is used to sort the Donation amounts in ascending order based on IDs
* Arguments: Number of donations.
* The data is sorted using insertion sort algorithm.
*/
public static void sortDonations(int numberRecords) {
for (int i=1; i<numberRecords; i++) {
// lets store the current element
int currentId = idnumbers[i];
int currentDonation = donations[i];
// j stores the index of the previous element
int j = i-1;
/* Move elements of donations[0 to i-1] that are greater than current to one position ahead
* of their current position. This step will make sure that the array elements upto index i
* will always be sorted.
*/
while (j>=0 && donations[j] > currentDonation) {
idnumbers[j+1] = idnumbers[j];
donations[j+1] = donations[j];
j = j-1;
}
// now we need to currentDonation to its correct index
donations[j+1] = currentDonation;
// we also need to move currentId to keep consistency
idnumbers[j+1] = currentId;
}
}
/*
* printData() functions prints the content of idnumbers and donations in a tabular fashion
*/
public static void printData(int numberRecords) {
System.out.println("The IDs and their corresponding Donation amounts are given below.");
System.out.println("\tID\t|\tDonations");
System.out.println("\t=========================");
// Loop to print each ID and donation from the arrays
for(int i=0; i<numberRecords; i++) {
System.out.println("\t" + idnumbers[i] + "\t|\t" + donations[i] + "");
}
System.out.println();
}
public static void main(String[] args) throws IOException {
// read data from text file
int donorcount = readData();
// print data
printData(donorcount);
// sort the data based on IDs
sortIDs(donorcount);
System.out.println("Data after sorting based on ID values.\n");
// print the data after sorting based on IDs
printData(donorcount);
// sort data based on donation amount
sortDonations(donorcount);
System.out.println("Data after sorting based on Donation values.\n");
//print data after sorting based on donation amounts
printData(donorcount);
}
}
OUTPUT
/////////////////////////////////////////////////////////
Content of donations_data.txt :
==========================
345 2000
123 80001
982 1000
561 901
676 7726
881 42
222 9888
652 1000
700 2000
901 8888
660 1250
100 7800
650 6000
510 5410
700 6999
800 460000
Output Screenshot
=================
Hi this program must be completed using a Eclipes Compiler for Java only . This is...
Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...
Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...
write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.txt and all output will go to the screen Assume there will not be more than 100 7 23.56 16 88.12 10 75.1 Design a Java class with a main method that does the following 1) Reads the data into two arrays of doubles,...
LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers and stores them in an array called data. Then call a method that accepts the array as an argument, sorts the array in ascending order and prints out the original AND sorted array (Hint: The method does not return anything in this case, so it should be set to void).
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...
CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods Create a folder called Unit03 and put all your source files in this folder. Write a program named Unit03Prog1.java. This program will contain a main() method and a method called printChars() that has the following header: public static void printChars(char c1, char c2) The printChars() method will print out on the console all the characters between c1 and c2 inclusive. It will print 10...
Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that holds an array for the integer values 1-10. Pass the array to a method that will calculate the values to the power of 2 of each element in the array. Then return the list of calculated values back to the main method and print the values
Using Java In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...
Note: According to the question, please write source code in java only using the class method. Sample Run (output) should be the same as displayed in the question below. Make sure the source code is working properly and no errors. Exercise #2: Design and implement a program (name it compareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array...