{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Ex5NeedObjects

Ex5NeedObjects - UseClasses The purpose of this class is to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
/* UseClasses * * The purpose of this class is to demonstrate how to USE classes that other programmers have * created. We do not want to reinvent the wheel – use what others have done! */ public class useClasses { public static void main(String args[]) { String fileString, findString; Scanner userInput; Boolean found; // allocate and initialize Scanner // this is the first step to using a class – allocating and initializing it with the // new keyword. This allocates space for the object and sometimes initializes it. userInput = new Scanner( System.in ); // open a file and read in all of the text into fileString // we don’t know how to do that yet, so just pretend it happens here ….. code here ….. // ask the user for a String to search in the file System.out.print(“What word would you like to find?”); // here we see how to call a method of a class // variablename.methodName(); findString = userInput.nextLine(); // find out whether the string is a substring of the other // notice we need to give it information to answer this question – the string to find // we place the input argument in the parens found = fileString.contains(findString); if (found) // same as if (found == true) { System.out.printf(“We found %s in the file!\n”,findString); } if (!found) // same as if (found == false) { System.out.printf(“We did not find %s in the file.\n”,findString); } } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Steps to using objects 1) Declare a variable ClassName variableName; 2) Allocate instance of class – an object variableName = new ClassName(); // sometimes put info in parens 3) Use methods variableName.methodName(); // sometimes put info in parens How do I choose what method to use?
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}