0% found this document useful (0 votes)
37 views9 pages

W1 C2 Student Worksheet

1. The document discusses creating integer variables in Java programming. It explains that integers are whole numbers without decimals, and variables allow storing and retrieving data in a program. 2. It provides an example program that declares two integer variables, x and y, assigns them values of 4 and 3, and uses them in print statements instead of hard-coded numbers. This demonstrates how variables make code more flexible. 3. It notes that when x and y are divided, the result is rounded down to an integer (1 instead of 1.3) since integers cannot store fractions. Changing the variable values modifies the overall behavior of the calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views9 pages

W1 C2 Student Worksheet

1. The document discusses creating integer variables in Java programming. It explains that integers are whole numbers without decimals, and variables allow storing and retrieving data in a program. 2. It provides an example program that declares two integer variables, x and y, assigns them values of 4 and 3, and uses them in print statements instead of hard-coded numbers. This demonstrates how variables make code more flexible. 3. It notes that when x and y are divided, the result is rounded down to an integer (1 instead of 1.3) since integers cannot store fractions. Changing the variable values modifies the overall behavior of the calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Week 1 Class 2

Student Worksheet

2024
Language Skills - What is that programmer doing?

1. Match the sentences 1-8 with the Java Language in the boxes below describing the
programmer’s intention with each code. You can check your answers here: Java
Variables

1. They are (create) a variable called name of type String with the value
“John”
2. They are (change) the value of myNum from 15 to 20.
3. They are (combine) both text and variable using + character.
4. They are (declare) more than one variable of the same type using a
comma-separated list.
5. They are (specify) a block of Java code to be executed if a condition
is true with the if statement.
6. They are (test) variables.
7. They are (use) the else statement to specify a block of code to be
executed if the condition is false.
8. They are (use) the for loop instead of a while loop because they know
how many times they want to loop through a block of code.

2. Complete the sentences above with the words in brackets using the present
progressive.

1
Reading skills

Before you read:


ACTIVATING PREVIOUS KNOWLEDGE:

1. Based on what you know and with the help of a simple internet search, answer the
following questions:

What are integers in programming?

What are variables in Programming?

What is a primitive type in Java?

What is NetBeans?

2. Now, read the text to see how to create code to evaluate variables in Java programming.

Chapter 2: Understanding Typed Variables


Text adapted and taken from: Lassoff’S, M. (2017). Java Programming for Beginners: Learn the fundamentals of programming with
Java. Packt Publishing.

Integer variables
To begin, let's create a new project in NetBeans. I'm calling this project “Variables”, and we'll
allow the development environment NetBeans to create the main class for us so that we can
do the coding as quickly as possible. We need to delete all the comments that are created
automatically by NetBeans when we create our new project to keep everything as readable as
possible. Then we are ready to go:

1. A
The first computers were little more than calculators, and Java retains this functionality. For
example, Java can evaluate 1+1, which evaluates to 2, of course. However, Java is very
complicated and designed to do a lot of different things, so we need to provide context to
our commands. Here, we tell Java that we want it to print the result of 1+1:

Our program
package will run as expected:
variables;

public class
Java can perform all the basic arithmetic operations. It can do addition, subtraction,
multiplication 2.
Variables { (for which we use *, not X on our keyboard), and division. If we run the
following program with the input of 2 and 3, we'll see four println() commands, all of which
will givepublic static void main(String[]
the appropriate args)
result of the calculations. We can change these numbers to be any
{ System.out.println(1+1);
combination of numbers we want:
}

package variables;

public class

Variables {

public static void main(String[]


args) { System.out.println(2+3);
System.out.println(2-3);
System.out.println(2*3);
System.out.println(2/3);
}
The following is the output of the previous code:

Changing these lines manually is a lot of work and quickly becomes impossible if we're
3.
writing very complicated programs or dynamic programs that take user input.

The solution of variables

Fortunately, programming gives us a way of storing and retrieving data: this is called the
variable. To declare a variable in Java, we first must specify what kind of variable we're going
use. Variables come in different types. For now, we're using whole numbers. These are
numbers that do not have a specified decimal place and aren't fractions.

To declare a variable of the integer primitive type, in other words, whole numbers, we use the
int keyword, all lowercase. After we do this, we need to give our variable a name.
This is a unique identifier that we'll use to access this piece of information in future. Each
variable in our local program should have its own name. Let's call our first variable x and our
second variable y:
We've just written two perfectly legitimate lines of Java code. If we run our program now,
we'll see the same output we did before:

However, in the background, Java will also be setting aside memory space for our x and y variables.
4.
This allocation doesn't affect our println commands because the variables are not referenced in them
yet.

So, let's store some information in our variables. We can reference a variable when we've
created it simply by using the variable's name. It's important that we do not reference our
variable by typing int x again because this is the command for Java to create a completely
new variable x, not access the existing variable x:

When we've referenced our variable, we can change its value using the equal sign. So let's
5.
set x to 4 and y to 3. Our println commands currently operate with two explicitly declared
integers: the numbers 2 and 3. Since x and y are also integers, we can simply replace the
existing numbers with the variables x and y:
The following is the output of the preceding code:

When our Java code comes to the variables x and y, it will try to see what integer value they
6.
have currently been given. It will find the numbers 4 and 3. So if we run our program, we
should expect the first printlnstatement, x+y, to evaluate to 4+3, which then evaluates to 7.
This is exactly what occurs.
So, here's something interesting. The last line of our program, in which we divide x by y, isn't
evaluating as we mathematically expect it to. In this line of code, x has the value 4, and y has
the value 3, Now 4 divided by 3 equals 1.3, but our program is simply outputting 1. That's
because 1.3 is not a valid integer value. Integers are only whole numbers and never fractions
or decimal numbers. So, to keep working with integers, Java simply rounds down any
calculations that have fractional portions to their nearest whole number. If we want to work
in an environment where we could have fractional results, we would need to use a primitive
type other than an integer.
Anyway, now that we've set up our println commands to take integer variable input instead
of explicit numbers, we can modify the behavior of all four lines of the calculation by simply
changing the values of these integer variables. For example, if we want our program to run
on the input values -10 and 5 (integers can be negative; they just can't have fractional
components), all we need to do is change the values we give to our variables x and y:
If we run the preceding code, we will see the expected results:

Awesome! Now you learned the very basics of working with both integers and
variables in Java. 7.
While you read:
Read the text again. Look at the pictures below. Where do they go in the text? Write the
number of the corresponding pictures in the blank boxes in the text. Look at the example.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy