Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Description
This laboratory activity reviews to the students the basic concepts in program logic
formulation and computer programming. Four main tasks were designed to measure the
knowledge and skills of the students on the basic programming concepts through a set of
hands-on activities and knowledge checks.
Objectives
In this laboratory activity, the students are expected to:
review the basic concepts in Java programming;
write simple programs using Java as programming language;
print outputs on a command-line interface; and
determine how Java stores different types of data.
Pre-Lab Activity
At the start of the activity, the students are expected to recall the:
anatomy of a Java program;
writing, compiling, debugging, and running Java programs;
submitting Java codes on Codeboard and Schoology; and
basic mathematical operations.
In-Lab Activity
This in-lab activity is composed of four tasks. The first task reviews the students on the basic
input and output statements in Java, which include the implementation of the Scanner class.
The second task reviews the student on the different primitive data types in Java and the
mechanics in transforming one type into another. This transformation, called type casting,
covers both the implicit and the explicit type coercions. The third task reviews the students
on identifiers, including variables and constants, specifically on the convention that Java
promotes in naming these identifiers. The last task reviews the students on the different
mathematical operators in Java, particularly on the five arithmetic operators, and the order
of precedence in deriving the value of an equation.
1
Task 1.1: Input and Output in Java
This task is composed of three machine problems: two were designed to assess the students’
understanding on the basic output statements in Java programming, while the last one was
designed to assess the students’ understanding on the built-in input statement in Java using
the Scanner class.
3. Compile and run the program. It should produce the following output:
*
***
*****
*
Screenshot 1.1: Tree.java Sample Run.
Note: We made sure that the program we have provided has no errors in it. However,
just in case you encountered any errors during your attempt to run the program, ask the
help of your instructor. The error might be IDE-related.
4. Your task is to produce the same output but using four System.out.print().
2
Machine Problem 1.1.2 | OneLinerTree.java
This machine problem assesses the students’ familiarity with another output statement
in Java – System.out.println(). The main task here is to produce an output the
same with Tree.java but using one System.out.println()only.
From Until
Interpretation
Hour Minute Hour Minute
00 00 11 59 MORNING
12 00 17 59 AFTERNOON
18 00 23 59 EVENING
Table 1.1: Time Range and Interpretation.
3
TASK 1.2: Data Types and Type Casting
This task is composed of three programming problems: one problem was designed to assess
the students’ knowledge in storing values of different types, another assesses the students’
familiarity with explicit type casting, while the last problem focuses on the utilization of
existing classes to derive numbers from Strings.
4
Machine Problem 1.2.3 | StringToInt.java
This machine problem assesses the student’s familiarity in converting a String to a
number. The main task of the students is to write a program that does the following:
5
1. Get an integer input from the user. This input represents the user’s total number
of hours worked in a month.
2. Get a double input from the user. This input represents the pay per hour.
3. Calculate the total salary of the user.
4. Display the result according to the following sample run:
1. Get a numeric input from the user. This input is the length of the adjacent side
of the right triangle.
2. Get another numeric input from the user, representing the base.
3. Compute for the hypotenuse using the following formula: 𝑎2 + 𝑏 2 = 𝑐 2
4. Display the result.
In Java, you can use the JOptionPane class to quickly create and customize several different
kinds of dialog boxes such as laying out standard dialogs, providing icons, specifying the
dialog title and text, and customize the button text. The algorithm is as follows:
6
1. Get a numeric input (length of the adjacent side of the right triangle) from the user using
an input dialog box, as shown below. By default, the input is of String type.
2. Convert the input from String to double, then store it to a double variable a.
3. Get another number input (length of the base) from the user using another input dialog
box.
4. Convert the input from String to double, then store it to b.
5. Write a method getSquare(x) that will return the square of x, where x is a numeric
value. Use this method to get the square of both a and b.
6. Write a method getSum(x,y) that will return the sum of x and y. Use this method to
solve for the sum of a and b.
7. Use the Math.sqrt(x) to solve for the square root of the sum of a and b. Store the result
to a double variable c, representing the hypotenuse.
8. Display the result using a message dialog box.
Knowledge Check
1. What is the difference between println() and print()?
2. Enumerate the different escape sequences and their functions.
3. What are the commands used to accept numeric input from the user?
4. What is the difference between Scanner.next() and Scanner.nextLine()?
7
5. Scanner class has no method that accepts a character input from the user. What if
your program requires a character input, how would you resolve it?
6. What are the other usages of Scanner aside from accepting input from the console?
7. What is the difference between implicit and explicit type casting? Provide an example
for each type casting (excluding the examples in this activity).
8. What is the basis of the integer values of characters? What if you want to associate
A=1, B=2, C=3, and so forth. How will you use the integer values of char to solve this?
9. What is the difference between division, integer division, and modulo division?
Provide an example code for each division.
10. Can we use the Calendar class to do something else aside from getting the current
system time? Support your answer.
11. Write a Java program that accepts a numeric input for radius and computes for the
area of a circle, as shown in the sample run below:
12. Write a Java program that accepts a character input from the user and converts it to
an integer. Note that a character has a corresponding integer value. For example
Enter a character: A
The integer value of A is 65
Screenshot 1.11: CharToInt.java Sample Run.
13. Based on the previous solution, write a Java program that accepts a single three-letter
word from the user and then computes for the sum of the integer values of all letter.
A sample run is shown below:
8
Rubrics
TASK 1.1 – Input and Output in Java
MP 1.1.1 3pts
MP 1.1.2 3pts
MP 1.1.3 8pts
TASK 1.2 – Data Types and Type Casting
MP 1.2.1 3pts
MP 1.2.2 3pts
MP 1.2.3 5pts
TASK 1.3 – Variables 10pts
TASK 1.4 – Mathematical Operators
MP 1.4.1 5pts
MP 1.4.2 10pts
TOTAL 50 pts
POST-LAB ACTIVITY
Program Execution 10 pts
Correctness 10 pts
Design of Output 8 pts
Design of Logic 10 pts
Standards 7 pts
Comments/Documentation 5 pts
TOTAL 50 pts
References
1. Farrell, J. (2016). Java Programming, 8th Ed. Cengage Learning.
2. Farrel, J. (2015). Programming Logic and Design, 8th Ed. Cengage Learning.
3. https://www.ducksters.com/kidsmath/pythagorean_theorem.php
4. https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html