Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem Description: Suppose that you want an operation for the ADT list that returns the position of a given object in the list. The header

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Problem Description: Suppose that you want an operation for the ADT list that returns the position of a given object in the list. The header of the method could be as follows: public int getPosition( T anObject) where T is the generic type of the objects in the list. Locates a given object in this list. @param anObject A given object. @return The position of anObject in the list. * public int getPosition ( T anObject) 3 Write an implementation of this method for the class AList. Submition files: Driver.java, ListInterface,java, AList.java. iCollege Submission and Grading Upload and submit the java files to iCollege under Assessments Assignments Programming Project 4. Always double check that your submission was successful on iCollege! The lab will be graded according to the following guidelines. - A score between 0 and 100 will be assigned. - If the source file(s) are not submitted before the specified deadline or if they do not compile, then a grade of 0 will be assigned. - Deductions for absences will be deducted in a way consistent with the course syllabus. - The programs will be cvaluated to determine if they output the correct Strings. import java.util. Arrays; A class that implements a 1 ist of objeots by using an array. Rintries in a 1 si have positions that begin with 1. Duplicate ontries are allowed. eauthor Frank. M. Carrano (6author Thingthy M. Honry. etyersion 5.0 * public class Auist f. private T[] list; // Array of 1ist entries; ignore ligt[0] private int numberofBntries; private boolean integrityoK; private static final int DEPAULT CAPACITY =25; private static final int MAX CAPACITY =10000; public AList() \{ this (DEFAULT CAPACITY); J 1/ end default constructor public AList (int initialcapacity) \{ integrityok = false; 1/ Is initialcapacity too sinal1? if (initialCapacity ]; list = templist; numberofEntries =0; integrityoK = true; 3. / ind constructor public void add (T newEntry) 1 add (numberoffintries + 1 , newintery ) Il Al ternate code chockintogrity () : 1.iet mumberofintries + 11 - newantry numborofentriesti ansorecapaoity ( ) ; ) // end atid public void addi int givenposition, T newentry) checkintegrity () ; if ((givenpoaition >o i) sk (givenponition [ ivenpogition] = newzntry: fumberofkintriestip enturecapacity () i (/ Ensuce onough koon tor next add 4 ctse throw hep IndescoutofBoundaException ("Given pondtion of add's nem entry ia out of bounda. ") ? y. ffiend add 1/. Maked room for a now entry at newPonition. (1. Preconditiont 1 ce mewllonjtion ce numberotintiea +1t (1. numberofzntrieg is lide's length before addition: (1. chookintegrity has benn called. private vofd makeRoon (int givenposition) 1 (1) Asaertiona (newposition >=1 ) kk (nesposition ci namherotintrios + 1) int newindex = givenpositiony int lastindex = numberofgntries: (1) Nove each entry to next higher index, stacting at and of (. list ond cont finuing unt 1 the entry at nemindex is inoved for (1nt index = Lastindex; index >a nowtndex; index -- ) 11 et [1ndex+11=1 int [1 ndex ]; ) // ond makeRocen If shifts entries that are beyond the ontry to bo renoved to the (1) next lower pogition. (1) Preconditton: 1 =2; index /1. Doop is part of of 1 ist [ index ]= nuli; numberofsntrien =07 1/h ond clear public T Teplace (int givenposition, T newtantry) \{ checkintegrity(); if ((givenposition >= 1) & (givenposition = nunberofentries)) ( /I Assertion: the 1 ist is not empty T originalBntry = 1 ist [g ivenposition ] 1 ist [givenposition] = neventry; return originalentry; 3 else throw new IndexoutofBoundsException( r11egal position given to replace operation. "); I /1 end replace public T getEntry (int givenPosition) f checkintegrity () ; if ((givenposition >= 1) 46 (givenposition /I and gotEntry publie T[1 torray () 1 checkintegrity () ? 1/ The cast is safe because the new array contains nu11 entriea esuppresabarnings ( "unchecked") T(] reault - (T[ 1) new object [numberofentries): // Unchecked cast. for (int index =0; index capacity) 1. int newcapacity =2 e capacity; checkCapacity (newcapacity) i // In capacity too bigh? 1 ist = Mrrays.oopyof ( liat, newCapacity +1) i ) H end if ) If end ensurecapaoity W( Throws on exception if this object is corrupt. private void eheckintegrity() f if (1integrityoK) thron new SecurityException ("Mist object is corrupt."): 1 /1 end checktntegrity If Throws an exception if the elient requests a capacity that in too large. private void checkCapacity (int capacity) 1 if (capacity > WaX CAPACITY) throw new IllegstateException ("Attempt to create a list " + "whose capacity exceeds " + "allowed maxinum. "); Y If end cheokcapacity /1 end Auiat Problem Description: Suppose that you want an operation for the ADT list that returns the position of a given object in the list. The header of the method could be as follows: public int getPosition( T anObject) where T is the generic type of the objects in the list. Locates a given object in this list. @param anObject A given object. @return The position of anObject in the list. * public int getPosition ( T anObject) 3 Write an implementation of this method for the class AList. Submition files: Driver.java, ListInterface,java, AList.java. iCollege Submission and Grading Upload and submit the java files to iCollege under Assessments Assignments Programming Project 4. Always double check that your submission was successful on iCollege! The lab will be graded according to the following guidelines. - A score between 0 and 100 will be assigned. - If the source file(s) are not submitted before the specified deadline or if they do not compile, then a grade of 0 will be assigned. - Deductions for absences will be deducted in a way consistent with the course syllabus. - The programs will be cvaluated to determine if they output the correct Strings. import java.util. Arrays; A class that implements a 1 ist of objeots by using an array. Rintries in a 1 si have positions that begin with 1. Duplicate ontries are allowed. eauthor Frank. M. Carrano (6author Thingthy M. Honry. etyersion 5.0 * public class Auist f. private T[] list; // Array of 1ist entries; ignore ligt[0] private int numberofBntries; private boolean integrityoK; private static final int DEPAULT CAPACITY =25; private static final int MAX CAPACITY =10000; public AList() \{ this (DEFAULT CAPACITY); J 1/ end default constructor public AList (int initialcapacity) \{ integrityok = false; 1/ Is initialcapacity too sinal1? if (initialCapacity ]; list = templist; numberofEntries =0; integrityoK = true; 3. / ind constructor public void add (T newEntry) 1 add (numberoffintries + 1 , newintery ) Il Al ternate code chockintogrity () : 1.iet mumberofintries + 11 - newantry numborofentriesti ansorecapaoity ( ) ; ) // end atid public void addi int givenposition, T newentry) checkintegrity () ; if ((givenpoaition >o i) sk (givenponition [ ivenpogition] = newzntry: fumberofkintriestip enturecapacity () i (/ Ensuce onough koon tor next add 4 ctse throw hep IndescoutofBoundaException ("Given pondtion of add's nem entry ia out of bounda. ") ? y. ffiend add 1/. Maked room for a now entry at newPonition. (1. Preconditiont 1 ce mewllonjtion ce numberotintiea +1t (1. numberofzntrieg is lide's length before addition: (1. chookintegrity has benn called. private vofd makeRoon (int givenposition) 1 (1) Asaertiona (newposition >=1 ) kk (nesposition ci namherotintrios + 1) int newindex = givenpositiony int lastindex = numberofgntries: (1) Nove each entry to next higher index, stacting at and of (. list ond cont finuing unt 1 the entry at nemindex is inoved for (1nt index = Lastindex; index >a nowtndex; index -- ) 11 et [1ndex+11=1 int [1 ndex ]; ) // ond makeRocen If shifts entries that are beyond the ontry to bo renoved to the (1) next lower pogition. (1) Preconditton: 1 =2; index /1. Doop is part of of 1 ist [ index ]= nuli; numberofsntrien =07 1/h ond clear public T Teplace (int givenposition, T newtantry) \{ checkintegrity(); if ((givenposition >= 1) & (givenposition = nunberofentries)) ( /I Assertion: the 1 ist is not empty T originalBntry = 1 ist [g ivenposition ] 1 ist [givenposition] = neventry; return originalentry; 3 else throw new IndexoutofBoundsException( r11egal position given to replace operation. "); I /1 end replace public T getEntry (int givenPosition) f checkintegrity () ; if ((givenposition >= 1) 46 (givenposition /I and gotEntry publie T[1 torray () 1 checkintegrity () ? 1/ The cast is safe because the new array contains nu11 entriea esuppresabarnings ( "unchecked") T(] reault - (T[ 1) new object [numberofentries): // Unchecked cast. for (int index =0; index capacity) 1. int newcapacity =2 e capacity; checkCapacity (newcapacity) i // In capacity too bigh? 1 ist = Mrrays.oopyof ( liat, newCapacity +1) i ) H end if ) If end ensurecapaoity W( Throws on exception if this object is corrupt. private void eheckintegrity() f if (1integrityoK) thron new SecurityException ("Mist object is corrupt."): 1 /1 end checktntegrity If Throws an exception if the elient requests a capacity that in too large. private void checkCapacity (int capacity) 1 if (capacity > WaX CAPACITY) throw new IllegstateException ("Attempt to create a list " + "whose capacity exceeds " + "allowed maxinum. "); Y If end cheokcapacity /1 end Auiat

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions