week 2 1.notes from “just java” by linden 2.an example java class - the biginteger class...
Post on 22-Dec-2015
224 views
TRANSCRIPT
![Page 1: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/1.jpg)
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
![Page 2: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/2.jpg)
1. Notes From “Just Java”
![Page 3: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/3.jpg)
Compiling and Executing
javac SomeClass.java produces syntax errors or SomeClass.class
java SomeClass runs the main routine found in SomeClass.class
![Page 4: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/4.jpg)
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 5: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/5.jpg)
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 6: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/6.jpg)
Primitive Types & Class Types
int a; what picture do we draw?
a
a = 3; what picture do we draw?
a 3
![Page 7: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/7.jpg)
Primitive Types & Class Types
BigInteger b; what picture do we draw? b
b = new BigInteger(“1234567897272727272”);
b
![Page 8: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/8.jpg)
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 9: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/9.jpg)
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 10: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/10.jpg)
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 11: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/11.jpg)
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 12: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/12.jpg)
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 13: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/13.jpg)
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 14: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/14.jpg)
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 15: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/15.jpg)
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 16: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/16.jpg)
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 17: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/17.jpg)
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 18: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/18.jpg)
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 19: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/19.jpg)
2. The BigInteger Class
![Page 20: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/20.jpg)
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 21: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/21.jpg)
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 22: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/22.jpg)
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 23: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/23.jpg)
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 24: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/24.jpg)
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 25: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/25.jpg)
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 26: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/26.jpg)
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 27: 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](https://reader030.vdocument.in/reader030/viewer/2022032704/56649d795503460f94a5d659/html5/thumbnails/27.jpg)
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); }}