Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Please help, confusing programing assignment.

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool Please help, confusing programing assignment.

    Okay so this is my first time taking a java course and I have been slow to learn. In any case here is my newest assignment and I am struggling.

    Assignment

    Create an Outlab5 project and put 'this' in a Driver.
    Read through the Driver and make sure you understand basically what is happening. You don't need to know details of how file reading is happening, but you should be able to figure out what is going on.
    Copy this into a file called sampleSearch.txt (it needs to be called exactly that). Put that file in the same folder that your Outlab5 source code will reside in (where Driver.java and package should currently be). This will be the file that the Driver reads from.
    Create the class and methods needed to replicate this 'output'.
    You do NOT need to search for words that are backwards. All words that you need to search for are top-to-bottom, left-to-right, or diagonally upper left to lower right.

    Here is the Driver:
    import java.util.*;
    import java.io.*;
     
    public class Driver
    {    
        public static void main (String[] args)
        {
            // try block needed to read in file
            try
            {
                //open the file "sampleSearch.txt"
                FileReader fileName = new FileReader("sampleSearch.txt");
                Scanner fileRead = new Scanner(fileName);
     
                //read in the number of rows
                int numRow = fileRead.nextInt();
                fileRead.nextLine();
     
                //read in the number of columns
                int numCol = fileRead.nextInt();
                fileRead.nextLine();
     
                //create a 2d array to hold the characters in the file
                char[][] array = new char[numRow][numCol];
     
                //for each row in the file
                for (int r = 0; r < numRow; r++)
                {
                    //read in the row as a string
                    String row = fileRead.nextLine();
     
                    //parse the string into a sequence of characters
                    for (int c = 0; c < numCol; c++)
                    {
                        //store each character in the 2d array
                        array[r][c] = row.charAt(c);
                    }
                }
     
                //create a new instance of the WordSearch class
                WordSearch ws = new WordSearch(array);
     
                //play the game
                ws.play();
            }
            catch (FileNotFoundException exception)
            {
                //error is thrown if file cannot be found.  See directions or email me...
                System.out.println("File Not Found");
            }
     
        }    
    }

    The text file:
    10
    15
    fqexfecmxdvjlgu
    cxomfslieyitqtz
    nucatfakuxofegk
    hfytpnsdlhcorey
    pgrhdqsypyscped
    ckadhyudtioapje
    yerjodxnqzztfmf
    hypmmgoronkzhuo
    hdskymmpkzokaao
    amuewqvtmrlglad

    and the output looks like this:

    fqexfecmxdvjlgu
    cxomfslieyitqtz
    nucatfakuxofegk
    hfytpnsdlhcorey
    pgrhdqsypyscped
    ckadhyudtioapje
    yerjodxnqzztfmf
    hypmmgoronkzhuo
    hdskymmpkzokaao
    amuewqvtmrlglad

    What word do you want to search for? (Type end to quit)
    cat
    cat found horizontally at row 2 and column 2!
    cat found vertically at row 4 and column 11!



    Alright so that's the information here is the code I have made so far. Most of it is based of of lecture notes that are somewhat hazy and a somewhat poor understanding of arrays and loops... There is only one look method because I am trying to take it one step at a time.

    The devil is in the details I guess. Most of my questions are in the code. Ie the pseudo code and comments are the things I don't understand. I would really appreciate some help here. So far my experience with java has been somewhat brutal... I dont think its my sport.

    public class WordSearch
    {
        char[][] array;
     
        public WordSearch(char[][] inArray)
        {
         array = inArray;
         found = false;//supposing i will need a boolean and I remember something about it being located here in this method
     
          for (int r = 0; r < array.length; r++)//step into array
            {
                for (int c = 0; c < array[r].length; c++)//look through each spot in array
                {
                   if(array[r][c]==word.CharAt[0])//not sure what's happening here exactly but compiles
                   {
                       if(word is found in sRight method)//"lookR(r,c)==true" does this look right?
                       {
                           //print and set found to true
                       }
     
                       if(word is not found)//"lookR(r,c)!true" ?
                       {
                           //print not found
                       }
                   }
                }
        }
     
        public boolean sRight(int inRow, int inColum)//not sure why this has to be boolean
        {
          r = inRow;//not sure if I need to initialize
          c = inColum;//not sure if I need to initialize
     
            for (int r = 0; r < array.length; r++)//not sure if I should be running through array
            {
                for (int c = 0; c < array[r].length; c++)//... still not sure
                {
     
                   if(out of bounds)//no idea was never taught this... somewhat surprising 
                                         //he told us to do something he hasn't showed us how to do
                   {
                       return false //not sure how to return false just know I should
                   }
                   else (mis-match)//Once again something i don't know how to do. However, I know I was
                                         //never taught this so once again I am surprised...
                   {
                       return false//once again not sure how but know I have to 
                   }
                   else//default case i believe
                   {
                       return true//not why or how but know I should
                   }
     
                }
            }
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Please help, confusing programing assignment.

    Summarizing:

    The playing field is defined in the file, 10 lines by 15 columns
    The user is asked for a word to find
    The program searches the playing field for the user's word
    Words are only valid left to right, top to bottom, or diagonally upper left to lower right

    The wordSearch() method (proper name) searches the playing field for letters that match the first letter in the user's word (similar to how you'd find a word in your head) and from that point should:

    looks for the rest of the word to the right
    looks for the rest of the word down
    looks for the rest of the word diagonally

    Each of the "looks for" tasks should be done by methods that return booleans, false if not found and true if found. The possible results of each of those searches are:

    out of bounds: should't occur if the for loops are setup properly
    mismatch: the next letter in the playing field doesn't match the next letter in the word, so the search fails
    word is found (default): return true.

    You have other questions in the code, but they're mostly basic. Local variables must be declared and initialized. Not sure you need some of them, but you decide.

    Keep working, let us know when you need help.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please help, confusing programing assignment.

    Okay well I think maybe my first method has been figured out. Tell me if I did something wrong:

      for (int r = 0; r < array.length; r++)//step into array
            {
                for (int c = 0; c < array[r].length; c++)//look through each spot in array
                {
                   if(array[r][c]==word.CharAt[0])//not sure what's hapening here exactly but compiles
                   {
                       if(sRight(r,c)==true)
                       {
                           System.out.println("The word was found!");
                           found = true;
                       }
     
                       if(sRight(r,c)==false)
                       {
                           System.out.println("The word was not found!");
                       }
                   }
                }
        }

    However, the search right method is still confusing me. So if I pass the method r,c then i'm passing it array[r][c] right? I am wondering what I need to put in the () for each if statement. I believe my simple return true and return false statements are all I need in the body right?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Please help, confusing programing assignment.

    Passing the method (r, c) gives the method the location in the field from which to begin the search. You may also want to pass the method the array of letters.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please help, confusing programing assignment.

    What is this line doing? if(array[r][c]==word.CharAt[0])???? lol sorry this dang line is confusing me.

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please help, confusing programing assignment.

    Okay so for my search method I have some questions. For some reason when I try to compile it says im missing a return statement. Any idea why?

      private boolean sHoriz(int inRow, int inColum)
        {
            int r = inRow;     
            int c = inColum; 
            for (r = 0; r < array.length; r++)
            {
                for (c = 0; c < array[r].length; c++)
                {
                    if (array[r][c]!=word.charAt(1))
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
            }
        }


    --- Update ---

    Nvm sorry I figured it out... finally my program is sorta working/doing something lol here is what I have so far. The assignment is due tomorrow at 8 so I dont expect to get it working by then. Maybe someone will be able to respond here in the next few hours if not thanks for the help you guys did give me!

    import java.util.Scanner;
    public class WordSearch
    {
        char[][] array;
        String word;
        Scanner in = new Scanner(System.in);
     
        public WordSearch(char[][] inArray)
        {
            array = inArray;
        }
     
        public void play()
        {
            print();
        }
     
        public void print()
        {
            for (int r = 0; r < array.length; r++)//step into array
            {
                for (int c = 0; c < array[r].length; c++)//look through each spot in array
                {
                    System.out.print(array[r][c] + " ");
                }
                System.out.println();
            }
            System.out.println("User, please imput word you wish to search for.");
            word = in.next();
            Search(word);
        }
     
        private void Search(String inWord)
        {
            word = inWord;
            boolean found = false;
            for (int r = 0; r < array.length; r++)//step into array
            {
                for (int c = 0; c < array[r].length; c++)//look through each spot in array
                {
                    if(array[r][c]==word.charAt(0))
                    {
                        if(sHoriz(r,c)==true)
                        {
                            System.out.println("The word was found!");
                            found = true;
                        }
     
                        if(sHoriz(r,c)==false)
                        {
                            System.out.println("The word was not found!");
                        }
                    }
                    if(array[r][c]==word.charAt(0))
                    {
                        if(sDia(r,c)==true)
                        {
                            System.out.println("The word was found!");
                            found = true;
                        }
     
                        if(sDia(r,c)==false)
                        {
                            System.out.println("The word was not found!");
                        }
                    }
                    if(array[r][c]==word.charAt(0))
                    {
                        if(sVert(r,c)==true)
                        {
                            System.out.println("The word was found!");
                            found = true;
                        }
     
                        if(sVert(r,c)==false)
                        {
                            System.out.println("The word was not found!");
                        }
                    }
                }
            }
        }
     
        private boolean sHoriz(int inRow, int inColum)
        {
            int r = inRow;     
            int c = inColum; 
            for (r = 0; r < array.length; r++)
            {
                 if (array[r][c]!=word.charAt(1))
                    {
                        return false;
                    }
                 else
                    {
                    }
            }
            return true;
        }
     
        private boolean sDia(int inRow, int inColum)
        {
            int r = inRow;
            int c = inColum;
            for (r = 0; r < array.length; r++)
            {
                if (array[r][c]==word.charAt(1))
                    {
                        return false;
                    }
                    else
                    {
                    }
            }
            return true;
        }
     
        private boolean sVert(int inRow, int inColum)
        {
            int r = inRow;
            int c = inColum;
            for (r = 0; r < array.length; r++)
            {
               if (array[r][c]==word.charAt(1))
                    {
                        return false;
                    }
                    else
                    {
                    }
            }
            return true;
        }
    }

    Output for one run:

    "f q e x f e c m x d v j l g u
    c x o m f s l i e y i t q t z
    n u c a t f a k u x o f e g k
    h f y t p n s d l h c o r e y
    p g r h d q s y p y s c p e d
    c k a d h y u d t i o a p j e
    y e r j o d x n q z z t f m f
    h y p m m g o r o n k z h u o
    h d s k y m m p k z o k a a o
    a m u e w q v t m r l g l a d
    User, please imput word you wish to search for.
    cant
    The word was not found!
    The word was not found!
    The word was not found!
    The word was not found!
    The word was not found!
    The word was not found!
    The word was not found!
    The word was not found!
    The word was not found!
    The word was not found!
    The word was found!
    The word was found!
    The word was not found!
    The word was not found!
    The word was not found!
    The word was not found!
    The word was not found!
    The word was not found!
    "

Similar Threads

  1. needed help on this programing assignment
    By byrantloh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2013, 04:39 PM
  2. Confusing question.
    By fok in forum Java Theory & Questions
    Replies: 9
    Last Post: April 10th, 2013, 09:27 AM
  3. Introduction to programing class. I am stuck on my assignment arrays.
    By dozindave in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 18th, 2012, 09:31 PM
  4. a very confusing exception
    By deathlypest in forum Java Theory & Questions
    Replies: 29
    Last Post: June 11th, 2012, 12:17 PM
  5. help with a programing assignment.
    By oscar_m in forum Object Oriented Programming
    Replies: 4
    Last Post: May 1st, 2011, 02:00 PM