CS 01 Chapter 3
CS 01 Chapter 3
1
INPUTS AND OUTPUTS
Prepared by:
DANILYN A. FLORES
NOR-AINE M. CORPUZ
CS 01 1-2023-2024
LEARNING OUTCOMES 2
CS 01 1-2023-2024
GETTING TO KNOW THE SOFTWARE
INTERFACE 4
Desktop/Laptop: Smartphone:
1. NetBeans 1. Anacode
2. Eclipse 2. Java N-IDE
3. JCreator 3. AIDE
4. Notepad++ 4. DCoder
CS 01 1-2023-2024
GETTING TO KNOW THE SOFTWARE
INTERFACE 6
CS 01 1-2023-2024
GETTING TO KNOW THE SOFTWARE
INTERFACE 7
CS 01 1-2023-2024
CREATING SIMPLE PROGRAMS 8
CS 01 1-2023-2024
CREATING SIMPLE PROGRAMS 10
CS 01 1-2023-2024
CREATING SIMPLE PROGRAMS 11
CS 01 1-2023-2024
CREATING SIMPLE PROGRAMS 12
CS 01 1-2023-2024
CREATING SIMPLE PROGRAMS 13
Step 6: Debug your code
if the code has error/s, a list
will appear in the Build Output
Window
the trick in debugging is
solving the first error first in
case of multiple errors
in JCreator there is the ^
symbol that points to the line
where the bug is
a description of the error is
also provided
CS 01 1-2023-2024
CREATING SIMPLE PROGRAMS 14
For example, in the figure above, the ^ points to ‘c’ in “class” but the error in that line is the
‘h’ in “hello” because it should be ‘H’. The ^ is in the correct line where the error is just not
the correct character. Sometimes the error is in the line above the line with the ^.
CS 01 1-2023-2024
CREATING SIMPLE PROGRAMS 15
CS 01 1-2023-2024
DISPLAYING OUTPUT ON CONSOLE 16
System.out.print() is the
code to display anything in
the General Output
window.
whatever needs to be
displayed in any literal
either data or variable
should be inside the ( ).
in this example, the
variable sum is placed in
the ( ) because the output
should be the value of
sum.
CS 01 1-2023-2024
DISPLAYING OUTPUT ON CONSOLE 18
CS 01 1-2023-2024
DISPLAYING OUTPUT ON CONSOLE 19
in the example, the string “Sum =
“ is added to the identifier sum
separated by a +.
do not be confused with the use
of the + here. It can be an
arithmetic operator if placed
between 2 integer or floating
point literals but it can act as a
separator when placed between 2
different literals assigned as value
to a string variable or an output in
a single line of code.
CS 01 1-2023-2024
DISPLAYING OUTPUT ON CONSOLE 20
Another example here is formatting output using
tabs and new line.
Problem: Solve for the average grade of a BSCS or BSInfoSys student enrolled in the 1st Year
1st semester.
Problem Analysis: Program Design: Program Coding:
CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 22
Using JOptionPane
CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 23
import
javax.swing.JOptionpane;
indicates the use of the
JOptionPane of javax.swing in
the program
name=JOptionPane.showInput
Dialog (“Enter name:”); creates
an input dialog which will
display a dialog box with a
prompt message “Enter name:”,
a textbox, an OK, and a Cancel
button.
Whatever is typed in the
textbox will be assigned as the
value of the variable name. CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 24
message = “Hello “ + name + “!”;
assigns the string “Hello “, value of
the variable name and an
exclamation point as the value of
variable message.
JOptionPane.showMessageDialog(n
ull, message); displays a dialog box
which contains the value of the
variable message and an OK button
CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 25
CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 26
Another example:
int num1=Integer.parseInt(Joption
Pane.showInputDialog(“Enter a
number:”)); prompts the user for
an input that will be converted to
an integer value through the
parseInt() method, and will be
assigned as the value of variable
num1 same for num2.
for floating point inputs, use
Double.parseDouble().
CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 27
CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 28
Message and input dialog boxes can be customized wherein the dialog
title and message type can be changed. For example:
import javax.swing.JOptionPane;
public class KeyboardIO3{
public static void main (String[] args) {
int num1 = 0;
int num2 = 0;
num1 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a number: ",
"NUMBER", JOptionPane.PLAIN_MESSAGE));
num2 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter another
number: ", "NUMBER", JOptionPane.PLAIN_MESSAGE));
int sum = num1+num2;
String msg = "The sum is: " + sum;
JOptionPane.showMessageDialog(null, msg, "OUTPUT", 1); } }
CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 29
The line
customizes the input dialog box (Fig. 34) to have the title NUMBER instead of
Input and plain message as its type.
CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 30
The line
customizes the output dialog box to have the title OUTPUT instead of Message
and 1 as message type.
CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 31
LESSON ACTIVITY 3.2
Create programs following the PDLC for the problems below. Write or draw the
output of your programs using assumed input values.
1. The clerk of the department wants to have a way to input the basic
information of each new student such as full name, email address, contact
number, and complete home address and will display those data for
information purposes. If you are tasked to create a simple solution, how will
you do it?
Problem Analysis: Program Design: Program Coding:
Output:
CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 32
2. An instructor wants to solve the grades of his students in Computer
Programming through a computer program. He wants a way that all he has to
type is the student’s raw scores and the grade will be automatically computed
from all criteria. Refer to the data in the table below:
Criteria Percentage Total
Attendance 10 15
Assignment 15 50
Quiz 20 80
Laboratory Activity 25 100
Exam 30 100
Output:
CS 01 1-2023-2024
GETTING INPUT AND DISPLAYING OUTPUT
USING GUI 33
3. A businessman who owns a small company wants to calculate the net monthly
salary of each of his employees using only their names and number of days
worked as entries. Since all his employees are minimum wage earners, their
daily rate is 500 pesos. Each of them has a monthly deduction of 220 pesos for
PhilHealth and 330 pesos for SSS. What will be your solution to this problem?
Output:
CS 01 1-2023-2024
CHAPTER SUMMARY 34
CS 01 1-2023-2024
REFERENCES 35
CS 01 1-2023-2024
36
THANK YOU!
Stay safe. #WeHealAsOne
CS 01 1-2023-2024