Docsity

Programming Languages - test Five Solved | CS 503, Exams of Programming Languages

Toggle sidebar
bg1
CS 403/503 Exam 5 Fall 2007 Solution
CS 403 has built-in extra credit up to 120 points. CS 503 will be prorated to 100-point scale.
1. Rewrite the following C++ program as an equivalent object-oriented Ruby program.
Use good object-oriented style, including encapsulation, constructors, methods,
inheritance, late method binding, polymorphism, information hiding, code reuse, etc.
[60 points]
#include <iostream>
#include <string>
using namespace std;
const int salariedEmployee=1, hourlyEmployee=2, commissionedEmployee=3;
struct Employee {
string name;
int employeeType;
int annualSalary;
int hourlyRate;
int commissionRate;
};
int computeWeeklyPay (Employee &e, int hoursWorked, int salesTotal) {
switch (e.employeeType) {
case salariedEmployee:
return e.annualSalary/52;
case hourlyEmployee:
return e.hourlyRate * hoursWorked;
case commissionedEmployee:
return salesTotal * e.commissionRate/100;
}
}
int main( ) {
struct Employee e[3];
e[0].name="Johnson";
e[0].employeeType=salariedEmployee;
e[0].annualSalary=52000;
cout << e[0].name << ':' << endl;
cout << '$' << computeWeeklyPay (e[0], 40, 0) << endl;
e[1].name="Smith";
e[1].employeeType=hourlyEmployee;
e[1].hourlyRate=15;
cout << e[1].name << ':' << endl;
cout << '$' << computeWeeklyPay (e[1], 50, 8000) << endl;
e[2].name="Williams";
e[2].employeeType=commissionedEmployee;
e[2].commissionRate=5;
cout << e[2].name << ':' << endl;
cout << '$' << computeWeeklyPay (e[2], 10, 25000) << endl;
}
pf3
pf4

Partial preview of the text

Download Programming Languages - test Five Solved | CS 503 and more Exams Programming Languages in PDF only on Docsity!

CS 403/503 Exam 5 Fall 2007 Solution

CS 403 has built-in extra credit up to 120 points. CS 503 will be prorated to 100-point scale.

1. Rewrite the following C++ program as an equivalent object-oriented Ruby program.

Use good object-oriented style, including encapsulation, constructors, methods,

inheritance, late method binding, polymorphism, information hiding, code reuse, etc.

[60 points]

#include #include using namespace std; const int salariedEmployee=1, hourlyEmployee=2, commissionedEmployee=3; struct Employee { string name; int employeeType; int annualSalary; int hourlyRate; int commissionRate; }; int computeWeeklyPay (Employee &e, int hoursWorked, int salesTotal) { switch (e.employeeType) { case salariedEmployee: return e.annualSalary/52; case hourlyEmployee: return e.hourlyRate * hoursWorked; case commissionedEmployee: return salesTotal * e.commissionRate/100; } } int main( ) { struct Employee e[3]; e[0].name="Johnson"; e[0].employeeType=salariedEmployee; e[0].annualSalary=52000; cout << e[0].name << ':' << endl; cout << '$' << computeWeeklyPay (e[0], 40, 0) << endl; e[1].name="Smith"; e[1].employeeType=hourlyEmployee; e[1].hourlyRate=15; cout << e[1].name << ':' << endl; cout << '$' << computeWeeklyPay (e[1], 50, 8000) << endl; e[2].name="Williams"; e[2].employeeType=commissionedEmployee; e[2].commissionRate=5; cout << e[2].name << ':' << endl; cout << '$' << computeWeeklyPay (e[2], 10, 25000) << endl; }

class Employee

def initialize (name)

@name = name

end

def display (hoursWorked, salesTotal)

puts @name + ":"

puts "$" + computeWeeklyPay(hoursWorked, salesTotal).to_s

end

end

class SalariedEmployee < Employee

def initialize (name, annualSalary)

super(name)

@annualSalary = annualSalary

end

def computeWeeklyPay(hoursWorked, salesTotal)

@annualSalary/ 52

end

end

class HourlyEmployee < Employee

def initialize (name, hourlyRate)

super(name)

@hourlyRate = hourlyRate

end

def computeWeeklyPay(hoursWorked, salesTotal)

@hourlyRate * hoursWorked

end

end

class CommissionedEmployee < Employee

def initialize (name, commissionRate)

super(name)

@commissionRate = commissionRate

end

def computeWeeklyPay(hoursWorked, salesTotal)

salesTotal * @commissionRate/

end

end

e = [SalariedEmployee.new("Johnson", 52000),

HourlyEmployee.new("Smith", 15),

CommissionedEmployee.new("Williams", 5)]

e[0].display(40,0)

e[1].display(50,8000)

e[2].display(10,25000)

3. Each time this Ruby program calls puts, write its output where indicated below. [32 points]

class Animal def initialize (name = "Anonymous", legs = 0) @name = name; @legs = legs end def speak "Hi" end def kind self.class.to_s end def inspect @name + " is a " + kind + " who has " + @legs.to_s + " legs and says "" + speak + "."" end end class Dog < Animal def initialize (name = "Fido", breed = "Mutt") super(name, 4); @breed = breed end def speak "Arf" end def kind super + " (a " + @breed + ")" end end class Parrot < Animal def initialize (name = "Polly") super(name, 2) end def speak @name + " wants a cracker" end end class Octopus < Animal def initialize (name = "Oliver") super(name, 8) end end zoo = [Animal.new, Animal.new("Homer", 6), Dog.new, Dog.new("Rover", "Terrier"), Parrot.new, Parrot.new("Fred"), Octopus.new] for k in 0..zoo.length- 1

puts zoo[k].inspect

end

Output:

Anonymous is a Animal who has 0 legs and says "Hi."

Homer is a Animal who has 6 legs and says "Hi."

Fido is a Dog (a Mutt) who has 4 legs and says "Arf."

Rover is a Dog (a Terrier) who has 4 legs and says "Arf."

Polly is a Parrot who has 2 legs and says "Polly wants a cracker."

Fred is a Parrot who has 2 legs and says "Fred wants a cracker."

Oliver is a Octopus who has 8 legs and says "Hi."