Ch11_Examples - Programming Logic and Design 8th Edition Chapter 11 Programming Exercises 1 Complete the following tasks a Design a class named Circle

Ch11_Examples - Programming Logic and Design 8th Edition...

This preview shows page 1 - 4 out of 34 pages.

Programming Logic and Design, 8th Edition Chapter 11 Programming Exercises 1. Complete the following tasks: a. Design a class named Circle with fields named radius , area , and diameter . Include a constructor that sets the radius to 1. Include get methods for each field, but include a set method only for the radius. When the radius is set, do not allow it to be zero or a negative number. When the radius is set, calculate the diameter (twice the radius) and the area (the radius squared times pi, which is approximately 3.14). Create the class diagram and write the pseudocode that defines the class. Answer: A sample solution follows Diagram: Circle -radius: num -area: num -diameter: num +setRadius(rad : num) : void +getRadius() : num +getArea() : num +getDiameter() : num Pseudocode: class Circle Declarations private num radius private num area private num diameter public Circle() radius = 1 return public void setRadius(num rad) if rad <= 0 then radius = 1 else radius = rad endif diameter = 2 * radius area = (radius * radius) * 3.14
Image of page 1

Subscribe to view the full document.

return public num getRadius() return radius public num getArea() return area public num getDiameter() return diameter endClass b. Design an application that declares two Circle s. Set the radius of one manually, but allow the other to use the default value supplied by the constructor. Then, display each Circle ’s values. Answer: A sample solution follows Pseudocode: start Declarations Circle myCircle Circle yourCircle myCircle.setRadius(5) output “Circle 1 info:” output myCircle.getRadius() output myCircle.getArea() output myCircle.getDiameter() output “Circle 2 info:” output yourCircle.getRadius() output yourCircle.getArea() output yourCircle.getDiameter() stop 2. Complete the following tasks: a. Design a class named PhoneCall with four fields: two strings that hold the 10- digit phone numbers that originated and received the call, and two numeric fields that hold the length of the call in minutes and the cost of the call. Include a constructor that sets the phone numbers to X s and the numeric fields to 0. Include get and set methods for the phone number and call length fields, but do not include a set method for the cost field. When the call length is set, calculate the cost of the call at three cents per minute for the first 10 minutes, and two cents per subsequent minute. Create the class diagram and write the pseudocode that defines the class. Answer: A sample solution follows
Image of page 2
Diagram: PhoneCall -origCall: string -recCall: string -length: num -cost: num +setOrigCall(number : string) : void +setRecCall(number : string) : void +setLength(len : num) : void +getOrigCall() : string +getRecCall() : string +getLength() : num +getCost() : num Pseudocode: class PhoneCall Declarations private string origCall private string recCall private num length private num cost public PhoneCall() origCall = “XXXXXXXXXX” recCall = “XXXXXXXXXX” length = 0 cost = 0 return public void setOrigCall(string number) origCall = number return public void setRecCall(string number) recCall = number return public void setLength(num len) length = len if length > 10 then cost = (3 * 10) + (2 * (length – 10)) else cost = 3 * length endif return public string getOrigCall() return origCall public string getRecCall() return recCall
Image of page 3

Subscribe to view the full document.

Image of page 4

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask bonus questions You can ask questions ( expire soon) You can ask questions (will expire )
Answers in as fast as 15 minutes