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

output strings input
SMART_READER_LITE
LIVE PREVIEW

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?


slide-1
SLIDE 1

Output, Strings, Input

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

Simplest Java Program?

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }

7 January 2019 OSU CSE 2

slide-3
SLIDE 3

Simplest Java Program?

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } public class declares this 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

slide-4
SLIDE 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

slide-5
SLIDE 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 Strings (called command-line arguments) when it is executed; details later.

7 January 2019 OSU CSE 5

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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();

  • ut.println("Hello World!");
  • ut.close();

} }

7 January 2019 OSU CSE 8

slide-9
SLIDE 9

import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class HelloWorld { private HelloWorld() { } public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L();

  • ut.println("Hello World!");
  • ut.close();

} } import indicates you want to use a component in your code; components is a package containing OSU CSE components; its simplewriter package offers a few advantages over using built- in System.out; details later.

7 January 2019 OSU CSE 9

Another Version (sans Comments)

slide-10
SLIDE 10

import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class HelloWorld { private HelloWorld() { } public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L();

  • ut.println("Hello World!");
  • ut.close();

} } public means anyone can use this class; final means no one can incrementally change this class by using inheritance; details later.

7 January 2019 OSU CSE 10

Another Version (sans Comments)

slide-11
SLIDE 11

import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class HelloWorld { private HelloWorld() { } public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L();

  • ut.println("Hello World!");
  • ut.close();

} } private HelloWorld() means the HelloWorld class does not define a type, i.e., no

  • ne can create an object from

the class HelloWorld because it is a utility class; details later.

7 January 2019 OSU CSE 11

Another Version (sans Comments)

slide-12
SLIDE 12

import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class HelloWorld { private HelloWorld() { } public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L();

  • ut.println("Hello World!");
  • ut.close();

} } SimpleWriter is the type of a newly declared variable;

  • ut is the name of that

variable; details later.

7 January 2019 OSU CSE 12

Another Version (sans Comments)

slide-13
SLIDE 13

import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class HelloWorld { private HelloWorld() { } public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L();

  • ut.println("Hello World!");
  • ut.close();

} }

7 January 2019 OSU CSE 13

Another Version (sans Comments)

new creates a new object to which the variable out is a reference; SimpleWriter1L is the class whose code should be used when any method of out is called; details later.

slide-14
SLIDE 14

import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class HelloWorld { private HelloWorld() { } public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L();

  • ut.println("Hello World!");
  • ut.close();

} }

  • ut has a println method,

too, nearly identical to that of System.out; details later.

7 January 2019 OSU CSE 14

Another Version (sans Comments)

slide-15
SLIDE 15

import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class HelloWorld { private HelloWorld() { } public static void main(String[] args) { SimpleWriter out = new SimpleWriter1L();

  • ut.println("Hello World!");
  • ut.close();

} }

  • ut has a close method as

well, and you need to call it when you are done using

  • ut;

details later.

7 January 2019 OSU CSE 15

Another Version (sans Comments)

slide-16
SLIDE 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

slide-17
SLIDE 17

Output Examples

consoleOut.print("Prompt: "); consoleOut.println(); fileOut.println("A line.");

7 January 2019 OSU CSE 17

slide-18
SLIDE 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

slide-19
SLIDE 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

slide-20
SLIDE 20

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!

a character string

7 January 2019 OSU CSE 20

slide-21
SLIDE 21

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!

a character string

7 January 2019 OSU CSE 21

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 24

String Variables

  • You may assign any other character-string

value to the same variable later, e.g.:

cheer = cheer + " Bucks!";

  • Before assignment above:

7 January 2019 OSU CSE 24

"Go"

slide-25
SLIDE 25

String Variables

  • You may assign any other character-string

value to the same variable later, e.g.:

cheer = cheer + " Bucks!";

  • After assignment above:

7 January 2019 OSU CSE 25

"Go Bucks!"

slide-26
SLIDE 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

slide-27
SLIDE 27

Input Examples

String line = keyboardIn.nextLine(); line = fileIn.nextLine();

7 January 2019 OSU CSE 27

slide-28
SLIDE 28

Input Examples

String line = keyboardIn.nextLine(); line = fileIn.nextLine();

7 January 2019 OSU CSE 28

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

  • nly method you need to read input

from the keyboard and text files.

slide-29
SLIDE 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

slide-30
SLIDE 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