Lab 1 - Java and Processing Basics
The purpose of this lab is to get you used to using jGRASP and Processing, to start experimenting with Java expressions,
and to understand some of the issues involved in representing and manipulating data with a computer.
There is nothing to submit for this lab, but you will need paper and a pencil for scratch work.
Do not rush through this lab. It is not essential that you complete the lab, but it is important
that you understand each step before proceding to the next.
Whatever you do not complete in lab today, you should try and complete on your own or with your lab partner in the next few days.
Rules For The Lab
You will play two different roles in the lab. You will either be the
driver or navigator.
- driver: The person typing at the keyboard.
- navigator: The person watching for mistakes, making suggestions,
and thinking ahead.
Important: The navigator must not touch the keyboard or the mouse.
If the navigator either types or moves the mouse, the navigator will get a
zero for the lab.
You have been assigned identifiers s1 and s2 when you
entered the lab. At the start of the lab, s1 will be the driver and
s2 will be the navigator. Your roles will switch during the lab.
Task 1: Introductions
Your first task is to introduce yourself to your lab partner.
Tell your lab partner your name and how your first week of the semster is going.
Task 2: Starting jGRASP
One of you must log onto the computer now.
jGRASP is what is known as an integrated development environment
(or IDE for short) for Java. An IDE is an application that assists programmers
in writing computer programs. There are a large number of IDEs available
for many different languages, and we will be using jGRASP for writing
Java in our class because jGRASP is particularly good for new programmers.
In this lab, you will be entering code fragments in the Interactions Pane
of jGRASP. At the bottom of the jGRASP window, you should see several tabs.
Click on the one that is labeled Interactions. You should see
Welcome to jGRASP.
>
You will enter the code fragments at the > prompt.
Try it now. Type:
true
at the > prompt and press the Enter key. The interactiosn pane should respond with
true
The Interactions Pane is the reason jGRASP is a very nice IDE for
beginning programmers. You can enter pieces of Java code into the
Interactions Pane in order to test the code and see what happens. This way,
you can get started working in Java without having to write an entire Java
program.
What you are to do
Follow the following procedure through the lab.
- Read the question/problem. If the question asks you to guess what will happen before you type the expressions
into jGRASP, then work with your lab partner to guess what will happen
- Type the expressions into the Interactions Pane of jGRASP and see what happens.
- Discuss the results with your lab partner. See if you can come up with a reason why you got the results given.
If the results are different than what you expected, try to figure out why that is the case.
- Click on the explanation link for the problem and read an explanation for the actual behavior of the
code fragment.
- After every step in Part 1, switch roles between driver and navigator.
Part 1
All programming languages have their own quirks with how they manipulate numbers and other data. In this lab, you will
begin exploring the syntactic form of Java expressions as well as their semantic meaning.
The questions below cover a lot of different parts of Java. Don't worry
about trying to remember all the syntax. We will be covering this
in more detail later. Right now, just focus on the behavior of the
different Java expressions below.
- Consider the following three expressions.
7 + 5
183 - 97
183 - -97
What do you think the values of each will be if you enter them into
the interactions pane? Discuss this with your lab partner.
Now enter each of the expressions one at a time into the interactions pane. (Press the Enter key after each expression).
Did you get the results you expected? Discuss what you think the operators + and - do. After you finish
discussing, read
this explanation to see if your answer matches mine.
- What do you think the result of each of the following two expressions is? Discuss this with your lab partner.
10 * 20 + 2
-10 * (20 + 2)
Now, enter the expressions, one at a time, in the interactions pane and see if the values returned match what you expect.
Discuss what you think the * operator does and what the effect parentheses (...) have. Then read
this explanation to see if you are correct.
- Now what do you think the result of this expression will be?
13 / 5
Once you have your answer, type it in and see. Did you get what you expected? Now try typing in these expressions
13 % 5
21 / 4
21 % 4
-10 % 3
10 % -3
Try to explain what the operators / and % do. Discuss with your lab partner and then read
this explanation.
- What do you think this expression will return?
-2147483648 - 1
Once you and your lab partner decide on the value, type it in the interactions pane and see. Did you get what you expected?
Now try
2147483647 + 1
Discuss the results with your lab parter. Can you figure out what is going on? Here is a hint. Type each of the following
four expressions into the interactions pane and see their results.
Integer.MAX_VALUE
Integer.MAX_VALUE + 1
Integer.MIN_VALUE
Integer.MIN_VALUE - 1
Discuss with your lab partner what is going on, and then read
this explanation.
- What do you think the result of the following expressions is?
13.0 / 5.0
Type the expression into the interactions pane to see if you are correct. Discuss with your lab partner what is going
on and then read
this explanation.
- For each of the following expressions, figure out what the values should be. Then type the expressions into
the interactions pane to see if you are correct.
3 + 5.5
3.0 / 5
2. / 5
(10.0 / 2) + 3
(1 / 3) + 5.0
Did the values returned match what you expected? Can you and your lab partner come up with a rule for determining what the
values should be? Once you think you have the rule, read this explanation.
- Do you remember how to convert between Celsius and Fahrenheit? You multiply the Celsius value by 9/5 and then
add 32. Try out the expression below for converting
from 100 degrees Celsius to Fahrenheit.
9/5 * 100 + 32
Did you get the proper temperature? (Hint: No.) Why did the expression not give you the proper value? Make the necessary
change and then
see the solution.
- What do you think the result of this expression will be?
2.3 + 1.3
Type the expression into the interactions pane and see. Is it what you expected? Discuss with your lab partner what is
going on and then read
this explanation.
- Type the following expressions one at a time into the interactions pane.
int x
x = 5
x
x + 1
x - 1
Discuss with your lab partner what is going on. Then read
this explanation.
- Type the following expressions one at a time into the interactions pane.
double y
y = 3.0
y
y * 2.0
y / 2.0
Discuss with your lab partner what is going on. Then read
this explanation.
- Now type in the following expressions one at a time.
double a
a = 10
a
5 / a
int b
b = 3.0
Discuss with your lab partner the results of each expresion. Can you come up with an explanation for why each value was
returned from the expressions? Then read
this explanation.
- You have used the = operator a few times. Type in these additional expressions.
int w
w = 7
w
w = -10
w
int val
val = w + 5
val
w
Now, discuss with your lab partner what the = operator does. How is this different from "mathematical" equality?
Then read
this explanation.
- Are you getting the hang of variables and assignments? If so, work with your lab partner
to guess the values returned by the following
expressions before you type them into the interactions pane.
int r
r = 5
r
r = r + 1
r
Type the expressions in. Where you correct? Discuss why you were or were not correct with your lab partner before reading
this explanation.
- Type in these expressions:
int q
q = 10
q
q == 11
q == 10
What is the purpose of the == operator? How does this differ from the = operator? When you think you know,
read
this explanation.
- A formula for converting from Celsius to
Fahrenheight would be more interesting if it worked for other
temperatures than just 100. Let's use a variable to store different Celsius values. Try out the expression below.
9.0/5.0 * celsius + 32
What happened? Now try:
int celsius
celsius = 100
9.0/5.0 * celsius + 32
celsius = 32
9.0/5.0 * celsius + 32
What can you conclude? Discuss this with your lab partner then read
this explanation.
- Because we declared the variable celsius with the type int, we can only
store integers in celsius. It would be nice to convert non-integer temperatures so let us change the celsius
variable to store floating point numbers. Type in the following.
double celsius
What happened? Press the Reset button at the top of the jGRASP window and type
double celsius
celsius = 25.5
9.0/5.0 * celsius + 32
Discuss this behavior with your lab partner. Can you figure out why the Java expressions behaved as they did? Then read
this explanation.
- Do you remember the rules for what types of values can be assigned to variables? Figure out the result
of each of the expressions below before you type them into the interactions pane.
int x
x = 5
x = 3.1415
double y
y = 7
x
y
y = x
x
y
x = y
x
y
Did you get the results you expected? Can you come up with the rule for when you can assign a value to a variable? Discuss
with your lab partner and then read
this explanation.
- Suppose you only need the integer portion
of a double. You already know that assigning a
double to an int causes problems. You can
reassure Java that you mean to assign the integer portion of
the variable dNum below by using what is called a type cast.
What happens to
dNum after the type cast, and why?
int num
num = 5
double dNum
dNum = 7.6
num = dNum
num = (int) dNum
num
dNum
Discuss the behavior with your lab partner and then read
this explanation.
- You have seen a bunch of different operators. Now let us combine them.
With your lab partner, guess the value of each expression before you type it in. Were
you correct?
3 + 5 * 2
5 * 2 + 3
4 / 5 - 2
2 * 4 - 3 / 2.0
(int) 3.0 / -5
int num
num = 5 + 12 / 6 % 4
Can you determine the rules of precedence (the order operators are applied) in an expression?
Discuss with your lab partner and then read
this explanation.
- You have seen two types, int and double, but there are more.
The char type is for characters. Type in the following expressions.
char c
c = 'a'
c
int d
d = c
d
c = d + 1
Discuss with your lab partner why you get the results for each expression. Can you come up with a way change the last
expression so that the value d + 1 can be stored in the variable c? Read
this explanation and solution.
- Here is another type, called boolean. Type in the following expressions to see what you get.
3 < 5
boolean truthVal
truthVal = 3 < 5
truthVal
truthVal = 3 > 5
truthVal
Give a short description of what the expressions evaluate to and how boolean variables work. Then read
this explanation.
Task 3: A review of what you learned so far
Numbers such as 4, -3.1415 are Java
expressions. So is 4 * -3.14159 / 7. Each yields a value.
Pretty much any arithmetic you'd like to do with numbers can be put
into the form of a Java expression and evaluated.
Java primitive types (int, double, char,
and boolean, for now) store values, and there are certain
operations we can apply to values of these types.
Division of integers is not the same as division
of doubles, and operators ``do the right thing'' according to
the types they are operating on. You can mix different types (when
this makes sense) in a Java expression, and the result will default to
the widest (or most general) type. You can force Java to evaluate an
expression to the type you require using what is called a type cast.
Computers have finite amounts of memory and processing power, so the
values they can operate on have limits. Integers and numbers with
fractional parts are each stored in a fixed amount of space, so there
are limits to how large or small numbers can be, and how precisely
they can express a given value (for example, the number of significant
digits in a double is limited).
Once a variable has been has been declared (an identifier, or name,
consisting of alphabetic and numeric characters that is associated with a
value of certain type) it can repeatedly store values of that type and
be treated just like any other expression of that type. Values are
stored, or assigned, in a variable using the ``='' operator, which
looks confusingly like mathematical equality. Beware! Below is the
situation in pictures when we change the content of num from
3 to 5. In the second line, num appears to the right
of the ``='' operator, where it is evaluated but not changed,
and then the entire right-hand expression (with num's
contribution) is assigned to ...num:
int num = 3; |
|
the variable num is now storing the value 3 |
num = num + 2; |
|
the variable num is now storing the value 5 |
It is an error to try to use a variable before you have stored
anything in it.
Mathematical operations are carried out in a particular order. If you
are uncertain, you should look up the precedence of operations in a
Java manual or verify the precedence you expect in the jGRASP
interactions pane. Precedence can be changed by using parentheses to
force Java to evaluate certain parts of an expression ahead of others.
Using jGRASP. Java and Java as a calculator
In each of the following steps, complete the task, verify your solution is correct, then show your instructor.
Switch driver/navigator roles in each of the following steps.
- Use data available at www.x-rates.com to write a valid Java expression
that converts US dollars to Euros.
Write another Java expression that converts Canadian dollars to US dollars.
Use variables to hold the based and converted currency values.
You can use the up-arrow in jGRASP. Java to edit a previous line.
-
Create two variables that represent the length and width of a rectangle respectively.
Create a valid java expression that computes the rectangle's area.
-
Write a Java expression that converts a variable in kilometers to its equivalent in miles
(1 mile = 1.60935 km).
-
Look over the set of mathematical functions defined in Java's Math API.
You will find these in a section labeled "Method Summay". You can use these in expression by preceding the name of the function with "Math".
An example:
> double x = 0.0;
> double y = Math.cos(x);
> y
1.0
>
Use jGRASP to verify cos2 t + sin2 t = 1 for five values of t.
Using jGrasp to write a program
In the edit pane of jGrasp, place the following code:
public class Hello {
public static void main ( String[] args ) {
System.out.println("Hello World!");
}
}
This is the standard first program written by beginning programmers. Save it in a file named Hello.java.
Then compile it using the Compile button on the menu bar. It you typed everything correctly, you will see the message "Compilation completed."
If not, verify every character is identical to the above.
Now run it using the Run button on the menu bar.
If correct, it will produce the message "Hello, World!" in the output pane.
Read more about this from Chapter 5 of our text.
Now, change the program so it outputs your names instead of "Hello, World!".
Show this to your instructor.
Using Processing
- Start Processing. Work through the Getting Started tutorial from the Processing website. Pay attention on how to run, stop, and save programs.
- Work through the Processing Overview tutorial up to the "Examples and reference" section. Copy and paste code from the website into Processing and run.
- Work through the Coordinate System and Shapes tutorial from the Processing website.
- Work through the Color tutorial from the Processing website. Copy and paste code from the website into Processing and run.
- Create a sketch containing a point, line, ellipse, rectangle. Use a different color for each shape and a different color for the background..
You are now done with the lab! Take a well deserved break.
Credits:
Portions of this lab is from Harold Connamacher, and is based on a document written by Danny Heap, and owes
a debt to past work by Michelle Craig, Paul Gries, and Jim Clarke.
Copyright © 2019, David A. Reimann. All rights reserved. |
|