P16.9 The LISP language, created in 1960, implements linked lists in a very elegant way. You...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
P16.9 The LISP language, created in 1960, implements linked lists in a very elegant way. You will explore a Java analog in this set of exercises. Conceptually, the tail of a list- that is, the list with its head node removed-is also a list. The tail of that list is again a list, and so on, until reach the empty list. Here is a Java interface for such a list: you public interface LispList { boolean empty(); Object head(); LispList tail(); } There are two kinds of lists, empty lists and nonempty lists: public class EmptyList implements LispList {...} public class NonEmptyList implements LispList { } These classes are quite trivial. The EmptyList class has no instance variables. Its head and tail methods simply throw an Unsupported OperationException, and its empty method returns true. The Non EmptyList class has instance variables for the head and tail. Here is one way of making a LISP list with three elements: LispList list = new NonEmptyList ("A", new NonEmptyList("B", new NonEmptyList("C", new EmptyList()))); This is a bit tedious, and it is a good idea to supply a convenience method cons that calls the constructor, as well as a static variable NIL that is an instance of an empty list. Then our list construction becomes LispList list = LispList.NIL.cons ("C"). cons("B"). cons ("A"); Note that you need to build up the list starting from the (empty) tail. To see the elegance of this approach, consider the implementation of a toString method that produces a string containing all list elements. The method must be implemented by both classes: public class EmptyList implements LispList { } public String toString () { return ""; } public class NonEmptyList implements LispList { "1 11 public String toString () { return head() + + tail().toString(); } } Note that no if statement is required. A list is either empty or nonempty, and the correct toString method is invoked due to polymorphism. In this exercise, complete the LispList interface and the EmptyList and NonEmptyList classes. Write a test program that constructs a list and prints it. P16.9 The LISP language, created in 1960, implements linked lists in a very elegant way. You will explore a Java analog in this set of exercises. Conceptually, the tail of a list- that is, the list with its head node removed-is also a list. The tail of that list is again a list, and so on, until reach the empty list. Here is a Java interface for such a list: you public interface LispList { boolean empty(); Object head(); LispList tail(); } There are two kinds of lists, empty lists and nonempty lists: public class EmptyList implements LispList {...} public class NonEmptyList implements LispList { } These classes are quite trivial. The EmptyList class has no instance variables. Its head and tail methods simply throw an Unsupported OperationException, and its empty method returns true. The Non EmptyList class has instance variables for the head and tail. Here is one way of making a LISP list with three elements: LispList list = new NonEmptyList ("A", new NonEmptyList("B", new NonEmptyList("C", new EmptyList()))); This is a bit tedious, and it is a good idea to supply a convenience method cons that calls the constructor, as well as a static variable NIL that is an instance of an empty list. Then our list construction becomes LispList list = LispList.NIL.cons ("C"). cons("B"). cons ("A"); Note that you need to build up the list starting from the (empty) tail. To see the elegance of this approach, consider the implementation of a toString method that produces a string containing all list elements. The method must be implemented by both classes: public class EmptyList implements LispList { } public String toString () { return ""; } public class NonEmptyList implements LispList { "1 11 public String toString () { return head() + + tail().toString(); } } Note that no if statement is required. A list is either empty or nonempty, and the correct toString method is invoked due to polymorphism. In this exercise, complete the LispList interface and the EmptyList and NonEmptyList classes. Write a test program that constructs a list and prints it.
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions
-
An airline company wants to create an online air ticket purchase for its customers. The company wants to attract customers to participate or purchase from its website by offering a discount to the...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
Develop a two-period weighted moving average forecast for periods 12 through 15. Use weights of 0.7 and 0.3, with the most recent observation weighted higher. PERIOD DEMAND 10............ 248...
-
INVOLVE was incorporated as a not-for-profit voluntary health and welfare organization on January 1, 2017. During the fiscal year ended December 31, 2017, the following transactions occurred. 1. A...
-
If an employee earned $ 7 2 5 this week and has contributed $ 2 7 2 1 . 4 0 to CPP so far, what will their next contribution cost likely be ?
-
What the paralegals role is during trial?
-
Deluca Solutions Inc. is an Ontario- based manufacturer. The company is listed on the TSX, but the family of founder David Deluca retains control through multiple- voting shares. Deluca undertook...
-
Consider a pan placed on an electric range to cook spaghetti. The bottom section of the pan is 0 . 5 cm thick and has a 2 0 cm diameter. The electric heating unit consumes 1 kW of power during...
-
An Internal Service Fund established by a county is intended to operate on a breakeven basis. How might a government dispose of increases or decreases in net position remaining at year end?
-
You should by now understand that things are changing within the Company, and this will affect all of us. We need to understand what these changes mean and how we can manage it. a. Explain why we...
-
Summarize the transactions for Space Travel, Inc. and identify the effects on the accounting. On December 1, 2022, Sean Spaceman formed a corporation, Space Travel, Inc. to launch a commercial...
-
Suppose Nabisco Corporation just issued a dividend of $2.65 per share yesterday. Subsequent dividends will grow at a constant rate of 7.49% indefinitely. If the required rate of return for this stock...
-
Develop a Python program that simulates a weather forecasting service . The program should allow users to perform the following operations: Set Location: Users can set their current location by...
-
Larry was in a skiing accident and suffered a serious back injury. Surgery was not necessary but he would have to do extensive therapy to recover full movement. He went to GTA Medical Clinic for his...
-
Mauro Products distributes a single product, a scarf; its selling price is $16 and its variable cost is $11 per unit. The company's monthly fixed expense is $4,700. Required: 1. Solve for the...
-
Question 1 Aron, Melinda, and Christopher were arrested by the police in an online scam. During prosecution, they made the following statements. If only one of them is involved in the scam and only...
-
How can NAFTA be beneficial to suppliers of Walmart?
-
What and where is the Backstage View?
-
Snick's Board Shop has several questions for you that Excel can help answer. The company is planning for the future and would like you to prepare a present value analysis. Using the file ch7-10,...
-
Describe the difference between the Query Wizard and Design view methods of creating a query.
-
A hospital reported the following statistics for the past year: births, 1,702; deliveries, 1,708; C-sections, 360; and obstetrical discharges, 1,827. The C-section rate for that year IS a. 0.21% b....
-
The consultation rate (including newborns) is _ _ . a. 0.03% b. 0.30% c. 3.09% d. 30.99%
-
The hospital-acquired infection rate (including newborns) is __ . a. 0.03% b. 0.30% c. 3.00% d. 30.00%
Study smarter with the SolutionInn App