Courses/Computer Science/CPSC 203/CPSC 203 Template/Labs Template/Problem Solving Week 1 - Lab 1

Introduction
Today's tutorial introduces programing, using the Jython programing language. Programs will be written using the Jython Environment for Students (JES). After completing this module, you should be comfortable in demonstrating the following skills:
 * using variables
 * using constants

Solving Problems with Algorithms
We solve problems and how to write solutions (algorithms). An algorithm is a step by step procedure for accomplishing some particular problem.

Computers are often used to solve problems, a person with knowledge of the problem must analyze the problem first, develop the instructions for solving the problem, and then have the computer carry out those instructions.

Computers can repeat the solution very quickly and consistently for different situations and data. The major advantage is that it frees people from repetitive and boring tasks.

The following will familiarize you with the fundamental operations (input/output, assignment, repetition, and selection) of programming.

Additional Reading
(none)

Introduction to JES
JES is a tool for teaching introductory programming, and we are going to use it to do some fun stuﬀ with graphics.

First open the JES application it should look something like the figure below:



Jython is an implementation of the python language written in the java programming language. So the real name of the programming language we are using is python.

Using the Interactive Window to Run Your Program
JES has two main ways of writing code (we call instructions for the computer “source code” or just “code” for short). If you are just trying stuﬀ out, trying to see what things do, you can use the interactive window. The interactive window is the black box at the bottom of the JES window see Figure below:



Saving Your Program
If you are writing code that you want to keep, then you want to write it in the upper window see Figure below:



The upper window lets you store your code to a ﬁle, or load it from a ﬁle later on. A good rule of thumb is that if you are writing a function you should write it in this upper window.

Otherwise, just use the interactive window.

If you decide to save your code to a file, write your code in the upper window, and save it to a file. When you want to run that file, you need to click on "Load Program" first (at the bottom of the upper window), and the run your program as shown below.



Variables and Arithmetic
Historically, the ﬁrst program that you should write in any language is what we call the “Hello World” program, a simple program that prints out the friendly message “Hello World!”

Type in the interactive window (the bottom window) see Figure below:

print "Hello World!"

When you hit Enter, JES goes oﬀ to interpret what commands you gave it.

JES interprets your command as an instruction to print the message in quotes. In JES a bit of text in quotes is called a “string”. You can do things with strings other than just printing them. For example, you can add two strings together (concatenation) using the “+” sign.

Look at this and guess what it does before you run it, then see the Figure below for comparison:

print "Hello" + "World!"

What if we want to put together two strings and save the results for later? Instead of printing the results to the screen, we can just assign the result of mashing those strings together to a variable, like so (see figure below):

x = "Hello " + "World!" print x

We can print a combined line of text and numbers, like so:

print "Sum = ", 7

JES supports several mathematical operations like addition, subtraction, multiplication, and division. Parentheses can also be used to indicated the order in which operations should be carried out.

JES also supports numeric calculations: print 5 * 10 print 3 / 2 print 3.0 / 2 print 10 + 5 print 32 % 3 print 5 % 2 print 3 - 1 total = 5 + 3 print total print 5+(3/(2+7))*2

Variables are also useful as place holders.

total = 5 + 3 total = total * 4 print total