Assignment 1: CMPT 101/104 Simon Fraser University, School of Computing Science Due by January 26, 2004 Please refer to the assignment web page for details of assignment preparation. !!!!!!!!! For problems 2 and 3 below!!!!!! Please follow the first three steps of the software development method. 1) State the problem, 2) Analyze the problem, define inputs and outputs, and develop a basic algorithm, with a test plan 3) Refine your algorithm, and present the final refined algorithm. Include results from all three steps in your submitted solution. 1. (20 points) A salesperson leaves his home every Monday and returns every Friday. He travels by company car. Each day on the road, the salesperson records the amount of gasoline put in the car. Given the starting odometer reading (the odometer reading before he leaves on Monday) and the ending odometer reading ( the odometer reading after he returns home on Friday), design an algorithm to find the average miles per gallon. A sample data is as follows: 68723 71289 15.75 16.3 10.95 20.65 30.00 Step 1: State the problem Find the average miles per gallon achieved by the of the salesman’s car. Step 2 Analyze the problem The inputs to the problem are the odometer reading before and after the salesman used the car the number of gallons of gasoline added to the cars gas tank each day The outputs of the problem are The number of miles per gallon of gasoline the car traveled that week An outline of the algorithm used to derive the solution from the inputs Use the Odometer readings to determine the total distance traveled. Find the total number of gallons of gasoline used Find the average miles per gallon Special cases or conditions we must assume that the amount of gasoline in the car is the same when the salesman picked up the car and when he dropped off the car. Assume the data entered to the algorithm are correct Test Plan Use at least two sets of realistic data such as the data set given in the problem to test the algorithm and program for correctness. Two sets of data will demonstrate the the algorithm works for more than a single data set. Develop the algorithm 1. Get startingOdometerReading and endingOdometerReading 2. distanceTraveled = startingOdometerReading – endingOdometerReading; 3. Get gasUsedD1, gasUsedD2, gasUsedD3, gasUsedD4, gasUsedD5 4. totalGasUsed = gasUsedD1 + gasUsedD2 + gasUsedD3 + gasUsedD4 + gasUsedD5 5. averageGasPerMile = distanceTraveled / totalGasUsed; 6. Print averageGasPerMile 2. (20 points) Develop an algorithm that takes N+1 coefficients, Ci, of the polynomial Y C 0 C1 X 1 C 2 X 2 C N X N , and a value of X as inputs and evaluates the polynomial to produce the value Y as output. Step 1: State the problem Evaluate a polynomial of order N, that is find the value of Y given values of the coefficients Ci , and a value of the variable X. The polynomial can be expressed Y C 0 C1 X 1 C 2 X 2 C N X N Step 2 Analyze the problem The inputs to the problem are The order of the polynomial N the N+1 coefficients of the polynomial Ci a value of the variable X The outputs of the problem are The value of the polynomial Y An outline of the algorithm used to derive the solution from the inputs For first solution Choose a maximum order M for which the algorithm will work for order N<M set all coefficients Cn+1 to Cm to zero Evaluate the polynomial by using the polynomial equation as given above for a polynomial of order M. OR For second solution Calculate Y according to (…((CNX +CN-1)X+CN-2)X+…C1)X+C0 or Store the value of CN, in Y Repeat the following steps for each N, N=N ... N=1, a. Multiply Y by X b. Add coefficient CN-1 to Y Add C0 to Y Special cases or conditions Soution1 N>= 0 Assume the data entered to the algorithm are correct or identify incorrect input as a special case and include a test for incorrect output in your test plan and detailed algorithm M<N more coefficients than can be evaluated by program OR Soution2 N>= 0 Assume the data entered to the algorithm are correct or identify incorrect input as a special case and include a test for incorrect output in your test plan and detailed algorithm Test Plan Soution1 Use 2 or more sets of coefficients with different N, with N<=M to test the algorithm and program for correctness in ‘normal’ situations Use 1 or more sets of coefficients with N>M to test the algorithm and program for correctness in ‘special’ situations Soution2 Use at least 2 sets of coefficients with different N to test the algorithm and program for correctness in ‘normal’ situations Develop the algorithm Soution1 1. Get N 2. If N>M print error message and stop 3. Get N+1 coefficients Ci 4. Set the coefficient CN+1 … CM to 0 5. Get a value of X 6. Calculate Y C 0 C1 X 1 C 2 X 2 C M X M 7. Print Y OR Soution2 1. Get N 2. Get N+1 coefficients Ci 3. Get a value of X 4. Store the value of CN, in Y 5. Repeat the following steps for each N, N=N ... N=0, a. Multiply Y by X b. Add coefficient CN-1 to Y 6. Print Y 3. (10 points) Exercise 6 Given int n, m, l; double x, y; which of the following assignments are valid? If an assignment is not valid, state the reason. When not noted, assume that each variable is declared. a. n=m=5; Valid: An integer value 5 is assigned to an int variable m, the value of int variable n is then assigned to int variable n b. m=l=2*n; Valid: The expression on the righ combines two integers and has an integer value. The integer value of the expression 2*n is assigned to an int variable l, the value of int variable l is then assigned to int variable m c. n=5; m=2+6; n=6/3; Valid: Each of the expressions on the right have integer values. The values of each of the expressions of the right are being assigned to integer variables. The arguments of all operators and assignments are integers so all operations and assignments are legal. d. m+n=l; Invalid: an assignment statement tells us that the value of the expression on the right is placed in the variable on the left. In this statement there is an expression on the left. An expression is not associated with a single location so we do not know where the assign (put) the value of the expression on the right. e. x=2*n+5.3; Valid: The expression on the right is first combining two integers (2*n) to give and integer. This resulting integer is them combined with a double. Therefore, the integer result will be converted to a double (conversion that will not loose information is chosen). The value of the expressioin on the right will therefore be a double. The assignment statement is assigning the double value of the expression to a double variable so the assignment is correct. f. l+1=n; Invalid: an assignment statement tells us that the value of the expression on the right is placed in the variable on the left. In this statement there is an expression on the left. An expression is not associated with a single location so we do not know where the assign (put) the value of the expression on the right. g. x/y=x*y Invalid: an assignment statement tells us that the value of the expression on the right is placed in the variable on the left. In this statement there is an expression on the left. An expression is not associated with a single location so we do not know where the assign (put) the value of the expression on the right. h. m=n%1; Valid: The expression on the right combines two integers using the operator % which is defined on the integer data types. The value of the expression on the right is an integer an can be assigned to the integer variable m. i. n=x%5; Invalid: The expression on the right is combining an integer and a double. The operator % is defined on the integer data types but not on the floating point data types. To evaluate the expression the double would have to be converted to an int. This conversion will cause a loss of information. Rather than doing the conversion an error results. The value of the expression on the right will therefore be invalid and thus the whole statement will be invalid j. x=x+5; Valid: The expression on the right is combining an integer and a double, therefore the integer will be converted to a double (conversion that will not loose information is chosen). The value of the expressioin on the right will therefore be a double. The assignment statement is assigning the double value of the expression to a double variable so the assignment is correct. k. n=3+4.6 Invalid: The expression on the right is combining an integer and a double, therefore the integer will be converted to a double (conversion that will not loose information is chosen). The value of the expressioin on the right will therefore be a double. The assignment statement is assigning the double value of the expression to a integer variable so the assignment may cause loss of information and is not allowed. Alternately, the student may have noticed that the ; is missing. This is an acceptable alternate reason. 4. (10 points) Write Java statements to accomplish the following. a. Declare int variables x and y int x, y; b. Initialze an int variable x to 10 and a char variable ch to ‘B’ x=10; ch = ‘B’; c. Update the value of an int variable x by adding 5 to it x+=5; d. Set the value of a double variable z to 25.3 z=25.3; e. Copy the content of an int variable y into an int variable z z=y; f. Swap the contents of the int variables x and y (Declare extra variables if necessary) int temp; temp = x; x=y; y=temp; g. Output the content of a variable x and an expression 2*x+5-y, where x and y are double variables System.out.println(x + (2*x+5-y) ); h. Declare a char variable grade and sets the value of grade to ‘A’ char grade=’A’; i. Declare int variables to store four integers int a, b, c, d; j. Copy the value of a double variable z to the nearest integer into an int variable x; z = (int)x; 5. (5 points) Write equivalent compound statements if possible. a. x = 2 * x; x += 2; b. x=x+y-2; x += y-2; c. sum=sum + num; sum += num; d. z= z * x + 2 * z; z *= x+2; e. y=y/(x+5); y /= x+5; 6. (10 points) Submit a completed assignment cover page to the submission server. To do this you should follow the following list of instructions: a. Log in in the assignment lab, download a copy of the cover page from the course web site. Load the cover page into Microsoft word and fill it in. b. Save the completed cover page with filename cover.doc (you may want to print it to use as the cover page for the rest of your assignment) c. Submit the file cover.doc to the submission server. If your submission was not received please see me sometime after class on Friday. I will email those of you whose cover page was not received, to assure you can submit assignment 2 correctly and on time. 7. (h of the following statement executes. The statements are executed in sequence. a. (1 point) c = b % a; The value of a before this statement is 3, a is used as an operator and is not changed by the execution of this statement The value of b before this statement is 8, b is used as an operator and is not changed by the execution of this statement The value of c is undefined before this statement. The value of the expression b%a is the remainder of 8/3 or 2. The value of the expression is assigned to the variable (the location in memory assigned to the variable) c. After execution of the statement c=2 b. (2 points) c= b/a; The value of a before this statement is 3, a is used as an operator and is not changed by the execution of this statement The value of b before this statement is 8, b is used as an operator and is not changed by the execution of this statement The value of c before this statement is 2. The value of the expression b/a is 8/3 or 2. The value of the expression is assigned to the variable (the location in memory assigned to the variable) c. After execution of the statement c=2 c. ( 3 points) c = a++ + - -b;. The value of a before this statement is 3, this value of a is used as an operator in the expression on the right, after it is used in the expression the value of a is incremented (increased) by one. After this line of code is completely executed a=4. The value of b before this statement is 8, this value is decremented by 1 to give 7, the value 7 is used as an operator in the expression on the right. After this line of code is completely executed b=7. The value of c before this statement is 2. The value of the expression is a b 3 7 or 10. The value of the expression is assigned to the variable (the location in memory assigned to the variable) c. After execution of the statement c=10 d. ( 2 points) b += a; a and b are both integers so the assignment statement does not require conversions The value of a before this statement is 4, a is used as an operator and is not changed by the execution of this statement The value of b before this statement is 7, The value of a is added to the value of b to give a new value of 11 for b. e. (2 points) c= -(a=b); The value of b before this statement is 11 The value of a before this statement is 4, The value of b is assigned to the variable a (both integer) so the new value of a is 11 The value of c before this statement is 10 The negative of the contents of a is assigned to c. c becomes -11. (5 points) Please print and fill in the “Getting to know you” page. (Page 2 of this assignment), attach a photo of yourself and attach the “Getting to know you” page as the last page of the assignment. The “Getting to know you” page will be removed before your assignment is returned to you. This one needs no solution!!! 8.