You are on page 1of 7

INSTRUCTIONAL MODULE AND ITS COMPONENTS

(GUIDE)

COURSE CCS 3
DEVELOPER AND THEIR JOSELITO T. TAN
BACKGROUND Email:jttan@tsu.edu.ph
ANNA CAZANDRA DELOS REYES
Email:acdelosreyes@tsu.edu.ph
COURSE DESCRIPTION
COURSE OUTLINE Course Content/Subject Matter
Week 1 General Orientation and Introduction to Arrays
Week 2 Declaration and Creation of Arrays
Week 3 Array Processing and Array Class
Week 4 Strings
Week 5 String I/O
Week 6 String Manipulations
Week 7 String Manipulation Functions
Week 8 Review week and or submission of missed
assessments
Week 9 Midterm Exam (Written Exam)
Week 10 Creating Methods
Week 11 Calling Methods
Week 12 Passing by Value and by Parameter
Week 13 Java Swing
Week 14 Swing Components
Week 15 Discussion of contents of Final Requirement
(Group Case Study)
Week 16 Creation of Final Requirement (Group Case
Study)
Week 17 Submission of Peer evaluation
Week 18 Submission of Final Requirement (Group Case
Study)
One week
(or an
equivalent
of three
hours)

CHAPTER # 5
TITLE String I/O
I. RATIONALE
INSTRUCTION TO THE USERS Lectures are on this module. MS Teams will be used for showing the
actual demonstration of java programming.

At the start of the module, you are to take the pre-assessment test to see
how much information and knowledge you have about the java
programming.

As you go through the lessons, you can read, analyze and understand
that java programming is attractive as an alternative approach and how
does it differ from traditional programming languages.

A self-check test, exercises, or other means of assessing will help you


assess how you progress as you go through the module. The self-check
test questions and other activities in this module will be evaluated by
your teacher. These will be part of your formative evaluation/grade.

Reminder: All answers on your pretest, self-check test and other


activities must be written in separate paper.

DO NOT WRITE IN THE MODULE

Chapter 1 Basic Concepts and Computer Evolution 1


PRE-TEST Direction: Read the questions carefully. Write your answers on a
separate paper.

1. Write a program that will ask the user to input 3 students


name and course. Display the 3 students name along with
their course.

II. LEARNING At the end of the chapter, students are expected to:
OBJECTIVES a. Learn the process of accepting and display string data from
the user.

III. CONTENT
In this lesson we are gonna see how to accept input from user. We are using Scanner class to get the input. In the below
example we are getting input String and integer.

Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings.
It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios
where time is a constraint like in competitive programming.

To create an object of Scanner class, we usually pass the predefined object System.in, which represents the
standard input stream. We may pass an object of class File if we want to read input from a file.
 To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For example, to read
a value of type short, we can use nextShort()
 To read strings, we use nextLine().
 To read a single character, we use next().charAt(0). next() function returns the next token/word in the input
as a string and charAt(0) function returns the first character in that string.
Let us look at the code snippet to read data of various data types.

// Java program to read data of various types using Scanner class.


import java.util.Scanner;
public class ScannerDemo1
{
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);

// String input
System.out.println("Name: “);
String name = sc.nextLine();
System.out.println("Gender: “);
// Character input
char gender = sc.next().charAt(0);
System.out.println("Age: “);
// Numerical data input
// byte, short and float can be read
// using similar-named functions.
int age = sc.nextInt();
System.out.println("Mobile Number: “);
long mobileNo = sc.nextLong();
System.out.println("CGPA: “);
double cgpa = sc.nextDouble();

// Print the values to check if the input was correctly obtained.


System.out.println("Name: "+name);
System.out.println("Gender: "+gender);
System.out.println("Age: "+age);
System.out.println("Mobile Number: "+mobileNo);
System.out.println("CGPA: "+cgpa);
}
}

Chapter 1 Basic Concepts and Computer Evolution 2


String is an array of characters, represented as:

//String is an array of characters


char[] arrSample = {'R', 'O', 'S', 'E'};
String strSample_1 = new String (arrSample);

In this example, looping and array will be used in asking the user the size and dimension of the array. After which string
values will be ask to be inputted by the user and later on inputted string will be displayed.

Java String array is used to hold fixed number of Strings. String array is very common in simple java programs, specially
among beginners to java and to test some specific scenarios. Even java main method argument is string array – public
static void main(String[] args). So today we will look into different aspects of java string array with example programs.

 Java String array is basically an array of objects.


 There are two ways to declare string array – declaration without size and declare with size.
 There are two ways to initialize string array – at the time of declaration, populating values after declaration.
 We can do different kind of processing on string array such as iteration, sorting, searching etc.

Let’s go over java string array example programs now.

Java String Array Declaration


Below code snippet shows different ways for string array declaration in java.

Note that we can also write string array as String strArray[] but above shows way is the standard and recommended
way. Also in the above code, strArray is null whereas strArray1 value is [null, null, null].

Java String Array Initialization


Let’s look at different ways to initialize string array in java.

Chapter 1 Basic Concepts and Computer Evolution 3


Iterating over java string array
We can iterate over string array using java for loop or java foreach loop.

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

// create an array
String[] city = new String[] {“Tarlac City”, “Quezon City”,”Caloocan City”};

// access each array elements


System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + city[0]);
System.out.println("Second Element: " + city [1]);
System.out.println("Third Element: " + city [2]);

String[] city = new String[] {"Tarlac City","Quezon City","Caloocan City"};

// access each array elements


System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + city[0]);
System.out.println("Second Element: " + city [1]);
System.out.println("Third Element: " + city [2]);

}
}

Output:
Accessing Elements of Array:
First Element: Tarlac City
Second Element: Quezon City
Third Element: Caloocan City

Looping through an array of string

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

// create an array

Chapter 1 Basic Concepts and Computer Evolution 4


String[] sports = new String[] {"Basketball","Baseball","Swimming"};
int i;

// loop through the array


// using for loop
System.out.println("Using for Loop:");
for (i=0;i<sports.length;i++)
{
System.out.println(sports[i]);
}

}
}

Output:
Using for Loop:
Basketball
Baseball
Swimming

Example

import java.util.Scanner;
public class ArrayInput
{
public static void main(String args[])
{
int m, n, i, j;
Scanner sc=new Scanner(System.in);

System.out.print("Enter the number of rows: ");


//taking row as input
m = sc.nextInt();
System.out.print("Enter the number of columns: ");
//taking column as input
n = sc.nextInt();
// Declaring the two-dimensional matrix
String array[][] = new String[m][n];

// Read the matrix values


System.out.println("Enter the elements of the array: ");
//loop for row
for (i = 0; i < m; i++)
//inner for loop for column
for (j = 0; j < n; j++)
{
System.out.print(" Enter Value for elament "+ i +" " +j+": ");
array[i][j] = sc.next();
}

//accessing array elements


System.out.println("Elements of the array are: ");
for (i = 0; i < m; i++)
{
System.out.print("Row Number "+i+" :");
for (j = 0; j < n; j++)
//prints the array elements
System.out.print(array[i][j] + " ");
//throws the cursor to the next line
System.out.println();
}
}
}

Output

Enter the number of rows: 2

Chapter 1 Basic Concepts and Computer Evolution 5


Enter the number of columns: 3
Enter the elements of the array:
Enter Value for elament 0 0: 4
Enter Value for elament 0 1: 5
Enter Value for elament 0 2: 8
Enter Value for elament 1 0: 9
Enter Value for elament 1 1: 5
Enter Value for elament 1 2: 4
Elements of the array are:
Row Number 0 :4 5 8
Row Number 1 :9 5 4

Assigning string value using a class

In Java, an object is created from a class. We created the class named io, so now we can use this to create objects.
To create an object of io, specify the class name, followed by the object name, and use the keyword new:

Example:

public class io{

String fnm = new String("John");


String lnm = new String("Doe");

public static void main(String[] args)


{
io strname = new io();

System.out.println("First Name :"+strname.fnm);


System.out.println("Last Name :"+strname.lnm);

Output:
First Name :John
Last Name :Doe

Assigning value from object and displaying items through a class.

Example

public class name_string{


public name_string(String name, String lname) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
System.out.println("Passed Last Name is :" + lname );
}

public static void main(String []args) {


// Following statement would create an object myPuppy
name_string studnm= new name_string( "John","Doe" );
name_string myPuppy = new name_string( "Doe","John" );
}
}

Output:

Passed Name is :John


Passed Last Name is :Doe
Passed Name is :Doe
Passed Last Name is :John

IV. SYNTHESIS / This chapter focus on the following:


GENERALIZATION
• String processing

Chapter 1 Basic Concepts and Computer Evolution 6


 Assignment
 Scanner
 Printing/Displaying
 Class

V. EVALUATION Directions: Read the statement carefully. Write your answer on a


separate paper.

1. Write a java program that will use looping, array and string
datatype that will accept 10 students name. Each student will
be ask to input 3 grade. The system will then diplay the
students name, the 3 grades and the average of the grades.

VI. ASSIGNMENT / Direction: Read the questions carefully. Answer it on a separate


AGREEMENT paper.

1. Write a java program that will accept 10 employee name and display
those name in their original sorted form and in their reverse form.

Example:

Employee 1: Joe
Employee 2: Karen
Employee 3: Franz

Original Form:
Joe
Karen
Franz
Reverse Form
Franz
Karen
Joe
REFERENCES Java Programming: Concepts and Applications, 6e – Joyce
Farell

Chapter 1 Basic Concepts and Computer Evolution 7

You might also like