Introduction to Programming 1 Laboratory Activity #01
Readings from the Java Tutorials: Learning the Language Basics
Focus on: Variables -> Primitive Data Types
Operators -> Assignment, Unary and Arithmetic Operators
1. Displaying information on screen
Load JCreator. Choose the open option by using the File menu or by clicking the Open icon. Browse to find
the folder containing the exercise files. Open Hello1.java.
Compile the file (Choose from the Build menu option or click the Compile File icon). The “Process Completed”
on the Output window at the bottom portion of the screen indicates that the program has compiled
successfully. You can open your folder to view its contents and check that Hello1.class is there.
Execute the program and observe what happens.
System.out.println() is a Java statement that displays on screen the sequence of characters (also referred
to as text or a string) inside the parentheses. Note that the string must be enclosed in quotation marks.
Observe the difference between System.out.print() and System.out.println().
Modify Hello1.java by adding other System.out.println() statements with your own choice of information to
display on screen. Ensure that there is a semi-colon (;) at the end of each statement.
2. Getting input using the keyboard
Open Hello2.java. Compile and execute (run) the program. You will have to type an answer to the questions
displayed on screen before the program can continue its execution.
String name = Keyboard.readString() “reads” the string that you typed and becomes the value of name.
int age = Keyboard.readInt() “reads” the number that you typed and becomes the value of age.
You can execute the program several times with different input.
Observe the lines in the program that display name and age. These are not enclosed in quotation marks
because the values associated with it depend on what you type on the keyboard when the program is
executed.
Modify Hello2.java by adding other “questions” that must be answered. Aside from int and String, try also
the other primitive data types as input. Change the Keyboard method accordingly depending on the data type
that you use (for example: Keyboard.readDouble if the data type used is double).
3. A Java program that solves a real-world problem
The sample program CellPhone.java is an implementation of the problem given below.
The remaining amount (balance) for a prepaid cellular phone, is computed by subtracting the cost of the phone usage from the
original phone load. The phone usage is based on the total cost of text messages and phone calls.
The cost of text messages is determined from the number of text messages sent and the cost of phone calls is determined
from the number of minutes of calls made.
For instance, assuming that one text message costs 2 pesos and a phone call costs 10 pesos per minute, and given that:
the load is 500
number of text messages is 50
number of minutes of calls made is 5
the following information can be determined
cost of text messages will be 100
cost of calls made will be 50
phone usage will be 150
balance will be 350
Depicted below are sample outputs when the program is executed (the items in red bold characters are inputted by the user,
while the items in blue bold characters are calculated and outputted by the program):
Load: 500 Load: 300
Number of messages: 50 Number of messages: 5
Calls in minutes: 5 Calls in minutes: 10
Cost of Messages: 100.00 Cost of Messages: 10.00
Cost of Calls: 50.00 Cost of Calls: 100.00
Total Usage: 150.00 Total Usage: 110.00
Balance: 350.00 Balance: 190.00
Compile and run the program and use the input information shown above. Try other combinations of input.
Modify the program by changing the values of perTxt and perMin (Remember, these variables may have
decimal values). Compile and run the program and observe the results.
4. More changes!!!
At this point, the cost of each text message and the cost of a call are fixed.
Modify the program again such that even the values of perTxt and perMin will be typed in by the user. Your
sample output should look something like the ones shown below:
Load: 300 Load: 50
Cost per text: 1.00 Cost per text: 1.20
Cost per minute: 5.50 Cost per minute: 7.50
Number of messages: 89 Number of messages: 30
Calls in minutes: 32 Calls in minutes: 0
Cost of Messages: 89.00 Cost of Messages: 36.00
Cost of Calls: 176.00 Cost of Calls: 0.00
Total Usage: 265.00 Total Usage: 36.00
Balance: 35.00 Balance: 14.00
Load: 300 Load: 50
Cost per text: 1.00 Cost per text: 1.20
Cost per minute: 5.50 Cost per minute: 7.50
Number of messages: 89 Number of messages: 30
Calls in minutes: 32 Calls in minutes: 0
Cost of Messages: 89.00 Cost of Messages: 36.00
Cost of Calls: 176.00 Cost of Calls: 0.00
Total Usage: 265.00 Total Usage: 36.00
Balance: 35.00 Balance: 14.00