Top Banner
MIT OpenCourseWare http://ocw.mit.edu 6.092 Introduction to Software Engineering in Java January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
22

6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

May 18, 2018

Download

Documents

trantram
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

MIT OpenCourseWare http://ocw.mit.edu

6.092 Introduction to Software Engineering in JavaJanuary (IAP) 2009

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

Page 2: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

6.092: Introduction to Java 1: Variables, Operators, Types

Page 3: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Goal

Learn enough Java to do somethinguseful

Examples: • Simulate a natural/engineering process • Manipulate PDFs • Draw pretty graphics

Page 4: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Assignments • View and submit via Stellar • Due at 7 PM the next day • Collaborate with others • Write your own code • Must submit first assignment (you will be

dropped if you donʼt: big waiting list)

Must submit a “reasonable” attempt for 7/8assignments to pass

Page 5: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

The Computer

Central Processing Unit

(CPU)

Input/Output (IO) Devices

Memory

Page 6: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

CPU Instructions

z = x + y Read location x Read location y Add Write to location z

Page 7: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Programming Languages

• Easier to understand than CPU instructions

• Needs to be translated for the CPU to understand it

Page 8: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Java

• “Most popular” language • Runs on a “virtual machine” (JVM) • More complex than some (eg. Python) • Simpler than others (eg. C++)

Page 9: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Compiling Java

Source Code (.java)

Byte Code (.class)

javac java

Page 10: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

First Program class Hello {

public static void main(String[] arguments) { // Program execution begins here

System.out.println("Hello world."); }

}

Page 11: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Compile and Run

javac Hello.java

java Hello

Page 12: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Program Structure class CLASSNAME {

public static void main(String[] arguments) { STATEMENTS

} }

Page 13: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

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 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Variables

Named location that stores a value

Form: TYPE NAME;

Example: String foo;

Page 15: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Assignment

Use = to give variables a value.

Example: foo = “IAP 6.092”;

Page 16: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

class Hello3 { public static void main(String[] arguments) {

String foo = "IAP 6.092"; System.out.println(foo); foo = "Something else"; System.out.println(foo);

} }

Page 17: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Types Limits a variable to kinds of values

String: plain text (“hello”) double: Floating-point, “real” valued number

(3.14, -7.0)

String foo = “hello”; double badPi = 3.14;

Page 18: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Operators Symbols that perform simple computations

Assignment: = Addition: + Subtraction: - Multiplication: * Division: /

Page 19: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

class DoMath { public static void main(String[] arguments) {

double score = 1 + 2 * 3; System.out.println(score); score = score / 2; System.out.println(score);

} }

Page 20: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

class DoMath2 { public static void main(String[] arguments) {

double 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 21: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

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 22: 6.092 Introduction to Software Engineering in Java January ...dspace.mit.edu/.../6-092January--IAP--2009/LectureNotes/lec1.pdf · 6.092 Introduction to Software Engineering in Java

Assignment: TempConverter

Convert a temperature from Fahrenheit toCelsius using:

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