Chapter 10 Problems and Solutions by 387BA9V

VIEWS: 33 PAGES: 8

									Problem 21:

Construct a driver program to test the ComputePerimeter and ComputeArea methods in Figure 10-
14.

                               PlotCalc Class—Implementation 2
                                         (Figure 10-14)




      Start
         MyLength = 230.0
         MyWidth = 330.0
         MyPerimeter = 0.0
         MyArea = 0.0
         PlotCalc: BigPlot
         MyPerimeter = BigPlot.ComputePerimeter(MyLength, MyWidth)
         MyArea = BigPlot.ComputeArea(MyLength, MyWidth)
         Write ‘Perimeter = ‘, MyPerimeter, ‘Area = ‘, MyArea
      Stop
                                       or
      Start
         MyPerimeter = 0.0
         MyArea = 0.0
         PlotCal: BigPlot
         MyPerimeter = BigPlot.ComputePerimeter(230, 330)
         MyArea = BigPlot.ComputerArea(230, 330)
         Write MyPerimeter, MyArea
      Stop
Problem 22:

Design a class that will input a temperature measured in Fahrenheit degrees, convert the value to
Celsius degrees, and output both the Fahrenheit and Celsius values.

Class Definition

Class Temperature
   private Farenht
   private Celsius
   public Temperature
       Farenht = 0
       Celsius = 0
   End
   public Temperature(AnyTemp)
       Farenht = AnyTemp
       Celsius = 0
   End
   public GetFarenhtTemp
       Input Farenht
   End
   public ComputeCelsiusTemp
       Celsius = (Farenht - 32) * 5/9
   End
   public ShowTemperatures
       Display “Fahrenheit Temperature:”‘, Farenht, “Celsius Temperature: “, Celsius
   End
End Class


Problem 23:

Construct a driver program to test the class specified in problem 22.

Driver Program

Start
   FarTest = 59
   CelTest = 0
   Temperature: TempWed(FarTest)
   TempWed.ComputeCelsiusTemp
   TempWed.ShowTemperatures
Stop
Problem 1:

Design a class that will input the diameter of a circle and will calculate and output the circumferance and area
of the circle. The formula for calculating the circumference of a circle is C = 2πr and the formula for calculating
the area of a circle is A = πr² where π = 3.14.

Class Definition

Class Circle
  private diameter

  public Circle
   diameter = 0
  End

  public getDiameter
   Read diameter
  End

   public calculateArea(area)
    radius = diameter / 2
     area = 3.14 * radius * radius            or              3.14 * radius ** 2
   End

   public calculateCirca(circa)
    radius = diameter / 2
     circa = 2 * 3.14 * radius
   End

   public displayArea
     calculateArea(area)
     Display "Area of circle with diameter ", diameter, "is ", area
   END

  public displayCircumference
    calculateCirca(circa)
    Display "Circumference of circle with diameter ", diameter, "is ", circa
  End
End Class

Driver Program

Start
   Circle: testCircle
   testCircle.getDiameter()
   testCircle.displayArea()
   testCircle.displayCircumference()
End
Problem 2:

Using OOD/OOP concepts, design and create a solution to read a file of credit card customer records
and prepare a monthly credit card billing report. Each input record will contain the name of the credit
card holder, previous month’s balance, total purchases and total payments for that month. The
printed output will list the credit card holder’s name, previous month’s balance, total purchases, total
payments, the amount subject to a finance charge, the interest charged, and the new balance with
appropriate messages.

The amount subject to a finance charge is calculated by adding the total purchases to the previous
balance and subtracting the total payments. If the amount subject to a finance charge is $250 or
more, the interest will be 1.5%. If the amount subject to a finance charge is less than $250, the
interest will be 1%. The new balance is calculated by adding the interest to the amount subject to a
finance charge. Following the processing of all input records, output the total interest accrued for all of
the customers.

   1. Design a class specification named “CreditCard” including the input and output data members,
      and the appropriate methods that will permit the creation (instantiation) of an object showing
      the desired output. Be sure to include a constructor method.

   2. Write a driver program or algorithm that will test the class specification described in step 1.
      Assume an automatic EOF.

Class CreditCard
   name
   pbal
   pur
   pay
   tint
   private CreditCard
        name = blank
       pbal = 0
        pur = 0
        pay = 0
        tint = 0
   End
   public getRecord
        Read name, pbal, pur, pay
   End
   private calculateInterest(amtSubFc, int)
        amtSubFc = pbal + pur – pay
        If amtSubFC >= 250
            int = amtSubFc * .015
        Else
            int = amtSubFc * .01
        EndIfAdd int to tint
   End
  private newBalance(amtSubFc, int, nbal)
     nbal = amtSubFC + int
  End
  public printDetailLine
     calculateInterest(amtSubFc, int)
     calculateNewBalance(amtSubFc, int, nbal)
     Print ‘Name: ‘, name, ‘Previous Balance: ‘, pbal, ‘Purchases: ‘, pur, ‘Payments: ‘, pay,
           ‘Amount Subject to Finance Charge: ‘, amtSubFc, ‘Interest: ‘, int,
           ‘New Balance: ‘, nbal
  End
  public printTotal
     Print ‘Total Interest: ‘, tint
  End
End Class



Driver Program

Start
   CreditCard: credCard1
   credCard1.getRecord
   DoWhile Not Eof
      credCard1.printDetailLine
      credCard1. getRecord
   EndDo
   credCard1.printTotal
End
                                      ITP 100 - Software Design
                                           Test 4 Problem
Problem:


At the end of each business day, a retail store needs to process a file of daily sales transaction records for
items purchased during that day. The data collected for each transaction includes item number, item
description, quantity purchased, and unit price. For purchase amounts >= $200, a 4% discount is given.
For purchase amounts >= $500, a 7% discount is given. A sales tax of 5.5% is charged on all purchases after
any discounts are applied. A detail line showing the net price for each item purchased including descriptions for
each variable is to be printed. After all transactions have been processed, the total net price and average net
price of all transactions are to be printed. Purchase amount = quantity purchase x unit price.



   1. Design the class and name it ‘ItemPrice’.


   2. Write an algorithm for each required operation or method in the table. Note: You can list the input data
      fields and any counters or accumulators as attributes in your class specification. All other variables
      must be local, i.e., you must pass these values as parameters when appropriate.


   3. Write a driver program or algorithm that will test the class specification described above.
                                    ITP 100 - Software Design
                                         Test 4 Solution

Class Description
Class ItemPrice
  private itemNum
  private itemDes
  private qty
  private uprice
  private tprice
  private tcnt

  public itemPrice()
    itemNum = “ “
    itemDes = “ “
    qty = 0
    uprice = 0
    tprice = 0
    tcnt = 0
  End

  public getRecord()
    Read itemNum, itemDes, qty, uprice
  End

  public calcPrice(nprice)
             gprice = qty * uprice
             If gprice >= 500 Then
                     dprice = gprice * .07
             Else
                     If gprice >= 200 Then
                             dprice = gprice * .04
                     Else
                             dprice = 0
                     Endif
             Endif
             tax = (gprice – dprice) *.055
             nprice = gprice – dprice + tax
             Add nprice to tprice
             Add 1 to tcnt
  End

  public printPrice()
    calcPrice(nprice)
    Print ‘Item No. ‘, itemNum, ‘Description ‘, itemDes, ‘Quantity’, qty, ‘Unit Price ‘, uprice,
                      ‘Net Price ‘, nprice
  End
  public printTotPrice()
              Print ‘Total Price:’, tprice
              Print ‘Average Price: ‘, tprice/tcnt
  End

Test Driver Algorithm:
testItemPrice()
       ItemPrice: itemPrice1
   itemPrice1.getRecord()
   Dowhile Not Eof
              itemPrice1.printPrice()
              itemPrice1.getRecord()
   EndDo
   itemPrice1.printTotPrice()
End

								
To top