Total members 10261 | Gratitudes |It is currently Wed May 23, 2012 9:05 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 3 posts ] 
Author Question
 Question subject: Java--What is wrong with my code?
PostPosted: Wed Nov 03, 2010 8:47 pm 
Offline
Newbie
User avatar

Joined: Wed Nov 03, 2010 8:42 pm
Posts: 1
Has thanked: 0 time
Have thanks: 0 time

2 parts of my assignment are failing junit tests and i dont know why, hoping someone on here can fix them. First part basicallyreturns the element whos position in the list matches the parameter. Second part returns true if the parameter is in the list, false if parameter is not. I know it must be a small logical error in both.

First part code
Code:
public int getAt(int index) {
if (index<0 || index>values[(values.length-1)]){
throw new IndexOutOfBoundsException();
}
int retValue = 0;
for(int i =0; i<values.length; i++){
if(values[i]==index){
retValue = values[index];
}
}
return retValue;
}


Second Part Code
Code:
public boolean contains(int number) {
boolean retValue = false;
for(int i =0; i<values.length-1; i++){
if(values[i]==number){
retValue = true;
}else{
retValue = false;
}
}
return retValue;
}


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Java--What is wrong with my code?
PostPosted: Wed Nov 03, 2010 11:53 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time
hi brother ,
to get element using index i think the function should be like this :
Code:
public int getAt(int index) {
if (
index<|| index>(values.length-1)){
throw new 
IndexOutOfBoundsException();
}
int retValue 0;
for(
int i =0i<values.lengthi++){
if(
i==index){
retValue values[index];
}
}
return 
retValue;
}
 

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Java--What is wrong with my code?
PostPosted: Thu Nov 04, 2010 7:59 am 
Offline
Newbie
User avatar

Joined: Wed Nov 03, 2010 3:48 pm
Posts: 4
Has thanked: 0 time
Have thanks: 1 time
Hello jeriffstive,

it's quite clear that the second code isn't working like expected.

You are iterating over the whole array using a for-loop. Inside the loop you are testing, if the value at position i is equal to the value number. But if you hit the position you don't stop iterating and thats the problem.

Example: your array values contains {1, 3, 5} and you are looking for 3. Your for-loop iterates the three values 1, 3 and 5. The variable retVal will have the following values:
for i=0, values[0]=1: retVal = false
for i=1, values[1]=3: retVal = true
for i=2, values[2]=5: retVal = false
and your function will return false.

You have to stop your searching, when you find the element. The correct code would be:

Code:
   public boolean contains(int number) {
      for(int i = 0; i < values.length - 1; i++){
         if(values[i] == number){
            return true;
         }
      }
      return false;
   }   


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 3 posts ] 
Quick reply


  

 Similar topics
 video chat application in java
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 Java Programing to communicating port parallel
 Code to open multiple links.
 I need help with checkers code
 Java Chat
 Steganography in java

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: Google Adsense [Bot] and 2 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team