Write a complete JAVA program to do the following program.
Hi,
Please find the code below.
package normalClasses;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Donor {
public static void main(String[] args){
int idNumbers[] = new int[50]; //created two array each of 50 size
int donations[] = new int[50];
int donorCount=readFile(idNumbers, donations); //calling readfile function
System.out.println("--------------------Original record--------------------");
printDetails(idNumbers,donations,donorCount); //printing details
sortByDonorId(idNumbers,donations,donorCount);
System.out.println("--------------------Sort by donor id--------------------");
printDetails(idNumbers,donations,donorCount);
sortByDonation(idNumbers,donations,donorCount);
System.out.println("--------------------Sort by donation amount--------------------");
printDetails(idNumbers,donations,donorCount);
}
private static void sortByDonation(int[] idNumbers, int[] donations, int donorCount) {
for(int i=0;i<=donorCount;i++){
for(int j=i+1;j<=donorCount;j++){
if(donations[i]>donations[j]){ //comparison based on donations
int temp = idNumbers[i];
idNumbers[i] = idNumbers[j];
idNumbers[j] = temp;
temp = donations[i];
donations[i] = donations[j];
donations[j] = temp;
}
}
}
}
private static void sortByDonorId(int[] idNumbers, int[] donations, int donorCount) {
for(int i=0;i<=donorCount;i++){
for(int j=i+1;j<=donorCount;j++){
if(idNumbers[i]>idNumbers[j]){ //comparison based on donor number
int temp = idNumbers[i];
idNumbers[i] = idNumbers[j];
idNumbers[j] = temp;
temp = donations[i];
donations[i] = donations[j];
donations[j] = temp;
}
}
}
}
public static void printDetails(int[] idNumbers, int[] donations, int donorCount){
System.out.printf("%-5s%s"," ","Donor information"); //using printf for format output
System.out.printf("\n%-20s%-20s","ID number","donations");
for(int i=0;i<=donorCount;i++){
System.out.printf("\n%-20s%-20s",idNumbers[i],donations[i]);
}
System.out.println();
}
public static int readFile(int idNumbers[],int donations[]){
File file = new File("input.txt"); //reading data from this file
Scanner reader;
int donorIndex=-1; //keep track of number of records
try {
reader = new Scanner(file);
while(reader.hasNext()){
donorIndex++;
String line = reader.nextLine();
idNumbers[donorIndex] = Integer.parseInt(line.split(" ")[0]); //spliting line by space,0 is idnumber
donations[donorIndex] = Integer.parseInt(line.split(" ")[1]); //spliting line by space,1 is donations
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return donorIndex;
}
}
output:
where input.txt file is
123 435235
523 652232
235 821903
312 432555
345 532555
624 532555
333 92555
223 532555
745 32555
900 132555
Make sure input.txt file is in the same folder in which .java file is there. Otherwise it will throw exception file not found.
Write a complete JAVA program to do the following program. The main program calls a method...
Hi this program must be completed using a Eclipes Compiler for Java only . This is and intro to java class , so only intro methods should be used for this assignment. Please include a copy of the input data and a sample of the out put data . Thanks a bunch Homework-Topic 9-Donations Write a complete program to do the following: The main program calls a method to read in (Erom an input file) a set of people's three-digit...
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,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once. This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
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...
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...
Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function calls. Be sure to read in data from the input file. Using the input file provided, run your program to generate an output file. Upload the output file your program generates. •Write a...
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).
Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from a file named dota.txt that is to be found in the same directory as the running program. The program should ignore al tokens that cannot be read as an integer and read only the ones that can. After reading all of the integers, the program should print all the integers back to the screen, one per line, from the smalest to the largest. For...
Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...
This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables: An array of integers A count of the number of elements currently in the array The class will have...