6.092: intro to java variables, types, operators · variables, types, operators cite as: evan...

25
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].

Upload: others

Post on 24-Jul-2020

21 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Assignment: TempConverter

Convert a temperature from Fahrenheit to Celcius using:

C = (5 ÷ 9) � (F - 32)

(see Assignment 1 for details)