Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following code implementing an immutable linked list (from assignment #1): public interface Immutablelist { public int numEvens(); } public class Cons implements Immutablelist

image text in transcribed

Consider the following code implementing an immutable linked list (from assignment #1): public interface Immutablelist { public int numEvens(); } public class Cons implements Immutablelist { public final int head; public final Immutablelist tail; public Cons(final int head, final Immutablelist tail) { this.head = head; this.tail = tail; } public int valueCount(int value) {...} } public class Nil implements ImmutableList { public Nil() {} public int valueCount(int value) { ... } } The valueCount method should return the number of times value appears in the list. Some examples are below: new Nil().valueCount(3); // returns 0 new Cons(1, new Nil()).valueCount(3); // returns 0 new Cons(2, new Nil()).valueCount(2); // returns 1 new Cons(1, new Cons(1, new Cons(3, new Nil()))).valueCount(1) // returns 2 new Cons(1, new Cons(2, new Cons(3, new Nil()))).valueCount(2) // returns 1 new Cons(1, new Cons(2, new Cons(3, new Nil()))).valueCount(4) // returns 0 Write the implementation of valueCount below. Unlike assignment #1, you may use whatever you want, including if and loops. As a hint, your recursive case should be in Cons, and your base case should be in Nil. Additionally, while if will probably be useful, loops shouldn't be necessary. Consider the following code implementing an immutable linked list (from assignment #1): public interface Immutablelist { public int numEvens(); } public class Cons implements Immutablelist { public final int head; public final Immutablelist tail; public Cons(final int head, final Immutablelist tail) { this.head = head; this.tail = tail; } public int valueCount(int value) {...} } public class Nil implements ImmutableList { public Nil() {} public int valueCount(int value) { ... } } The valueCount method should return the number of times value appears in the list. Some examples are below: new Nil().valueCount(3); // returns 0 new Cons(1, new Nil()).valueCount(3); // returns 0 new Cons(2, new Nil()).valueCount(2); // returns 1 new Cons(1, new Cons(1, new Cons(3, new Nil()))).valueCount(1) // returns 2 new Cons(1, new Cons(2, new Cons(3, new Nil()))).valueCount(2) // returns 1 new Cons(1, new Cons(2, new Cons(3, new Nil()))).valueCount(4) // returns 0 Write the implementation of valueCount below. Unlike assignment #1, you may use whatever you want, including if and loops. As a hint, your recursive case should be in Cons, and your base case should be in Nil. Additionally, while if will probably be useful, loops shouldn't be necessary

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

0764549634, 9780764549632

More Books

Students also viewed these Databases questions

Question

How to find if any no. is divisble by 4 or not ?

Answered: 1 week ago

Question

Explain the Pascals Law ?

Answered: 1 week ago

Question

=+ c. a company president deciding whether to open a new factory

Answered: 1 week ago