Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-01-2008, 01:21 PM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default help to understand this problem clear
a problem i get into a book entitled Absolute JAVA.
Quote:
Write a static method called deleteRepeats that hasa partially filled array of characters as a formal parameter and that deletes all repeated letters from the array. Because a partially filled array requires two arguments, the method should actually have two parameters: an array parameter and a formal parameter of type int that gives the number of array positions used. When a letter is deleted, the remaining letters are moved up one position to fill in the gap. This creates empty positions at the end of the array so that the less of the array is used. Because the formal parameter is partially filled array, a second formal parameter cannot be changed by a JAVA method, so have the method return the new value for this parameter. For example, consider the following code:
Code:
char a[10];
a[0] = 'a';
a[1] = 'b';
a[2] = 'a';
a[3] = 'c';
int size=4;
size = deleteRepeats(a, size);
After this code is executed, the value of a[0] is 'a', the value os [1] is 'b'. the value of a[2] is 'c', and the value of size is 3.(The value of a[3] is no longer of any concern, because the partially filled array no longer uses this indexed variable.)
here is the problem..
i cant understand it more clearly..
is there anyone who could summarize this in order for me to understand it clearly?
i dont said that you write a program for me..
i just said is to help me understand this problem clearly..
help
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-01-2008, 01:28 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,446
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Something like this,

You have an array of char types. You can added up to 10 elements there. Another variable is also there to handle the number of filed index in the array, type of int. You cannot have duplicate letters.

__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-01-2008, 01:35 PM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default
is this work:
Code:
char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
int x;
x as the variable that handle the number of filled index in the array..
is that what you trying to say?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-01-2008, 01:41 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,446
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Yes, simply it's hold the number of indexes without duplicating.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-01-2008, 01:50 PM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default
i came up w/ this code:
Code:
public class Exercise
{
	public static void main(String[]args)
	{
	char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
	int x;
	for(x=i;x<letter.length;x++)
	System.out.println(x);
	}
}
i want to print those values in my array using the int x.
but it doesnt work..
what was the problem?

Last edited by beginner21; 12-01-2008 at 01:50 PM. Reason: tag
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-01-2008, 02:07 PM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 484
Rep Power: 2
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
Default
What is that value i?Where did you get the variable i?

Last edited by serjant; 12-01-2008 at 02:12 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-01-2008, 02:11 PM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default
that is my problem..
how can i print the values in the array using the int x?
as you can see in my previous post, the code doesn't work..
i want to print the values inside the array but the i has no value..
what should i do?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-01-2008, 02:21 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,446
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
What the error message you get? Where you define the variable i?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-01-2008, 02:27 PM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default
here is the error message i got:
Code:
Num4.java:7: cannot resolve symbol
symbol : variable i
location: class Num4
       for(x=i;x<letter.length;x++)
             ^
i think the error said that where i initialized variable i?
is there another way how to print the values in the array?

Last edited by beginner21; 12-01-2008 at 02:27 PM. Reason: spacing
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-01-2008, 02:40 PM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 484
Rep Power: 2
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
Default
it should be
Code:
for(x=0;x<letter.length;x++)
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-01-2008, 02:49 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,160
Rep Power: 3
CJSLMAN is on a distinguished road
Default hhhmm...
  • First of all, it is good practice to put curly brackets "{}" after for & if statements, even if they're one liners.
  • Next, get rid of the i variable.
  • Initialize the x variable in you for statement as you would in any other for statement.
  • If you want to print the array elements, you can use the x variable:
Code:
System.out.println("letter[" + x + "] = " + letter[x]);
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 12-01-2008, 02:51 PM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default
Quote:
it should be
Code:
for(x=0;x<letter.length;x++)
it wont work,
the output in that code display 0-9..
0123456789
because you declare the x as 0 not the index of the array...

Last edited by beginner21; 12-01-2008 at 02:52 PM. Reason: tag
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 12-01-2008, 02:57 PM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default
tnx CSJLMAN..
your code works
my modified code is this:
Code:
public class Exercise
{
	public static void main(String[]args)
	{
	char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
	int x;
	for(x=0;x<letter.length;x++)
	System.out.println("letter[" + x + "] = " + letter[x]);
	}
}
what was the next based on the problem?
i cant understand what should be next after filling the index of array.
is it im going to do now the method deleteRepeats();?
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 12-01-2008, 03:20 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,160
Rep Power: 3
CJSLMAN is on a distinguished road
Default Let's see if you understood...
Before you can go to the next step (methods), you have to understand what you just did.

Questions:
  • Do you know why my statement in my last post worked for you?
  • Serjant's code was correct, he was correcting an error you had in your for statement.
  • Do you completely understand everything that in the the println statement?

Statement: you didn't follow my suggestion about using the curly brackets "{}" for the "for" statement.

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 12-01-2008, 03:23 PM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default
sorry CJSLMAN
ok here is my modify code:
Code:
public class Exercise
{
	public static void main(String[]args)
	{
	char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
	int x;
	for(x=0;x<letter.length;x++)
	{
	System.out.println("letter[" + x + "] = " + letter[x]);
	}
	}
}
sorry again,
is that what you're trying to say?
it works also, is the method is the next?
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 12-01-2008, 03:49 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,160
Rep Power: 3
CJSLMAN is on a distinguished road
Default Did you understand?
I just want to be sure you understand how yoiur code is working.
  • Did you understand what changed in you code to make it working that way you wanted it to? Do NOT be satisfied that it is working without understanding why.
  • Yes, creating a method is next. Here's some help:
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 12-01-2008, 04:04 PM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default
of course i understand it..
x is now the index of my array,
print new line after every execution of the loop because the loop will continuously executing until the condition satisfies,
is it right?
for the method,i cant understand what should i write inside deleteRepeats();?
and where did this method return?
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 12-01-2008, 05:44 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,160
Rep Power: 3
CJSLMAN is on a distinguished road
Default
Yes... that is the function of the println... I'm more interested if you understand what's in the println.
Anyway....
Your original question was to understand the book assignment:
  • Original array has the following elements {'a','b','a','c'}.
  • delete the repeated letters/elements in the array
  • move the remaining elements to fill the empty space left by the deleted element
  • return the new size of the array (the number of remaning elements
  • Example:
  • Original array = {'a','b','a','c'}
  • Array after deleting repeated elements = {'a','b', ,'c'} or { ,'b','a','c'}
  • Array after moving elements = {'a','b','c', ,}
  • return size = 3
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
clear cache Jadellll New To Java 0 03-20-2008 10:27 AM
Please clear my doubt............... vinaytvijayan Advanced Java 0 12-26-2007 07:23 PM
how to clear the data of an object katie AWT / Swing 1 08-06-2007 11:50 PM
Help, someone clear up Interfaces for me mathias New To Java 1 08-06-2007 03:26 AM
I want to do is clear the console paul Advanced Java 7 08-03-2007 07:54 PM


All times are GMT +2. The time now is 12:18 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org