• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help to review my code

 
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:
I refer to this exercise : https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html#zz-8.

the code given is :



and the requirements ;

Try:

   Modify the push() method to throw an IllegalStateException if the stack is full.
   Modify the push() to return true if the operation is successful, or false otherwise.
   Modify the push() to increase the capacity by reallocating another array, if the stack is full.



I just want to find out if the way i did my code that try to modify the push method is correct or not cos I will not write to the author, after I did not get any reply at all.



Thanks alot alot.

 
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:
The three instructions you quoted are mutually exclusive, so I think you want three solutions.

tangara goh wrote:. . .. . .

How many errors did you find in that code? I have found three, at least one of them potentially serious. An error similar to one of them in a Stack<E> might become a serious error. I also think the // comment in line 5 constitutes incorrect style.
In line 32, I think you are throwing the wrong exception. There is nothing wrong with the argument, but with the implementation. Did you read the instruction properly?
The method starting line 41 is incorrect. I think it is beyond redemption and you should start again. I also think that it is wrong to create another method name; you were told to modify push(int).
The three instructions you quoted are mutually exclusive, so I think you want three solutions.
 
tangara goh
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:
Actually, I changed the push name for convenience sake.

Sorry I thought you could tell.

So, basically, I created 3 solutions in the same class.

Please ignore the comment, as I was first struggling how to push the element to the array without an index first.

So, the thing is I check with ai.  I was told that the exceptions should be thrown at the peek() and pop() and I thought I better hear the experts' opinion.
 
Saloon Keeper
Posts: 15620
366
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
All three of your solutions have issues.

Your first modified push() method throws the wrong exception type.

Your second one throws an exception when instead, it's supposed to return false.

Your third solution throws an exception when instead, it's supposed to increase the capacity of the existing stack.
 
Campbell Ritchie
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:

tangara goh wrote:Actually, I changed the push name for convenience sake. . . .

You now have a method which your users will not know when to use, so you are adding confusion. That method is incorrect regardless; I shall let you work out why.

So, the thing is I check with ai.

Don't rely on AI.

I was told that the exceptions should be thrown at the peek() and pop() . . .

Yes, pop() and peek() should be able to throw exceptions, but you don't seem to have implemented that, nor does the original, nor is it mentioned in your instructions.
I still think you are supposed to produce three different classes, each using a different solution to a possible error. That is the error I noticed straight away.
 
Marshal
Posts: 8880
638
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:


1. rename variable to topOfTheStack or similar
2. remove now redundant comment explaining what the variable is
 
tangara goh
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:

Stephan van Hulst wrote:All three of your solutions have issues.

Your first modified push() method throws the wrong exception type.

Your second one throws an exception when instead, it's supposed to return false.

Your third solution throws an exception when instead, it's supposed to increase the capacity of the existing stack.



Yes.  Silly me on not reading the question carefully.  I guess I should also did a test ...

I am puzzled about the capacity that part.

If there is no capacity  stated in the constructor, how will the stack know that it's full ?

Or this class is a 'utility' class that is to be made use of with other List like ArrayList, LinkedList, or Tree(am not sure about this one as I haven't go deep into it yet)
 
Stephan van Hulst
Saloon Keeper
Posts: 15620
366
  • Number of slices to send:
    Optional 'thank-you' note:

tangara goh wrote:I am puzzled about the capacity that part.

If there is no capacity  stated in the constructor, how will the stack know that it's full ?


But there IS a capacity set in the constructor, and the stack DOES know that it's full, by calling the isFull() method. It's really the same as the first two solutions.

The difference is that when the stack is full, you don't throw an exception and you don't return false, but instead you increase the capacity by creating a new larger array, and then copying the existing elements to the larger array.

How much larger? That's up to you, but it's convention to just double the size of the previous array.

Or this class is a 'utility' class that is to be made use of with other List like ArrayList, LinkedList, or Tree(am not sure about this one as I haven't go deep into it yet)


No. It's meant to use an array, not a List. Using a List instead of an array is what I would normally recommend, but it defeats the purpose of the assignment, which is to teach you how to do it with an array.

And using a List will not make your class a 'Utility class'. That means something completely different.
 
Campbell Ritchie
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:

. . . increase the capacity by creating a new larger array, and then copying the existing elements to the larger array. . . .

There is a way to do both in one fell stroke.

And using a List will not make your class a 'Utility class'. That means something completely different.

A utility class usually means a class designed to help other objects; its members are all static and it should have a private constructor to prevent instantiation.
I think Stephan and I agree that you should produce three solutions, one for each of the three instructions.
 
Campbell Ritchie
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:

Liutauras Vilda wrote:. . . now redundant comment explaining what the variable is

That was my least serious mistake in my first post; a comment explaining a variable name always constitutes proof that the variable name is wrong.
 
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |