Some questions people had in class #34

 

Can states be made up of an unlimited number of things?

 

Pretty much. They can include anything you need to keep track of (limited by amount of computer memory available, but that’s a lot)

 

Why do you have to be more careful with withdrawal than deposit?

 

Because the only thing that can go wrong with a deposit is if the amount being deposited is negative.  With withdrawal, not only can it go wrong if the amount is negative, but also if the amount to be withdrawn is more than is in the account. The withdrawal method must catch and stop both of these from happening.

 

Are we going to finish talking about withdrawal on Friday?

 

Ok. It is not much different than deposit though.

 

Are all of these things just for keeping the code shorter?

With a lot of these methods, can you write a longer main that would eliminate side classes, be it less efficient?

 

Ok, first direct answers. These do make the main code shorter, but that’s not the main advantage. You certainly could write a longer main instead – and it would be more efficient – for the initial programmer, and for running the program on the computer. However, by creating classes and putting code in them we 1) make it easier to divide up work between multiple people and 2) make it easier to re-use code – code that is somewhere in main would be hard to find and re-use, code that is in a separate class is re-used merely by importing the class; 3) makes the code safer – less likely to have bugs – since the methods in the class can centralize checking to make sure that info is ok – making it impossible for bad data to get in.

 

What is the boolean in the public boolean deposit (double amount)?

 

Ok, first, methods (except constructors) return some type of result – as with mathematical functions producing a result.  A “call” of that method (which requests its behavior) can go any place in the program where the return type can go. The type of result to be returned is defined in the method header after the visibility keyword (public or private). So, the deposit method, when it is done, returns a boolean – a true or a false to the “caller” – the place where the method is requested (or “called” in programming lingo).  When I designed the Account class, I determined that users of deposit (programmers who wrote code that “calls” deposit) might want to know whether the deposit was successful of not. So I defined deposit to return a boolean.  The code that calls deposit could then assign the result into a boolean variable and then test the result to see if the deposit was successful.

 

Are Booleans used often in writing methods?

 

Yes, very often. If the calling code needs to know whether a method (behavior) was successful (and we don’t need to return something else), then returning a boolean is very helpful.

 

 

Can an object name be the same as for another class?

 

Suppose you have two classes that you are importing, Skater and Judge (you haven’t seen this yet, but it could exist). I think you are asking could we do both of the following:

Skater first = new Skater (“Kwan”, “USA”);

Judge first = new Judge( whatever parameters are needed);

If that is the question, the answer is “No”. The system could not tell the difference between the two later in the program.

 

 

I am still unsure how the user can use my class after I write it.

 

You give them the code, over the network, or on disk or whatever. They put it in a package with /near their own stuff. They import the class if it is not in the same package as their work. Then they proceed as you have done on assignments on which you used my code..

 

Everything, especially how to write a method.

How are we going to do tomorrows assignment in the lab?

Wondering if I’m going to be able to program classes tomorrow.

 

Writing a method is like writing main (main is actually a method), except that most methods are shorter than main.  You need to identify what will be known/given – that will be different (main starts from scratch – nothing given) and what you need to accomplish. Then plan out a sequence of steps to carry it out. Tomorrow’s methods will be very short.  Still, at least the first one will be a challenge. But, we learn and grow by stepping up to challenges. And we’ll be there to help you get over the hump.