Java Variables, Instantiation, Arrays
Instantiation
BankAccount myAccount = new BankAccount (20202, "fred", 100.00)
- Memory that contains the instance date for myAccount
- Also contains references to methods defined for objects of this class
- Java requests dynamic memory when NEW is Called
Valued Meethods return a single value, and should be used in an expression
- Returns a value (primitive type)
- Void methods – no return
- Arguments are associated in order with formal parameters
- Formal parameter in the method becomes an alias for the argument passed to it, treated as a local variable
Formal parameter is discarded when method terminatesPass by value- For primitive type, parameter is initialized to value of argument in call
- For primitive type, parameter is initialized to value of argument in call
Pass by reference- For a class type, formal parameter is initialized to the address of the object in the call
- For a class type, formal parameter is initialized to the address of the object in the call
Garbage memory – removed if not being used by JVM
Mutator method – setter method – changeable parameters
Default value of every VALUE in ARRAY is NULL!
Arrays- They are data structures
- Contain primitive or objects
- Ex – data[0] represents a element in a allocated spot
- Data.length – gives the length of the array
- They have fixed length
- Arrays are always passed by reference
- They are data structures
Partially filled arrays- Array length = max number of elements in array
- Usually it is partially filed
- Do not overfill the array or Indexoutofboundsexception will be thrown
Resize array if underestimated size- Must make new array
- Copy old elements to new array
- Must make new array
Resizing is bad- Takes time, memory
- Bad because have to copy over array
- Takes time, memory
- Array length = max number of elements in array
Designing Class
- Nouns are classes
- Verbs are methods
Cohesion- Represent single concept
- Public interface of class is cohesive
- Represent single concept
Post Conditions
- Condition that is true after a method has completed
- Return something if the program fulfills its code
Project 1
- Create DiceTester
- Gets user input (n)
Repeats- RunTest(n)
- Report()
- RunTest(n)
Until user chooses to quit- Note:
userinterface is developed in lab 2
- Note:
DiceTester
Constructor- Sets up arrays for counters
- Sets up arrays for counters
RunTest()- Generates and counts dice rolls
- Generates and counts dice rolls
Report()- Analyzes counter arrays
- Analyzes counter arrays
- User Interface
Interface:
- A interface is a formal contract
- If called, need to implement all methods or program will not compile
- Contain public constants, signatures for public methods, comments
- Interface type is similar to a class, but many different differences:
INTERFACES- All methods in Interface are ABSTRACT
All methods are automatically public
- Public modified not needed
- Public modified not needed
- Interface type does not have instance fields
Ex:
Public interface doable {
MAX = 100;
Void doThis();
Int doThat();
Void doThis (float value, char ch);
Boolead done (int num);
- All methods in Interface are ABSTRACT
}
- Interface cannot be instantiated
- Multiple classes can implement same interface
- A class can implement more than one interface
An interface can be used as a data type"
Public void someMethod ( someInterface x){}
in any class that implements an interface:
- methods return type or parameter type may be Object
a type case would probably be needed within that method
public class CanDo implements Doable{
public void doThis(){
// code you want to put in
}
Publick int doThat(){
// code that you want to put in
}
…… and other methods in DoAble Interface.
}
- methods return type or parameter type may be Object
Comparable Interface
- compareTo method returns 0, positive, negative number. If positive then first object is greater than second. Negative number means first object is less than second. If 0 then both equal
- up to the coder to determine what makes a object less greater or equal
- compareTo method returns 0, positive, negative number. If positive then first object is greater than second. Negative number means first object is less than second. If 0 then both equal
Interfaces and Polymorphism
Interfaces
- Contract. Need to implement methods no matter what
- Specifies operations that must be implemented
- Contract. Need to implement methods no matter what
Public class DebtList
{
Private debt[] mydebts;
Private int count;
Public debtList (int size)
{
Count = 0
myDebts = new Debt[size];
}
public void addSorted (Debt d)
{
int k = 0;
while (K<count && d.greaterThan(myDebts[k]))
k++;
for (int j=count; j>k; j--)
mydebts[j] = mydebts[j-1];
myDebts[k] = d;
count++;
}
- Interface creates a contract – if you call it then ALL methods need to be implemented
How to use Class:
GenericList debtList = new GenericList(10);
GenericList shapeList = new GenericList(10);
Debtlist.addSorted (new debt(5000.00 , 327.51, 0.055 ) );
debtList.addSorted ( new debt (2000, 200, 0.06) );
shapeList.addSorted ( new circiel (3.0) );
shapeList.addSorted ( new square (7.1) ) ;
Nothing stopping from putting Shape in Debt or Debt in Shape
- debtList.addSorted ( new circle (3.0 )); //No error but wrong
- debtList.addSorted ( new circle (3.0 )); //No error but wrong
- if ( p instance of Debt)
Packages
- Set of related classes
To put classes into a package, you must place a line:
- Package packageName;
- Package packageName;
- Set of related classes
You can leave a response, or trackback from your own site.
Wednesday, April 14, 2010
nahid5692001
,
0 Response to "Java Variables, Instantiation, Arrays"
Post a Comment