Write a program that converts spelled-out numbers such as zero and two into digits, such as 0 and 2. When the user inputs a number, the program should print out the corresponding digit. Do it for the...
This project is a continuation of the previous project. Many applications require pseudorandom number sequences that are not uniformly distributed. For example, a program that simulates the birth of...
Write a class for rational numbers. Each object in the class should have two integer values that define the rational number: the numerator and the denominator. For example, the fraction 5/6 would...
This project requires a little understanding of velocity and gravity, but dont let that scare you away! Its actually an easy project. The assignment is to write a class in which each object...
Give five examples of legal names that you shouldnt use because they are likely to cause confusion.
Define a rule to help decide if a conversion from one type to another is safe or unsafe.
Using Appendix I as a guide, implement an applet for interactive testing of the sequence class from the previous project. Appendix I Its useful to have a small interactive test program to help you...
In this project, you will design and implement a class called Towers, which is part of a program that lets a child play a game called Towers of Hanoi. The game consists of three pegs and a collection...
Implement the Statistician class from Project 2 on page 95, but include a new method that returns the median value of all the numbers. The median is a number that is greater than or equal to at least...
Implement the previous project with the following modification: All of the input numbers to the Statistician are required to be integers in the range from 0 to 100. This modification means that its...
Another way to store a collection of items is called a keyed bag. In this type of bag, whenever an item is added, the programmer using the bag also provides an integer called the key. Each item added...
Write a method that takes a linked list of integers and rearranges the nodes so that the integers stored are sorted into the order of smallest to largest, with the smallest integer in the node at the...
Write a program to solve quadratic equations. A quadratic equation is of the form ax 2 + bx + c = 0 If you dont know the quadratic formula for solving such an expression, do some research. Remember,...
Revise the set class (Programming Project 5 on page 169) so that it stores the items in a linked list instead of in an array. Data from Project 5 Using Appendix I as a guide, implement an applet for...
Revise the Statistician with median (Programming Project 15 on page 172) so that it stores the input numbers on a doubly linked list using the doubly linked node class from the previous project....
Do exercise 6, but with three string values. So, if the user enters the values Steinbeck, Hemingway, Fitzgerald, the output should be Fitzgerald, Hemingway, Steinbeck.
Reimplement the bag class from Figure 4.17 so that the items of the bag are stored with a new technique. Heres the idea: Each node of the noefw linked list contains two integers. The first integer is...
The bags clone method creates a copy of an ArrayBag. As with other clone methods, adding or removing elements from the original bag will not affect the copy, nor vice versa. However, these elements...
Modify the bag from the previous exercise so that all of the add methods attempt to make a clone of any item that is added to the bag. These clones are then put in the bag (rather than just putting a...
Enhance the calculator program given in Figure 6.5 on page 330 so that it has all of the following features: After one expression is evaluated, the user is asked if he or she wants to evaluate...
In Figure 6.5 on page 330, we presented a program to evaluate arithmetic expressions. In this exercise, you will write a similar program to evaluate boolean expressions. Rather than arithmetic...
Redo the calculator program given in Figure 6.5 on page 330, but this time implement it in a different way. To evaluate the arithmetic expression, your program will first convert the expression to...
Write a program that evaluates an arithmetic expression in infix notation, without full parentheses. Use the following algorithm: There are two stacks: a numbers stack and an operators stack. When a...
Suppose that you have n queens from a chess game, and that you also have an n-by-n chess board. Is it possible to place all n queen on the board so that no two queens are in the same row, no two...
In Figure 7.3, we presented a program that checks a string to see if the letters in the string read the same forward and backward. The previous exercise performed a similar check using words in place...
Find a good cookbook. Read the instructions for baking blueberry muffins (if you are in a country where blueberry muffins is a strange, exotic dish, use a more familiar dish instead). Please note...
Enhance the car wash simulation method in Figure 7.8 so that it has the following additional property. There is an additional parameter, which is a maximum length for the queue. When the queue gets...
A permutation is an ordered subset of a set. For example, say you wanted to pick a combination to a vault. There are 60 possible numbers, and you need three different numbers for the combination....
Build a regular expression that captures all nonempty sequences of letters other than file, for, and from. For notational convenience, you may assume the existence of a not operator that takes a set...
Write a simulation program for a small airport that has only one runway. There will be a queue of planes waiting to land and a queue of planes waiting to take off. However, only one plane can use the...
Do an airplane simulation that is more complex than the previous project. In this version, planes arrive for landing with a random amount of fuel, which determines how long they can remain in the...
Give a grammar that captures all levels of precedence for arithmetic expressions in C.
Write top-down and bottom-up grammars for the language consisting of all well-formed regular expressions. Arrange for all operators to be left associative. Give Kleene closure the highest precedence...
Suppose that the expression grammar in Example 2.8 were to be used in conjunction with a scanner that did not remove comments from the input, but rather returned them as tokens. How would the grammar...
This project uses the Towers class from Chapter 3s Programming Project 12. For the project, write a recursive methodxtxhxat computes and prints a solution to the Towers of Hanoi game. The method...
Give three concrete examples drawn from programming languages with which you are familiar in which a variable is live but not in scope.
Implement a new method for the BTNode from Figure 9.10. The new method creates a Java Vector that contains the data from all the nodes in a tree, as specified in Figure 9.15. Details about the Java...
Java has a generic interface called Comparable. A class that implements the Comparable interface must have a method with this specification: compareTo public boolean compareTo(E obj) Compare this...
Consider the design of a Fortran 77 compiler that uses static allocation for the local variables of subroutines. Expanding on the solution to the previous question, describe an algorithm to minimize...
Write a simple program in Scheme that displays three different behaviors, depending on whether we use let, let*, or letrec to declare a given set of names.
Give two examples of reasonable semantic rules that cannot be checked at reasonable cost, either statically or by compiler-generated code at run time.
Expand the class from Project 10 or 11 so that there is an extra method that produces a Java Iterator for the bag. Data from Project 10 Write a class for a bag of strings, where the strings are...
One potential criticism of the obvious solution to the previous problem is that the values in internal nodes of the parse tree do not reflect the value, in context, of the fringe below them. Create...
A CFG-based attribute evaluator capable of handling non-L-attributed attribute flow needs to take a parse tree as input. Explain how to build a parse tree automatically during a top-down or bottom-up...
In our open-address hash tables, we have used linear probing or double hashing. Another probing method, which avoids some clustering, is called quadratic probing. The simplest version of quadratic...
(a) Show the NFA that results from applying the construction of Figure 2.7 to the regular expression letter ( letter | digit )*. (b) Apply the transformation illustrated to create an equivalent DFA....
Build a nested-case-statements finite automaton that converts all letters in its input to lower case, except within Pascal-style comments and strings. A Pascal comment is delimited by { and }, or by...
For certain values of x, (0.1 + x) * 10.0 and 1.0 + (x * 10.0) can differ by as much as 25%, even when 0.1 and x are of the same magnitude. Verify this claim. (Warning: If youre using an x86...
Languages that employ a reference model of variables also tend to employ automatic garbage collection. Is this more than a coincidence? Explain.
In Section 6.1.2 (Orthogonality), we noted that C uses = for assignment and == for equality testing. The language designers state: Since assignment is about twice as frequent as equality testing in...
If you are familiar with Javas Comparable interface (Programming Project 11), then rewrite one of the sorting methods so that it sorts an array of Comparable objects. You may choose selectionsort,...
Consider the following expression in C: a/b > 0 && b/a > 0. What will be the result of evaluating this expression when a is zero? What will be the result when b is zero? Would it make sense to try to...
As noted in Section 6.4.2, languages vary in how they handle the situation in which the controlling expression in a case statement does not appear among the labels on the arms. C and Fortran 90 say...
The performance of the radix sort from the previous project can be improved by using more supplementary lists (rather than just list0 and list1). For example, you can have an array of 16 lists, which...
In Section 7.2.2 we introduced the notion of a universal reference type (void * in C) that refers to an object of unknown type. Using such references, implement a poor mans generic queue in C, as...
Write regular expressions to capture the following. (a) Strings in C. These are delimited by double quotes ("), and may not contain newline characters. They may contain double-quote or backslash...
Extend the grammar of Figure 2.25 to include if statements and while loops, along the lines suggested by the following examples: abs := n if n < 0 then abs := 0 - abs fi sum := 0 read count while...
The dangling else problem of Pascal was not shared by its predecessor Algol 60. To avoid ambiguity regarding which then is matched by an else, Al-gol 60 prohibited if statements immediately inside a...
As part of the development team at MumbleTech.com, Janet has written a list manipulation library for C that contains, among other things, the code in Figure 3.16. (a) Accustomed to Java, new team...
Consider the following program in Scheme: What does this programprint? What would it print if Scheme used dynamic scoping and shallow binding? Dynamic scoping and deep binding? Explain your answers....
Discuss the relative merits of classes and type extensions. Which do you prefer? Why?
In the spirit of Example 10.8, write a double-ended queue (deque) abstraction (pronounced deck), derived from a doubly linked list base class.
Using the C# indexer mechanism, create a hash table class that can be indexed like an array. (In effect, create a simple version of the System.Collections.Hashtable container class.) Alternatively,...
As noted, C# has unusually sophisticated support for firstclass subroutines. Among other things, it allows delegates to be instantiated from anonymous nested methods, and gives local variables and...
Explain the meaning of the following C declarations: double *a[n]; double (*b)[n]; double (*c[n])(); double (*d())[n];
In Section 8.2.3 we discussed how to differentiate between the constant and variable portions of an array reference, in order to efficiently access the subparts of array and record objects. An...
Repeat the previous exercise using the underlying CFG of Figure 4.3. Figure 4.3 1. T TT.st := T.val E.val := TT.val 2. Ti + T T. + T TT2 TT2.st := TT1.st + T.val TT1.val := TT2.val %3D 3. TTi - ...
Repeat Exercise 4.7 using ad hoc attribute space management. Instead of accumulating the translation into a data structure, write it to a file on the fly. Data From Exercise 4.7: Suppose that we want...
Explain how to obtain the effect of Fortran 90s allocate statement for one-dimensional arrays using pointers in C. You will probably find that your solution does not generalize to multidimensional...
One problem with automatic space management for attributes in a top-down parser occurs in lists and sequences. Consider for example the following grammar: block begin stmt list end stmt list stmt...
As noted in Figure 6.1, Fortran and Pascal give unary and binary minus the same level of precedence. Is this likely to lead to nonintuitive evaluations of certain expressions? Why or why not? Figure...
In Example 6.9 we described a common error in Pascal programs caused by the fact that and and or have precedence comparable to that of the arithmetic operators. Show how a similar problem can arise...
Modify the solution to the postfix expression evaluation problem so that it checks for the validity of the expression that is entered by the user. Issue an appropriate error message when an erroneous...
In Lisp, most of the arithmetic operators are defined to take two or more arguments, rather than strictly two. Thus (* 2 3 4 5) evaluates to 120, and (- 16 9 4) evaluates to 3. Show that parentheses...
Consider a language implementation in which we wish to catch every use of an uninitialized variable. In Section 6.1.3 we noted that for types in which every possible bit pattern represents a valid...
Write an attribute grammar, based on the following context-free grammar, that accumulates jump code for Boolean expressions (with short-circuiting) into a synthesized attribute code of condition, and...
(Difficult) Use call-with-current-continuation (call/cc) to implement the following structured nonlocal control transfers in Scheme. (This requires knowledge of material in Chapter 11.) You will...
Write an algorithm for the add method that will add at the end of the list instead of at the beginning. What is the time complexity of this algorithm?
Modify the maze problem in this chapter so that it can start from a user defined starting position (other than 0, 0) and search for a user-defined ending point (other than row-1, column-1).
Suppose you are implementing an Ada compiler, and must support arithmetic on 32-bit fixed-point binary numbers with a programmer-specified number of fractional bits. Describe the code you would need...
When Sun Microsystems ported Berkeley Unix from the Digital VAX to the Motorola 680x0 in the early 1980s,many C programs stopped working, and had to be repaired. In effect, the 680x0 revealed certain...
Rewrite the code of Figure 7.3 in Ada, Java, or C#. Figure 7.3: template class queue { item items [max_items]; int next_free, next_full, num_items; public: queue () : next_free (0), next_full(0),...
Figure 7.3 passes integer max_items to the queue abstraction as a generic parameter. Write an alternative version of the code that makes max_items a parameter to the queue constructor instead. What...
In Section 9.3.1 we noted that Ada 83 does not permit subroutines to be passed as parameters, but that some of the same effect can be achieved with generics. Suppose we want to apply a function to...
In the language of an alien race, all words take the form of Blurbs. A Blurb is a Whoozit followed by one or more Whatzits. A Whoozit is the character x followed by zero or more ys. A Whatzit is a q...
Write an LL(1) parser generator in purely functional Scheme. If you consult Figure 2.24, remember that you will need to use tail recursion in place of iteration. Assume that the input CFG consists of...
Write a recursive method that returns the value of N! (N factorial) using the definition given in this chapter. Explain why you would not normally use recursion to solve this problem.
What is the time complexity of a radix sort?
Calculate the time complexity of the find method.
Pointers and recursive type definitions complicate the algorithm for determining structural equivalence of types. Consider, for example, the following definitions: type A = record x : pointer to B y...
Suppose A is a 1010 array of (4-byte) integers, indexed from [0][0] through [9][9]. Suppose further that the address of A is currently in register r1, the value of integer i is currently in register...
Write some Java statements that use the String methods indexOf and substring to find the first word in a string. We define word to be a string of characters that does not include whitespace. For...
Consider the following fragment of code: if (x > 5) System.out.println("A"); else if (x < 10) System.out.println("B"); else System.out.println("C"); What is displayed if x is a. 4; b. 5; c. 6; d. 9;...
Suppose we are compiling for a machine with 1-byte characters, 2-byte shorts, 4-byte integers, and 8-byte reals, and with alignment rules that require the address of every primitive data element to...
Write the equivalent of Figure 6.5 in C# or Ruby. Write a second version that performs an in-order enumeration, rather than preorder. Figure 6.5: class BinTree: def -_init_ (self): # constructor...
What is the value of each of the following boolean expressions if x is 5,y is 10, and z is 15? a. (x < 5 && y > x) b. (x < 5 || y > x) c. (x > 3 || y < 10 && z == 15) d. (! (x > 3) && x!= z || x + y...
There is a variation of the bubble sort algorithm called a gap sort that, rather than comparing neighboring elements each time through the list, compares elements that are i positions apart, where i...
What changes to the ChangeMaker program in Listing 2.3 are necessary if it also accepts coins for one dollar and half a dollar? Listing 2.3 import java.util.Scanner; public class ChangeMaker { public...
Write a program that reads a bank account balance and an interest rate and displays the value of the account in ten years. The output should show the value of the account for three different methods...
Every profession uses computers today. Use aWeb browser and a search engine like Google to find sites that relate your field of study with computer science or computing or computation. For example,...