Courses/Computer Science/CPSC 203/CPSC 203 Template/Labs Template/TA Examples for JES

From wiki.ucalgary.ca
Jump to: navigation, search

TA Examples for JES

TAs please upload your examples of JES documents that explain concepts or help with skills. Also provide the following information:

  1. Your name
  2. What concept the JES file helps to explain

JES Examples

  • Navneet: Week 1 - Lab 1
    • Concepts:introduction to JES/Jython, print command, comments, types (int, float, and string), operators (including string concatenation and division/modulo operators for int), and variables
  • Navneet: Week 1 - Lab 2
    • Concepts: for loops, if-else conditions, if-elif-else conditions, and indentation/blocks
  • Ibrahim: Week 1 - Lab 2
    • Concepts: If-else conditions, For Loops
  • Navneet: Week 2 - Lab 1
    • Concepts: function declarations, input parameters, return values
    • Concepts: Parctice For Loop, If statement, function

Navneet: Week 1 - Lab 1

In today's tutorial (section T25) we covered:

  • introduction to JES/Jython
  • print command
  • comments
  • types (int, float, and string)
  • operators (including string concatenation and division/modulo operators for int)
  • variables

Here's a copy of the Jython code we went through today in tutorial (section T25). You can copy-paste the code into a new program (copy to the program area). To run the program type the following in the interactive console: helloWorld()

 def helloWorld():
   #use the number sign to create a comments (note: comments are not executed when you run your program).
   print "Hello world!"
   print "Navneet"  #comments can appear on a single line, or at the end. Chnage the text in the string to your own name.
   
   #Comments can appear throughout your program. However, if you want to end your program with a comment, you must have a blank line proceeding it (otherwise you'll have a syntax error).
   
   #There are three tpes in Jython: int (a negative/positive natural number), float (negative/positive decmal number), and string (a set of characters, i.e. text).
   print 3
   print 3.0
   print "here's some text"
   
   #Some operators work differently depending on the type being used.
   print "hello" + "world" #In this case the + operator is used to concatenate two string together.
   print 7 / 2 #The division operator when used with int will return the quotient.
   print 7 & 2 #The modulo operator will return the remainder.
   
   #Variables can store data. Use the assignemtn operator to assign data to a variable. 
   #By convention, variable names use lower-case letters. If the variable name has more than 'word' as part of the name, the first letter of 
   #each word (except for the first word) starts with an upper-case letter.
   var1 = 3 #Here, a variable named var1 has been created, and assigned the int 3.
   var2 = -11.76 #Here, a variable named var2 has been created, and assigned the float -11.76.
   someText = "some text" #Here, a vriable named someText has been created, and assigned the string "some text".  
   
   #You can print the data that is stored in a variable. 
   print var1
   print var2
   print someText

A screenshot is provided of what you should see in both the program area, and the result of running the program in the interactive console.

Navneet - JES Example 1.png


Navneet: Week 1 - Lab 2

 def loopExample1():
   for x in [1, 2, 3]:
     print "inside the loop"
 
 
 def loopExample2():
   for i in [1, 2, 3, 4, 5]:
     print i
 
 
 def loopExample3():
   for i in range(1, 5):
     print i
 
 
 def loopExample4():
   a = 0  
 
   for i in [1, 2, 3, 4]:
      a = a + i
      print "The value of a is: ", a 
 
 
 def loopExample5():
   squareSize = 3
 
   for i in range(0, squareSize):
     for j in range(0, squareSize):
       print "*",
     print ""
 
 
 def conditionExample1():
   grade = 50  
 
   if grade >= 50:
     print "Pass"
   else:
     print "Fail"
 
 
 def conditionExample2():
   income = -1
 
   if number > 0:
     print "Made a profit"
   elif number < 0:
     print "In the red"
   else:
     print "Broke even"
 
 
 def conditionExample3():
   grade = 82
 
   if grade >= 90:
     print "Your grade is an A"
   elif grade >= 80:
     print "Your grade is a B"
   elif grade >= 70:
     print "Your grade is a C"
   elif grade >= 60:
     print "Your grade is a D"
   elif grade >= 50:
     print "Your grade is an E"
   else:
     print "Your grade is an F"
 
 
 def loopAndConditionExample():
   for x in range(1, 15):
     if (x % 5) == 0:
       print "The number ", x, " is a multiple of 5"

Ibrahim: Week 1 - Lab 2

Concepts

  • If-Else Conditions
  • For Loops

If-Else Conditions

Create a function named compare which takes in 2 parameters x and y

If x is a multiple of y print "MULTIPLE"

If x is even print "EVEN" Else print "ODD"

If x is larger than y print "LARGER"

Else If x is smaller than y print "SMALLER"

Else print "SAME SIZE"

Ifelseif2.PNG


For Loops

Create a function named luckyMultiples which takes in 2 parameters start and end and it scans through all the numbers from start till end inclusive and prints the sum of all the numbers from there that are mutliples of 7

ForLoop2.PNG


Practice Problem

Create a function named bankStatement with parameters

  • initialDeposit – the initial deposit placed the account
  • numberMonths – number of months you want to show the transactions for
  • annualInterestRate – the annual interest rate given to the account
  • takeOutValue – whenever the deposit increases to this or larger value, the extra is taken out and the deposit comes back down to the initial value

BankStatement3.PNG

For Solution Look at BankStatement Solution

Navneet: Week 2 - Lab 1

We'll go over these examples during tutorial (for section T25). You can also download the file with the code from Blackboard (under Course Documents). The name of the file is Navneet_JESFunctionsExamples. Or, you can copy-paste the code below into JES.

  • function declarations
  • input parameters
  • return values
 #parameter: none
 #return: none
 #description: this function prints "a simple function"
 def functionA():
   print "a simple function"
 
 
 #parameter a: can either by an Integer, Float, or String
 #return: none
 #description: this function prints the value associatd with the input parameter a
 def functionB(a):
   print a
 
 
 #parameter a: an Integer or Float
 #parameter b: an Integer
 #return: none
 #description: this function prints the sum of the two input parameters (a and b)
 def functionC(a, b):
   c = a + b
   print c
 
 
 #parameter i: an Integer, which is greater than 0
 #parameter j: an Integer, which is greater than 0
 #return: none
 #Description: this function prints a rectangle of dimensions heigt (input parameter i) and width (input parameter j)
 def functionD(i, j): 
   for m in range(0, i): #go through each column
     for n in range(0, j): #go through each row
       print "#", #print the charcter # to a row and then a blank space
   print "" #(at the end of this for loop, printing will move to the next row)
 
 
 #parameter a: an Integer or a Float
 #parameter b: an Integer of a Float
 #return: an Integer or a Float
 #description: this function returns the difference between input parameter a and input parameter b
 def functionE(a, b):
   c = a - b
   return c
 
 
 #parameter: none
 #return: none
 #description: this function shows an example of how to call a function that returns a value. Note what happens when the input variables are reversed.
 def functionF():
   n1 = 9
   n2 = 3   
 
   v = functionE(n1, n2)
   print v
  
   v = functionE(n2, n1)
   print v
 
 
 #parameter p: a Float (principal amount (initial investment))
 #parameter r: a Float (annual nominal interest rate, as a decimal)
 #parameter n: a Float (number of times the interest is compounded per year)
 #parameter t: a Float (number of years)
 #return: an Float
 #description: this function calculates compound interest, and returns the result, a is the total  (function taken from http://en.wikipedia.org/wiki/Compound_interest on 2/11/2008)
 def compoundInterest(p, r, n, t):
   a = 0
   a = p * (1 + r / n) ** (n * t) #the exponent operator is **
   return a
 
 
 #parameter: none
 #return: none
 #description: this function calls the function compundInterest, and prints the returning value. E.g. With an initial ivestement of $1 500, with an annual interest rate of 4.3%, 
 #compounded quarterly, what is the return on this investment after 6 years?
   def functionG():
   amount = compoundInterest(1500, 0.043, 4, 6)
   print amount


Dina: Week 2 - Lab 2

  • Make a function that takes two parameters, name and age. Then, print the name (only once) and age after 10, 20, 30 years (You should use loops). Your output should look the same as the following for name=Dina and age=27

Dina

Age after 10 years= 37

Age after 20 years= 47

Age after 30 years= 57


def NameAge(name,age):
 print name
  for x in (10,20,30):
    print "Age after", x, "years=", age+x
  • Make a function that takes two parameters; start and end. Then, it scans all the numbers  from start till end inclusive and prints only numbers that are multipliers of 3.

Your function should also print the summation of numbers in the range that are not multipliers of 3 As an example, For a range (0,10), your output should be like this

Number that are multipliers of 3 are:

0

3

6

9

Sum of numbers that are not multipliers of 3 is 37

 def mul3(start, end):
   print "Numbers that are multipliers of 3 are:"
   sum=0
   for x in range(start,end+1):
     if (x%3==0):
        print x
     else:
        sum=sum + x
   print "Sum of numbers that are not multipliers of 3 is", sum