Week 2 ______ ------ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||

Slides:



Advertisements
Similar presentations
Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
Advertisements

Expressions, variables, and for loops (oh my!) spam, spam, spam, spam
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Copyright 2008 by Pearson Education 1 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Building Java Programs
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Week 4. >>> Overview * if else * returns * input.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
Week 7 Review and file processing. >>> Methods Hello.java public class Hello { public static void main(String[] args){ hello(); } public static void hello(){
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
1/10/2008. >>> About Us Paul Beck * Third quarter TA * Computer Engineering * Ryan Tucker * Second quarter TA * Computer.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
10/9/07 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Topic 6 loops, figures, constants Based on slides bu Marty Stepp and Stuart Reges from "Complexity has and will maintain.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
8/2/07. >>> About Me Scott Shawcroft * Junior * Computer Engineering * Third Quarter TA * Creative Commons Intern * Small-time Open Source Developer
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
10/16/07.
Chapter 2 Basic Computation
10/9/07 ______ < Moo? > \ ^__^ \ (oo)\_______ (__)\ )\/\
Introduction to Python
Lecture 5: Some more Java!
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Primitive Data, Variables, Loops (Maybe)
basic Python programs, defining functions
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
expressions, variables, for loops
expressions, variables, for loops
Building Java Programs
CSc 110, Spring 2018 Lecture 7: input and Constants
Building Java Programs
Variables, Types, Operations on Numbers
Building Java Programs Chapter 2
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Topic 6 loops, figures, constants
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Programming Basics.
Building Java Programs
Suggested self-checks:
Building Java Programs
Drawing complex figures
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
JAVA. Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of platforms, such.
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

Week 2 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||

>>> Last Time * print * escape sequences * functions >>> print "a string" a string >>> print 'a string' a string >>> print "A word", "and another " A word and another >>> print "Hello," + "world!" Hello, world! A whaa? function – a subroutine independent of other code. method – a subroutine associated with a class (think public class...) or object.

>>> Overview * types * variables * for loops * 1337 ASCII art _________ \ ^__^ \ (**)\_______ (__)\ )\/\ U ||----w | || ||

int float str bool >>> Types Python cares very little about types. In Java, one must declare a variable with a particular type and maintain that type throughout the existence of that variable. In other words, ints can be only stored in places designated for ints, same for doubles etc. This is not the case in Python. Python does not care about types until the very last moment. This last moment is when values are used in certain ways, such as concatenation of strings. int double String char boolean Java python “wow” 'w' True

>>> String concatenation Like Java, we can concatenate strings using a “+”. Unlike Java, when a number has to be concatenated with a string in python, you need to explicitly perform the conversion to a string using the str() function because it hasn’t made sure that the types match before run time. >>> "Suppose " " swallows carry it together." Traceback (most recent call last): File " ", line 1, in TypeError: cannot concatenate 'str' and 'int' objects >>> "Suppose " + str(2) + " swallows carry it together." 'Suppose 2 swallows carry it together.'

>>> Python expressions Python is very similar to Java in the way that it handles expressions such as:  + - * / %  Integer division – rounds down to nearest int  Precedence – same rules  Mixing types – numbers change to keep precision  Real numbers are kept as “floats” or floating point numbers

>>> Differences in expressions There are a few things that differ between Python and Java, such as: You can multiply strings in python! There are no increment operators in python (++, --)‏ so we have to use -= and += >>> "Hello!"*3 'Hello!Hello!Hello!' >>> x = 1 >>> x += 1 >>> print x 2

... int x = 2; x++; System.out.println(x); x = x * 8; System.out.println(x); double d = 3.0; d /= 2; System.out.println(d); >>> Variables As we said earlier, Python cares less about types. When we create a variable in Python, the type of the variable doesn’t matter. As a result, in Python, creating a variable has the same syntax as setting a value to a variable. Variables.java x = 2 x += 1 print x x = x * 8 print x d = 3.0 d /= 2 print d s = "wow" print s expressions.py

>>> Constants Continuing Python's free spirited ways, it has much less restrictions than Java. Because of this, constants are possible but not in a commonly used manner. Instead, we'll designate constants in Python solely by the variable capitalization. We do need to write the constants at the top of our program so that every function can see them! SIZE = 2 x = 10 * SIZE print x # main … constants.py

>>> Python's For for i in range(4): # (end)‏ print i for i in range(1,4): # (start,end)‏ print i for i in range(2,8,2): # (start,end,step_size)‏ print i # for in range([,] [, ]): # for.py Unlike Java's for loop, Python's for loop loops over elements in a sequence. To loop over a certain sequence of integers use the range() function. Later we will learn objects that we can use a for loop to go through all of the elements.

System.out.print("Hello world! ")‏ System.out.print("This will all be")‏ System.out.println(" on the same line.")‏ >>> Complex Printing Sometimes more complex output is needed. To produce output but not go to the next line, just write a comma after the last quotes. This adds whitespace, so sometimes you need “sys.stdout.write()” which just writes what is in the quotes. You also have to import “sys”! Hello.java import sys sys.stdout.write("Hello world! ")‏ print "This will all be", print " on the same line." hello2.py

>>> Nested loops In Python, a lot of the time we can do nested loops in a much more straightforward way using string multiplication. for (int i = 1; i <= 5; i++) { for (int j = 1; j <= (5 - i); j++) { System.out.print(" "); } for (int k = 1; k <= i; k++) { System.out.print(i); } System.out.println(); } Nested.java for i in range(5,0,-1): print " " * (i-1) + str(i)*(6-i)‏ nested1.py import sys for i in range(5,0,-1): sys.stdout.write(" " * (i-1))‏ sys.stdout.write(str(i)*(6-i))‏ print nested2.py

>>> Mirror #================# | <><> | | <>....<> | | <> <> | |<> <>| | <> <> | | <>....<> | | <><> | #================# yossarian ~ $ python mirror.py // Marty Stepp, CSE 142, Autumn 2007 // This program prints an ASCII text figure that // looks like a mirror. // This version uses a class constant to make the figure resizable. public class Mirror2 { public static final int SIZE = 4; // constant to change the figure size public static void main(String[] args) { line(); topHalf(); bottomHalf(); line(); } // Prints the top half of the rounded mirror. public static void topHalf() { for (int line = 1; line <= SIZE; line++) { // pipe System.out.print("|"); // spaces for (int j = 1; j <= -2 * line + (2 * SIZE); j++) { System.out.print(" "); } // <> System.out.print("<>"); // dots. for (int j = 1; j <= 4 * line - 4; j++) { System.out.print("."); } // <> System.out.print("<>"); // spaces for (int j = 1; j <= -2 * line + (2 * SIZE); j++) { System.out.print(" "); } // pipe System.out.println("|"); } // Prints the bottom half of the rounded mirror. public static void bottomHalf() { for (int line = SIZE; line >= 1; line--) { // pipe System.out.print("|"); // spaces for (int j = 1; j <= -2 * line + (2 * SIZE); j++) { System.out.print(" "); } // <> System.out.print("<>"); // dots. for (int j = 1; j <= 4 * line - 4; j++) { System.out.print("."); } // <> System.out.print("<>"); // spaces for (int j = 1; j <= -2 * line + (2 * SIZE); j++) { System.out.print(" "); } // pipe System.out.println("|"); } // Prints the #==# line at the top and bottom of the mirror. public static void line() { System.out.print("#"); for (int j = 1; j <= 4 * SIZE; j++) { System.out.print("="); } System.out.println("#"); }

Except where otherwise noted, this work is licensed under © 2007 Scott Shawcroft, Some Rights Reserved Python® and the Python logo are either a registered trademark or trademark of the Python Software Foundation. Java™ is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.