CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o...
Transcript of CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o...
![Page 1: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/1.jpg)
CSS 161 Fundamentals of Compu3ng
Assignments, Expressions, Operators, Console input & output
October 1, 2012
CSS 161: Fundamentals of Compu3ng
Instructor: Uma Murthy
CSS SKL 161 A Instructor: Joe McCarthy
![Page 2: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/2.jpg)
Outline
• Introduc3on • Recap, reminders
• Assignments, expressions, and operators
• Console input and output
CSS 161: Fundamentals of Compu3ng
![Page 3: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/3.jpg)
Who am I?
• Uma Murthy, Ph.D. o Lecturer, Compu3ng and SoPware Systems, UWB
o Naming conven3ons • “Uma”
• “Professor Murthy” • “Dr. Murthy”
CSS 161: Fundamentals of Compu3ng
![Page 4: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/4.jpg)
Background
• Ph.D. from Virginia Tech, 2011 o Digital libraries, informa3on retrieval, personal informa3on
management
• Research associate, Kenan Ins3tute, UNC-‐Chapel Hill o SoPware agents
• SoPware Developer, IBM o Incuba3on projects, mainframe emulator (Java development)
• Master’s in Computer Applica3ons, Na3onal Ins3tute of Technology, Trichy, India
• Bachelor’s in Electronics, Bangalore University, India
CSS 161: Fundamentals of Compu3ng
![Page 5: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/5.jpg)
Current
• Lecturer, Compu3ng and soPware systems, UWB
• Human Factors Engineer, URCVentures o 3-‐dimensional reconstruc3on from image sets o Assessing technology and evalua3ng usability
CSS 161: Fundamentals of Compu3ng
![Page 6: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/6.jpg)
Recap
CSS 161: Fundamentals of Compu3ng
![Page 7: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/7.jpg)
Updates/ reminders
• Assigned readings o Any material in assigned readings is poten3al fodder for exams, regardless of whether we cover it in a lecture, lab or assignment
• Slides will always be posted on web site o Typically, shortly aPer each class
• Please complete survey if not already done
CSS 161: Fundamentals of Compu3ng
![Page 8: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/8.jpg)
Homework 1
• Available by 5pm today, due midnight, Mon, Oct 8
CSS 161: Fundamentals of Compu3ng
![Page 9: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/9.jpg)
Required Textbook
Absolute Java, 5th Edition Walter Savitch & Kenrick Mock Addison-Wesley, 2013
CSS 161: Fundamentals of Compu3ng
4th Edition OK, too
![Page 10: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/10.jpg)
Supplemental Material (1/2)
CSS 161: Fundamentals of Compu3ng
Java: An Introduction to Problem Solving & Programming, 6th Edition Walter Savitch Addison-Wesley, 2012
http://courses.washington.edu/css161/joemcc/Notes/SavitchCh01.pdf
(access via “Notes” link on course homepage)
![Page 11: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/11.jpg)
Supplemental material (2/2)
• Prac3ce It! hfp://webster.cs.washington.edu:8080/prac3ceit/
• Sign up for a Prac3ce-‐It account o Make sure to use the school value, "University of Washington Bothell”
o Add the course, "CSS 161A Autumn 2012 Uma Murthy", to your list of enrolled courses for your account
• You can do this by clicking My Courses, then Add Course.
CSS 161: Fundamentals of Compu3ng
![Page 12: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/12.jpg)
Java
• Goal: o Write once, run anywhere o JDK: Java Developers Kit o JRE: Java Run3me Environment
• Edi3ons o SE: Standard Edi+on o EE: Enterprise Edi3on o ME: Micro Edi3on (mobile, embedded) o Embedded: flash memory, closed systems
• Versions o 1.0 (1992), .. 1.6 (2006), 1.7 (2011)
CSS 161: Fundamentals of Compu3ng
![Page 13: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/13.jpg)
Downloading Java [op3onal]
• hfp://www.java.com/getjava/ o Current: Version 7 update 7 (1.7u7)
• On Mac, requires Mac OS X 10.7.3 or higher
o Can also use Version 6 update 35 (1.6u35) • hfp://www.java.com/en/download/manual_v6.jsp • Supported thru February 2013
• We will be wri3ng Java, so we want JDK o Which includes JRE
CSS 161: Fundamentals of Compu3ng
![Page 14: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/14.jpg)
CSS 161: Fundamentals of Compu3ng
![Page 15: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/15.jpg)
Integrated Development Environments (IDEs)
• Programming tools o Edit text (code) + compile + run
o Graphical representa3ons of components
• We’ll be using BlueJ, but you can use others
CSS 161: Fundamentals of Compu3ng
netbeans.org eclipse.org bluej.org
![Page 16: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/16.jpg)
Downloading BlueJ
CSS 161: Fundamentals of Compu3ng
http://www.bluej.org/download/download.html
![Page 17: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/17.jpg)
Recap
• Computer program?
CSS 161: Fundamentals of Compu3ng
![Page 18: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/18.jpg)
Recap
• Computer program? o Set of instruc3ons that tell a computer to execute some task
CSS 161: Fundamentals of Compu3ng
![Page 19: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/19.jpg)
Recap: Intro to Java
CSS 161: Fundamentals of Compu3ng
Class Method main() Iden3fier Reserved word Variable Declara3on Assignment Operator System.out.println()
Output: 10 + 20 = 30
![Page 20: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/20.jpg)
Recap: Intro to Java
CSS 161: Fundamentals of Compu3ng
Class Method main() Iden3fier Reserved word Variable Declara3on Assignment Operator System.out.println()
Output: 10 + 20 = 30
![Page 21: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/21.jpg)
CSS 161: Fundamentals of Compu3ng
![Page 22: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/22.jpg)
Assignments & Expressions
• Syntax: variable = expression
• Seman3cs: Evaluate expression, assign value to variable
• Expression: Constant (1, 1.23, 3.45e6, ‘a’, “css161”, true, false)
• final static int COURSENUMBER = 161; Variable (num2) expression operator expression
• Operators: *, /, % +, -‐ …
CSS 161: Fundamentals of Compu3ng
![Page 23: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/23.jpg)
Operators & Precedence
Can use parentheses to change precedence 1 + 2 * 3
(1 + 2) * 3
When operators in an expression have the same precedence, the expression is evaluated from leP to right.
CSS 161: Fundamentals of Compu3ng
![Page 24: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/24.jpg)
Integer division
int x = 4;
int y = 7; int z = y/x;
System.out.println(z);
Answer: 1
In integer division, the result is only the integer part and the part aPer the decimal is discarded.
CSS 161: Fundamentals of Compu3ng
![Page 25: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/25.jpg)
CSS 161: Fundamentals of Compu3ng
Focus only on these for now
![Page 26: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/26.jpg)
Shorthand assignments
CSS 161: Fundamentals of Compu3ng
Example: Equivalent To: count += 2; count = count + 2;
sum -= discount; sum = sum – discount;
bonus *= 2; bonus = bonus * 2;
time /= rushFactor;
time = time / rushFactor;
change %= 100; change = change % 100;
amount *= count1 + count2;
amount = amount * (count1 + count2);
![Page 27: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/27.jpg)
Increment & Decrement Operators • Syntax:
variable++ | ++variable | variable-‐-‐ | -‐-‐variable Note: can be applied only to a single variable
• Seman3cs: Set variable to next / previous value
• Examples int num1 = 1; char char1 = ‘B’; num1++; char1--;
• Precedence: increment/decrement before/aPer use 2*(++num1) 4 2*(num1++) 2
CSS 161: Fundamentals of Compu3ng
![Page 28: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/28.jpg)
Longer expressions with unary operators
int y = 3;
System.out.println((y++ + y++ + y--)); System.out.println(y);
CSS 161: Fundamentals of Compu3ng
![Page 29: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/29.jpg)
Longer expressions with unary operators
int y = 3;
System.out.println((y++ + y++ + y--)); System.out.println(y);
Answer:
12
4
CSS 161: Fundamentals of Compu3ng
![Page 30: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/30.jpg)
Longer expressions with unary operators
int y = 3;
System.out.println((y++ + y++ + y--)); (y=3)
3
System.out.println(y);
Answer: 12
4
CSS 161: Fundamentals of Compu3ng
![Page 31: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/31.jpg)
Longer expressions with unary operators
int y = 3;
System.out.println((y++ + y++ + y--)); (y=3) (y=4)
3 + 4
System.out.println(y);
Answer: 12
4
CSS 161: Fundamentals of Compu3ng
![Page 32: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/32.jpg)
Longer expressions with unary operators
int y = 3;
System.out.println((y++ + y++ + y--)); (y=3) (y=4) (y=5)
3 + 4 + 5 y = y – 1; System.out.println(y);
Answer: 12
4
CSS 161: Fundamentals of Compu3ng
![Page 33: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/33.jpg)
Longer expressions with unary operators
int y = 3;
System.out.println((y++ + y++ + y--)); (y=3) (y=4) (y=5)
3 + 4 + 5
y = y – 1; System.out.println(y); (y=4)
Answer: 12
4
CSS 161: Fundamentals of Compu3ng
![Page 34: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/34.jpg)
Assignment Compa3bility
CSS 161: Fundamentals of Compu3ng
doublefloatlongintshortbyte char
![Page 35: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/35.jpg)
Assignment compatability
OK
int num1 = 23;
int num2 = 'a';
double real1 = 23;
Not OK
int num3 = 23.0;
char c = 1200000;
CSS 161: Fundamentals of Compu3ng
doublefloatlongintshortbyte char
![Page 36: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/36.jpg)
Type cas3ng & coercion
• Syntax: – variable = (type) expression
• Seman3cs – Convert expression to type, assign value to variable – Only allowed if expression type is compa3ble with variable type
• Type coercion: – Automa3c type cas3ng: double float int
• Examples – short num3 = (short) ‘A’; – int num4 = (int) 1.9; //truncate to 1 – double num5 = 123; // coercion – int num6 = 1.23; // error
CSS 161: Fundamentals of Compu3ng
![Page 37: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/37.jpg)
Console Input & Output
• System.out – A Java object associated with the console (screen)
• print(), println() – Methods associated with the System.out object
• Syntax: – System.out.print(expression) – System.out.println([expression])
• Seman3cs: – Evaluate expression, output its value – println:
• expression is op3onal (hence the square brackets []) • output a carriage return / line feed (newline) at end of line
CSS 161: Fundamentals of Compu3ng
![Page 38: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/38.jpg)
Formafed output: printf
• Syntax: System.out.printf(format[,expression]*)
• Seman3cs: Use format string to output expression(s)
• Examples: System.out.printf(“Hello”); System.out.printf(“Hello\n”); System.out.printf(“%5.1f”, 1.25); System.out.printf(“%-5.1f”, 1.25); System.out.printf(“$.2f”, 1.2);
CSS 161: Fundamentals of Compu3ng
![Page 39: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/39.jpg)
Format string specifiers
CSS 161: Fundamentals of Compu3ng
![Page 40: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/40.jpg)
Console input: Scanner class
• Not built-‐in, have to import import java.util.Scanner;
• Next: create an instance of the class Syntax:
• classname variable = new classname([args]) Seman3cs:
• Create an instance of classname, ini3alize it with args, assign instance to variable
Example: • Scanner keyboard = new Scanner(System.in); • Create an instance of the Scanner class (in java.u3l package) • Ini3alize it to accept input from the system console (System.in) • Assign it to keyboard (NB: keyboard not a reserved word)
CSS 161: Fundamentals of Compu3ng
![Page 41: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/41.jpg)
Console Input Using the Scanner Class
• The method nextInt reads one int value typed in at the keyboard and assigns it to a variable: int numberOfPods = keyboard.nextInt();
• The method nextDouble reads one double value typed in at the keyboard and assigns it to a variable: double d1 = keyboard.nextDouble();
• Mul3ple inputs must be separated by whitespace and read by mul3ple invoca3ons of the appropriate method o Whitespace is any string of characters, such as blank spaces, tabs,
and line breaks that print out as white space
2-‐41 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 42: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/42.jpg)
Console Input Using the Scanner Class
• The method next reads one string of non-‐whitespace characters delimited by whitespace characters such as blanks or the beginning or end of a line
• Given the code String word1 = keyboard.next(); String word2 = keyboard.next();
and the input line jelly beans
The value of word1 would be jelly, and the value of word2 would be beans
2-‐42 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 43: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/43.jpg)
Console Input Using the Scanner Class
• The method nextLine reads an en3re line of keyboard input • The code,
String line = keyboard.nextLine(); reads in an en3re line and places the string that is read into the variable line
• The end of an input line is indicated by the escape sequence '\n' o This is the character input when the Enter key is pressed o On the screen it is indicated by the ending of one line and the beginning
of the next line • When nextLine reads a line of text, it reads the '\n' character, so
the next reading of input begins on the next line o However, the '\n' does not become part of the string value returned
(e.g., the string named by the variable line above does not end with the '\n' character)
2-‐43 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 44: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/44.jpg)
Keyboard Input Demonstra3on (Part 1 of 2)
2-‐44 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 45: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/45.jpg)
Keyboard Input Demonstra3on (Part 2 of 2)
2-‐45 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 46: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/46.jpg)
Another Keyboard Input Demonstra3on (Part 1 of 3)
2-‐46 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 47: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/47.jpg)
Another Keyboard Input Demonstra3on (Part 2 of 3)
2-‐47 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 48: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/48.jpg)
Another Keyboard Input Demonstra3on (Part 3 of 3)
2-‐48 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 49: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/49.jpg)
Lecture 4 stopped here
• Prac3ce it problems done in class o Self-‐check 1.6 – Legal iden3fiers
o Self-‐check 2.1 – Legal in literals o Self-‐check 2.2 – Expressions
• Prac3ce it problems to try by yourself o Self-‐check 2.3 (note that some problems have decimal numbers and hence will follow decimal division)
CSS 161: Fundamentals of Compu3ng
![Page 50: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/50.jpg)
Pi}all: Dealing with the Line Terminator, '\n'
• The method nextLine of the class Scanner reads the remainder of a line of text star3ng wherever the last keyboard reading leP off
• This can cause problems when combining it with different methods for reading from the keyboard such as nextInt
• Given the code, Scanner keyboard = new Scanner(System.in); int n = keyboard.nextInt(); String s1 = keyboard.nextLine(); String s2 = keyboard.nextLine();
and the input, 2 Heads are better than 1 head.
what are the values of n, s1, and s2?
2-‐50 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 51: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/51.jpg)
Pi}all: Dealing with the Line Terminator, '\n'
• Given the code and input on the previous slide n will be equal to "2", s1 will be equal to "", and s2 will be equal to "heads are better than"
• If the following results were desired instead n equal to "2", s1 equal to "heads are better than", and s2 equal to "1 head"
then an extra invoca3on of nextLine would be needed to get rid of the end of line character ('\n')
2-‐51 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 52: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/52.jpg)
Methods in the Class Scanner (Part 1 of 3)
2-‐52 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 53: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/53.jpg)
Methods in the Class Scanner (Part 2 of 3)
2-‐53 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 54: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/54.jpg)
Methods in the Class Scanner (Part 3 of 3)
2-‐54 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 55: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/55.jpg)
Java Documenta3on for Scanner
CSS 161: Fundamentals of Compu3ng
hfp://download.oracle.com/javase/6/docs/api/java/u3l/Scanner.html
![Page 56: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/56.jpg)
Programming Tip: Prompt for Input
• A program should always prompt the user when he or she needs to input some data: System.out.println( "Enter the number of pods followed by"); System.out.println( "the number of peas in a pod:");
2-‐56 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 57: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/57.jpg)
Programming Tip: Echo Input
• Always echo all input that a program receives from the keyboard
• In this way a user can check that he or she has entered the input correctly o Even though the input is automa3cally displayed as the user enters it, echoing the input may expose subtle errors (such as entering the lefer "O" instead of a zero)
2-‐57 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 58: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/58.jpg)
Self-‐Service Checkout Line (Part 1 of 2)
2-‐58 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 59: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/59.jpg)
Self-‐Service Checkout Line (Part 2 of 2)
2-‐59 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 60: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/60.jpg)
The Empty String
• A string can have any number of characters, including zero characters o "" is the empty string
• When a program executes the nextLine method to read a line of text, and the user types nothing on the line but presses the Enter key, then the nextLine Method reads the empty string
2-‐60 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 61: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/61.jpg)
Other Input Delimiters
• The delimiters that separate keyboard input can be changed when using the Scanner class
• For example, the following code could be used to create a Scanner object and change the delimiter from whitespace to "##" Scanner keyboard2 = new Scanner(System.in); Keyboard2.useDelimiter("##");
• APer invoca3on of the useDelimiter method, "##" and not whitespace will be the only input delimiter for the input object keyboard2
2-‐61 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 62: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/62.jpg)
Changing the Input Delimiter (Part 1 of 3)
2-‐62 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 63: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/63.jpg)
Changing the Input Delimiter (Part 2 of 3)
2-‐63 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 64: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/64.jpg)
Changing the Input Delimiter (Part 3 of 3)
2-‐64 Copyright © 2010 Pearson Addison-‐Wesley. All rights
reserved.
![Page 65: CSS161 Fundamentals#of#Compu3ng# …courses.washington.edu/css161/umurthy/learning... · o Incubaon#projects,#mainframe#emulator#(Javadevelopment)# • Master’s#in#Computer#Applicaons,#Naonal#Ins3tute#of#](https://reader033.fdocuments.in/reader033/viewer/2022042319/5f0946627e708231d4260afb/html5/thumbnails/65.jpg)
Next Time
• Branching mechanism