The This reference any object can refer to itself by using the keyword 'this'
public void setName(String nm) { name = nm; //stored in global variable name }
This can be written aspublic void setName(String name) { this.name = name; }
//instead of 'self' use the word 'this'. Only on back end stuff, setting methods. Comparing two objects; passing an object as a parameter using "this" Often necessary to compare two obj to see if theyre the same bacct1 and bacct2
public boolean compare(BankAcct ba) { boolean result = false if (this.acctID == ba.acctID) if (this.name.equals(ba.name)) return true; return false }
Returning an object from a method assume you are writing a programing having zombie class. when the player achieves a certain lvl...............................................
public Zombie copy() { Zombie newZ = new Zombie(); newZ.setName(this.getName()); newZ.setPoints(this.getPoints()); newZ.setNumLives(this.getNumLives()); return newZ; }
Zombie z2 = copy(z1); //z1 is the object 'this' public void display() { System.out.println("address of "+zombieName_": "+this"); --address of current obj. }
Overloading methods Overloaded methods are methods within a class that have the same name, but different parameter lists (also called method's signature) a default constructor and a non defualt constructor a class can have 2 display methods one display has no parameters and outputs on the OS screen another ___ has graphics g and x and y and has parameters and it outputs to the JFRAME
1 default constructor, as many non default as I want default constructor creates default values, 0 stuff out, fresh slate. Initializes. non default constructor has list of parameters. This is overloaded method. Size, number of, list are the difference between default and non default
public void display() { } public void display(_---,----,---,----,--) { }
public void display(Graphics) { z1.display(this, g, 50, 100); z1.display() }
i can have 8 or 9 display buttons as long as they match when called
Aggregation Any class can contain one or more other classes Consider the BankAcct class it does not make sense to define a class that only one customer can use the bank will have many customers so define a bank class that contains an array of customer bankaccts
c++ has linked list, java cant use linked list very well arraylists has idea of linked lists, linked lists can be deleted (deleting a node) with java, arraylist array is like array with ability to add, to delete something with array i still have to have the spot available in the array
list method details isEmpty() isFull() search(searchValue) insert delete display displayOne
array of 5 slots, all filled up. use isFull() or isEmpty() check ID
call the default constructur, this will reset the array (what it's designed for) count each time being added when insert, first check is to make sure counter is less than cap counter + 1
//reorder array arr[i] = arr[i +1] assuming room to add (if count == max) then add. // from spot 2 to 4, i +1 will move each for i = (spot that I deleted) x, x less than=Count end
insert is count == maxnum, if not then count+1 and put new guy in there search for using match, smart way of deleting an array by not cheating whatever spot i start from is where I want to delete. Things in front of it go cnt - 1
So to delete an array without cheating i have to clear the other objects so if I want to implement something into Array[4] then I do for(int i = 4, 4 less than= Count, x++) arr[x] = arr[x+1] arr[cnt+1] import java.awt.*;
public class Bank { private int numOfAccts; private Bankacct[] bankaccts = new Bankacct[10];
public Bank() { numOfAccts = 0; for(int = i, i less than bankaccts.length; i++) { bankaccts[i] = new Bankacct(); }
this zeroes out the array 0-9
numOfAccts = 0;
bankaccts[numOfAccts].setName("Smitty");
bankaccts[numOfAccts].setId(111);
bankaccts[numofAccts].setBalance(100.00);
numOfAccts = count
numOfAccts++
public int search(int id) // pass in id
{
int index = 0; //start at 0
while(index less than numOfAccts) //keep searching until match is found or all ids are search
{
if (bankaccts[index].getId() == id) // since acctid is private in bankacct class
return index; //to call the bankacct getId method to see a copy of its data
else //iff the current acctID matches the id passed in, stop else
index++; //go to the next acct in the array and try again
}
//This instance is better to use while loop rather than for. a Chk // can be any number of times, varying number, because index is not static // could go through once, fifteen times, whatever. -1 if not foundpublic int insert(String nm, int id, float bal)
{
if (numOfAccts greater than= bankaccts.length) //if array is full
return 0; //0 is full
int index = search(id);
if (index == -1)
{
bankaccts[numOfAccts].setName(nm);
~~~~~~~
~~~~~~~~~
numOfAccts++
return 1;}
return -1;
}
-1 = Sentinal Value ( minimum checkpoint, representing not found )
-------------------------------------public int insert(String nm, int id, float bal)
{
if (numOfAccts greater than= bankaccts.length)
return 0
int index = search(id);
if (index == -1)
{
if (bankaccts[numOfAccts].assign(nm, id, bal) == true) //Better for organizing compared to above
{
numOfAccts++;
return 1;
}
else
return 2;
}
return -1
}
//assign is a method i set as programmer for better organization compared to method above
public void display (Graphics g, int x, int y)
{
for(int i = 0, i less thannumOfAccts; i++)
{
bankaccts[i].display(g,x,y)
}
}
// This will display all contents in array
/* delete
Ok
Delete ID 195
1)Pass in user selected ID as parameter
2) search for index value where id 195 is found
a) if not found return 01
b) else subtract 1 from numOfAccts
3)write data from numOfAccts position on top of the data to be deleted (overwrite),
that is the data found at the index returned by search
a)remember, use get methods to get all data at location numOfAccts
Better
NumOfAccts 5
3) write the address from ~~~~~~~~~~~~~~~~
NumOfAccts 4
assuming count at 3, to delete array[1], copy array[4] into array[1] then move last pointer to array[3]
It wont ever check array[5] because by deleting [4] into [1]
by overwriting a previoous array I am deleting it sorta
one being deleted at a time.
array[3] moving to array[0] while pointing at array[2]
//This way is probably easiest, better than moving each one down 1 at a time.
array[i (spot in array) ] = array[counter (whatever the last guy in the array) ]
Array[7]
Counter pointing at 4, last spot in array that's filled (++)
Take data from array[4] and place into array[1]
new counter is array[3] then count +1
is count equals 7 and if it is then its at the end which displays a msg
is cutting the line
very similar to linked list from c++
taking this guy out, putting in new position, not deleting but copying and placing into new position better than having everyone shift over
public class TestBank extends JFrame implements~~