25
6.092: Java for 6.170 Lucy Mendel MIT EECS MIT 6.092 IAP 2006 1

mit lecture ppts of core java Lecture 1A

  • Upload
    zebain

  • View
    1.626

  • Download
    1

Embed Size (px)

DESCRIPTION

this is the ppt ..from the mit lecturers......of core java go have fun.....but study well.....

Citation preview

Page 1: mit lecture ppts of core java Lecture 1A

6.092: Java for 6.170

Lucy Mendel MIT EECS

MIT 6.092 IAP 2006 1

Page 2: mit lecture ppts of core java Lecture 1A

Course Staff

z Lucy Mendel z Corey McCaffrey z Rob Toscano z Justin Mazzola Paluska z Scott Osler z Ray He

Ask us for help!

MIT 6.092 IAP 2006 2

Page 3: mit lecture ppts of core java Lecture 1A

Class Goals

z Learn to program in Javaz Java z Programming (OOP)

z 6.170 problem sets are not supposed to take you 20 hours! z Tools, concepts, thinking

MIT 6.092 IAP 2006 3

Page 4: mit lecture ppts of core java Lecture 1A

Logistics

z 5 days long, optional second week z 2 hrs lecture, 1 hr lab z End of week might be 1 hr lecture, 2 hr lab z Breaks!

z Labs z Work on homework with staff assistance (like LA

hours in 6.170) z Mandatory even for listeners z Each is expected to take ~1-2 hrs

MIT 6.092 IAP 2006 4

Page 5: mit lecture ppts of core java Lecture 1A

Object Oriented Programming

z Objects have statez A person is an object and has a name, age, SS#,

mother, &e. z Programmers call methods on objects to

compute over and potentially modify that state z programmer: How old are you?z object: I am 22.z programmer: Today is your birthday!z object: I have incremented my age by 1.

MIT 6.092 IAP 2006 5

Page 6: mit lecture ppts of core java Lecture 1A

Java Programpackage hello; import java.util.System;

class HelloWorld { String myString;

void shout() {myString = new String("Hello, World!“); System.out.println(myString);

}

public static void main(String[] args) { HelloWorld myHelloWorld = new HelloWorld(); myHelloWorld.shout();

}

MIT 6.092 IAP 2006

} 6

Page 7: mit lecture ppts of core java Lecture 1A

Class

z Template for making objects

z Java is about objects Æ everything is in a class

class HelloWorld { // classname … <everything> …

}

MIT 6.092 IAP 2006 7

Page 8: mit lecture ppts of core java Lecture 1A

Field

z Object state

class Human {int age;

}

<class type> <variable name>;

MIT 6.092 IAP 2006 8

Page 9: mit lecture ppts of core java Lecture 1A

Making objects

Human lucy = new Human();

z All object creation requires a “new” z objects = instances (of classes) z lucy is a pointer to the object z We assign the constructed object to lucy

<type> <variable name> = <new object>; MIT 6.092 IAP 2006 9

Page 10: mit lecture ppts of core java Lecture 1A

Using Objects

Human lucy = new Human();lucy.age = 22; // use ‘.’ to access fields

Human david = new Human();david.age = 19;System.out.println(lucy.age); // prints 22

System.out.println(david.age); // prints 19

MIT 6.092 IAP 2006 10

Page 11: mit lecture ppts of core java Lecture 1A

22

z Why did we not have to write

lucy.age = new int(22); ??

MIT 6.092 IAP 2006 11

Page 12: mit lecture ppts of core java Lecture 1A

Primitives

z Not everything is an object

z Some things are too simple and too frequently used to be bothered with objects: z boolean, byte, short, int, long, double, float, char

MIT 6.092 IAP 2006 12

Page 13: mit lecture ppts of core java Lecture 1A

Field myString

class HelloWorld { String myString;

void shout() { myString = new String(“Hello, World!“); System.out.println(myString);

}

public static void main(String[] args) { HelloWorld myHelloWorld = new HelloWorld(); myHelloWorld.shout();

} }

MIT 6.092 IAP 2006 13

Page 14: mit lecture ppts of core java Lecture 1A

Methods

z Process object state

<return type> <method name>(<parameters>) { <method body>

}

myHelloWorld.shout(); // use ‘.’ to access methods

MIT 6.092 IAP 2006 14

Page 15: mit lecture ppts of core java Lecture 1A

Constructors

z Constructors are special methodsz no return type z use them to initialize fields z take parameters, normal method body (but no

return)

MIT 6.092 IAP 2006 15

Page 16: mit lecture ppts of core java Lecture 1A

Method Body

String firstname(String fullname) { int space = fullname.indexOf(“ ”); String word = fullname.substring(0, space); return word;

} z Any number of parameters z declare local variables z return one thing (void = return nothing)MIT 6.092 IAP 2006 16

Page 17: mit lecture ppts of core java Lecture 1A

Control Flow

if (lucy.age < 21) { // don’t do stuff

} else if (lucy.hasCard()) { // do other stuff

} else { // doh

}

if (<predicate1>) { …

} else if (<predicate2>) { …

} else if (<predicate3>) { …. } else if (<predicateN>) {

… } else { … }

MIT 6.092 IAP 2006 17

Page 18: mit lecture ppts of core java Lecture 1A

Predicates

z predicate = true or false (boolean)z <, >, ==, <=, >=, !

box.isEmpty()box.numberBooks() == 0

!(box.numberBook() > 1)box.numberBooks != MAX_NUMBER_BOOKS

MIT 6.092 IAP 2006 18

Page 19: mit lecture ppts of core java Lecture 1A

For Loop

for (int i = 0; i < 3; i++) { System.out.println(i); // prints 0 1 2

}

for (<initialize> ; <predicate> ; <increment>) {execute once every time

Stop when predicate is false

MIT 6.092 IAP 2006 19

Page 20: mit lecture ppts of core java Lecture 1A

While Loop

int i = 0; while (i < 3) {

System.out.println(i); // prints 0 1 2 }

while (<predicate>) { …

} MIT 6.092 IAP 2006 20

Page 21: mit lecture ppts of core java Lecture 1A

Combining Predicates

z && = logical and z || = logical or

a. lucy.age >= 21 && lucy.hasCard

b. !someone.name.equals(“Lucy”))c. (!true || false) && true

MIT 6.092 IAP 2006 21

Page 22: mit lecture ppts of core java Lecture 1A

Arrays

z Objects, but special like primitives

String[] pets = new String[2]; pets[0] = new String(“Fluffy”); pets[1] = “Muffy”; // String syntactic sugar

String[] pets = new String[] {“Fluffy”, “Muffy”}; System.out.println(pets.length); // print 2 MIT 6.092 IAP 2006 22

Page 23: mit lecture ppts of core java Lecture 1A

Whoa, how many types are there?

z primitives z int a = 3 + 5;

z Objects z Integer a = new Integer(3); z Integer sum = a.add(5);z Arrays

MIT 6.092 IAP 2006 23

Page 24: mit lecture ppts of core java Lecture 1A

Objects Cause Trouble!!

z pets[3] >> halt program, throw ArrayOutOfBoundsException

z String[] str; z str.length >> halt, throw NullPointerExceptoin

z Integer a = new Integer(3); // aÆ[3] z a.add(5); // aÆ[3] z a = a.add(5); // aÆ[8] MIT 6.092 IAP 2006 24

Page 25: mit lecture ppts of core java Lecture 1A

Break (10 min)

z When we get back, more on Objects from Corey

MIT 6.092 IAP 2006 25