Question

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 peoples 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 numbers and donations Then the main program calls another method to sort the donation amounts into ascending order, carrying along the corresponding ID numbers. It the, once again, prints the sorted lists, giving both ID numbers and donations Here are the details: (a) The main program calls a method to read in the data from a file The data consists of sets of data, each of which contains a person s three-digit ID number and an integer (e.g., 456 20000 or 123 30234) . The file is read until end-of-file is reached. The method returns how many sets of data were read in. The main program calls the return value The main program calls these arrays idnumbers and donations. A separate printing method prints the original set of data in the form of a neat table. hen the arrays print, there should be an overall heading, plus headings for the columns of ID numbers and donations (b) Then the main program sends the array of ID numbers, the array of donations, and the size donorcount to a sorting method. This method sorts the ID numbers into numerical order Be sure to maintain the match-up of ID numbers and donations. For example, 456 should always be associated with 20000, no matter where 456 moves in numerical order similarly, 123 should stay with 30234 When the sorting method finishes and returns control to the main program, the main program calls the printing method to once again print the two arrays )Next, the main program sends the same three parameters to the second sorting method, which sorts the donations into numerical order, being sure to maintain the linkup of ID numbers and donations When this sorting method finishes and returns control to the main program, the main program, once again, calls the printing method to print the two arrays with appropriate headings Your arrays should have room for up to 50 entries. To test the program, have a set of data with at least 15 to 20 values in each array Make sure that your original order in not close to numerical order for either array and that the two numerical orders are not close to each other

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

console X <terminated>Donor [Java Application] C:\Program FilesJavajre7 bin\javaw.exe (Jan 18, 2018, 11:14:24 AM) Original re

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.

Add a comment
Know the answer?
Add Answer to:
Write a complete JAVA program to do the following program. The main program calls a method...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi this program must be completed using a Eclipes Compiler for Java only . This is...

    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.t...

    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,...

    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...

    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...

    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...

    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...

    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...

    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 ...

    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...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT