Computer Science 180



Computer Science 172

Lab 2: Modifying and Extending a Program

1) Open a web browser and navigate to the course page available from the course webpage. Find the link to the program for lab 2, and save it to the CS172 folder you created in last week's lab.

2) Open AdaGIDE, and then open the program you saved in step 1. Read the program and try to understand what the program does. Once you have read the program, compile, build and run it.

What does this program do? Write your answer here.

_____________________________________________________________

What packages does this program use? Write your answer here.

_____________________________________________________________

3) Feet and inches are part of the imperial system of measurement used in the U.S. The rest of the world uses the vastly superior metric system. You can convert feet to meters by dividing the number of feet by 3.281.

Modify the program so that, in addition to converting feet to inches, it also converts the calculated number of feet to meters. To do this, perform the following steps:

a) Declare a constant called Feet_Per_Meter, with a value of 3.281.

b) Declare a Float variable called Number_Of_Meters.

c) Write an assignment statement that calculates the correct value of the variable Number_Of_Meters from the variable Number_Of_Feet. Put this assignment statement immediately after the calculation of Number_Of_Inches.

4) Modify the program to output the calculated number of meters as follows:

a) Select all the output statements in the program beginning with the line that says

Ada.Text_Io.Put (Item => "There are ");

and ending with the line that says

Ada.Text_Io.New_Line;

b) Choose edit > copy

c) Paste the copied statements at the end of the program, just before the line

end Convert_Distance;

Be sure to leave blank lines between the old statements and the new statements, and between the new statements and the end of the program. (Ada doesn't require this, but it is the standard format.)

d) Change the pasted line that says

Ada.Float_Text_Io.Put (

Item => Number_Of_Inches,

Aft => Output_Precision);

to

Ada.Float_Text_Io.Put (

Item => Number_Of_Meters,

Aft => Output_Precision);

e) Change the output statement that contains the string " inches in " to the string

" meters in ".

h) Save the program, then compile, build and run it. The output should look like this. User input is shown in bold.

Please enter the number of feet: 1000.0

There are 1.20E+04 inches in 1.00E+03 feet.

There are 3.05E+02 meters in 1.00E+03 feet.

5) A "parsec" is a large unit of distance used in astronomy. One foot equals[pic] parsecs. Modify the program to calculate the number of parsecs in the number of feet entered by the user. To do this:

a) Declare an appropriately named constant to hold the conversion factor. Use scientific notation for the value of the constant.

b) Declare an appropriately named variable to hold the number of parsecs.

c) Write a statement to calculate the number of parsecs. Be sure to put this statement in the appropriate place in the program.

e) Print the answer in the same manner as in step 4.

7) When you are finished, save, compile, build and run your program. The output should look like this:

Please enter the number of feet: 5280.0

There are 6.34E+04 inches in 5.28E+03 feet.

There are 1.61E+03 meters in 5.28E+03 feet.

There are 5.22E-14 parsecs in 5.28E+03 feet.

Be sure to double-check your output to see that your result is correct and correctly labeled.

What to turn in: When you have finished with the changes to the program and are sure the calculations and output are correct, put your name and date in the appropriate places in the comment at the beginning of the declarative part, and then print out a copy of the compiler listing file. Write your name at the top of this lab sheet, staple it on top of the compiler listing, and turn it in.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download