DOC PREVIEW
WUSTL CSE 131 - lecture1

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Welcome to CSE 131LogisticsCS: Automation of processesHow Do Computers Work?A Little About ComputersProgramming LanguagesThe Java Programming LanguageWelcome to the World of JavaEclipseYour First ProgramToday: understand basic building blocks of JAVABasic unit: variableBasic Data Types in JavaBasic DefinitionsSlide 16IntegersString: a sequence of charactersString operationSlide 20Slide 21Slide 22BooleansComparisonsLeap YearData Type ConversionBasic Expressions in JavaVariable typesMethodProcedural abstraction (method)Method Call and ReturnMethod for Length of HypotenuseSolving a Problem by ReductionCSE 131 Computer Science 1Module 1: (basics of Java)2Welcome to CSE 131Professor: Yixin ChenHead TA: Rachel BlakeURL: http://www.cse.wustl.edu/cse131Discussion forum: https://piazza.com/wustl/spring2014/cse131/homeAll emails: [email protected]Structure of the course»Lecture, studios, labs, examsFeel free to ask questions»We move pretty fast (learn it while you are here)»Getting help: office hours, recitations, tutoringExpect to spend lots of time programming»Practice makes perfectNo late lab allowedZero tolerance on cheating!!Reading assignment before each lecture»It’s for your best interest to finish it4CS: Automation of processesCS is about automation of computation and processesComputers and computer software are everywhere»laptops, cell phones, game systems, music players, televisions, networks, cars, medical devices, appliances5How Do Computers Work?6A Little About ComputersConceptually very simpleMemory is storage of data: an array of numbered storage locations»each location can store a numeric value»each location identified by its addressProcessor is a device that carries out simple machine instructionsM[2] <= M[3]M[2] <= M[3] + M[4] (also, -, *, /,...)if M[3] > M[0] then skip ahead 3 (or “go back 5”)M[M[1]] <= M[2] + M[M[4]] (one location “refers” to another)Modern processor can carry out >109 instructions/secSo what is programming? – composing instructions to do a certain task01234...MemoryProcessorProgram7Programming Languages8The Java Programming Language•Developed by Sun in 1991 for a Green Project for refrigerators•The project failed, but Java thrived•Today, 1.1 Billion devices are running java9Welcome to the World of Java10EclipseIntegrated Development Environment (IDE)»“smart” editing of Java programs – continuous syntax checks»compiles program files automatically as necessary»integrated program testing and debugging tools»has plug-ins for version control system (Subversion)Can be a powerful tool for managing large projects»but, can also be a bit overwhelming and mysterious at first»watch the first few Eclipse videos on the web site before labworks the same way in Windows, Mac and Linux11Your First Programpublic class HelloWorld {public static void main(String[] args) {System.out.println("Hello, world!");}}»defines a class called HelloWorld (a class defines an object)»class contains a single method called main•in Java, the main method is the starting point of a program•the main method has an array of arguments (not used here)•Any program has one and only one main() method»program “prints” a brief message and halts12Today: understand basic building blocks of JAVAYou will be able to»Output a slogan on the screen »Tell if a year is a Leap Year»Calculate the area of a circle»Output anything you like»Do all kinds of calculation13Basic unit: variable A variable in Java is a piece of information stored in a computer’s memory: like a jar holding foodEach variable has a type»Like different kinds of jars (different food, size, usage, etc.)Main types: int a; double b; String s; boolean x;14Basic Data Types in JavaType (keyword) Meaning Examples Primitive Operatorsint Integer 3, 0, -5, 256 +, -, *, /double Decimal number 1.5, -3.1415, 0.0 +, -, *, /boolean true or false true, false &&, ||, !(return boolean: <,>,<=,>=,!=, ==)String a sequence of characters“hello world” + (concatenation)1515Basic DefinitionsType. A category of values and a set of operations on such values.Variable. A name that refers to a value of a declared type.Assignment statement. Associates a value with a variable.type16 The int type17IntegersThis surprises most students. Don’t feel bad: experts forget this too sometimes.18String: a sequence of characters19String operationMeaning of characters depends on context.Example: report +, -, *, / of two integers20 The double type21 Example: compute the area of a circle22 Boolean2323Booleansboolean data type. Useful to control the logic of a program.2424ComparisonsComparisons. Take two operands of one type (e.g., int) and produce a result of type boolean.2525Leap YearQ. Is a given year a leap year?A. Yes if either (i) divisible by 400 or (ii) divisible by 4 but not 100.public class LeapYear { public static void main(String[] args) { int year = 2012;boolean isLeapYear; // divisible by 4 but not 100 isLeapYear = (year % 4 == 0) && (year % 100 != 0); // or divisible by 400 isLeapYear = isLeapYear || (year % 400 == 0); System.out.println(isLeapYear); }}26Data Type ConversionOperations must be applied to same types, with the following exceptionsJava automatically converts some types of values to make them compatibleint i, j; double x, y;i = 5; j = i/2; y = 2; x = i/y; - As long as there is a double in an expression, the type of the expression is promoted to doubleCombining strings and numeric valuesint i = 5; String S = “two plus two =” + i;27Basic Expressions in JavaExpression Value Type3+5 8 int7/2.0 3.5 double7/2 3 int3+5-2/4+2 10 int3+5-2.0/4+2 9.5 double(3+5-2)/(4+2) 1 int“Hello”+”there” “Hellothere” String“I have “+5+5+” toes” “I have 55 toes” Stringtrue && false false booleanfalse || true true boolean!true false boolean(true&&false)||(!false) true boolean28Variable typesA variable can only hold values of the proper type int x; (default 0) int x = 1; … x = false; (error! Type-checking problem)Any expression of type T can appear in an expression where a value of type T is required»E.g. int x = 3+5;int x = 7/2.0; (type error)int x= (int) (7/2.0) (cast, x has value 3)29 Method What if we need to find areas of different circles?»Do we have write the code over and over again?30Procedural abstraction


View Full Document

WUSTL CSE 131 - lecture1

Documents in this Course
sp14_4

sp14_4

28 pages

sp14_3

sp14_3

29 pages

sp14_2

sp14_2

43 pages

sp14_10

sp14_10

19 pages

sp14_9

sp14_9

16 pages

sp14_8

sp14_8

22 pages

sp14_7

sp14_7

33 pages

sp14_6

sp14_6

27 pages

sp14_5

sp14_5

55 pages

lab0

lab0

6 pages

Load more
Download lecture1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lecture1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lecture1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?