Top Banner
Miscellaneous Miscellaneous topics topics Chapter 2 Savitch Chapter 2 Savitch
32

Miscellaneous topics

Dec 31, 2015

Download

Documents

marvin-carney

Miscellaneous topics. Chapter 2 Savitch. Miscellaneous topics. Standard output using System.out Input using Scanner class. The standard System class. System contains a number of interesting static methods. System.exit( 0 ); Terminates our program immediately - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Miscellaneous topics

Miscellaneous topicsMiscellaneous topics

Chapter 2 SavitchChapter 2 Savitch

Page 2: Miscellaneous topics

Miscellaneous topicsMiscellaneous topics

Standard output using System.outStandard output using System.out

Input using Scanner classInput using Scanner class

Page 3: Miscellaneous topics

The standard System classThe standard System class

System contains a number of interesting System contains a number of interesting static methods.static methods. System.exit( 0 );System.exit( 0 );

Terminates our program immediatelyTerminates our program immediately long start = System.currentTimeMillis();long start = System.currentTimeMillis();

Useful for timing programsUseful for timing programs System.out and System.inSystem.out and System.in

The standard output and input streamsThe standard output and input streams

See See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html

Page 4: Miscellaneous topics

Output and System.outOutput and System.out

Page 5: Miscellaneous topics

System.outSystem.out

Static member (not a method)Static member (not a method)Type is PrintStreamType is PrintStream See See

http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html

Useful PrintStream methods:Useful PrintStream methods: print()print() println()println() printf()printf()

Page 6: Miscellaneous topics

print() and println()print() and println()

print() prints its argumentprint() prints its argument

println() prints its argument followed by a println() prints its argument followed by a newlinenewline

Ex.Ex.int i=12;int i=12;

String s = “sally”;String s = “sally”;

System.out.println( i );System.out.println( i );

System.out.print( s + “ is “ + i + “ years old.” );System.out.print( s + “ is “ + i + “ years old.” );

System.out.println();System.out.println();

Page 7: Miscellaneous topics
Page 8: Miscellaneous topics
Page 9: Miscellaneous topics
Page 10: Miscellaneous topics
Page 11: Miscellaneous topics
Page 12: Miscellaneous topics
Page 13: Miscellaneous topics
Page 14: Miscellaneous topics
Page 15: Miscellaneous topics
Page 16: Miscellaneous topics
Page 17: Miscellaneous topics
Page 18: Miscellaneous topics
Page 19: Miscellaneous topics
Page 20: Miscellaneous topics

printf and roundingprintf and rounding

What is the output of the following:What is the output of the following:class Test {class Test {

public static void main ( String args[] ) {public static void main ( String args[] ) {

double d2 = 12.999999;double d2 = 12.999999;

System.out.printf( "%f %.2f \n\n", d2, d2 );System.out.printf( "%f %.2f \n\n", d2, d2 );

}}

}}

Page 21: Miscellaneous topics

printf and roundingprintf and rounding

What is the output of the following:What is the output of the following:class Test {class Test {

public static void main ( String args[] ) {public static void main ( String args[] ) {

double d2 = 12.999999;double d2 = 12.999999;

System.out.printf( "%f %.2f \n\n", d2, d2 );System.out.printf( "%f %.2f \n\n", d2, d2 );

}}

}}

12.999999 13.0012.999999 13.00

Page 22: Miscellaneous topics

printf()printf()

Format string examples:Format string examples:d = 12.00000;d = 12.00000;

System.out.printf( “%010.2f %n”, d );System.out.printf( “%010.2f %n”, d );

d = 12.99999999;d = 12.99999999;

System.out.printf( “%010.2f %n”, d );System.out.printf( “%010.2f %n”, d );

Page 23: Miscellaneous topics

printf()printf()

Useful for formatting other data types as Useful for formatting other data types as well.well.int i = 22;int i = 22;

System.out.printf( "%d %n", i );System.out.printf( "%d %n", i );

System.out.printf( "%6d %n", i );System.out.printf( "%6d %n", i );

System.out.printf( "%06d %n", i );System.out.printf( "%06d %n", i );

System.out.printf( "%-6d %n", i );System.out.printf( "%-6d %n", i );

System.out.printf( "%x %n", i );System.out.printf( "%x %n", i );

Page 24: Miscellaneous topics

printf()printf()

Useful for Strings too.Useful for Strings too.String s = "sally";String s = "sally";

System.out.printf( "%s eats here.%n", s );System.out.printf( "%s eats here.%n", s );

System.out.printf( "%12s eats here.%n", s );System.out.printf( "%12s eats here.%n", s );

//exception thrown://exception thrown:

//System.out.printf( "%012s eats here.%n", s );//System.out.printf( "%012s eats here.%n", s );

System.out.printf( "%-12s eats here.%n", s );System.out.printf( "%-12s eats here.%n", s );

Page 25: Miscellaneous topics

More OO alternatives to printf()More OO alternatives to printf()

NumberFormatNumberFormat

DecimalFormatDecimalFormat

Page 26: Miscellaneous topics

Input, System.in, and the Input, System.in, and the Scanner classScanner class

Page 27: Miscellaneous topics

Scanner classScanner class

Note that System.in is of type InputStream Note that System.in is of type InputStream (see (see http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html).).

And the InputStream can only read arrays And the InputStream can only read arrays of bytes! How can we read ints, floats, of bytes! How can we read ints, floats, doubles, etc.?doubles, etc.?

Use the Scanner class.Use the Scanner class.

Page 28: Miscellaneous topics

Scanner classScanner class

import java.util.Scanner;import java.util.Scanner;

class Tester {class Tester {

public static void main ( String params[] ) {public static void main ( String params[] ) {

Scanner kbd = new Scanner( System.in );Scanner kbd = new Scanner( System.in );

……

}}

}}

Page 29: Miscellaneous topics

Scanner classScanner class

Scanner kbd = new Scanner( System.in );Scanner kbd = new Scanner( System.in );

System.out.println( “Enter weight and age” );System.out.println( “Enter weight and age” );

int weight = kbd.nextInt();int weight = kbd.nextInt();

int age = kbd.nextInt();int age = kbd.nextInt();

System.out.println( “Thanks.” );System.out.println( “Thanks.” );

Page 30: Miscellaneous topics

Some useful Scanner class Some useful Scanner class methodsmethods

nextInt()nextInt()

nextLong()nextLong()

nextByte()nextByte()

nextShort()nextShort()

nextDouble()nextDouble()

nextFloat()nextFloat()

hasNextInt()hasNextInt()

hasNextLong()hasNextLong()

hasNextByte()hasNextByte()

hasNextShort()hasNextShort()

hasNextDouble()hasNextDouble()

hasNextFloat()hasNextFloat()

Page 31: Miscellaneous topics

More useful scanner class methodsMore useful scanner class methods

nextBoolean() (and hasNextBoolean())nextBoolean() (and hasNextBoolean()) Response should be either true or falseResponse should be either true or false

next() (and hasNext())next() (and hasNext()) Returns a string of the next characters up to Returns a string of the next characters up to

but not including the next whitespace but not including the next whitespace charactercharacter

nextLine() (and hasNextLine())nextLine() (and hasNextLine()) Returns a string of the next characters upt to Returns a string of the next characters upt to

but not including the next newline characterbut not including the next newline character

Page 32: Miscellaneous topics

Exercises on p. 91Exercises on p. 91