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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Introduction to Programming Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
Introduction to Programming Overview of Week 2 25 January Ping Brennan
Introduction to Programming Java Lab 7: Loops 22 February JavaLab7 lecture slides.ppt Ping Brennan
22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan
Introduction to Programming
Enter. The Scene Type text here Choice A1 Text for decisions Guidelines Guideline text Click on A, B or C A B C Choice B1 Choice C1 Click on A, B or C.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Air Force Institute of Technology Electrical and Computer Engineering
CS110 Programming Language I
Strings Testing for equality with strings.
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
 Pearson Education, Inc. All rights reserved Formatted Output.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP C Part III.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
A: A: double “4” A: “34” 4.
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
28 Formatted Output.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Line Continuation, Output Formatting, and Decision Structures
Formatted Output (printf)
Multiple variables can be created in one declaration
OUTPUT STATEMENTS GC 201.
Formatting Output.
System.out.println for console output
IDENTIFIERS CSC 111.
Introduction to Programming
Line Continuation, Output Formatting, and Decision Structures
An Introduction to Java – Part I, language basics
Introduction to Programming
Introduction to Java Applications
Data Types and Expressions
Introduction to Programming
Question 1a) What is printed by the following Java program? int s;
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
CS 1054 Introduction to Programming in Java
Presentation transcript:

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

Java Project Project Name: JavaLab4 2 FormattedOutput LexicographicOrdering

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

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) ?

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?

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 ?

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

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

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

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