Question

Please develop a Java program to: 1. Read numbers from the input file, HW2Data.txt into an...

Please develop a Java program to: 1. Read numbers from the input file, HW2Data.txt into an integer array. 2. Call maxSubSum1 and display the output. 3. Display the CPU time used by maxSubSum1. 4. Call maxSubSum2 and display the output. 5. Display the CPU time used by maxSubSum2

1.(29755 49953 10773 48214 10121 32881 4101 8807 25671 46882 39403 37011 36156 12194 45373 3868 12228 31972 25012 30794 45518 18249 14013 49786 37593 2529 117 42511 46920 20876 23626 19824 5480 47782 18773 26007 25685 26582 46698 38729 3835 6723 13427 36627 38543 49831 10420 21237 21250 7160 11020 9002 21239 775 3439 2085 29419 9599 39805 3787 43536 19324 31218 15538 22456 42105 20794 37604 8684 47949 20194 44940 16398 24756 312 22951 6911 48079 33451 25293 24712 44842 42581 7331 45023 27501 23085 44372 38126 6423 13784 13359 36673 8618 6954 394 49604 40395 1565 44111 375 29976 20032 36735 27785 23687 26394 30463 24823 35915 3030 36707 4713 18725 13758 9689 23603 21755 3509 23908 6167 6798 8607 37061 13853 14372 27157 26152 15415 1450 26019 44731 15947 30571 39440 23474 23721 40545 44510 24623 33771 18119 30319 13239 33415 18122 10951 24626 37667 46839 17223 37849)

2. (41849 29692 38526 9815 34612 29630 23978 33049 47061 17084 41683 2981 22693 15810 35889 17363 28467 11500 14516 31215 39473 11658 43668 5647 18401 35893 21536 17809 26283 45540 1395 42274 42821 40306 32258 21227 43047 7496 36231 11961 22005 6312 43570 35416 30685 5686 10046 48740 34558 19177 46310 28192 30671 44555 45685 26090 10541 18881 5013 6985 33819 42121 43053 23725 32060 3639 13685 42871 16260 22719 29062 32113 22044 1575 12848 45947 32364 47227 43858 45035 2468 5344 23 10431 17272 32825 29042 24352 30858 26643 19460 19371 6835 26793 33235 6360 556 6480 42952 34954 7024 38066 12978 10266 20270 362 24067 36127 10931 21356 15165 28189 28847 30642 4540 39941 26583 27466 4698 20855 27959 3594 12065 28686 32918 44982 32864 47313 6346 22649 1262 17376 43829 21048 4449 19470 41755 41762 2061 8674 22648 11421 14091 34787 29189 15322 2279 44812 1955 22489 34882 )

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

package readintegers;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.Scanner;

public class ReadIntegers {

public static void main(String[] args) {
int arr[] = new int[200];
Scanner file = null; //initializes file to empty
ReadIntegers ri = new ReadIntegers();
double time1 = ri.getCpuTime();
System.out.println("Time before:"+time1);
int cnt=0;
int sum = 0;
double average;
try
{
//attempt to open file.
file = new Scanner(new FileInputStream("data.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("File Not found. Error 404");
System.exit(2);
}

int number = 0;
int max = Integer.MIN_VALUE; //Give lowest 'int' number (-2147483648) if your file has -ve numbers also.
int min = Integer.MAX_VALUE; //Highest value for int
  
int i=0;
while(file.hasNext()) {
number = file.nextInt();
arr[i] = number;
if(number > max) {
max = number; //assuming you have all positive numbers in your file
}
if(number < min) {
min = number;
}
i++;
}
System.out.println("MAX and MIN :"+max+" "+" "+min);
int tot = 0;
for(int j=0;j<arr.length;j++){
tot = tot + arr[j];
}
System.out.println("sum is ="+tot);
double time = ri.getCpuTime();
System.out.println("Time after:"+time);
  
System.out.println("Total elapsed time = " + (time -time1));
}
public long getCpuTime( ) {
ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
return bean.isCurrentThreadCpuTimeSupported( ) ?
bean.getCurrentThreadCpuTime( ) : 0L;
}
}

Add a comment
Know the answer?
Add Answer to:
Please develop a Java program to: 1. Read numbers from the input file, HW2Data.txt into an...
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
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