Download presentation

Presentation is loading. Please wait.

Published byAlexandra Ramirez Modified over 2 years ago

1
Introduction to Programming Java Lab 4: Formatted Output and Strings 1 February JavaLab4 lecture slides.ppt Ping Brennan

2
Java Project Project Name: JavaLab4 2 FormattedOutput LexicographicOrdering

3
Class FormattedOutput Objectives –Using the printf method to specify how values should be formatted. –Applying formatted output in a program. Format types Decimal integer - use %wd w: the width of field in characters d: decimal Floating point - use %w.pf w: the width of field in characters p: the number of digits after the decimal point f: fixed floating point 3

4
Class FormattedOutput (2) Use a format specifier of the form %nd to print out the numbers a1, b1, c1, d1 in the following way: 4 a1: 28 b1: 418 c1: -87 d1:-3007 Note: the rightmost digits of the numbers are aligned, and the width of the field containing the numbers is chosen such that the minus sign in the number is immediately to the right of the colon. Whats the value of n (i.e. the width of the field) ?

5
Class FormattedOutput (3) Next use a format specifier of the form %r.sf to print out the numbers a2, b2, c2, d2 in the following way: 5 a2: b2: c2: 0.01 d2: Note: the numbers are printed with the decimal points aligned and there are three spaces between the colon and the first digit of the number Whats the value of r? Whats the value of s?

6
Anatomy of Class FormattedOutput public class FormattedOutput { public static void main(String[] args) { // initialise the following variables int a1 = 28, b1 = 418, c1 = -89, d1 = -3007; double a2 = , b2= -1.2, c2 = , d2 = 587.2; System.out.printf("a1:%?d\n", a1); // Write more Java code to print out b1, c1, and d1 System.out.printf("a2:%?.?f\n", a2); // enter the ? values // Write more Java code to print out b2, c2, and d2 } 6 insert the correct value for ?

7
Class LexicographicOrdering Objectives –Use compareTo() method to compare the lexicographic ordering of two strings. –Understand the relational operators for comparing strings. compareTo() method Relational operators, >=, ==, != 7 string1.compareTo(string2) == 0 string1 and string2 are equal string1.compareTo(string2) < 0 string1 comes before string2 string1.compareTo(string2) > 0 string1 comes after string2

8
Lexicographic Ordering of Strings Dictionary order Java lexicographic ordering: –Uppercase letters precede lower case letters –Numbers precede letters –The space character precedes all printable characters 8

9
Anatomy of Class LexicographicOrdering public class LexicographicOrdering { public static void main(String[] args) { // Declare String variables a1, a2,..., a9 and b1,..., b9 String a1 = "Tom", b1 = "Jerry", a2 = "Tom", b2 = "Tomato"; // The statement below compares string a1 with string b1. // It prints a Boolean value true if string a1 precedes // string b1 in lexicographic order or if a1 is equal to b1. // Otherwise the Boolean value false is printed. System.out.println(a1.compareTo(b1) <= 0); 9

10
Anatomy of Class LexicographicOrdering (2) System.out.println(a2.compareTo(b2) <= 0); /* Continue in the same way as above to print * out the remaining 7 Boolean values for the * given pairs of strings. */ } // end of main method } // end of class LexicographicOrdering 10

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google