Homework #10

Assigned: April 14
Due: April 21, 4:30pm


  1. (5 points) Define a PROLOG predicate adjacent which accepts only 3 arguments and succeeds if its first two arguments are adjacent in its third list argument and fails otherwise. For example,
    ?- adjacent(1,2,[1,2,3]).
    true.
    
    ?- adjacent(2,1,[1,2,3]).
    true.
    
    ?- adjacent(2,3,[1,2,3]).
    true.
    
    ?- adjacent(3,1,[1,2,3]).
    false.
    
    For 5 points extra credit make the list circular such that:
    ?- adjacent(1,4,[1,2,3,4]).
    true.
    
    ?- adjacent(4,1,[1,2,3,4]).
    true.
    
    ?- adjacent(2,4,[1,2,3,4]).
    false.
    
  2. (5 points) Define a PROLOG predicate last which accepts only 2 arguments and succeeds if its first argument is the last element of its second list argument and fails otherwise. For example,
    ?- last(X,[1,2,3]).
    
    X = 3 
    
    ?- last(4,[1,2,3]).
    false.
    
  3. (10 points) Define a PROLOG predicate validdate which accepts only 3 arguments, which represent a month, day, and year, in that order, and succeeds if these arguments represent a valid date in Gregorian calendar, and fails otherwise. Recall, a leap year is one which is divisible by 400, or divisible by 4, but not also 100. Therefore, 2000, 2004, 2008 were leap years, while 1800 and 1900 were not. Your solution may contain helper predicates, but must not exceed 20 lines of code. The following are some examples:
    ?- validdate(feb,29,2000).
    true.
    
    ?- validdate(feb,30,2000).
    fail.
    
    ?- validdate(feb,29,2004).
    true.
    
    ?- validdate(feb,29,1900).
    fail.
    
    ?- validdate(may,16,2007).
    true.
    
    ?- validdate(jun,31,2007).
    fail.
    
    ?- validdate(apr,-10,3).
    fail.
    
    ?- validdate(apr,32,3).
    fail.
    
    ?- validdate(apr,30,-100).
    fail.
    
    ?- validdate(apr,30,0).
    true.
    
    ?- validdate(jul,0,0).
    fail.
    
    ?- validdate(jul,1,0).
    true.
    
    ?- validdate(fun,15,2006).
    fail.
    
  4. (5 points) Consider the following Smalltalk implementation of Euclid's gcd (greatest common divisor) algorithm:
    gcd: other
       [self = other]
            ifTrue: [^ self]
        [self < other]
            ifTrue: [^ self gcd: (other - self)]
            ifFalse: [^ other gcd: (self - other)]
    
    Trace all messages involved in evaluating 4 gcd: 6.

  5. (5 points) In Smalltalk, everything is an object, even a class. What advantages does this design provide over other object-oriented languages such as C++ or Java where everything is not an object? What disadvantages does this approach have?

  6. (5 points) Read A Conversation with Alan Kay from ACM Queue, 2(9), 2004-05 and write a 250 word commentary on it. In your commentary, take a stance and defend your arguments with concepts you have leaned in this course. Also, address the advantages and disadvantages to dynamic binding and its potential to have an impact on software development. Be clear and concise in your prose.

    ``Alan Kay led the team that developed Smalltalk at Xerox PARC. He is the recipient of the 2003 ACM A.M. Turing Award for pioneering many of the ideas at the root of contemporary object-oriented programming languages, leading the team that developed Smalltalk, and for fundamental contributions to personal computing. The Turing Award is the analog of the Nobel Prize for computer scientists. Kay said `the best way to predict the future is to invent it.'''

  7. (20 points) First complete John Maloney's Squeak tutorial and implement the BankAccount class. You will then inherit from this class and add new functionality, specifically:
    1. create a subclass of BankAccount called SavingsAccount which has, in addition to balance and history, an allotment of money in various categories. The allotment is an associative array involving (category, value) pairs. For instance, a sample allotment is: {('education',20),('marriage',30),('parking tickets',70),('Having Perugini for CPS 343', priceless)}. Recall from lecture how such an associative array can be implemented. The idea of allotment is to describe how the balance (from BankAccount) is distributed (note that this differs from the tutorial). Therefore, if balance is $1000, and allotment is: {('education',10),('marriage',30)}, then this means that 10/(10+30) of balance is dedicated toward education, and the remaining 30/(10+30) of balance is reserved for marriage costs. In other words, there is three times more money reserved for marriage than education.

    2. SavingsAccount must support the following messages:
      • new which creates a SavingsAccount object, initializes balance to zero, and history and allotment to empty lists.
      • initialize, which sets balance to zero and initializes history and allotment to empty lists (this is meant for use with an existing SavingsAccount object).
      • plansavings: factor for: reason adds an entry in allotment of (reason,factor).
      • showdistribution which plots a bar chart of money versus the categories in which it is allotted. For instance, when balance is $1000, and allotment is: {('education',10),('marriage',30)}, then the bar chart will show two bars: one of height $250 for education and the other of height $750 for marriage.

    3. (15 points extra credit) Implement a message pies in SavingsAccount which presents the percentage distribution of allotments in the form of a pie chart, with each slice of a different color and clearly labeled with the corresponding category. It is not necessary to build the pie chart from scratch. Use an existing pie chart class available in Squeak or from the web.

    Your program must adhere to the tenets of object-oriented programming (encapsulation, information hiding) as well as our programming style guide.

    There is sample code to execute in your project workspace available here.

    Your entire class hierarchy must be stored as a single file. All documentation and identifying information should be placed in comments within your source code. Each class must begin with the comment header given in the style guide and each method must begin with comments.

    Save your class category HW10 from the System Browser by ctrl-clicking on the category name in the browser and selecting fileOut. A file with the .st extension will be generated in the current directory. For more details, including how to open such a saved file in the Squeak environment, see Opening and Saving files in Squeak.

    Submit a printout of all of your code, including your code for the BankAccount class. To access all of your code for a category of classes or a particular class, save the category or class and then open it. When opening the file, the bottom section of the file dialog box will contain all of the code for the particular category of classes or class which you are attempting to open. HTML generation.

    Squeak your way in to Smalltalk and have fun!

  8. (10 points extra credit) Write a 250 word essay which answers the following question. Is LISP a functional language or a multi-paradigmed language, or something else? More generally, does LISP belong to any one particular paradigm or is it unique unto itself (i.e., its own paradigm)? Perl, Python, and Ruby have imperative, functional, and object-oriented features and are thus multi-paradigmed languages.

  9. (10 points extra credit) Write a 250 word essay which answers the following question. Is there a such thing as a perfect or universal programming language? If so, which language is it. If not, describe if a perfect language is possible or not. If impossible, describe why. If possible, but not present, describe the attributes of this perfect programming language in light of the concepts studied in this course.

  10. (10 points extra credit) Write a 250 word synopsis of this course. The audience of your essay is someone with a Bachelors degree in Computer Science who did not take a programming languages course in their program of study. Answer the following two questions in your essay: what is a course in programming languages about? What is studied? Full credit will only be awarded to students who write a clear and succinct essay.

  11. (15 points extra credit) Write a 250 word reflection essay of this course. The audience of your essay is someone with a Bachelors degree in Computer Science who did not take a programming languages course in their program of study. Answer the following questions in your essay:
    • How are the seven modules of this course related to each other? Are they dependent on each other or independent of each other or both? How do these modules fit together to paint a picture of programming languages? Explain. If you say the modules/topics are independent of each, submit a diagram illustrating how they are related.
    • Did you see the courses themes and objectives come to life throughout the content of this course?
    Full credit will only be awarded to students who write a clear and succinct essay.

Return Home