Answer: Geometry Calculator



Download 66.29 Kb.
Date20.05.2017
Size66.29 Kb.
#18658
1. Look for ways to modularize the algorithm that you designed for Algorithm Workbench Question 3 and modify it accordingly.

 

Answer:


 

2. Geometry Calculator

Write a program that displays the following menu:

Geometry Calculator

1. Calculate the Area of a Circle.

2. Calculate the Area of a Rectangle.

3. Calculate the Area of a Triangle.

4. Quit

Enter your choice (1-4).



 

If the user enter 1, the program should ask for the radius of the circle and then display its area. Use the following formula to calculate the circle's area:

area = pi*r^2

Use 3.1459for pi and the radius of the circle for r.

 

If the user enters 2, the program should ask for the length and width of the rectangle and then display the rectangle's area. Use the following formula to calculate the rectangle's area:



area = length x width

 

If the user enters 3, the program should ask for the length of the triangle's base and its height, and then display its area. Use the following formula to calculate the area of a triangle:



area = base x height x .5

 

If the user enter 4, the program should end.



 

Answer:

Declare integer choice

Declare float area

Declare integer radius

Declare integer length

Declare integer width

Declare integer base

Declare integer height

Display "Geometry Calculator"

Display "1. Calculate the Area of a Circle."

Display "2. Calculate the Area of a Rectangle."

Display "3. Calculate the Area of a Triangle."

Display "4. Quit"

Display "Enter your choice (1-4)."

Input choice

Switch choice

Case 1

Display "Enter the radius of the circle"



Input radius

Area = 3.1459 * radius power 2

Display "Area of the circle is " & Area

  Break


Case 2

Display "Enter the length of the rectangle"

Input length

Display "Enter the width of the rectangle"

Input width

Area = length * width

Display "Area of the rectangle is " & Area

Break


Case 3

Display "Enter the length of the triangle's base"

Input base

Display "Enter the triangle's height"

Input height

Area = base * height * 0.5

Display "Area of triangle is " & Area

Break


Case 4

Exit Program

End Switch
 

note the first two parts are the program to be modified

 

3. Golf Scores

The Springfork Amateur Golf Club has a tournament every weekend. The club president has asked you to design two programs.

(1) A program that will read each player's name and golf score as keyboard input, and then save these records in a file named golf.dat. (Each record will have a field for the player's name and a field for the player's score.)

(2) A program that reads the records from the golf.dat file and displays them.



Answer:

(1)
Declare string name


Declare integer score
Display "Enter name."
Input name
Display "Enter Score"
Input score
Open "C:\FOLDERNAME\ golf.dat" For Append As #1
' appends the input to an existing file
' write to the textfile
Write #1, name,score
close #1 ' close the file

(2)
Declare string name


Declare integer score
Open "C:\FOLDERNAME\ golf.dat " For Input As #1 ' Open file for input.
do while not eof(1)
Input #1, name,score
display name & vbtab & score
loop
close #1

 

Best Golf Score

Modify program #2 that you wrote for Programming Exercise 6 so it also displays the name of the player with the best (lowest) golf score. (Hint: Use a technique similar to the one that was discussed in Chapter 8 for finding the lowest value in a array. You do not need to read the file in the array to use this technique, however. It can be adapted for use with a file.)

Answer:

Declare string name


Declare integer score

declare string bestPlayer


declare integer lowest

lowest = integer.maxvalue

Open "C:\FOLDERNAME\ golf.dat " For Input As #1 ' Open file for input.
do while not eof(1)
Input #1, name,score
display name & vbtab & score
if score < lowest then
lowest = score
bestPlayer = name
end if
loop
close #1
display "Lowest score was: " & bestPlayer

3. Golf Score Modification

In Programing Exercise 6 in Chapter 10 you design the following two programs for Springfork Amatuer Golf Club.

 

(1) A program that will read each player's name and golf score as keyboard input, and then save these records in a file named golf.dat.



(2) A program that reads the records from the golf.dat file and displays them.

Consolidate these programs into a single program that presents a menu, allowing the user to select the operation he or she wants to perform.



Answer:

Function Program1

Declare string name
Declare integer score
Display "Enter name."
Input name
Display "Enter Score"
Input score
Open "C:\FOLDERNAME\ golf.dat" For Append As #1
' appends the input to an existing file
' write to the textfile
Write #1, name,score
close #1 ' close the file

End Function

'------------------------------------------

Function Program 2

Declare string name
Declare integer score
Open "C:\FOLDERNAME\ golf.dat " For Input As #1 ' Open file for input.
do while not eof(1)
Input #1, name,score
display name & vbtab & score
loop
close #1

End Function

'------------------------------------

Main()


Declare integer choice

Display "Menu"

Display "1. Reading Golf Data"

Display "2. Displaying Golf Data"

Display "3.Exit the program"

Display "Please make a choice"

Input choice

Switch choice

Case 1

CALL Program1



Break

Case 2


CALL Program2

Break


Case 3

Exit Program

End Main

 

4. What will the following program display?



Module main ()

Declare Integer num = 0

Call showMe (num)

End Module

 

Module showMe (Integer arg)



If arg < 10 then

Call showMe (arg + 1)

Else

Display arg



End If

End Module

 

Answer:

10

 

 



 

5. What will the following program display?

 

Module main ()



Declare Integer num = 0

Call showMe (num)

End Module

 

Module showMe (Integer arg)



If arg < 10 then

Call showMe (arg + 1)

End If

End Module



 

Answer:

 Nothing


 

 

6. The following module uses a loop. Rewrite it as a recursive module that performs the same operation.



Module trafficSign (int n)

While n > 0

Display "No Parking"

Set n = n - 1

End While

End Module

 

Answer:

 Module trafficSign (int n)

IF n > 0

Display "No Parking"

trafficSign (n-1)

End IF


End Module

 

 



Programing Exercise

7. Recursive Power Method

Design a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer.

 

Answer:

 Module Power(int a, int pow)

IF (pow == 0)

Return 1


Else

Return a* power( a, pow -1 )

End Module

 

 



8. Ackermann's Function

Ackerman's Function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Design a function Ackermann (m,n), which solves Ackermann's Function. Use the following logic in your function:

 

If m = 0 then return n+1



If n = 0 then return Ackermann (m - 1 ,1)

Otherwise, return ackermann (m - 1, Ackermann(m, n - 1 )

 

Answer:

Module Ackermann(m,n)

IF (m == 0)

Return n+1

Else IF (n == 0)

Return  Ackermann (m - 1 ,1)

Else

Return Ackermann( m-1, Ackermann(m,n-1))



End Module
 

9. Suppose myCar is the name of a class variable that references an object, and go is the name of the method. ( The go method does not take any arguments.) Write a psuedocode statement that uses the myCar variable to call the method.



Answer:

 Call mycar.go()

10. In pseudocode, write the first line of the definition for a Poodle class. The class should extend the Dog class.

Answer:

 Class Poodle Extends Dog

11. Look at the following pseudocode class definitions:

Class Plant

Plublic Module message()

Display "I'm a plant."

End Module

End Class

Class Tree Extends Plant

Public Module message()

Display "I'm a tree."

End Module

End Class

Given these definitions, what will the following psuedocode display?

Declare Plant p

Set p=new Tree()

Call p.message()

Answer:

I'm a tree 



Programming Exercise

12. Pet Class

Design a class named Pet, which should have the following fields:



  • name-The name field holds the name of a pet.

  • type-The type field holds the type of animal that a pet is. Example values are "Dog", "Cat", and "Bird".

  • age-The age field holds the pet's age.

The Pet class should also have the following methods:

  • setName-The setName method store a value in the name field.

  • setType-The setType method store a value in the type field.

  • SetAge-The setAge method stores a value in the age field.

  • getName-The getName method returns the value of the name field.

  • getType-The getType method returns the value of the type filed.

  • GetAge-The getType method returns the value of the age field.

Once you have designed the class, design a program that creates an object of the class and prompts the user to enter the name, type, and age of his or her pet. This data should be stored in the object. Use the object's accessor methods to retrieve the pet's name, type, and age and display this data on the screen.

Answer:

Class Pet

Declare private string Name

Declare private string Type

Declare private integer age

Public Module setName( string name)

This.Name =name

End Module

Public Module setType( string type)

This.Type =type

End Module

Public Module setAge( integer age)

This.Age =age

End Module

String Public Module getName( )

Return This.Name

End Module

String Public Module getType( )

Return This.Type

End Module

String Public Module getAge( )

Return This.Age

End Module

End Class


Module Main()

Declare string Pname

Declare string Ptype

Declare integer Page

Declare Pet myPet

Set myPet = new Pet()

Display "Please enter your pet's Name"

Input Pname

Display "Please enter your pet's type"

Input Ptype

Display "Please enter your pet's age"

Input Page


Call myPet.setName(Pname)

Call myPet.seType(Ptype)

Call myPet.setage(Page)
Pname = Call myPet.getName()

Ptype = Call myPet.getType()

Page = Call mypet.getAge()

Display Pname & " is a " & Ptype &"and aged" & Page

End Module
13. Personal Information Class

Design a class that holds the following personal data: name, address, age, and phone number. Write appropriate accessor and mutator methods. Also, design a program that creates three instances of the class. One instance should hold your information, and the other two should hold your friends' or family members' information.



Answer:

Class Person

Declare Private string Name

Declare Private string Address

Declare Private integer Age

Declare Private string PhoneNumber

Public Module setName( string name)

This.Name =name

End Module

Public Module setAddress( string address)

This.Address =address

End Module

Public Module setAge( integer age)

This.Age =age

End Module

Public Module setPhoneNumber( string phonenumber)

This.PhoneNumber =phonenumber

End Module


String Public Module getName( )

Return This.Name

End Module

String Public Module getAddress( )

Return This.Address

End Module

String Public Module getAge( )

Return This.Age

End Module

String Public Module getPhoneNumber( )

Return This.PhoneNumber

End Module

End Class

Module Main()

Declare Person me

Declare Person sister

Declare Person father

Set me = new Person()

Set sister = new Person()

Set father = new Person()

Call me.setName ("Tim")

Call me .setAddress("22 ChinaTown NY")

Call me.setAge(24)

Call me.setPhoneNumber("383723829")

Call sister.setName ("Jackie")

Call sister.setAddress("22 ChinaTown NY")

Call sister.setAge(18)

Call sister.setPhoneNumber("386643829")

Call father.setName ("Solivan")

Call father.setAddress("22 ChinaTown NY")

Call father.setAge(57)

Call fathre.setPhoneNumber("325533829")



End Module

Download 66.29 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page