output strings input
play

Output, Strings, Input 7 January 2019 OSU CSE 1 Simplest Java - PowerPoint PPT Presentation

Output, Strings, Input 7 January 2019 OSU CSE 1 Simplest Java Program? public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } 7 January 2019 OSU CSE 2 Simplest Java Program?


  1. Output, Strings, Input 7 January 2019 OSU CSE 1

  2. Simplest Java Program? public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } 7 January 2019 OSU CSE 2

  3. Simplest Java Program? public class HelloWorld { public static void main(String[] args) { public class declares this System.out.println("Hello World!"); code to be a software } component for which } bytecode should be generated by the compiler; HelloWorld is the name of the class; details later. 7 January 2019 OSU CSE 3

  4. Simplest Java Program? public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } public static void is } required here when you want a class to include a “main” program that can be executed by the JVM (and it must be called main ); details later. 7 January 2019 OSU CSE 4

  5. Simplest Java Program? public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } String[] args means that } main expects the JVM to hand it an array of String s (called command-line arguments ) when it is executed; details later. 7 January 2019 OSU CSE 5

  6. Simplest Java Program? public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } System.out is an object you may use to give output to the user; println is a method of that object that you may call ( invoke ) to output something on its own line; details later. 7 January 2019 OSU CSE 6

  7. Simplest Java Program? public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } "Hello World!" is a character string to be output to the user; details later. 7 January 2019 OSU CSE 7

  8. Another Version (sans Comments) import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class HelloWorld { private HelloWorld() { } public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L(); out.println("Hello World!"); out.close(); } } 7 January 2019 OSU CSE 8

  9. Another Version (sans Comments) import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class HelloWorld { import indicates you want to use private HelloWorld() { a component in your code; } components is a package public static void main(String[] args) { containing OSU CSE components; SimpleWriter out = new SimpleWriter1L(); its simplewriter package offers out.println("Hello World!"); a few advantages over using built- out.close(); in System.out ; } details later. } 7 January 2019 OSU CSE 9

  10. Another Version (sans Comments) import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class HelloWorld { private HelloWorld() { public means anyone can } use this class; public static void main(String[] args) { final means no one can SimpleWriter out = new SimpleWriter1L(); incrementally change this out.println("Hello World!"); class by using inheritance ; out.close(); details later. } } 7 January 2019 OSU CSE 10

  11. Another Version (sans Comments) import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class HelloWorld { private HelloWorld() { } public static void main(String[] args) { private HelloWorld() means the HelloWorld class SimpleWriter out = new SimpleWriter1L(); does not define a type , i.e., no out.println("Hello World!"); one can create an object from out.close(); the class HelloWorld because } it is a utility class ; } details later. 7 January 2019 OSU CSE 11

  12. Another Version (sans Comments) import components.simplewriter.SimpleWriter; SimpleWriter is the type of a newly declared variable ; import components.simplewriter.SimpleWriter1L; out is the name of that public final class HelloWorld { variable; private HelloWorld() { details later. } public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L(); out.println("Hello World!"); out.close(); } } 7 January 2019 OSU CSE 12

  13. Another Version (sans Comments) new creates a new object to which the variable out is a reference ; import components.simplewriter.SimpleWriter; SimpleWriter1L is the class import components.simplewriter.SimpleWriter1L; whose code should be used when public final class HelloWorld { any method of out is called; private HelloWorld() { details later. } public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L(); out.println("Hello World!"); out.close(); } } 7 January 2019 OSU CSE 13

  14. Another Version (sans Comments) import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; out has a println method, public final class HelloWorld { too, nearly identical to that of private HelloWorld() { System.out ; } details later. public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L(); out.println("Hello World!"); out.close(); } } 7 January 2019 OSU CSE 14

  15. Another Version (sans Comments) import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; out has a close method as public final class HelloWorld { well, and you need to call it private HelloWorld() { when you are done using } out ; public static void main(String[] args) { details later. SimpleWriter out = new SimpleWriter1L(); out.println("Hello World!"); out.close(); } } 7 January 2019 OSU CSE 15

  16. Output: SimpleWriter • The OSU CSE components provide a simple way to provide output to a user via the console or a file SimpleWriter consoleOut = new SimpleWriter1L(); SimpleWriter fileOut = new SimpleWriter1L("foo.txt"); 7 January 2019 OSU CSE 16

  17. Output Examples consoleOut.print("Prompt: "); consoleOut.println(); fileOut.println("A line."); 7 January 2019 OSU CSE 17

  18. Closing Output • When you are done writing output to a SimpleWriter stream , you must close the stream: consoleOut.close(); fileOut.close(); 7 January 2019 OSU CSE 18

  19. Character Strings • Java has special features to deal with character strings • Examples SimpleWriter fileOut = new SimpleWriter1L("foo.txt"); fileOut.print("Hi, Mr. Foo."); • This intro is just the tip of the iceberg! 7 January 2019 OSU CSE 19

  20. Character Strings • Java has special features to deal with character strings • Examples a character string SimpleWriter fileOut = new SimpleWriter1L("foo.txt"); fileOut.print("Hi, Mr. Foo."); • This intro is just the tip of the iceberg! 7 January 2019 OSU CSE 20

  21. Character Strings • Java has special features to deal with character strings • Examples a character string SimpleWriter fileOut = new SimpleWriter1L("foo.txt"); fileOut.print("Hi, Mr. Foo."); • This intro is just the tip of the iceberg! 7 January 2019 OSU CSE 21

  22. Character-String Literals • Character-string constants, also called String literals , are enclosed in double- quotes, e.g.: "Hello World!" • Character strings can be concatenated (joined together to create new character strings) using the + operator, e.g.: "Hello " + "World!" 7 January 2019 OSU CSE 22

  23. String Variables • You may declare a String variable , and assign an initial character-string value to it, as follows: String cheer = "Go"; "Go" 7 January 2019 OSU CSE 23

  24. String Variables • You may assign any other character-string value to the same variable later, e.g.: cheer = cheer + " Bucks!"; • Before assignment above: "Go" 7 January 2019 OSU CSE 24

  25. String Variables • You may assign any other character-string value to the same variable later, e.g.: cheer = cheer + " Bucks!"; • After assignment above: "Go Bucks!" 7 January 2019 OSU CSE 25

  26. Input: SimpleReader • The OSU CSE components provide a simple way to get input from a user via the keyboard or a file SimpleReader keyboardIn = new SimpleReader1L(); SimpleReader fileIn = new SimpleReader1L("foo.txt"); 7 January 2019 OSU CSE 26

  27. Input Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine(); 7 January 2019 OSU CSE 27

  28. Input Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine(); This method, which reads up through and including the next line separator , and returns everything it reads except that next line separator, is really the only method you need to read input from the keyboard and text files. 7 January 2019 OSU CSE 28

  29. Closing Input • When you are done reading input from a SimpleReader stream , you must close the stream: keyboardIn.close(); fileIn.close(); 7 January 2019 OSU CSE 29

  30. Resources • Java Tutorials ("Hello World" program) – http://docs.oracle.com/javase/tutorial/getStarted/application/index.html • OSU CSE components API ( SimpleWriter , SimpleReader ) – http://cse.osu.edu/software/common/doc/ 7 January 2019 OSU CSE 30

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend