You must complete a partially written program by adding code into the loop in ma
ID: 3745825 • Letter: Y
Question
You must complete a partially written program by adding code into the loop in main, then filling in the two methods below main.
STEP #1 FILL IN THE LOOP IN THE MAIN METHOD
You are given an input file such as this: P1input.txt which contains around a thousand integers.
The code you write into the loop in main must find the min and max of the file. Don't write anything to the screen, just assign the smallest value of the file into min, and the largest into max. Let my code output of the values after the loop ends. A brute force approach to min max would be to perform a test of each value to see if it is the new min, then perform a similar test on that same number to see if it is the new max. Such a strategy requires 2n comparisons on a file of n numbers.
See if you can do it less than 2n comparisons.
STEP #2 FILL IN THE TWO METHODS BELOW MAIN.
You are given skeleton of method isPrime() and a method isPerfect(). You must write the code for both.
The first method: static boolean isPrime( int n ) takes a number and returns true if and only if it is prime. A prime number is any number greater than 1 that is divisible only by 1 and itself. You may search wikipedia for a list of prime numbers. Try to be efficient in how you determine if it is prime.
The second method: static boolean isPerfect( int n ) takes a number and returns true if and only if it is perfect. A perfect number is a number equal to the sum of all its factors.
In this case the number 1 is counted in the set of factors but the number n is not counted in. You may search wikipedia for a list of perfect numbers. Try to be efficient in how you determine if it is perfect.
This is how the program is to be executed and correct output for the given input file and range of 1 through 30
// ALWAYS TEST FIRST TO VERIFY USER PUT REQURED CMD ARGS if (args.length // .e. c:> java Project! Plinput.txt System.exit (0) iava 1 30 Lab2 input file name> "); // grab args [0] and store into a String var named infile // grab args 1] and conver to int then store into a var named lo // grab args [2] and conver to int then store into a var named hi // STEP 1: OPEN THE INPUT FILE AND COMPUTE THE MIN AND MAX, NO OUTPUT STATMENTS ALLOWED REPLACE THE 3 COMMENTS WITH CODE THAT STORES THE THREE ARG VALUES INTO VARIABLESExplanation / Answer
// i think above code should help you. Still, In case of any doubts, please ask in comments. If the answer helps you, please upvote. I am in great need of upvotes. Thanks!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.