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, examsFeel free to ask questions»We move pretty fast (learn it while you are here)»Getting help: office hours, recitations, tutoringExpect to spend lots of time programming»Practice makes perfectNo late lab allowedZero tolerance on cheating!!Reading assignment before each lecture»It’s for your best interest to finish it4CS: Automation of processesCS is about automation of computation and processesComputers and computer software are everywhere»laptops, cell phones, game systems, music players, televisions, networks, cars, medical devices, appliances5How Do Computers Work?6A Little About ComputersConceptually very simpleMemory is storage of data: an array of numbered storage locations»each location can store a numeric value»each location identified by its addressProcessor 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/secSo 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 Java10EclipseIntegrated 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 labworks 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 JAVAYou 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 foodEach 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 DefinitionsType. 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 operationMeaning of characters depends on context.Example: report +, -, *, / of two integers20 The double type21 Example: compute the area of a circle22 Boolean2323Booleansboolean data type. Useful to control the logic of a program.2424ComparisonsComparisons. Take two operands of one type (e.g., int) and produce a result of type boolean.2525Leap YearQ. 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 ConversionOperations must be applied to same types, with the following exceptionsJava 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 doubleCombining 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 typesA 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