6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan...
Transcript of 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan...
![Page 1: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/1.jpg)
6.092: Intro to Java
Variables, Types, Operators
Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering in Java, IAP 2008. MIT OpenCourseWare (http://ocw.mit.edu/), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
![Page 2: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/2.jpg)
Class Details
Instructors Usman Akeju, Evan Jones, Olivier Koch
8 Lectures + Assignments
Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering in Java, IAP 2008. MIT OpenCourseWare (http://ocw.mit.edu/), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
![Page 3: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/3.jpg)
Assignments
• Due at 4 PM the next day • Submit via email
Must submit a “reasonable” attempt for all assignments to pass
![Page 4: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/4.jpg)
Goal
Use Java to do something useful
Examples: • Simulate a natural/engineering process
• Manipulate PDFs • Draw pretty graphics
![Page 5: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/5.jpg)
The Computer
Central Processing Unit
(CPU)
Input/Output (IO) Devices
Memory
![Page 6: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/6.jpg)
CPU Instructions
z = x + y Read location x
Read location y
Add
Write to location z
![Page 7: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/7.jpg)
Programming Languages
• Easier to understand than CPU instructions
• Needs to be translated for the CPU to understand it
![Page 8: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/8.jpg)
Java
• “Most popular” language • Runs on a “virtual machine” (JVM)
![Page 9: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/9.jpg)
Compiling Java
Source Code (.java)
Byte Code (.class)
javac java
![Page 10: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/10.jpg)
First Program
class Hello { public static void main(String[] arguments) {
// Program execution begins here System.out.println("Hello world.");
} }
![Page 11: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/11.jpg)
Compile and Run
javac Hello.java
java Hello
![Page 12: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/12.jpg)
Program Structure
class CLASSNAME { public static void main(String[] arguments) { STATEMENTS
} }
![Page 13: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/13.jpg)
Second Program
class Hello2 { public static void main(String[] arguments) {
System.out.println("Hello world."); // Print once System.out.println("Line number 2"); // Again!
} }
![Page 14: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/14.jpg)
Variables
Named location that stores a value
Form: TYPE NAME;
Example: String foo;
![Page 15: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/15.jpg)
Assignment
Use = to give variables a value.
Example: foo = “IAP 6.092”;
![Page 16: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/16.jpg)
class Hello3 { public static void main(String[] arguments) {
String foo; foo = "IAP 6.092"; System.out.println(foo); foo = "Something else"; System.out.println(foo);
} }
![Page 17: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/17.jpg)
Types
Limits a variable to kinds of values
String: plain text (“hello”) int: Integer (-42, 7)
![Page 18: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/18.jpg)
Operators
Symbols that perform simple computations
Assignment: = Addition: + Subtraction: -Multiplication: * Division: /
![Page 19: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/19.jpg)
class DoMath { public static void main(String[] arguments) { int score; score = 1 + 2 * 3; System.out.println(score); score = score / 2; System.out.println(score);
} }
![Page 20: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/20.jpg)
Floating-point: double
Floating-point: number with a decimal point
Example: double pi = 3.14;
![Page 21: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/21.jpg)
Conversions
“safe” conversions are automatic double score = 5;
“unsafe” conversions are not int score = 5.0;
![Page 22: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/22.jpg)
class DoMath2 { public static void main(String[] arguments) { int score; score = 1 + 2 * 3; System.out.println(score); double copy = score; copy = copy / 2; System.out.println(copy); score = copy; System.out.println(score);
} }
![Page 23: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/23.jpg)
Forced Conversions: Casts
Format: (TYPE)
Example: int score = (int) 5.5;
![Page 24: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/24.jpg)
class DoMath3 { public static void main(String[] arguments) { int score; score = 1 + 2 * 3; System.out.println(score); double copy = score; copy = copy / 2; System.out.println(copy); score = (int) copy; System.out.println(score);
} }
![Page 25: 6.092: Intro to Java Variables, Types, Operators · Variables, Types, Operators Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software](https://reader034.fdocuments.net/reader034/viewer/2022042922/5f6c2535db4eb325e62f3bef/html5/thumbnails/25.jpg)
Assignment: TempConverter
Convert a temperature from Fahrenheit to Celcius using:
C = (5 ÷ 9) � (F - 32)
(see Assignment 1 for details)