DOC PREVIEW
UNI CS 4550 - Command-Line Arguments, Strings, and Files

This preview shows page 1-2-3-18-19-37-38-39 out of 39 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Session 4Options for User InputUsing the command lineSlide 4Fixing this problemYour turn to write a simple program using the command lineSimple Calculations at the Command LineFirst Attempt - What’s wrong?Correct CalculateThe wrapper classesBut wait a minute!Lifetime ModifiersSlide 13Slide 14Slide 15Slide 16Homework #2 – Constructors and Command-line ArgumentsHomework #2Homework #2 - TasksHomework #2 - Sample ExecutionsSlide 21Slide 22Java Strings and FilesExerciseCharacterCount templateStringTokenizerSlide 27StringTokenizer ExampleSlide 29StringTokenizer Example 2Slide 31Split – alternative to StringTokenizerUNIX/Linux pipeStringTokenizer Example 3Slide 35Java File I/OJava File I/O Example: Echo.javaStudy Echo.java’s File I/OSlide 39Session 4Command-Line Arguments, Strings, and FilesOptions for User Input•Options for getting information from the user–Write event-driven code•Con: requires a significant amount of new code to set-up•Pro: the most versatile.–Use System.in•Con: less versatile then event-driven•Pro: requires less new code–Use the command line (String[ ] args)•Con: very limited in its use•Pro: the simplest to set upUsing the command line•Remember what causes the “big bang” in our programs?public static void main (String [] args) {•main expects an array of strings as a parameter.•The fact of the matter is that this array has been a null array in each of our programs so far.Using the command line•However, we can give this array values by providing command line arguments when we start a program running.$ java MyProgram String1 String2 String3 args[0] args[1] args[2]Fixing this problempublic class MyEcho2 { public static void main( String[] args ) { if (args.length == 2) { System.out.println("args[0] is ” + args[0]); System.out.println("args[1] is " + args[1]); } else { System.out.println( "Usage: java MyEcho2 " + "string1 string2"); } // end if } // end main} // end MyEcho2$ java MyEcho2 MarkUsage: java MyEcho2 string1 string2 $ java MyEcho2 Mark Alan FienupUsage: java MyEcho2 string1 string2Your turn to write a simple program using the command line•Write a program to echo all command-line arguments to System.out$ java EchoAll This is a long lineargs[0] is Thisargs[1] is isargs[2] is aargs[3] is longargs[4] is lineSimple Calculations at the Command Line$ java Calculate 6 + 410$ java Calculate 8 - 53First Attempt - What’s wrong?public class Calculate { public static void main( String[] args ) { int operand1 = args[0]; String operator = args[1]; int operand2 = args[2]; if ( operator.equals("+") ) { System.out.println( operand1 + operand2 ); } else if ( operator.equals("-") ) { System.out.println( operand1 - operand2 ); } else { System.out.println("Invalid operator: " + operator); } // end if } // end main} // end Calculate$ javac Calculate.javaCalculate.java:3: incompatible typesfound : java.lang.Stringrequired: int int operand1 = args[0]; ^Correct Calculatepublic class Calculate { public static void main( String[] args ) { int operand1 = Integer.parseInt( args[0] ); String operator = args[1]; int operand2 = Integer.parseInt( args[2] ); if ( operator.equals("+") ) { System.out.println( operand1 + operand2 ); } else if ( operator.equals("-") ) { System.out.println( operand1 - operand2 ); } else { System.out.println( "Invalid operator: " + operator ); } // end if } // end main} // end CalculateThe wrapper classes•Primitive data types (int, double, boolean, etc.) are not actually objects. Because of this, you can’t use them easily in certain OO situations•Therefore, Java has “wrapper classes” such as Integer, Double, and Boolean.•These are true classes in the OO sense of the word since they contain data which store information (often the value in it’s corresponding primitive data type) and methods that can act on this data.But wait a minute!•How is it that we can use the parseInt() method from the Integer class without actually creating an instance of Integer class.Lifetime ModifiersWhat does static mean?Lifetime ModifiersWhat does static mean?•The item being declared is a feature of the class – what we usually call “class methods” or “class variables.”•The item being declared exists at load time, before any instance is created.Lifetime Modifiers•A “class method” is one that can be invoked without sending a message to an instance of the class.the main method of MemoPadAppint operand1 = Integer.parseInt(args[0]);double myRandom = Math.random();Lifetime Modifiers•A “class variable” is one that every instance has access to and shares.In chapter 5, Budd creates windows in which bouncing balls live. Every instance of his BallWorld class shares the same height and width dimensions, implemented as static class variables:public static int frameWidth=200;public static int frameHeight=250;Lifetime Modifiers•We will use these rarely in the code we write.–The more you use static stuff, the less flexible and modifiable your code tends to be.•The Java class library uses these more frequently. Budd will use them occasionally.–Thus, you will still get to see plenty of examples before we are done!Homework #2 – Constructors and Command-line Arguments•Goals for this assignment: practice implementing constructors practice factoring common behavior into helper methods experience working with command-line arguments •You will continue to work with the MemoPad program for this assignment, including the database class that you implemented for Homework 1Homework #2 •For this homework, you will modify the MemoPadApp class to accept some optional command-line arguments. java MemoPadApp Use the original DefaultMemoDatabase java MemoPadApp –d Use the original DefaultMemoDatabasejava MemoPadApp -s Use your original student version of MyMemoDatabase from homework 1java MemoPadApp -s 100 Use your MyMemoDatabasethat is created by a new constructor that limits the database to the specified maximum size (in this example it is 100)Homework #2 - Tasks1. Add a new constructor to your MyMemoDatabase class that takes one argument: the maximum number of memos that can be stored. 2. Eliminate any code duplication in your MyMemoDatabase class's


View Full Document

UNI CS 4550 - Command-Line Arguments, Strings, and Files

Download Command-Line Arguments, Strings, and Files
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Command-Line Arguments, Strings, and Files and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Command-Line Arguments, Strings, and Files 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?