46-935 java lecture 1 introduction. course web site mm6

40
46-935 Java Lecture 1 Introduction

Post on 19-Dec-2015

219 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

46-935 Java

Lecture 1 Introduction

Page 3: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Course TA

Guozhu (Patrick) Zhu

[email protected]

Office Hours: Wednesday: 7:30-10:00 Fast Lab

Page 4: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Structure of the Course

•Lectures / class participation

•Homeworks (3 programming projects)

•Final exam

Page 5: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Readings

The required text is “Just Java” Fifth edition, Peter van der Linden

Readings will be assigned each week.

Readings from the web will also be assigned.

Page 6: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Grading

• Homework (3) 50%

• Final Exam 50%

Page 7: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Two mini Course Coverage

The Java programming Language

Server Side Java (JSP’s, Servlets)

XML/FpML and Web Services

Page 8: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Week 1

1. Notes From “Just Java” by Linden

2. The BigInteger class

Page 9: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

1. Notes From “Just Java”

Chapter 1

Page 10: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Compiling and Executing

javac SomeClass.java produces syntax errors or SomeClass.class

java SomeClass runs the main routine found in SomeClass.class

Page 11: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Chapter 1 Example Program

import java.awt.*;

class MyFrame extends Frame {

static int x = 0, y = 120; static int i = 0; static int horizScroll = 1; static int mess = 0; Font fb = new Font("TimesRoman", Font.BOLD, 36); String msg[] = { "Java", "Portable", "Secure", "Easy" }; Color color[] = { Color.blue, Color.yellow, Color.green, Color.red };

Page 12: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

public void paint(Graphics g) {

g.setFont(fb);

g.setColor( color[i]) ;

g.drawString(msg[i],x, y); }

Page 13: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

static public void main(String s[]) throws Exception { MyFrame mf = new MyFrame(); mf.setSize(200,200); int pixelsPerLine = 200, totalLines = 4; mf.setVisible(true);

for(int j = 0; j < pixelsPerLine * totalLines; j++) { Thread.sleep(25); mf.repaint(); if( horizScroll == 1) { if(( x += 3) < 200) continue; i = ++i % 4; x = 50; y = 0; horizScroll = 0; }

Page 14: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

else { if(( y += 3) < 200) continue; i = ++i % 4; x = 0; y = 120; horizScroll = 1; } } System.exit(0); }}

Page 15: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Text moves left to right and then top to bottom.

Page 16: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

MyFrame.java

Two Steps:

javac MyFrame.java java MyFrame

Page 17: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Chapter 2 OOPAbstraction

Ignore the details Java provides a very large library of Classes You can’t get by without ignoring details

Encapsulation

Information hiding Data and operations are bundled together Java provides the Class mechanism

Composition

The “has a” relationship

Page 18: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Chapter 2 OOP

Inheritance

A Car “is a” kind of Vehicle So, properties of a vehicle should be inherited by Car Java provides the “extends” key word

Polymorphism

What happens when we say a = b + c; It depends on the types of the operands Java provides both static and dynamic polymorphism

Page 19: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Primitive Types & Class Types

int a; what picture do we draw?

a

a = 3; what picture do we draw?

a 3

Page 20: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Primitive Types & Class Types

BigInteger b; what picture do we draw? b

b = new BigInteger(“1234567897272727272”);

b

Page 21: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Creating a Class and an Object

class Fruit { public int grams; public int calsPerGram;}

public class TestFruit { public static void main(String a[]) {

Fruit f = new Fruit(); f.grams = 30; }}

Some issues: -initialization -encapsulation -data without methods -like a struct in c

Page 22: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Adding a constructorclass Fruit { public int grams; public int calsPerGram; Fruit(int grams, int c) { this.grams = grams; calsPerGram = c; }}

public class TestFruit { public static void main(String a[]) { Fruit f = new Fruit(100,5); f.grams = 30; }}

Page 23: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Adding Some Methodsclass Fruit { private int grams; private int calsPerGram; Fruit(int grams, int c) { this.grams = grams; calsPerGram = c; } int getGrams() { return grams; } int getCalsPerGram() { return calsPerGram; } void setGrams(int g) { grams = g; } void setCalsPerGram(int cpg) { calsPerGram = cpg; } }

Page 24: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

public class TestFruit {

public static void main(String a[]) {

Fruit melon = new Fruit(4,5); Fruit banana = new Fruit(2,6);

banana.setGrams(87);

System.out.println(banana.getGrams());

}}

Page 25: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

A Glimpse at Inheritance

class Fruit { private int grams; private int calsPerGram; Fruit(int grams, int c) { this.grams = grams; calsPerGram = c; } int getGrams() { return grams; } int getCalsPerGram() { return calsPerGram; } void setGrams(int g) { grams = g; } void setCalsPerGram(int cpg) { calsPerGram = cpg; } }

Page 26: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

class Citrus extends Fruit { private double acidity; Citrus(double acid, int grams, int calsPerGram) { super(grams,calsPerGram) ; acidity = acid; } double getAcidity() { return acidity; } void setAcidity(double a) { acidity = a; }}

public class TestFruit { public static void main(String a[]) { Citrus lemon = new Citrus(5.0,4,5); System.out.println(lemon.getAcidity()); System.out.println(lemon.getGrams()); }}

Page 27: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Per-Instance & Per-Class Members

The keyword static makes something exists on a per-class basis.

The keyword is used for

data methods blocks classes

Page 28: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Static dataclass Employee { String name; long salary; short employee_number; static int total_employees;}public class EmployeeTest {

public static void main(String arg[]) { Employee a = new Employee(); Employee b = new Employee(); }}

We have twocopies ofname, salary,employee_number.

We have one copyof total_employees.

total_employeesis in an object of class Class thatis used to createEmployee objects.

Page 29: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Static methods

public class MainIsStatic {

int a; void foo() { a = 3; } public static void main(String arg[]) {

MainIsStatic x = new MainIsStatic(); MainIsStatic y = new MainIsStatic(); x.foo(); }}

Conceptually,there are twofoo()’s. Therereally are twoa’s and there isonly one main().

x’s a is 3 and y’s a is 0.

Page 30: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Passing Primitive Parameters

void foo(int a) { // changes to a are local to foo()}

You can’t pass a pointer to an int (like c).You can’t pass a reference to int (like c++).You can just pass an int.

Page 31: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Passing Object Parameters

void foo(Object a) { // changes to the Object referenced by a // are known outside of foo().}

We can’t pass the object itself (like c++)We can’t pass a struct by value (like c)We can only pass a pointer to an Object.

Page 32: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

2. The BigInteger Class

Page 33: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Object-Oriented Programming and Java’s BigInteger Class

• Abstraction The focus is on “what” not “how”.• Encapsulation Information hiding is used to promote modularity and the use of abstractions.• Polymorphism We may want to treat an object not as an instance of its specific type but as an instance of its base type.• Inheritance OOP promotes code reuse via the “is-a” relationship.• Composition OOP promotes code reuse via the “has-a” relationship.

Page 34: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Using Java’s BigInteger Classimport java.math.*;public class TestBigInts { public static void main(String args[] ) {

BigInteger x = new BigInteger("1234567890123" + "45678901234567890"); BigInteger y = new BigInteger("93939393929292" + "9191919191919192");

BigInteger z = x.multiply(y); System.out.println(z);

}}

AbstractionEncapsulation

Page 35: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

The toString() method

public class BigInteger {

public String toString() {

// Returns the decimal String representation of this // BigInteger. // Overrides: toString() in class Object }}

A BigInteger “is a”Object.

The println() methodcalls toString().

Polymorphism

Page 36: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Another Exampleimport java.math.*;import java.util.*;

public class TestBigInts {

public static void main(String args[] ) {

BigInteger m = new BigInteger(4,10, new Random(2456)); BigInteger n = new BigInteger(4,10, new Random(94));

if(m.compareTo(n) < 0) System.out.println(m + " < " + n ); else System.out.println(m + " >= " + n ); }

}

bitLength - bitLength ofthe returned BigInteger.

certainty - a measure of the uncertainty that the caller is willing to tolerate. Prob(prime) = 1-(2-10).

A Random objectassists with the computation.

Page 37: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Consider compareTo()if(m.compareTo(n) < 0) System.out.println(m + " < " + n ); else System.out.println(m + " >= " + n );

public class BigIntegerextends Numberimplements Comparable

Comparable is an interface that BigIntegerimplements.

The BigInteger classtherefore has a methodcalled compareTo().

Any method foo(Comparable x) canbe called with a BigInteger.

Inheritance and Polymorphism

Page 38: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Another exampleimport java.math.*;public class TestBigInts {

public static void main(String args[] ) { BigInteger m = new BigInteger("34"); foo(m); } public static void foo(Number n) { long x = n.longValue(); x++; System.out.println("x = " + x); }}

BigInteger extendsthe abstract Number class.

No problem

Page 39: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Compositionimport java.math.*;

class BigFraction {

private BigInteger numerator; private BigInteger denominator;

public BigFraction(BigInteger num, BigInteger denom) {

numerator = num; denominator = denom; } public BigFraction(String num, String denom) {

this(new BigInteger(num), new BigInteger(denom)); }

A BigFraction“has-a” BigIntegernumerator and a BigInteger denominator.

Page 40: 46-935 Java Lecture 1 Introduction. Course Web Site mm6

Composition (cont.)public String toString() { return numerator + "/" + denominator; }}

public class TestBigInts {

public static void main(String args[]) {

BigFraction x = new BigFraction("1","2");

System.out.println(x); }}