Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

QUARTER 3

MODULE 7
Computer Programming (Java) – Grade 12
Quarter 3 – Module 7: String Formatting in Java

First Edition, 2020

Republic Act 8293, Section 176 states that no copyright shall subsist
in any work of the Government of the Philippines. However, prior approval of
the government agency or office wherein the work is created shall be
necessary for exploitation of such work for profit. Such agency or office may,
among other things, impose as a condition the payment of royalties.

Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand


names, trademarks, etc.) included in this module are owned by their
respective copyright holders. Every effort has been exerted to locate and seek
permission to use these materials from their respective copyright owners. The
publisher and authors do not represent nor claim ownership over them.

Published by the Department of Education - Schools Division of Pasig


City

Development Team of the Self-Learning Module


Writer: Paola T. Galario
Editor: Mrs. Lerma O. Cantanero
Reviewers: Mrs. Rowena O. Dimagiba
Illustrator:
Layout Artist:
Management Team: Ma. Evalou Concepcion A. Agustin
OIC-Schools Division Superintendent
Aurelio G. Alfonso EdD
OIC-Assistant Schools Division Superintendent
Victor M. Javeña EdD
Chief, School Governance and Operations Division and
OIC-Chief, Curriculum Implementation Division

Education Program Supervisors

Librada L. Agon EdD (EPP/TLE/TVL/TVE)


Liza A. Alvarez (Science/STEM/SSP)
Bernard R. Balitao (AP/HUMSS)
Joselito E. Calios (English/SPFL/GAS)
Norlyn D. Conde EdD (MAPEH/SPA/SPS/HOPE/A&D/Sports)
Wilma Q. Del Rosario (LRMS/ADM)
Ma. Teresita E. Herrera EdD (Filipino/GAS/Piling Larang)
Perlita M. Ignacio PhD (EsP)
Dulce O. Santos PhD (Kindergarten/MTB-MLE)
Teresita P. Tagulao EdD (Mathematics/ABM)
Printed in the Philippines by Department of Education – Schools
Division of Pasig City

Computer
Programming

QUARTER
+ 3
MODULE

7 String Formatting in Java


Writer : Paola T. Galario
Editor : Mrs. Lerma O. Cantanero
Reviewers : Mrs. Rowena O. Dimagiba

Introductory Message

For the Facilitator:


Welcome to the Computer Programming (Java) 12 Self-Learning Module
on String Formatting in Java!
This Self-Learning Module was collaboratively designed, developed and
reviewed by educators from the Schools Division Office of Pasig City headed
by its Officer-in-Charge Schools Division Superintendent, Ma. Evalou
Concepcion A. Agustin, in partnership with the City Government of Pasig
through its mayor, Honorable Victor Ma. Regis N. Sotto. The writers utilized
the standards set by the K to 12 Curriculum using the Most Essential
Learning Competencies (MELC) in developing this instructional resource.
This learning material hopes to engage the learners in guided and
independent learning activities at their own pace and time. Further, this also
aims to help learners acquire the needed 21st century skills especially the 5
Cs, namely: Communication, Collaboration, Creativity, Critical Thinking, and
Character while taking into consideration their needs and circumstances.
In addition to the material in the main text, you will also see this box
in the body of the module:

Notes to the Teacher


This contains helpful tips or strategies
that will help you in guiding the
learners.

As a facilitator you are expected to orient the learners on how to use


this module. You also need to keep track of the learners' progress while
allowing them to manage their own learning. Moreover, you are expected to
encourage and assist the learners as they do the tasks included in the module.
For the Learner:
Welcome to the Computer Programming (Java) 12 Self-Learning Module
on String Formatting in Java!
This module was designed to provide you with fun and meaningful
opportunities for guided and independent learning at your own pace and time.
You will be enabled to process the contents of the learning material while
being an active learner.
This module has the following parts and corresponding icons:

Expectations - This points to the set of knowledge and


skills that you will learn after completing the module.

Pretest - This measures your prior knowledge about the


lesson at hand.

Recap - This part of the module provides a review of


concepts and skills that you already know about a previous
lesson.

Lesson - This section discusses the topic in the module.

Activities - This is a set of activities that you need to


perform.

Wrap-Up - This section summarizes the concepts and


application of the lesson.

Valuing - This part integrates a desirable moral value in


the lesson.

Posttest - This measure how much you have learned


from the entire module.
EXPECTATIONS
After completing this lesson, you should be able to:

1. describe the String Formatting in Java,


2. understand the use of String Formatting in coding, and
3. apply the concept of String Formatting in accordance to Java
framework.

PRETEST
DIRECTIONS: Identify the following Java string Format. Choose your answer
inside the box.
Octal number Floating Point %d Unicode character
Hex String of value from hashcode() method

Format Specifier Data Type Output


%c Character
Integer Decimal Integer
%h Any type
%a Returns Hex output of
floating point number
%o Integer

RECAP

DIRECTIONS: Write True if statement is correct and False if it is not. Write


your answer on the space provided.

________ 1. The Don't Repeat Yourself (DRY) principle is a common principle


across programming paradigms.
________ 2. Only one thing is constant in software field and that is "Change".
________ 3. The benefit of the Delegation principle is the duplication of code
and easy to modify behavior.
________ 4. Design Principles are generalized pieces of advice or proven good
coding practices.
________ 5. One should often prefer inheritance over composition when
designing their systems.
LESSON
String Formatting

Programming Languages have a long tradition of formatting Strings.


Over 60 years ago, Fortran was released with the format keyword:

shorturl.at/kqzRY

The more commonly known C-style format strings, used by the printf,
and Java’s String.format originated from BCPL (Basic Combined Programming
Language).

shorturl.at/kqzRY

Java wasn’t incepted with string.format. It took eight (8) years until the
release of Java 1.5 for it to be included.

Java String format()

The java string format() method returns the formatted string by given
locale, format and arguments. If you don't specify the locale in String.format()
method, it uses default locale by calling Locale.getDefault() method. The
format() method of java language is like sprintf() function in c language and
printf() method of java language.

Internal Implementation

shorturl.at/kqzRY

Signature – there are two types of string


format() method:

shorturl.at/kqzRY
Parameters –
locale : specifies the locale to be applied on the format () method.
format : format of the string.
args : arguments for the format string.

Returns – formatted strings

Throws –
NullPointerException : if format is null.
IllegalFormatException : if format is illegal or incompatible.
Java String format() Method Example

shorturl.at/kqzRY
Java String Format Specifiers
Here, we are providing a table of format specifiers supported by the Java
String.

shorturl.at/kqzRY

ACTIVITIES
DIRECTIONS: Create a simple code/program that will show an output of at
least 3 different Java String Format Specifiers which consist of Decimal
Floating point, Character and Integer Data type.

RUBRICS:
10 8 5 1
The program The program The program None matches
showed at least 3 showed at least showed at least the 3 different
different Java 2 different Java 1 different Java Java String
String Format String Format String Format Format.

WRAP-UP

DIRECTIONS: Identify the following Java String Formats that will show the
same output at the end. Write your answers on the space provided.

Public class Main


{
public static void main(String[] args) {
String var1 = String.format(“My Full Name is ___”,”Juan Dela
Cruz”);
String var2 = String.format(“____”,704);
String var3 = String.format(“____”,”true”);
String var4 = String.format(“____”,’c’);

System.out.println(var1);
System.out.println(var2);
System.out.println(var3);
System.out.println(var4);

}
}

VALUING
DIRECTIONS: Answer the following questions below, and write your answer
in the space provided.

1. Based on what you have learned in this lesson, what do you think is
importance of Java String formatting in your program? And why do you
think you must consider these things in your program?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________

POST-TEST
Modified True or False.
DIRECTIONS: Write TRUE if the given words are correct but if it is false,
change the underlined word or group of words to make the statement true.

_________ 1. locale : format of the string.


________ 2. %b : decimal number
________ 3. %t : hex string
________ 4. %e : decimal number in scientific notation.
________ 5. Returns : Formatting string
KEY TO CORRECTION
5. Octal number
4. floating point (Except Big decimal) 5. FALSE
method 4. TRUE
3. Hex string of value from hashcode () 3. FALSE
2. %d 2. TRUE
1. Unicode character 1. TRUE
PRE TEST RECAP

5. TRUE
4. %c 4. TRUE
3. %b 3. FALSE – DATE/TIME
2. %d 2. FALSE - %f
1. %s 1. FALSE – FORMAT
WRAP UP POST TEST

References
Websites:
Java String Format, Accessed Feb 2, 2021, 12:30:05 PM,
https://www.javatpoint.com/java-string-format

String.Format Java Example, Accessed Feb 2, 2021, 3:33:00 PM,


https://examples.javacodegeeks.com/string-format-java-example/

Java String Format Accessed Feb 3, 2021, 10:15:01 AM,,


https://www.journaldev.com/17850/java-string-format

You might also like