Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) { head = h; tail = t; } public int

image text in transcribed

public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) { head = h; tail = t; } public int getHead() { return head; } public ConsCell getTail() { return tail; } public void setTail(ConsCell t) { tail = t; } }

public class IntList { private ConsCell start; public IntList (ConsCell s) { start = s; } public IntList cons(int h) { return new IntList(new ConsCell(h, start)); } public int length() { int len = 0; ConsCell cell = start; while (cell != null) { len++; cell = cell.getTail(); } return len; } public void print() { System.out.print("["); ConsCell a = start; while(a != null) { System.out.print(a.getHead()); a = a.getTail(); if(a != null) System.out.print(","); } System.out.println("]"); } //exercise 5 public IntList reverse() { IntList Reverselist = new IntList(null); ConsCell begin = start; while (begin != null) { Reverselist = Reverselist.cons(begin.getHead()); begin = begin.getTail(); } return Reverselist; } }

public class Driver { public static void main (String[] args) { IntList a = new IntList (null); IntList b = a.cons(2); IntList c = b.cons(1); IntList d = c.reverse(); int x = a.length() + b.length() + c.length(); a.print(); b.print(); c.print(); d.print(); System.out.println(x); } }

Exercise 3 Add an equals instance method to the IntList class, so that x.equals(y) returns true if the IntList x and the IntList y have exactly the same integers in the same order and returns false otherwise. It should be true that x.equals(y) is always equivalent to y.equals(x). It should also be true that if x==y then x.equals (y), although the reverse should not necessarily be true

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

AutoCAD Database Connectivity

Authors: Scott McFarlane

1st Edition

0766816400, 978-0766816404

More Books

Students also viewed these Databases questions