Colorado State University Logo | CS 163/4: Java Programming (CS 1) Colorado State University Logo | CS 163/4: Java Programming (CS 1)
CS 163/4: Java Programming (CS 1)
Computer Science

Lab 01 - Variables and Printing

Introduction

Welcome to your first lab! These labs will be critical to your success in this course, as they give you another opportunity to practice your programming skills.

They will be structured in multiple parts and will get progressively harder as the semester goes on.

Structure of a Program

Programs exist within classes in Java. This means that every piece of code you write should be within a class. Many programs also have a special block of code, usually referred to as “main”. This is where the control of your program begins. When you run a program, the compiler, which translates the code you write to the computer, reads and executes your code, starting in main.

The main method declaration looks like this:

public static void main(String [] args){
 // code goes between these brackets
}

Commenting a Program

Commenting in Java is a way to document your code. Comments are indicated by // in front of the line, or multiple lines can be documented like this:

/**
 * This comment 
 * spans multiple
 * lines.
 */

The compiler knows to ignore these comments when running your program.

Variables

You’ll get more into variables more during lecture, but for right now, it’s important to know that Java allows you to store values and data in things called variables.

These variables come in many different types, including int, String, and double. They can be numbers or letters or whole words, but you have to make sure to choose the appropriate type.

Today, we will be dealing with ints and Strings.

Strings hold a sequence of characters. We can think of these as typically storing words, but 7dss23d would be a valid String too.

When we create a variable in Java, we must use a specific format we usually refer to as “declaring the variable”. First, declare the variable type, then the variable name. Follow this with a semicolon.

To declare a variable:

variableType variableName; 

OR

To declare AND initialize a variable:

variableType variableName = initializeValue; 

You can choose to initialize the variable, or store a value in it immediately when declaring it. If you would like to initialize the variable (store a value in it), you can do that at the same time as the variable declaration. You may also do it later. Here’s an example of a variable declaration and initialization for both a String variable and an Int.

int number = 4;
String word = "Hello";

Notice that when assigning values to a String, the type must be capitalized, and the value must be put in quotation marks. When we use an expression that would typically be stored inside a String variable without actually assigning it to a variable, it is what is called a literal.

After declaring and initializing a variable, your program knows that any time you include the name of your variable, you’re referring to the value you assigned to it.

Print statements allow programs to output data. We use System.out.print() and System.out.println() to do so. Both of these can output variables or literal values.

We can print directly using these statements without any variables, like so:

System.out.println("Hello, world!");

Or we can print variables, like this:

String greeting = "Hello, world!";
System.out.println(greeting);

Both of these code samples would print “Hello, world!” followed by a new line.

System.out.print() prints without a new line, and System.out.println() prints with a new line.

Step 1

Identify the class within the code already given to you for this lab. Label it “Class” with a comment, like we explained in the Commenting section of the Introduction.

Step 2

Identify the main method within the code given to you. Label it “Main method, program execution starts here” with a comment, like in the first step.

Step 3

Declare a String variable, named whatever you would like, initialized to the name of the founder of Java. Remember, a String variable must have the uppercase first letter for the type, and the value assigned to it must be assigned with quotation marks.

Step 4

Print out the expression “The founder of Java is “ inside a print statement. Do not use a variable to store the String, just print the expression directly using a literal. Do not include a new line.

Hint Which statement, println() or print() gives you a new line after your output?

Step 5

Print out your String variable initialized with the founder of Java’s name inside a print statement. Include a new line after the name.

Step 6

Declare an Integer variable, named whatever you like, initialized to the year that Java was founded. What keyword specifies integer as the variable type? And should the year that initializes the variable be in 1111.0 or 1111 format if it is an integer?

Print “Java was founded in “ and then include your variable in the print statement. Follow the print statement with a new line.

Step 7

Run your program! It should print out “The founder of Java is “ followed by the name of the founder of Java, then on a new line, “Java was founded in “ and then the year it was founded, followed by a new line.

Computer Science Department

279 Computer Science Building
1100 Centre Avenue
Fort Collins, CO 80523
Phone: (970) 491-5792
Fax: (970) 491-2466

CS 163/4: Java Programming (CS 1)

Computer Programming in Java: Topics include variables, assignment, expressions, operators, booleans, conditionals, characters and strings, control loops, arrays, objects and classes, file input/output, interfaces, recursion, inheritance, and sorting.