Download JustJava1 - Andrew.cmu.edu

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Week 2
1. Notes From “Just Java” by
Linden
2. An example Java class - The
BigInteger class
3. Homework 1 Pricing a fixed-rate
bond
1. Notes From “Just Java”
Compiling and Executing
javac SomeClass.java
produces syntax errors
or SomeClass.class
java SomeClass
runs the main routine
found in SomeClass.class
OOP
Abstraction
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
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
Primitive Types & Class Types
int a;
what picture do we draw?
a
a = 3;
what picture do we draw?
a
3
Primitive Types & Class Types
BigInteger b;
what picture do we draw?
b
b = new BigInteger(“1234567897272727272”);
b
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
Adding a constructor
class 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;
}
}
Adding Some Methods
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; }
}
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());
}
}
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; }
}
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());
}
}
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
Static data
class 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 two
copies of
name, salary,
employee_number.
We have one copy
of total_employees.
total_employees
is in an object of
class Class that
is used to create
Employee objects.
Static methods
public class MainIsStatic {
int a;
void foo() { a = 3; }
public static void main(String arg[]) {
Conceptually,
there are two
foo()’s. There
really are two
a’s and there is
only one main().
MainIsStatic x = new MainIsStatic(); x’s a is 3 and
MainIsStatic y = new MainIsStatic(); y’s a is 0.
x.foo();
}
}
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.
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.
2. The BigInteger Class
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.
Using Java’s BigInteger Class
import java.math.*;
public class TestBigInts {
public static void main(String args[] ) {
Abstraction
Encapsulation
BigInteger x = new BigInteger("1234567890123" +
"45678901234567890");
BigInteger y = new BigInteger("93939393929292" +
"9191919191919192");
BigInteger z = x.multiply(y);
System.out.println(z);
}
}
The toString() method
public class BigInteger {
public String toString() {
A BigInteger “is a”
Object.
The println() method
calls toString().
// Returns the decimal String representation of this
// BigInteger.
// Overrides: toString() in class Object
}
}
Polymorphism
Another Example
import java.math.*;
import java.util.*;
public class TestBigInts {
public static void main(String args[] ) {
bitLength - bitLength of
the returned BigInteger.
A Random object
assists with the
computation.
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 );
}
}
certainty - a measure of
the uncertainty that the
caller is willing to
tolerate. Prob(prime) =
1-(2-10).
Consider compareTo()
if(m.compareTo(n) < 0)
System.out.println(m + " < " + n );
else
System.out.println(m + " >= " + n );
public class BigInteger
extends Number
implements Comparable
Inheritance and Polymorphism
Comparable is an
interface that BigInteger
implements.
The BigInteger class
therefore has a method
called compareTo().
Any method
foo(Comparable x) can
be called with a
BigInteger.
Another example
import java.math.*;
public class TestBigInts {
BigInteger extends
the abstract Number class.
public static void main(String args[] ) {
BigInteger m = new BigInteger("34");
foo(m);
No problem
}
public static void foo(Number n) {
long x = n.longValue();
x++;
System.out.println("x = " + x);
}
}
Composition
import java.math.*;
class BigFraction {
private BigInteger numerator;
private BigInteger denominator;
A BigFraction
“has-a” BigInteger
numerator and a
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));
}
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);
}
}