12 Replies - 635 Views - Last Post: 07 January 2010 - 04:03 AM Rate Topic: -----

#1 1984oran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-January 10

a little problem with a class

Post icon  Posted 05 January 2010 - 02:40 PM

hi all,
im new here, its looks like a great forum i gald i found it.
here is my problem
i wrote a class that represnts a new langug
i got stuck here

i need to wirte a method that to this thig

CheckContinuity (int num, char c) public boolean method that accepts two parameters - a signal c integer num and returns a boolean value. The system checks whether there is a sequence of received signal c, as many times as num. If indeed there is a sequence number of letters that will return true, otherwise return no.
For example: reading method: CheckContinuity (2, A) operated on the object type MyLang his word feature is BAcAABAcAb checking that a sequence of 2 letters A and returns trueCheckContinuity (int num, char c) public boolean method that accepts two parameters - a signal c integer num and returns a boolean value. The system checks whether there is a sequence of received signal c, as many times as num. If indeed there is a sequence number of letters that will return true, otherwise return no.
For example: reading method: CheckContinuity (2, A) operated on the object type MyLang his word feature is BAcAABAcAb checking that a sequence of 2 letters A and returns true

please can some one help me

Is This A Good Question/Topic? 0
  • +

Replies To: a little problem with a class

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: a little problem with a class

Posted 05 January 2010 - 02:42 PM

Hi, welcome to DIC! We'd be glad to help you, but you need to follow some of our guidelines as well. We do not just give out code here, you must provide some sort of effort first.

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 1984oran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-January 10

Re: a little problem with a class

Posted 05 January 2010 - 02:46 PM

of course i dont want the answer i want an explanation
public class MyLang
{
	// instance variables - replace the example below with your own
	private String _word;
	
	 private static final int MAX_LENGTH=15;

	/**
	 * Constructor for objects of class MyLang
	 */
	public  MyLang (String w)
	{
	  if (isValid(w)==true)
	   _word=w;
	   else
	   _word = "";
	}
	
	public boolean isValid(String s)
	{
		 int len =s.length();
		if (s.length() > MAX_LENGTH)
			return false;
		if (s.charAt(0)!='A'&&(s.charAt(0)!='B'))
			return false;
			if  ((s.charAt(s.length()-1))=='c')   
		for ( int i=0; i<len; i++)
		{
			if (s.charAt(i)!='A' &&s.charAt(i)!='B' &&s.charAt(i)!='c')
				return false;
		}
		for ( int i=0; i<len; i++)
		{
			if(s.charAt(i)=='c')
				if((s.charAt(i-1)!='A') || (s.charAt(i+1)!='A'))
					return false;
		 }
			return true;
	}
	
	public char lastLetter() 
	{
		if (_word=="")
			return '\0';
		else
		return   (_word.charAt(_word.length()-1));
	}
	public char letterInPlace(int i)
	{
		int abs=Math.abs(i);
		if (_word.length()>abs)
			return _word.charAt(abs);
		 else
			return '\0';
	 }
			
	 public String toString()
	  {
		  return _word;
	  }
	 public boolean  countA()
	 {
		int counter =0;
	   for(int i=0; i<_word.length();i++)
	   {
			 if(_word.charAt(i)=='A')
				 counter++;
	   }
			if ((counter%2)==0)
				return true;
			else	
				return false; 
	}
	public int countUpper()
	{
		int counter =0;
	   for(int i=0; i<_word.length();i++)
	   {
			 if(_word.charAt(i)=='A' || (_word.charAt(i)=='B') )
				 counter++;
	   }
			 if (_word=="")
				return '\0';
			else
				return counter;
		}   
	  public boolean addToWord(String other)
	  {
		  String add = _word+other;
		  return isValid(add);
	  }
	  public String eraseLetter(int i)
	  {
		  int abs1 = Math.abs(i);
		  if (abs1>_word.length())
			return "";
		  
			String o1=_word.substring(0,abs1);
			String o2=_word.substring(abs1+1);_word.length();
			return (o1+o2);
	  }
	public int getLastIndex(char c)
	 {
		 int last = -1;
		for(int i=0; i<_word.length(); i++)
		{
			if (_word.charAt(i)== c)
				last= i;
		 }
			  return last; 
	  }
	  
	public boolean checkContinuity(int num,char c)
	 {
		  
		 
		 
		 
	 }
	

Was This Post Helpful? 0
  • +
  • -

#4 1984oran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-January 10

Re: a little problem with a class

Posted 06 January 2010 - 10:02 AM

help anyone?
plz
Was This Post Helpful? 0
  • +
  • -

#5 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: a little problem with a class

Posted 06 January 2010 - 10:20 AM

View Post1984oran, on 6 Jan, 2010 - 04:02 PM, said:

help anyone?
plz


You need to loop through the characters in _word, and check for an index i whether characters letterInPlace(i) up to letterInPlace(i+num) (num being the 1st parameter of checkContinuity) are equal to char c (the second param).

The good thing is that "your" letterInPlace is pretty idiot-proof, so you won't even need to check whether i+num is within the string...

This post has been edited by EdwinNameless: 06 January 2010 - 10:22 AM

Was This Post Helpful? 1
  • +
  • -

#6 1984oran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-January 10

Re: a little problem with a class

Posted 06 January 2010 - 11:44 AM

thanks man,
you helped me alot
:^:
Was This Post Helpful? 0
  • +
  • -

#7 1984oran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-January 10

Re: a little problem with a class

Posted 06 January 2010 - 12:32 PM

hi all this is my code
public class MyLang
{
	// instance variables - replace the example below with your own
	private String _word;
	
	 private static final int MAX_LENGTH=15;

	/**
	 * Constructor for objects of class MyLang
	 */
	public  MyLang (String w)
	{
	  if (isValid(w)==true)
	   _word=w;
	   else
	   _word = "";
	}
	
	public boolean isValid(String s)
	{
		 int len =s.length();
		if (s.length() > MAX_LENGTH)
			return false;
		if (s.charAt(0)!='A'&&(s.charAt(0)!='B'))
			return false;
			if  ((s.charAt(s.length()-1))=='c')   
		for ( int i=0; i<len; i++)
		{
			if (s.charAt(i)!='A' &&s.charAt(i)!='B' &&s.charAt(i)!='c')
				return false;
		}
		for ( int i=0; i<len; i++)
		{
			if(s.charAt(i)=='c')
				if((s.charAt(i-1)!='A') || (s.charAt(i+1)!='A'))
					return false;
		 }
			return true;
	}
	
	public char lastLetter() 
	{
		if (_word=="")
			return '\0';
		else
		return   (_word.charAt(_word.length()-1));
	}
	public char letterInPlace(int i)
	{
		int abs=Math.abs(i);
		if (_word.length()>abs)
			return _word.charAt(abs);
		 else
			return '\0';
	 }
			
	 public String toString()
	  {
		  return _word;
	  }
	 public boolean  countA()
	 {
		int counter =0;
	   for(int i=0; i<_word.length();i++)
	   {
			 if(_word.charAt(i)=='A')
				 counter++;
	   }
			if ((counter%2)==0)
				return true;
			else	
				return false; 
	}
	public int countUpper()
	{
		int counter =0;
	   for(int i=0; i<_word.length();i++)
	   {
			 if(_word.charAt(i)=='A' || (_word.charAt(i)=='B') )
				 counter++;
	   }
			 if (_word=="")
				return '\0';
			else
				return counter;
		}   
	  public boolean addToWord(String other)
	  {
		  String add = _word+other;
		  return isValid(add);
	  }
	  public String eraseLetter(int i)
	  {
		  int abs1 = Math.abs(i);
		  if (abs1>_word.length())
			return "";
		  
			String o1=_word.substring(0,abs1);
			String o2=_word.substring(abs1+1);_word.length();
			return (o1+o2);
	  }
	public int getLastIndex(char c)
	 {
		 int last = -1;
		for(int i=0; i<_word.length(); i++)
		{
			if (_word.charAt(i)== c)
				last= i;
		 }
			  return last; 
	  }
	  
	public boolean checkContinuity(int num,char c)
	 {
	   for(int i =0; letterInPlace(i)==c;letterInPlace(i+num))
			return true;
	
	   return false;
	 }




i have problem writing this part of the program
int CheckWords (String other) - method receives another word and returns the number of int which is the number of letters in common between the two words. That is, the method returns the number of organs in the intersection between the two words. That is when identical letters in two words, then the method returns the number of letters there are both - that there is consideration of repeated letters. If the words completely identical returns the number 16 (using constant defined). If you do not cut all the words the method returns 0.
For instance, if a received word is BAcAB: BAcAA number will be returned 4. (The letters are highlighted cutting group
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: a little problem with a class

Posted 06 January 2010 - 12:41 PM

this is pretty simple, first compare them using the predefined equal() method..if they are equal then return 16 else loop through the string and compare each 2 corresponding characters..if they are equal then increment the count
Was This Post Helpful? 0
  • +
  • -

#9 1984oran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-January 10

Re: a little problem with a class

Posted 06 January 2010 - 12:43 PM

how to compare with what?
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: a little problem with a class

Posted 06 January 2010 - 12:47 PM

the strings
if(str1.equals(str1))
	return 16;
//rest of the code here


Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: a little problem with a class

Posted 06 January 2010 - 12:50 PM

Threads merged, please do not create duplicate threads on the same topic.
Was This Post Helpful? 0
  • +
  • -

#12 1984oran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-January 10

Re: a little problem with a class

Posted 06 January 2010 - 12:53 PM

this part i know.
my problem is with the second part
Was This Post Helpful? 0
  • +
  • -

#13 1984oran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-January 10

Re: a little problem with a class

Posted 07 January 2010 - 04:03 AM

help please anyone?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1