Sei sulla pagina 1di 9

LABORATORY ACTIVITY 01 3 HOURS

The Basic Concepts

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.

Machine Problem 1.1.1 | Tree.java


This machine problem assesses the students’ familiarity with the one of the two
commonly used output statements in Java – System.out.print(). Moreover, this
machine problem covers escape sequences, such as \n. Perform the following steps
to complete this machine problem:

1. Download Tree.java from our course page on Schoology.


2. Open the file in IntelliJ IDEA. It should contain the following code:

public class Tree{


public static void main(String[] args){
System.out.println(" *");
System.out.println(" ***”);
System.out.println("*****");
System.out.println(" *");
}
}
Source Code 1.1: Tree.java.

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.

Machine Problem 1.1.3 | Greeting.java


This machine problem assesses the students’ familiarity with the input statement in
Java using the Scanner class. Moreover, this machine problem introduces to the
students the usages of the Calendar class, especially in determining and getting the
current system time. The program should consider the following requirements:

1. Use a Scanner object to get the user’s name.


2. Store it into a String variable named name.
3. Use the Calendar class to get the current system hour and minute. Consider
the following conditions:

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.

4. Greet the user using the following template:

Good <greeting>, <name>!


Screenshot 1.2: Template of the Output for Greeting.java.

5. Refer to the following sample run:

What is your name? John


Good evening, John! It’s 7:56PM.
Screenshot 1.3: Greeting.java Sample Run.

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.

Machine Problem 1.2.1 | Summary.java


This machine problem assesses the students’ understanding on the primitive data
types of Java, such as int and char. The main task of the students is to write a Java
program named Summary that does the following requirements:

1. Get the user’s name and store it to a String name.


2. Get the user’s middle initial and store it to char mi.
3. Get for the user’s age and store it to int age.
4. Display the summary according to the following sample runs:

What is your name? John


What is your middle initial? M
How old are you? 20

Hi, John M.! How are you?


Wow, time flies fast! You’re turning 21 soon...
Well, it’s nice to see you again!
Screenshot 1.4: Summary.java Sample Run.

Machine Problem 1.2.2 | DoubleToInt.java


This machine problem assesses the students’ familiarity with the explicit type casting
in Java. The main task of the students is to write a program that does the following:

1. Get an input number of type double from the user.


2. Convert the input into an integer.
3. Display the result according to the following sample run:

Enter a number of type double: 34.6


34.6 is 34 when casted to integer.
Screenshot 1.5: DoubleToInt.java Sample Run.

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:

1. Get an input number of type String from the user.


2. Get another input number of type String from the user.
3. Convert both inputs into integers.
4. Add both integer values.
5. Display the result according to the following sample run:

Enter a number of type String: 45


Enter anther number of type String: 65
The sum of 45 and 54 is 110.
Screenshot 1.6: StringToInt.java Sample Run.java

TASK 1.3: Variables


This task has one machine problem only, which was designed to assess the students’
familiarity with the rules in naming variables in Java. Perform the following steps:

1. Download Variable.java from our course page on Schoology.


2. The program contains 10 bugs. Your task is to search for this bugs and resolve them
without using an IDE.
3. Once you’re done, upload the program back to Schoology.

TASK 1.4: Mathematical Operations


This task is composed of two programming problems, each was designed to assess the
students’ understanding about the different mathematical operations in Java and their
applications. Moreover, this machine problem requires the students to learn how to use the
Math class to perform complex mathematical computations.

Machine Problem 1.4.1 | Salary.java


This machine problem assesses the students’ ability to write a simple Java program that
uses arithmetic operators in solving computing problem. The main task of the students
is to write a Java 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:

How many hours have you worked this month? 20


What is your rate per hour? 120.00
Your total salary this month is 2400.00
Screenshot 1.7: Salary.java Sample Run.

Machine Problem 1.4.2 | Pythagorean.java


This machine problem assesses the students’ knowledge and skills in solving complex
mathematical problems that include complex computations, such as square and
square root. Students are required to study the Math class of Java to complete this
machine problem. The program should do the following:

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.

Post-lab Activity: Pythagorean Theorem GUI


Previously, while working on Machine Problem 1.4.2, you have studied and implemented a
Java program that computes for the hypotenuse using the Pythagorean Theorem. In this
practical assignment, your main objective is to restructure your solution on MP 1.4.2. The
formula and mechanics are still the same. However, instead of using the Scanner class to
get the inputs from your user and the println() command to display an output message
on the screen, this time, you will be using dialog boxes for both input and output mechanics
on your program.

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.

Screenshot 1.8: Input Dialog Box for the Adjacent Side.

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.

Screenshot 1.9: Output 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:

Enter radius of a Circle: 16


The computed area is 804.2496
Screenshot 1.10: CircleArea.java Sample Run

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:

Enter a word: ACE


A = 65, C = 67, E = 69
The computed sum is 201
Screenshot 1.12: ThreeLetterSum.java Sample Run.

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

Potrebbero piacerti anche