37
CSE 1321 - Module 1 A Programming Primer 1/23/19 CSE 1321 MODULE 1 1

CSE 1321 -Module 1 A Programming PrimerSkeletons BEGIN MAIN END MAIN 1/23/19 CSE 1321 MODULE 1 5 Note: every time you BEGIN something, you must END it! Write them as pairs! Skeletons

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

CSE1321- Module1

AProgrammingPrimer

1/23/19 CSE1321MODULE1 1

MotivationYou’regoingtolearn:◦ The“skeleton”◦ Printing◦ Declaringvariables◦ Readinguserinput◦ Doingbasiccalculations

You’llhavelecturescoveringthesetopics

1/23/19 CSE 1321 MODULE 1 2

Beforewebegin...You’regoingtoseethingsin4differentlanguages1. Pseudocode

2. Python

3. C#

4. Java

1/23/19 CSE 1321 MODULE 4 3

SkeletonsSkeletonprograms:1. Arethesmallest programyoucanwrite2. Aretheminimalamountofcodethatcompiles3. DoNOTHING4. Definetheentry/startingpointoftheprogram5. Aresomethingyoushouldmemorize

Youprobablywon’tunderstandwhatyou’reabouttosee…andit’sOK!

1/23/19 CSE 1321 MODULE 4 4

Skeletons

BEGIN MAIN

END MAIN

1/23/19 CSE 1321 MODULE 1 5

Note: every time you BEGIN something, you must END it!Write them as pairs!

Skeletonsusing System;

class MainClass {

public static void Main (string[] args) {

}

}

1/23/19 CSE 1321 MODULE 1 6

Note: The opening “{“ means BEGIN and the closing “}” means END

Skeletons

class MainClass {

public static void main (String[] args) {

}

}

1/23/19 CSE 1321 MODULE 1 7

Note: Capitalization matters!

Skeletons

def main():

pass

if __name__ == '__main__':

main()

1/23/19 CSE 1321 MODULE 1 8

Note: technically, you don’t have to do this. It’s complicated…

Mainideas

1/23/19 CSE 1321 MODULE 4 9

All of them defined “main”, which is the entry/starting point of the program

They have a BEGIN and END(yes, even Python)

Printing

BEGIN MAIN

PRINT “Hello, World!”

PRINT “Bob” + “ was” + “ here”

END MAIN

1/23/19 CSE 1321 MODULE 1 10

Printingusing System;

class MainClass {

public static void Main (string[] args) {

Console.WriteLine ("Hello, World!");

Console.WriteLine ("Bob"+" was"+" here");

}

}

1/23/19 CSE 1321 MODULE 1 11

Note: There’s also a Console.Write()

Printing

class MainClass {

public static void main (String[] args) {

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

System.out.println("Bob"+" was"+" here");

}

}

1/23/19 CSE 1321 MODULE 1 12

Note: There’s also a System.out.print()

Printing

def main():

print ("Hello, World!")

print ("Bob"+" was"+" here")

if __name__ == '__main__':

main()

1/23/19 CSE 1321 MODULE 1 13

MainIdea

1/23/19 CSE 1321 MODULE 4 14

All (decent) languages have the abilityto print to the console/screen

VariablesAvariable:◦ Isachunkofthecomputer’smemory◦ Canholdavalue◦ Isofaparticular“type”◦ Integer◦ Floatingpointnumber◦ Character◦ String– whichisjusttext

Usually,it’stwosteps:◦ “Declare”thevariable– tellthecomputerwhatitis(dothisonlyonce)

◦ Assignvaluestothevariable

1/23/19 CSE 1321 MODULE 4 15

Declaring/AssigningVariables

BEGIN MAIN

END MAIN

1/23/19 CSE 1321 MODULE 1 16

Declaring/AssigningVariables

BEGIN MAIN

CREATE userName

END MAIN

1/23/19 CSE 1321 MODULE 1 17

?userName

Declaring/AssigningVariables

BEGIN MAIN

CREATE userName

CREATE studentGPA

END MAIN

1/23/19 CSE 1321 MODULE 1 18

?userName

?studentGPA

Declaring/AssigningVariables

BEGIN MAIN

CREATE userName

CREATE studentGPA

userName ← “Bob”

END MAIN

1/23/19 CSE 1321 MODULE 1 19

“Bob”

userName

?studentGPA

Declaring/AssigningVariables

BEGIN MAIN

CREATE userName

CREATE studentGPA

userName ← “Bob”

studentGPA ← 1.2

END MAIN

1/23/19 CSE 1321 MODULE 1 20

“Bob”

userName

1.2studentGPA

Declaring/AssigningVariablesusing System;

class MainClass {public static void Main (string[] args) {

string userName;float gpa;userName = "Bob";gpa = 1.2f;

}}

1/23/19 CSE 1321 MODULE 1 21

Declaring/AssigningVariables

class MainClass {public static void main (String[] args) {

String userName;float gpa;userName = "Bob";gpa = 1.2f;

}}

1/23/19 CSE 1321 MODULE 1 22

Declaring/AssigningVariables

def main():

userName = 'Bob'

studentGPA = 1.2

if __name__ == '__main__':

main()

1/23/19 CSE 1321 MODULE 1 23

MainIdea

1/23/19 CSE 1321 MODULE 4 24

All languages have the variables and the ability to assign values to them

ReadingTextfromtheUser

BEGIN MAIN

CREATE userInput

PRINT “Please enter your name”

READ userInput

PRINT “Hello, ” + userInput

END MAIN

1/23/19 CSE 1321 MODULE 1 25

ReadingTextfromtheUserusing System;

class MainClass {

public static void Main (string[] args) {

string userInput;

Console.Write("Please enter your name: ");

userInput = Console.ReadLine();

Console.WriteLine("Hello, "+userInput);

}

}

1/23/19 CSE 1321 MODULE 1 26

ReadingTextfromtheUserimport java.util.Scanner;class MainClass {

public static void main (String[] args) {String userInput;System.out.print("Please enter your name: ");Scanner sc = new Scanner (System.in);userInput = sc.nextLine();System.out.println("Hello, "+userInput);

}}

1/23/19 CSE 1321 MODULE 1 27

ReadingTextfromtheUser

def main():

userInput = input("Please enter your name: ")

print ("Hello, ” + userInput)

if __name__ == '__main__':

main()

1/23/19 CSE 1321 MODULE 1 28

ReadingNumbersfromtheUser

BEGIN MAIN

CREATE userInput

PRINT “Please enter your age: ”

READ userInput

PRINT “You are ” + userInput + “ years old.”

END MAIN

1/23/19 CSE 1321 MODULE 1 29

ReadingNumbersfromtheUserusing System;class MainClass {

public static void Main (string[] args) {string userInput;Console.Write("Please enter your age: ");userInput = Console.ReadLine();int age = Convert.ToInt32(userInput);Console.WriteLine("You are "+age+" years old.");

}}

1/23/19 CSE 1321 MODULE 1 30

ReadingNumbersfromtheUserimport java.util.Scanner;class MainClass {

public static void main (String[] args) {int age;System.out.print("Please enter your age: ");Scanner sc = new Scanner (System.in);age = sc.nextInt();System.out.println("You are "+age+" years old.");

}}

1/23/19 CSE 1321 MODULE 1 31

ReadingNumbersfromtheUser

def main():

age = int(input("Please enter your age: "))

print ("You are ",age," years old.")

if __name__ == '__main__':

main()

1/23/19 CSE 1321 MODULE 1 32

BasicOperatorsMathoperators◦ Addition(+)◦ Subtraction(-)◦ Multiplication(*)◦ Division(/)

1/23/19 CSE 1321 MODULE 4 33

In-classProblem#0Writeaprogramthataskstheusersweight(inpounds)andprintsouthowmuchshe/heweighsonthemoon.

Note:yourmoonweightis16.5%ofyourEarthweight

1/23/19 CSE 1321 MODULE 4 34

In-classProblem#1WriteaprogramthataskstheuserforatemperatureinFahrenheitandprintsoutthattemperatureinKelvin.

Formula:Kelvin=(Farh– 32)x5/9+273.15

1/23/19 CSE 1321 MODULE 4 35

In-classProblem#2Assume:◦ Theaveragepersonburns2500caloriesperday.◦ Thereare3500caloriesinonepoundoffat.◦ Thereare365daysinayear

Writeaprogramthataskstheuserhowmanycaloriestheyconsumeinaday,andalsoaskshowmanyyearstheywillconsumethatmanycalories.

Printoutthenumberofpoundsthepersonwillgain/loseinthattime.

1/23/19 CSE 1321 MODULE 4 36

• Allprogramshaveanentrypoint (aBEGINandEND)• Ifyoudon’tknowwheretobegin,startwiththeskeleton

• Alllanguagescanprinttothescreen• Alllanguageshavevariables• Alllanguagescanreadfromtheuser• Alllanguageshavebasicoperators

1/23/19 CSE1321MODULE1 37

Summary

Fromnowon,youshouldlookforpatternsinlanguages