You are on page 1of 21

College of Computing Education

3rd Floor, DPT Building


Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

UNIT LEARNING OUTCOME (ULO)

Week ULO
1
Discuss the program structure and the basic elements of Java
2
programming and apply them appropriately in a program.
3
4 Use Control structures to efficiently implement branch and
5 looping algorithm in programs
6
7 Explore user-defined methods and ways of manipulating
8 records and to correctly apply all concepts in a self-developed
9 application

ULO 1 (WEEK 1-3)

Discuss the program structure and the basic elements of Java programming and apply them
appropriately in a program.

METALANGUAGE

In this ULO, you will encounter the following terms frequently:

Programming Language are languages that people us which are similar to human
language, which will be translated into machine code for the computer to understand.

Algorithm is a step-by-step problem-solving process in which a solution is arrived at in a


finite amount of time.

Flowchart is a graphical form of problem-solving process.

A Token is a smallest individual unit of a program.

Comments are denoted by // for a single line comment and /* at the start of a block of
statements to comment and */ to close the block of statements that you want to comment.

Identifiers are Nouns that describe identities of variables.

Data types specify the different sizes and values that can be stored in the variable.

Primitive type variables directly store data into their memory space.

Reference variables store the address of the object containing the data.
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

WEEK 3: INTRODUCTION TO OBJECTS AND INPUT/OUTPUT

ESSENTIAL KNOWLEDGE

There will be FOUR sections in this week’s learning:

1. Object and Reference Variables


2. Predefined Methods and Classes
3. Class String
4. Input and Output

1. Object and Reference Variables

There are two kinds of Java variables


• Primitive type variables
• Reference variables

When a reference variable of a class type is declared, memory space is allocated for
the data and an object of that class type is instantiated. The address of the object is
stored in a reference variable.

From the above diagram, the primitive type variables directly store data into their
memory space. However, the reference variables store the address of the object
containing the data.
An object is an instance of a class, and the operator new is used to instantiate an
object.

2. Predefined Methods and Classes

There are many predefined packages, classes, and methods in Java


• Library: collection of packages
• Package: contains several classes
• Class: contains several methods
• Method: set of instructions
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

To use a method, you must know


• Name of the class containing method (e.g Math)
• Name of the package containing class (e.g java.lang)

• Name of the method - (pow), it has two parameters


• Math.pow(x, y) = xy

• Example method call


import java.lang; //imports package
Math.pow(2, 3); //calls power method in class Math
• Dot (.) operator: used to access the method in the class

3. Class String

• String variables are also reference variables


• Given:
String name;
Similar statements:
name = new String("Lisa Johnson");
name = "Lisa Johnson";
• A String object is an instance of class String
• The address of a String object with the value "Lisa Johnson" is stored in
name
• String methods are called using the dot operator

Consider the following example

Sentence = “Programming with Java”;


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

Predefined methods that can be used for String class:


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

4. Input and Output

A program performs three basic operations:


• Gets data into the program
• Manipulate the data
• Output the results

There are three ways of input/output in Java:


• Console or Standard stream input/output
• Dialog Boxes
• File Handling

4.1 Console or Standard stream input/output

• java.util must be imported in order for the Scanner class to be


used.
• Standard input stream object: System.in
• Input numeric data to program
o Separate by blanks, lines, or tabs
• To read data:
1. Create an input stream object of the class Scanner
2. Use the methods such as next, nextLine, nextInt, and
nextDouble
b) Example

static Scanner console = new Scanner(System.in);


int feet;
int inches;

Suppose the input is

23 7

feet = console.nextInt(); //Line 1


inches = console.nextInt(); //Line 2

c) Standard output object: System.out


d) Methods
o print
o println
• Syntax
System.out.print(stringExp);
System.out.println(stringExp);
System.out.println();
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

• Commonly used escape sequences that can be used in output


statements

• Output can also be formatted using the printf statement


o A syntax to use the method printf to produce output on the
standard output device is:
o System.out.printf(formatString);
o or:
o System.out.printf(formatString, argumentList);
o formatString is a string specifying the format of the output,
and argumentList is a list of arguments
o argumentList is a list of arguments that consists of constant
values, variables, or expressions
o If there is more than one argument in argumentList, then the
arguments are separated with commas
o System.out.printf("Hello there!");
consists of only the format string, and the statement
o System.out.printf("There are %.2f inches in %d
centimeters.%n", centimeters / 2.54, centimeters);
consists of both the format string and argumentList
o %.2f and %d are called format specifiers
o By default, there is a one-to-one correspondence between
format specifiers and the arguments in argumentList
o The first format specifier %.2f is matched with the first
argument, which is the expression centimeters / 2.54
o The second format specifier %d is matched with the second
argument, which is centimeters
o The format specifier %n positions the insertion point at the
beginning of the next line
o A format specifier for general, character, and numeric types
has the following syntax:

o The expressions in square brackets are optional; they may


or may not appear in a format specifier
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

o The option argument_index is a (decimal) integer indicating


the position of the argument in the argument list
o The first argument is referenced by "1$", the second by "2$",
etc.
o The option flags is a set of characters that modify the output
format
o The option width is a (decimal) integer indicating the
minimum number of characters to be written to the output
o The option precision is a (decimal) integer usually used to
restrict the number of characters
o The required conversion is a character indicating how the
argument should be formatted

• Example
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

4.2 Using Dialog Boxes for input/output


• javax.swing needs to be imported for JOptionPane class to be
used
• Use a graphical user interface (GUI)
• Contained in package javax.swing
• Contains methods showInputDialog and showMessageDialog
• Syntax
o str = JOptionPane.showInputDialog(strExpression)
• Program must end with System.exit(0);
• Example of Dialog Box

Parameters for the Method showMessageDialog


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

JOptionPane Options for the Parameter messageType

JOptionPane Example

• As the inputs are string, conversion is required for calculations. A


string consisting of only integers or decimal numbers is called a
numeric string
• Integer, Float, and Double are classes designed to convert a
numeric string into a number
• These classes are called wrapper classes
• parseInt is a method of the class Integer, which converts a numeric
integer string into a value of the type int
• parseFloat is a method of the class Float and is used to convert a
numeric decimal string into an equivalent value of the type float
• parseDouble is a method of the class Double, which is used to
convert a numeric decimal string into an equivalent value of the type
double
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

• To convert a string consisting of an integer to a value of the type int,


we use the following expression:
o Integer.parseInt(strExpression)
o Integer.parseInt("6723") = 6723
o Integer.parseInt("-823") = -823
• To convert a string consisting of a decimal number to a value of
the type float, we use the following expression:
o Float.parseFloat(strExpression)
o Float.parseFloat("34.56") = 34.56
o Float.parseFloat("-542.97") = -542.97
• To convert a string consisting of a decimal number to a value of the
type double, we use the following expression:
o Double.parseDouble(strExpression)
o Double.parseDouble("345.78") = 345.78
o Double.parseDouble("-782.873") = -782.873
• String method format can be used to format the output

4.3 File input/output


• java.util needs to be imported in order to use the Scanner class
• File: area in secondary storage used to hold information
• You can also initialize a Scanner object to input sources other than
the standard input device by passing an appropriate argument in
place of the object System.in
• We make use of the class FileReader
• Suppose that the input data is stored in a file, say prog.dat, and this
file is on the floppy disk A
• The following statement creates the Scanner object inFile and
initializes it to the file prog.dat
• Scanner inFile = new Scanner (new FileReader("prog.dat"));

• Next, you use the object inFile to input data from the file prog.dat
just the way you used the object console to input data from the
standard input device using the methods next, nextInt, nextDouble,
and so on
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

• To store the output to a file of a program in a file, you use the class
PrintWriter
• Import java.io to use the PrintWriter class
• Declare a PrintWriter variable and associate this variable with the
destination
• Suppose the output is to be stored in the file prog.out
• Consider the following statement:
• PrintWriter outFile = new PrintWriter("prog.out");
• This statement creates the PrintWriter object outFile and associates
it with the file prog.out
• You can now use the methods print, println, and printf with outFile
just the same way they have been used with the object System.out
• The statement:
outFile.println("The paycheck is: $" + pay);
stores the output—The paycheck is: $565.78—in the file prog.out

• This statement assumes that the value of the variable pay is 565.78
• Closing the file; you close the input and output files by using the
method close
inFile.close();
outFile.close();
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

• Closing the output file ensures that the buffer holding the output will
be emptied; that is, the entire output generated by the program will
be sent to the output file
• If an input file does not exist, the program throws a
FileNotFoundException
• If an output file cannot be created or accessed, the program throws
a FileNotFoundException
• The method main does not need to handle the
FileNotFoundException exception, it will be included in a command
in the heading of the method main to throw the
FileNotFoundException exception
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

SELF-HELP

Predefined Methods Documentation

You can refer to https://docs.oracle.com/javase/7/docs/api/overview-summary.html to see the


available predefined packages, class and methods.

Take for eg. you want to find out the methods that are available for Scanner class.

At the hyperlink, click on java.util. This will bring you to the package java.util and the
description of the package. Under the Class Summary, click on Scanner. You will see the
details of the Class Scanner. It also has the descriptions of all the methods available under
this class.

Getting started on Java

You will need to use IDE (Integrated Development Environment) – either Eclipse or
NetBeans for doing the programming activities. Please refer to Self-Help section under
Week 2 if you have not already installed your IDE.

Using Eclipse
Launch Eclipse
Click File -> New -> Java Project
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

While Selecting your project from the package explorer window on the left,
click on File -> New -> Class

The following window will appear. Check the source folder and make sure it is the project
you have created. Type the name of your Java Class. Note: No spaces and special symbols.
Also, check the public static void main(String[] args) box in the method stubs.
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

You will see the screen below if you have successfully created your Java class.

Once you have completed your program, click on Run -> Run or click on Ctrl+F11.

Using NetBeans
Launch NetBeans.
Click on File -> New Project
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

Click Next

Enter name for the Project.


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

You will see this when you are done and you are ready to get going for your lab activities.
Once you have completed your program, click on Run -> Run File or click on Shift+F6.

You might also like