Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

I am getting 5 codes that i need help with

here are the errors

Sentence.java:15: ')' expected
            String[] x = Pattern.compile(" ").split(words[]);
                                                                ^
Sentence.java:46: illegal start of expression
  public static double getMeasure()
  ^
Sentence.java:52: illegal start of expression
   public String sentence;
   ^
Sentence.java:54: '}' expected
^
5 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.


Here is what each method is to do and criteria for instances:
Create a class called Sentence.

   1. A Sentence implements the Measurable interface.
   2. The constructor takes in a String which represents a sentence and breaks or splits it up into words.  Each word is stored in an element of an Array or ArrayList.  This collection(Array or ArrayList) holds the words that make up the sentence.(Hint if you create an array you can use the String split method)
   3. The class has at least one instance field - the collection that holds the words in the sentence.
   4. Methods
         1. getWord
               1. Given the index of the word returns the word at that index.
               2. Return an empty string (""), If the index < 0 or >= the number of words in the list.
         2. toString
               1. Recreates the sentence from the collection.
               2. Returns the newly created sentence.
         3. getMeasure - returns a double that represents the number of words in the sentence.

import java.lang.String.*;
/**
   A sentance 
*/
public class Sentence implements Measurable
{
   /**
      Constructs a sentence.
      @param aSentence the name of the the sentence
   */
   public Sentence(String aSentence) 
   { 
      sentence = aSentence;
		String[] words = str.split(" ");
		String[] x = Pattern.compile(" ").split(words[]);
     
	   for (int i=0; i<x.length; i++)
	     {
          System.out.println(i + " \"" + x[i] + "\"");
        }
 
   }
 
   public String getWord(int g) 
   {
     	
	for(int i =0; i<x.length; i++)
		
		{
		if(i==g ||i >= x.length)
			return null;
		else 
			return x[i];
		}
		
	
   }
   
	public void toString()
	{
	 for (int i=0; i<x.length; i++) {
      System.out.println(x[i] + " ");
	
	}
	
  public static double getMeasure() 
   {
      return count;
   }
 
   private double count;
   public String sentence;
}

Open in new window

0
jrb47
Asked:
jrb47
  • 25
  • 18
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
See my comments:
/**
 * A sentance
 */
import java.lang.String.*;
/**
   A sentance 
*/
public class Sentence implements Measurable
{
   /**
      Constructs a sentence.
      @param aSentence the name of the the sentence
   */
   public Sentence(String aSentence) 
   { 
      sentence = aSentence; // i would store String[] words instead of sentence
                String[] words = str.split(" "); // should be aSentence.split(" ")
                // may want to use regex in split to get other punctuation aside from space.
                String[] x = Pattern.compile(" ").split(words[]); // not sure the purpose of this line
                // fyi, words[] does make that a CharArray.
     
                // can just do for each word in words to print out
           for (int i=0; i<x.length; i++)
             {
          System.out.println(i + " \"" + x[i] + "\"");
        }
 
   }
 
   public String getWord(int g) 
   {
        // x wouldn't be visible here, which is why I would suggest storing words[]
	   // then as long as g is a valid index you can just return word[g]
        for(int i =0; i<x.length; i++)
                
                {
                if(i==g ||i >= x.length)
                        return null;
                else 
                        return x[i];
                }
                
        
   }
   
   // should be public String toString()
        public void toString()
        {
         for (int i=0; i<x.length; i++) {
      System.out.println(x[i] + " ");
        
        }
        
         // if this is the method from your Measure interface should not be static.
  public static double getMeasure() 
   {
      return count;
   }
 
   private double count;
   public String sentence; // public String[] words;
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
Another note, if you do as I suggest and use words as your instance variable instead of sentence, then you can implement getMeasure like this:
public double getMeasure() {
    return words.length;
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
And more safely:
public double getMeasure() {
    return (words==null)?0:words.length;
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jrb47Author Commented:
Okay, I am going to look at all of this, adjust and then continue on.....I hope that you don't have a hot date tonight as it seems you have been the one to help me most in the last few months!

Thanks again so very very much.
0
 
jrb47Author Commented:
okay,
String[] x = Pattern.compile(" ").split(words[]);  
it was recommended to us that we use the string split to make the array  The test program passes the sentence then we are supposed to take each word and store it in the array

   // not sure the purpose of this line
                // fyi, words[] does make that a CharArray.
     
                // can just do for each word in words to print out
        This is so that i can see how the array is populated

  for (int i=0; i<x.length; i++)
             {
          System.out.println(i + " \"" + x[i] + "\"");

the purpose of this assignment is to use interfaces and different classes.
I did post the whole assignment with the files that are supposed to be used.....in another post......but i need to be able to understand this.
I will post the document here from the other one.
again thank you so much - this is an online class and i dont get a lot of response from the class on these things...and the book is a little to vague for me.



Dataset-file.doc
0
 
jrb47Author Commented:
ok
new errors:

 ----jGRASP exec: javac -g D:\CEN5990\Workshops\workshop9\Sentence.java

Sentence.java:16: not a statement
      String[]= words; // i would store String[] words instead of sentence
            ^
Sentence.java:16: ';' expected
      String[]= words; // i would store String[] words instead of sentence
              ^
Sentence.java:19: '.class' expected
      String[] x = Pattern.compile(" ").split(words[]); // not sure the purpose of this line
                                                     ^
Sentence.java:19: ')' expected
      String[] x = Pattern.compile(" ").split(words[]); // not sure the purpose of this line
                                                      ^
Sentence.java:55: illegal start of expression
 public double getMeasure()
 ^
Sentence.java:60: illegal start of expression
   public String[]words; // public String[] words;
   ^
Sentence.java:61: '}' expected
}
 ^
7 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

new code

/**
 * A sentance
 */
import java.lang.String.*;
/**
   A sentance 
*/
public class Sentence implements Measurable
{
   /**
      Constructs a sentence.
      @param aSentence the name of the the sentence
   */
   public Sentence(String aSentence) 
   { 
      String[]= words; // i would store String[] words instead of sentence
      words = aSentence.split(" "); // should be aSentence.split(" ")
                // may want to use regex in split to get other punctuation aside from space.
      String[] x = Pattern.compile(" ").split(words[]); // not sure the purpose of this line
                // fyi, words[] does make that a CharArray.
     
                // can just do for each word in words to print out
           for (int i=0; i<x.length; i++)
             {
          System.out.println(i + " \"" + x[i] + "\"");
        }
 
   }
 
   public String getWord(int g) 
   {
        // x wouldn't be visible here, which is why I would suggest storing words[]
           // then as long as g is a valid index you can just return word[g]
        for(int i =0; i<words.length; i++)
                
                {
                if(i==g ||i >= words.length)
                        return null;
                else 
                        return words[i];
                }
                
        
   }
   
   // should be public String toString()
        public String toString()
        {
         for (int i=0; i<x.length; i++) {
      System.out.println(x[i] + " ");
        
        }
        
         // if this is the method from your Measure interface should not be static.
 public double getMeasure()
  {
    return (words==null)?0:words.length;
	} 
   private double count;
   public String[]words; // public String[] words;
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
That comment was for the String[] x line as Pattern.compile(" ") is used for regular expressions typically and not necessary to use String.split as recommended to you.  The line above gets the words into array just fine, except as I said it doesn't take into account the presence of punctuation marks as well as space; however, get it working with spaces first then can try to work on that.

Once you understand that, you will see you can directly just use words.length in the for loop or simply use the for each syntax: for (String word : words) { }.  However, you will notice the difference, is you don't get the index with for each, so if you want to display that then for with index is fine.

No problem.

Will be here to help you through the code so that you understand it which is why I didn't just post corrected code but instead my thoughts. ;)

Best regards,
Kevin
0
 
jrb47Author Commented:
Thank you SO much......I need to understand and not just have the corrections.

for this assignment i need the index as my getWord method should receive the index number and print return the word assigned to that number. I am assuming that i will pass the index number via my sentenceTest file, which i will post once i get this one good.

I have fixed what you suggested and understand why. But i am still getting errors. I believe it has to do with how i am initializing the String[] = word portion

here are the errors:

 ----jGRASP exec: javac -g D:\CEN5990\Workshops\workshop9\Sentence.java

Sentence.java:16: not a statement
      String[]= words; // i would store String[] words instead of sentence
            ^
Sentence.java:16: ';' expected
      String[]= words; // i would store String[] words instead of sentence
              ^
Sentence.java:52: illegal start of expression
 public double getMeasure()
 ^
Sentence.java:57: illegal start of expression
   public String[]words; // public String[] words;
   ^
Sentence.java:58: '}' expected
}
 ^
5 errors

 ----jGRASP wedge2: exit code for process is 1.

here is the new code

/**
 * A sentance
 */
import java.lang.String.*;
/**
   A sentance 
*/
public class Sentence implements Measurable
{
   /**
      Constructs a sentence.
      @param aSentence the name of the the sentence
   */
   public Sentence(String aSentence) 
   { 
      String[]= words; // i would store String[] words instead of sentence
      words = aSentence.split(" "); // should be aSentence.split(" ")
                // may want to use regex in split to get other punctuation aside from space.
                    // can just do for each word in words to print out
           for (int i=0; i<words.length; i++)
             {
          System.out.println(i + " \"" + x[i] + "\"");
        }
 
   }
 
   public String getWord(int g) 
   {
        // x wouldn't be visible here, which is why I would suggest storing words[]
           // then as long as g is a valid index you can just return word[g]
        for(int i =0; i<words.length; i++)
                
                {
                if(i==g ||i >= words.length)
                        return null;
                else 
                        return words[i];
                }
                
        
   }
   
   // should be public String toString()
        public String toString()
        {
         for (int i=0; i<x.length; i++) {
      System.out.println(x[i] + " ");
        
        }
        
         // if this is the method from your Measure interface should not be static.
 public double getMeasure()
  {
    return (words==null)?0:words.length;
  } 
  
   private double count;
   public String[]words; // public String[] words;
}

Open in new window

0
 
jrb47Author Commented:
Also can you tell me how to delete the previous question I posted
0
 
Kevin CrossChief Technology OfficerCommented:

/**
 * A sentance
 */
public class Sentence {
	/**
	 * Constructs a sentence.
	 * 
	 * @param aSentence
	 *            the name of the the sentence
	 */
	public Sentence(String aSentence) {
		// split sentence and store in instance array words.
		words = aSentence.split(" ");
	}
 
	public String getWord(int g) {
		// check for invalid index.
		if (g < 0 || g >= words.length)
			return null;
 
		// return word for given index.
		return words[g];
	}
 
	public String toString() {
		// need to update this to return a String
		// representing original sentence
		for (int i = 0; i < words.length; i++)
			System.out.println(words[i] + " ");
	}
 
	// if this is the method from your Measure interface should not be static.
	public double getMeasure() {
		return (words == null) ? 0 : words.length;
	}
 
	public String[] words; // public String[] words;
}

Open in new window

0
 
jrb47Author Commented:
figured out how to delete the previous question......amazing what happens when you open your eyes and take 10 deep breaths.


0
 
jrb47Author Commented:
okay, now the only error i am getting is a missing return statement.
I did not think we needed to return anything. I would just call the method from test file and it would reprint the sentence ......what am i missing.

i did not change anything in your last code accept implement measurable interface.

error
Sentence.java:31: missing return statement
    }
        ^
1 error

 ----jGRASP wedge2: exit code for process is 1.


code

/**
 * A sentance
 */
public class Sentence implements Measurable
{
	/**
	 * Constructs a sentence.
	 * 
	 * @param aSentence
	 *            the name of the the sentence
	 */
	public Sentence(String aSentence) {
		// split sentence and store in instance array words.
		words = aSentence.split(" ");
	}
 
	public String getWord(int g)
	{
		// check for invalid index.
		if (g < 0 || g >= words.length)
			return null;
 
		// return word for given index.
		return words[g];
	}
 
	public String toString()
	{
		// need to update this to return a String
		// representing original sentence
		for (int i = 0; i < words.length; i++)
			System.out.println(words[i] + " ");
	}
 
	// if this is the method from your Measure interface should not be static.
	public double getMeasure() {
		return (words == null) ? 0 : words.length;
	}
 
	public String[] words; // public String[] words;
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
Glad you found the delete button. :)
public Sentence(String aSentence) {
		// split sentence and store in instance array words.
		words = aSentence.split(" ");
		
		// print out words with index.
		for (int i=0; i<words.length; i++)
			System.out.println(i + " \"" + words[i] + "\"");
	}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
The problem is with toString() it is a method from Object so java thinks you are incorrectly trying to override it.  Try changing the name if you don't want to return a string.
public void printSentence() {
		// need to update this to return a String
		// representing original sentence
		for (int i = 0; i < words.length; i++)
			System.out.println(words[i] + " ");
	}

Open in new window

0
 
jrb47Author Commented:
no it is supposed to return the newly created sentence;

so would i have to declare another variable then pass it to the method called from the test class.

2. toString
               1. Recreates the sentence from the collection.
               2. Returns the newly created sentence.

0
 
Kevin CrossChief Technology OfficerCommented:
Yes you could have a variable in your test class like this:
Sentence s = new Sentence("Mary had a little lamb!");
String s2 = s.toString();

Open in new window

0
 
jrb47Author Commented:
okay, i know i am majoring on minors BUT i have to keep the methods named exactly like in the instructions and i think i get errors when trying to name it
public void toString

here is my test code......I have not even started trouble shooting it.

Again thank you so much for helping me with this. I appreciate it so much because you take the time to explain....are you an instructor somewhere.

public class DataSetTester2
{
   public static void main(String[] args)
   {
      DataSet sentenceData = new DataSet();
 
      sentenceData.add(new Sentence("This program is difficult"));
 		System.out.println(sentenceData.getWord(2)); 
		System.out.println(sentenceData.toString);
		System.out.println.(sentenceData.getMeasure);   
	
	  
	   DataSet sentenceData2 = new DataSet();  
		sentenceData2.add(new Sentence("Java is a object oriented language"));
      System.out.println(sentenceData2.getWord(4));
		System.out.println(sentenceData2.toString);
		System.out.println.(sentenceData2.getMeasure); 
		
	   DataSet sentenceData3 = new DataSet(4);     
		sentenceData3.add(new Sentence("This is a difficult concept"));	
		System.out.println(sentenceData3.getWord(4));
		System.out.println(sentenceData3.toString);
		System.out.println.(sentenceData3.getMeasure);		
	
           
   }
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
And toString would look something like this:
public String toString() {
		// use a string buffer to build up string
		// strings are immutable, so appending to a normal string really creates new instance.
		StringBuffer sb = new StringBuffer();
		// for each word in words append to sb.
		for (String word : words) sb.append(word + " ");
		// return string from the sb.
		return sb.toString();
	}

Open in new window

0
 
jrb47Author Commented:
i just tested my sentenceTest file and there is something wrong with the way i am calling the getmeasure instance.


0
 
jrb47Author Commented:
woot wooot woohoooooo
Sentence.java compiles with no error.

Now i need to work the tester file would you like me to give points on this solution and open another question for additional points. - not sure how important that is to you.
0
 
Kevin CrossChief Technology OfficerCommented:
New question would increase your chance you get others input on the new requirement; however, I wouldn't mind helping you determine if you even have an issue.  The one thing I noticed is that you were calling methods like toString and getMeasure without parenthesis.  You can get away with that in VB, but Java always needs the () even if no arguments being passed to distinguish a method from a property.
0
 
jrb47Author Commented:
want to keep working with you

the getMeasure is supposed to return the number in the array - a double
But i am not calling it correctly

I will correct the parens and see how that comes out.


0
 
jrb47Author Commented:
new errors:
DataSetTester2.java:13: <identifier> expected
        System.out.println.(sentenceData.getMeasure());  
                                   ^
DataSetTester2.java:13: not a statement
        System.out.println.(sentenceData.getMeasure());  
                                  ^
DataSetTester2.java:20: <identifier> expected
        System.out.println.(sentenceData2.getMeasure());
                                   ^
DataSetTester2.java:20: not a statement
        System.out.println.(sentenceData2.getMeasure());
                                  ^
DataSetTester2.java:26: <identifier> expected
        System.out.println.(sentenceData3.getMeasure());        
                                   ^
DataSetTester2.java:26: not a statement
        System.out.println.(sentenceData3.getMeasure());        
                                  ^
6 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

new code


/**
   This program tests the DataSet class.
*/
public class DataSetTester2
{
   public static void main(String[] args)
   {
      DataSet sentenceData = new DataSet();
 
      sentenceData.add(new Sentence("This program is difficult"));
 		System.out.println(sentenceData.getWord(2)); 
		System.out.println(sentenceData.toString());
		System.out.println.(sentenceData.getMeasure());   
	
	  
	   DataSet sentenceData2 = new DataSet();  
		sentenceData2.add(new Sentence("Java is a object oriented language"));
      System.out.println(sentenceData2.getWord(4));
		System.out.println(sentenceData2.toString());
		System.out.println.(sentenceData2.getMeasure()); 
		
	   DataSet sentenceData3 = new DataSet(4);     
		sentenceData3.add(new Sentence("This is a difficult concept"));	
		System.out.println(sentenceData3.getWord(4));
		System.out.println(sentenceData3.toString());
		System.out.println.(sentenceData3.getMeasure());		
	
           
   }
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
You have an extra dot (.) after the println.  That is your type-o. :)
0
 
jrb47Author Commented:
I looked back at the tester for the sample and found something i left out.
I inserted into my code but am still getting errors

new errors

 ----jGRASP exec: javac -g D:\CEN5990\Workshops\workshop9\DataSetTester2.java

DataSetTester2.java:13: <identifier> expected
        System.out.println.(max.getMeasure());  
                                   ^
DataSetTester2.java:13: not a statement
        System.out.println.(max.getMeasure());  
                                  ^
DataSetTester2.java:21: <identifier> expected
        System.out.println.(max.getMeasure());
                                   ^
DataSetTester2.java:21: not a statement
        System.out.println.(max.getMeasure());
                                  ^
DataSetTester2.java:28: <identifier> expected
        System.out.println.(max.getMeasure());        
                                   ^
DataSetTester2.java:28: not a statement
        System.out.println.(max.getMeasure());        
                                  ^
6 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

new code


/**
   This program tests the DataSet class.
*/
public class DataSetTester2
{
   public static void main(String[] args)
   {
      DataSet sentenceData = new DataSet();	
      sentenceData.add(new Sentence("This program is difficult"));
 		System.out.println(sentenceData.getWord(2)); 
		System.out.println(sentenceData.toString());
		Measurable max = sentenceData.getMaximum();
		System.out.println.(max.getMeasure());   
	
	  
	   DataSet sentenceData2 = new DataSet();  
		sentenceData2.add(new Sentence("Java is a object oriented language"));
      System.out.println(sentenceData2.getWord(4));
		System.out.println(sentenceData2.toString());
 		max = sentence2Data.getMaximum();
		System.out.println.(max.getMeasure()); 
		
	   DataSet sentenceData3 = new DataSet(4);     
		sentenceData3.add(new Sentence("This is a difficult concept"));	
		System.out.println(sentenceData3.getWord(4));
		System.out.println(sentenceData3.toString());
		max = sentence3Data.getMaximum();
		System.out.println.(max.getMeasure());		
	
           
   }
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
You still have an extra dot (.) after the println.

e.g.
System.out.println.(max.getMeasure());      

should be:
System.out.println(max.getMeasure());      
0
 
jrb47Author Commented:
dur to dur......go figure
okay changed that after laughing......can you tell i am stressed

new errors

 DataSetTester2.java:10: cannot find symbol
symbol  : variable word
location: class DataSetTester2
         System.out.println(sentenceData.getWord(word[1]));
                                                        ^
DataSetTester2.java:18: cannot find symbol
symbol  : method getWord(int)
location: class DataSet
      System.out.println(sentenceData2.getWord(4));
                                      ^
DataSetTester2.java:20: cannot find symbol
symbol  : variable sentence2Data
location: class DataSetTester2
         max = sentence2Data.getMaximum();
                      ^
DataSetTester2.java:23: cannot find symbol
symbol  : constructor DataSet(int)
location: class DataSet
       DataSet sentenceData3 = new DataSet(4);    
                                   ^
DataSetTester2.java:25: cannot find symbol
symbol  : method getWord(int)
location: class DataSet
        System.out.println(sentenceData3.getWord(4));
                                                ^
DataSetTester2.java:27: cannot find symbol
symbol  : variable sentence3Data
location: class DataSetTester2
        max = sentence3Data.getMaximum();
                      ^
6 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

new code

I think i am not passing the index of word i wish returned to check the getWord method.


/**
   This program tests the DataSet class.
*/
public class DataSetTester2
{
   public static void main(String[] args)
   {
      DataSet sentenceData = new DataSet();	
      sentenceData.add(new Sentence("This program is difficult"));
 		System.out.println(sentenceData.getWord(word[1])); 
		System.out.println(sentenceData.toString());
		Measurable max = sentenceData.getMaximum();
		System.out.println(max.getMeasure());   
	
	  
	   DataSet sentenceData2 = new DataSet();  
		sentenceData2.add(new Sentence("Java is a object oriented language"));
      System.out.println(sentenceData2.getWord(4));
		System.out.println(sentenceData2.toString());
 		max = sentence2Data.getMaximum();
		System.out.println(max.getMeasure()); 
		
	   DataSet sentenceData3 = new DataSet(4);     
		sentenceData3.add(new Sentence("This is a difficult concept"));	
		System.out.println(sentenceData3.getWord(4));
		System.out.println(sentenceData3.toString());
		max = sentence3Data.getMaximum();
		System.out.println(max.getMeasure());		
	
           
   }
}

Open in new window

0
 
jrb47Author Commented:
this has to have something to do with how i am trying to call the methods and pass the index variable.
I changed the first grouping but it is still wrong

0
 
jrb47Author Commented:
i fixed that....now my error is in calling the getWord method
new errors
 ----jGRASP exec: javac -g D:\CEN5990\Workshops\workshop9\DataSetTester2.java

DataSetTester2.java:10: cannot find symbol
symbol  : method word(int)
location: class DataSetTester2
         System.out.println(sentenceData.getWord(word(1)));
                                                        ^
DataSetTester2.java:18: cannot find symbol
symbol  : method getWord(int)
location: class DataSet
      System.out.println(sentenceData2.getWord(4));
                                      ^
DataSetTester2.java:20: cannot find symbol
symbol  : variable sentence2Data
location: class DataSetTester2
         max = sentence2Data.getMaximum();
                      ^
DataSetTester2.java:23: cannot find symbol
symbol  : constructor DataSet(int)
location: class DataSet
       DataSet sentenceData3 = new DataSet(4);    
                                   ^
DataSetTester2.java:25: cannot find symbol
symbol  : method getWord(int)
location: class DataSet
        System.out.println(sentenceData3.getWord(4));
                                                ^
DataSetTester2.java:27: cannot find symbol
symbol  : variable sentence3Data
location: class DataSetTester2
        max = sentence3Data.getMaximum();
                      ^
6 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

/**
   This program tests the DataSet class.
*/
public class DataSetTester2
{
   public static void main(String[] args)
   {
      DataSet sentenceData = new DataSet();	
      sentenceData.add(new Sentence("This program is difficult"));
 		System.out.println(sentenceData.getWord(word(1))); 
		System.out.println(sentenceData.toString());
		Measurable max = sentenceData.getMaximum();
		System.out.println(max.getMeasure());   
	
	  
	   DataSet sentenceData2 = new DataSet();  
		sentenceData2.add(new Sentence("Java is a object oriented language"));
      System.out.println(sentenceData2.getWord(4));
		System.out.println(sentenceData2.toString());
 		max = sentence2Data.getMaximum();
		System.out.println(max.getMeasure()); 
		
	   DataSet sentenceData3 = new DataSet(4);     
		sentenceData3.add(new Sentence("This is a difficult concept"));	
		System.out.println(sentenceData3.getWord(4));
		System.out.println(sentenceData3.toString());
		max = sentence3Data.getMaximum();
		System.out.println(max.getMeasure());		
	
           
   }
}

Open in new window

0
 
jrb47Author Commented:
the getWord method is supposed to receive a number that is the index of the array
it then should return the string that is held in that index and print it out.

what am coding wrong....please
0
 
Kevin CrossChief Technology OfficerCommented:
You are confusing sentence object and DataSet object.  The methods you are calling are from the sentence object and so don't exist within DataSet.  Now since you are only putting one sentence into each DataSet, you are guaranteed that getMaximum will return your sentence object.  Relook at what you are supposed to be doing with the test.  If you still have issues, then post a different question with new piece of code and what the issues are.
0
 
jrb47Author Commented:
okay, you completely confused me.
I have been looking at the bankAccount.java
and the Coin.java and I do not see where i am messing up.

This is what i understand.
I am supposed to be adding sevral sentences to the DataSet class
then I am supposed to use the sentence class to design a getWord method and a toString method

i am then supposed to test those methods.
in looking at all the files i have been given that is what i did but .......
0
 
jrb47Author Commented:
I think i see what you are saying
The getWord method has nothing to do with the DataSet class file:
   except to call getMaximum......and that interfaces with measurable

so how then would i add sentences to the sentence class using all of these examples.

I really am confused as to how to proceed but i think i understand what you are telling me to look at.....
make since?

0
 
Kevin CrossChief Technology OfficerCommented:

/**
   This program tests the DataSet class.
*/
public class DataSetTester2
{
   public static void main(String[] args)
   {
      Sentence s = new Sentence("This program is difficult");
      DataSet ds = new DataSet();
      ds.add(s);
      System.out.println(s.getWord(1));
      System.out.println(s.toString());
      System.out.println(ds.getMaximum().getMeasure());
 
      // repeat, noting you can reuse s and just add other Measurable objects to ds.
   }
}

Open in new window

0
 
jrb47Author Commented:
okay let me make sure i understand this
I am using the Sentence class by initializing the variable s
The DataSet is initialized by ds and passing the string s to it
and then i call the methods from two different class files.


am i write

oh boy am i going to have to study and practice.....
0
 
Kevin CrossChief Technology OfficerCommented:
Yes you are testing two different classes.  You are testing sentence and then testing the functionality of dataset while proving that your sentence object properly implements measurable as it should not matter that it is a sentence to getMeasure().  This will allow you to add any kind of measurable to the same dataset.
0
 
jrb47Author Commented:
and the light has come on.......just don't ask me to redo this........yet
thank you so much for your help
I would like it if you could explain one last thing

your comment that
// repeat, noting you can reuse s and just add other Measurable objects to ds.
   
here is the code i wound up with and it works


/**
   This program tests the DataSet class.
*/
public class DataSetTester2
{
   public static void main(String[] args)
   {
     Sentence s = new Sentence("Java is a object oriented language");
      DataSet ds = new DataSet();
      ds.add(s);
      System.out.println(s.getWord(0));
      System.out.println(s.toString());
      System.out.println(ds.getMaximum().getMeasure());
      System.out.println(); 
	  	  
	 Sentence s2 = new Sentence("This program is difficult");
      DataSet ds2 = new DataSet();
      ds2.add(s2);
      System.out.println(s2.getWord(2));
      System.out.println(s2.toString());
      System.out.println(ds2.getMaximum().getMeasure());
		System.out.println();
 
 
	  Sentence s3 = new Sentence("Multiple class is a difficult concept");
      DataSet ds3= new DataSet();
      ds3.add(s3);
      System.out.println(s3.getWord(4));
      System.out.println(s3.toString());
      System.out.println(ds3.getMaximum().getMeasure());
		System.out.println();
		
	            
   }
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
instead of:
ds2.add(s2);

you could:
ds.add(s2); // add to existing ds
0
 
jrb47Author Commented:
okay i see what you mean.
on another little note
I need the getWord method to return an empty string if the index is less then 0 or >= words.length

this is the code...but i have not initialized string s correctly


	public String getWord(int g)
	{
	   public string s;
		s = (" ");
	// check for invalid index.
		if (g < 0 || g >= words.length)
			return s;
 		// return word for given index.
		return words[g];
	}

Open in new window

0
 
jrb47Author Commented:
got it!!!!!
0
 
Kevin CrossChief Technology OfficerCommented:
Glad you got it.  :) You could also just have used:
return "";

Best regards,
Kevin
0
 
jrb47Author Commented:
Again thank you so much for helping me through this.
0
 
Kevin CrossChief Technology OfficerCommented:
You are most welcome.

Happy coding!

Regards,
isa
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 25
  • 18
Tackle projects and never again get stuck behind a technical roadblock.
Join Now