Java programming question.
Results 1 to 2 of 2

Thread: Java programming question.

  1. #1
    Banned
    Join Date
    May 2005
    Posts
    12

    Java programming question.

    I have to do a lab for my class in java and it involves the following:
    The purpose of this lab is to better prepare you for your next visit to the Ohio Renaissance Festival this coming fall… you HAVE already been there before, HAVEN’T YOU? You will create a program to randomly create Shakespearian insults. The input to this is a file named insult.txt. The format looks like this:



    artless base-court apple-john

    bawdy bat-fowling baggage

    beslubbering beef-witted barnacle

    bootless beetle-headed bladder

    churlish boil-brained boar-pig

    cockered clapper-clawed bugbear

    clouted clay-brained bum-bailey

    craven common-kissing canker-blossom

    currish crook-pated clack-dish

    dankish dismal-dreaming clotpole



    Create an object class that will open the file name passed in the constructor call, read one line at a time into a string and then use the StringTokenizer class to break the line into three words, the first word going into one Vector, the second word going into a second Vector and the last word into a third Vector, all located in this class object call cat. If, for example, you had Vectors a, b and c, a would hold the string “artless b0] would hold the string “base-court” d c0] would hold the string “apple-john”. This will all be done in the constructor for class cat. There will be one more object method in cat… get(), which will randomly Vectorsult by randomly gathering one word from each of the three Vectors. Some examples:



    dankish sheep-biting death-token

    churlish rude-growing canker-blossom

    mangled hedge-born clotpole

    spongy idle-headed mumble-news

    mewling motley-minded joithead

    qualling rough-hewn gudgeon

    Notice that a space has been added between the first and second and between the second and third words in the phrase. Please add the word Thou in front of the whole mess to produce the following output:



    Thou craven boil-brained clack-dish



    Add a main() method to this object class. This will also have a menu with two optons, one to display one insult and one to exit. Formatting is your choice. I have inspected the entire insult.txt file and found nothing that should be upsetting to a 21st century college student. Feel free, however, delete any lines in the file that you deem inappropriate. You must keep at least 20 lines of the file. Please do NOT alter any lines in the file. I do not want anyone offended when they run your program as part of the grading process.



    I have provided information on the use of the random function and text file input in class. This and the StringTokenizer class reference will give you sufficient information to generate the arrays of words from which you will build the insult.



    You will need the following import statement in your application:

    import java.util.*;
    This is the outline that I am supposed to follow, or best to do the program.

    Code:
    //import statements
    public class cat  
    {
    		// Variables
    		//Constructor will take filename as input
    		public cat()
    		{
    			//use a loop to read all the lines in the file
    			   //use StreamTokenizer and add the 3 words to 3 different vectors
    		}
    		 
    		public String get()
    		{
    			//This method will use the random number generation and the 		//elementAt methods to get a random word from the vector
    		}
    		public static void main (String[] args) 
    		{
    			//In this method, use a loop to print a menu and accept the user input
    			//call the methods depending on the user input
    		}//End of main
    }//End of class
    And this is my code so far:
    Code:
    //import statements
    import java.io.* ;
    import java.util.*;
    
    public class cat
    {
            //Variables
            BufferedReader in;
            //Constructor will take filename as input
    
         public cat()
          {
            //use loop to read all the lines in the file
            try
             {
               in = new BufferedReader(new FileReader("insult.txt"));
                 String str;
                 int noOfLines = 0;
                 while (true)
                  {
                    str = in.readLine();
                    if(str == null)
                     {
                      break;
                     }
                    else
                     {
                      System.out.println(str);
                       noOfLines++;
                     }
                  }//END while
            
                   in.close();   
            }// END try
             catch (IOException e) {}
          
            //use StreamTokenizer and add the 3 words to 3 different vectors
                      
            String s = "Computer programming fundamentals";
            String str1,str2,str3;
            StringTokenizer a = new StringTokenizer(s);
            str1 = a.nextToken();
            str2 = a.nextToken();
            str3 = a.nextToken();
            System.out.println(str1 + " and " + str2 + " " + str3);
          }//END cat
    
         public String get()
          {
            //This method will use the random number generation and the
            //elementAt methods to get a random word from the vector
          }
    
         public static void main (String[] args)
          {
            cat a = new cat();
            //In main method, use a loop to print a menu and accept usr input
    
            do
             {
             System.out.println("Enter 1 to display an insult");
             System.out.println("Enter 2 to Exit");
             System.out.print("Enter Choice: ");
             int choice = MyInput.readInt();
              if (choice == 1)
                    //DONT KNOW how to call constructor here
              else
                    break;
            } while (true);
            //call the methods dependong on the user input
    
          }//End main
    
    
    } //End of Class
    My problem is that I don't know what to do next. I don't know how to do the radomizing of the strings. And I also am a bit lost in vectors. Will somone please take a look at this code and let me know if they can give any suggestions. Thank you.

  2. #2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •