THis is what I need to do...I need help with the third point
1. Write a class OrderedList. The implementation of OrderedList must be as a linked list of Comparable elements. The list items are maintained in ascending order at all times. On this assignment, no indexing methods are allowed. The following methods are required:
int size() //Return the number of items in the list
boolean contains(Comparable item)//Return true iff item is stored in the OrderedList
void insert(Comparable item)//Add an item to the OrderedList
Comparable remove(Comparable item) //Remove and return a matching item
Also, provide a default constructor and a toString() method.

2. Write a class, Record, to represent a concordance entry consisting of a word, a count, and a line-number list. The line-number list should be implemented as an ArrayList.

3. Write the Concordance class. There should be exactly 2 instance variables:
 the name of the text file from which the Concordance is created,
 and an OrderedList of Record elements.
Each Record element stores the information about one word from the text file. The constructor has a parameter for the file name, and builds the Concordance by processing the words from the text file. Provide a toString() method, a method Record lookup(String word) to look up a given word in a Concordance, and a method to write a Concordance to a text file.


this is what I have so far

import java.util.LinkedList;
public class OrderedList 
{
    private Node first;
    
    public OrderedList()
    {
        this.first = null;
    }
    public int size()
    {
        int count = 0;
        
        Node pointer = this.first;
        while(pointer != null)
        {
            count++;
            pointer = pointer.next;
        }
        return count;
    }
    public boolean contains(Comparable item)
    {
        Node pointer = this.first;
        while (pointer!= null && !pointer.data.equals(item))
            pointer = pointer.next;
        return (pointer != null) && (pointer.data.equals(item));
    }
    
    public void insert( Comparable newI)
    {
        Node pointer = this.first;
        Node newbee= new Node(newI);
            
        while(pointer != null)
            pointer = pointer.next;
        
        if (pointer == null)
        {
            newbee.next = this.first;
            this.first = newbee;
        }
        else
        {
            newbee.next = pointer.next;
            pointer.next = newbee;
        }
    }
    public Comparable remove(Comparable item)
    {
        if (this.first == null)
            return null;
        Node pointer = this.first;
        Node trailer = null;

        while(pointer.next != null)
        {
            trailer = pointer;
            pointer = pointer.next;
        }
       if (pointer!= null)
            if (trailer ==null)
                this.first = pointer.next;
            else
                trailer.next = pointer.next;
       return pointer.data;
    }
    public String toString()
	{
		String image = this.size() + " Items";
		
		Node pointer = this.first;
		while (pointer != null)
		{
			image += "\n" + pointer.data;
			pointer = pointer.next;
		}
			
		return image;
	}	
    private class Node
    {
        public Comparable data;
        public Node next;
        
        public Node(Comparable item)
        {
            this.data = item;
            this.next = null;
        }
    }
            
    
    
}

***************************************************
import java.util.ArrayList;
public class Record
{
    public String word = "";
    public int count = 0;
    public ArrayList<Integer> list = new ArrayList<Integer>();

    public Record (String w, int c, ArrayList<Integer> l)
    {
        this.word  = w;
        this.count = c;
        this.list = l;
    }

}
******************************************************

import java.util.Comparator;

public class Concordance
{
    public String filename = "";
    public OrderedList list = new OrderedList();
    
    public Concordance(String filename)
    {

    }

    public Record lookup(String word)
    {
       // Record r = ;
        return null;
    }
  
   public String toString()
   {
       String r ="";
      //for (Record term: list)
      //     r = r + term;
       
       return r;
   }
}

Please I have been trying to figure it out but could not figure it out.

Recommended Answers

All 8 Replies

Can you give a short example text file, and tell us how the records would be created for that text file? Because the design that your teacher gave you makes no sense. Why would you pass the name of a text file to 'Concordance' only to pass the same name to 'Record' over and over? How are you supposed to know what you already read in from the text file and what remains to be read in from the text file? Without passing more information to the Record constructor, there is no reasonable way to do this. (If that doesn't make sense to you, just give a short example text file and show me how the Records should be created for it).

Text
See Spot run.
See Jane run.
See Spot bite.
Run Jane, run!
Concordance
bite 1 3
jane 2 2, 4
run 4 1, 2, 4
see 3 1, 2, 3
spot 2 1, 3

mainly I need help figuring out the last 2 parts
I would really appreciate if I could get as any help and/or tips on how to solve it as possible thanks

What I told you earlier about the Record constructor doesn't follow your teacher's directions. Your teacher wants a constructor that accepts one String as a parameter, where the String is the file name. Keep reading and you'll see.

Scanner file = new Scanner(new File(yourTextFilePathname));
OrderedList list = new OrderedList();
while (file.hasNext()){
String word = file.next();
Record rec = new Record();
rec.lookup(word);
}

In your lookup method, it should search through the file using a Scanner and every time it sees the word you're looking for (the one that was passed into the method) it should record the line number and it should also add the line it was seen on to an ArrayList. That part will be a little tricky since if you use the next() method, you aren't going to know which line number you're on. More resources you can look into for figuring out which line words are on:

Scanner class's nextLine() method which will help you read in an entire line. Then, on that line, you can create another Scanner to look through that line using the Scanner class's next() method. So in summary to write your lookup method do something like this:

public class Record{
String word;
ArrayList<Record> list = new ArrayList<Record>();
String filename;

public Record(String fname){
filename = fname;
}

public void lookup(String wordWanted){
//Create a Scanner (using the filename) to read line by line

/* while the file has another line{
1. String line = read in the whole next line using Scanner's nextLine()
2. Create another Scanner and pass in 'line' (from above)
3. Use Scanner's next() method and the String class's equals() method
to figure out if 'wordWanted' was on the line.
*/
}
}
}

In addition to what I said for your lookup method, you're going to want an integer counter to keep track of what line you are on. If what I've said above doesn't make sense then devise your own way of keeping track of lines, etc. But using two Scanners like I mentioned above is probably the easiest way to do it. You're going to want to take a look at the method documentation for nextLine() as well as next() in the Scanner class.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

If you don't know how to use the equals() method, it is essential for your project, so I suggest you get started.
http://technologiquepanorama.wordpress.com/2009/03/19/what-is-difference-between-and-equals-in-java/

this is what I have done so far but it still does nothing ... can someone please help me ?

import java.util.Comparator;
import java.util.Scanner;

public class Concordance
{
    public String filename = "";
    public OrderedList list = new OrderedList();
    
    public Concordance(String filename)
    {
        this.filename = filename;
        extractWords();
        
    }
    
public void extractWords()
{
    Scanner rp = new Scanner(this.filename);
    while(rp.hasNext())
    {
         String word = rp.next();
      //   Record w = new Record(word);
         list.insert(word);
    }
           
}
public Record lookup(String wordWanted){

    Record t = new Record(wordWanted);
    if (list.contains(wordWanted))
         return t;
    return null;

}
  
   public String toString()
   {
       String r =this.list.toString();
       return r;
   }
  
               

}

*********************************************************

import java.util.LinkedList;
public class OrderedList 
{
    private Node first;
    
    public OrderedList()
    {
        this.first = null;
    }
    public int size()
    {
        int count = 0;
        
        Node pointer = this.first;
        while(pointer != null)
        {
            count++;
            pointer = pointer.next;
        }
        return count;
    }
    public boolean contains(Comparable item)
    {
        Node pointer = this.first;
        while (pointer!= null && !pointer.data.equals(item))
            pointer = pointer.next;
        return (pointer != null) && (pointer.data.equals(item));
    }
    
    public void insert( Comparable newI)
    {
        Node pointer = this.first;
        Node newbee= new Node(newI);
            
        while(pointer != null)
            pointer = pointer.next;
        
        if (pointer == null)
        {
            newbee.next = this.first;
            this.first = newbee;
        }
        else
        {
            newbee.next = pointer.next;
            pointer.next = newbee;
        }
    }
    public Comparable remove(Comparable item)
    {
        if (this.first == null)
            return null;
        Node pointer = this.first;
        Node trailer = null;

        while(pointer.next != null)
        {
            trailer = pointer;
            pointer = pointer.next;
        }
       if (pointer!= null)
            if (trailer ==null)
                this.first = pointer.next;
            else
                trailer.next = pointer.next;
       return pointer.data;
    }
    public String toString()
	{
		String image = this.size() + " Items";
		
		Node pointer = this.first;
		while (pointer != null)
		{
			image += "\n" + pointer.data;
			pointer = pointer.next;
		}
			
		return image;
	}	
    private class Node
    {
        public Comparable data;
        public Node next;
        
        public Node(Comparable item)
        {
            this.data = item;
            this.next = null;
        }
    }   
}

*********************************************************

import java.util.ArrayList;
public class Record
{
    public String word = "";
    public int count = 0;
    public ArrayList<Integer> list = new ArrayList<Integer>();

    public Record (String w)
    {
        this.word  = w;
       
    }
    public void increaseCount()
    {
        this.count++;
    }
    public void addLine(int line)
    {
        this.list.add(line);
    }
    public int getCount()
    {
        return this.count;
    }
    public ArrayList<Integer> getLine()
    {
        return this.list;
    }
    public String getWord()
    {
        return this.word;
    }
}
******************************************************

import java.io.*;
import javax.swing.JOptionPane;
public class ConcordanceClient
{
	public static void main(String[] args) throws IOException
	{
		String fileName;
		do
		{
			fileName = JOptionPane.showInputDialog(null,
							"Enter the name of a text file or hit CANCEL to quit");
			if (fileName != null)
			{
				Concordance list = new Concordance(fileName);
				System.out.println(list);
				
                              
                              
                                
                                list.toString();
                                list.lookup("love");
                                list.toString();
                                
                                 FileWriter writer = new FileWriter(new File("rose.txt"));
                                 writer.write(list.toString());
                                 System.out.println(list.toString());
				
			}
		} while (fileName != null);
	}
}

Any help to solve this program would be appreciated..Thanks
PW

I already gave you step by step directions. What didn't you understand?

I already gave you step by step directions. What didn't you understand?

he needs zuh koduz asap! :(

PETER!
did you ever figure out this program, or get the solution from the teacher?!?! im doing the exact same program and im so lost! i would insanely appreciate any help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.