Question

write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner; //for Scanner class
import java.io.*; //for FileReader and IOException class
class TestData{ //driver class named TestData
//readData method takes two double arrays and returns integer
public static int readData(double arr1[],double arr2[])throws IOException {
int k=0;
//opens file and creates input stream using Scanner object
Scanner read = new Scanner(new FileReader("input.txt"));
while(read.hasNext()){ //reads until file has data tokens
double arrOne = read.nextDouble(); //reads double value
double arrTwo = read.nextDouble(); //reads double value
//stores into arrays
arr1[k] = arrOne;
arr2[k] = arrTwo;
k += 1; //incrments k value
}
return k; //returns k
}
//changeArray method
public static void changeArray(double alpha[],double beta[],int k){
//traverses through arrays and does the evaluation
for(int i=0;i<k;i++){
beta[i] = beta[i]*alpha[i];
}
}
//sortArray method applies bubble sort on array
public static void sortArray(double arrToOrder[],int k){
for(int i=0;i<k-1;i++){
for(int j=0;j<k-i-1;j++){
if(arrToOrder[j]>arrToOrder[j+1]){
double temp = arrToOrder[j];
arrToOrder[j] = arrToOrder[j+1];
arrToOrder[j+1] = temp;
}
}
}
}
//main method
public static void main(String[] args){
//creates two double arrays of size.
double arr1[] = new double[100];
double arr2[] = new double[100];
  
try{ //try block as readData method may arise FileNotFoundException
int k = readData(arr1,arr2); //calls readData method
changeArray(arr1,arr2,k); //calls changeArray method
sortArray(arr1,k); //calls sortArray method for arr1
sortArray(arr2,k); //calls sortArray method for arr2
//prints the arr values to console
System.out.println(" arr1 arr2");
for(int i=0;i<k;i++){
System.out.printf("%5.2f %5.2f\n",arr1[i],arr2[i]);
}
}catch(Exception e){ //if exception arises then
//message gets printed
System.out.println("Exception arised: input file not found");
}
  
}
}

//input.txt file screenshot

input Notepad File Edit Format View H 7 23.56 16 88.12 10 75.1

//output screenshot

C: \java>java TestData arr1 arr2 7.00 164.92 10.00 751.060 16 . 1409 . 92

//any query, post in the comment section

Add a comment
Know the answer?
Add Answer to:
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...
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
  • Write a complete JAVA program to do the following program. The main program calls a method...

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

  • java estion7 For this question, assume all input comes from the keyboard, and al output goes...

    java estion7 For this question, assume all input comes from the keyboard, and al output goes to the screen. Include method prototypes and comments. The array should have room for 100 integers Write a complete Java program, including at least one comment in the main propram and one in e to do the following: Write a main program which will call the methods described below (a) First the main program will read an integer (this integer is a parameter or...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

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

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

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

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows...

    Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows in a graphical way, as shown below, the names stored in a file. The coordinates to print the names will be also read from another file. Here is an example of what your graphic output should look like with the two files that are provided for test purposes. Drawing Panel Adam Bernie Cameron Daniel Fanny Gerard Harold Issac Jackie Raitlyn Note that the colors...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

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