AP Computer Science - Classes and Objects (7)
Latest Quiz For The Students
True/False:
- The following lines run properly: double findTax = 6; double taxRate = .06*findtax; (F - findtax is different from findTax)
- System.out.print(13-3*6/4%3); returns 0. (F - 12)
- You only need to use Scanner input = new Scanner(System.in); ONE time for multiple inputs. (T)
- The integer 2^32+1 will have a weird output. (T)
- The command i * = 4 is equivalent to i = i*4. (T)
Short Answer:
- Write the output of the following program clip. What happens?
int power2 = 1;
while (power2 != 20) {
power2 *= 2;
System.ou.print(power2);
}
Answer: Nothing happens... It's an infinite loop.
- Write a program that asks the user to enter a number. Make your program square the number and then square that number. Print num1 + " and " num2.
Answer:
By the way browxy.com is an awesome place to learn basic Java!
Today's Lesson
- Objects
An object in Java is the created program. It can have many traits, classified by its state and behavior.
A good example of an object would be a Shape object, with operations like getShapeType(), findPerimeter(), and findArea().
Variables are called object references if it is a value for an object. - Classes
An objects is an instance of class.
Data fields and instance variables give the state of an object.
Methods give behaviors of the object and operations that manipulate it. - Public, Private, and Static
public allows any client program on the computer to use it, same for public methods.
If a class is not public, it can only be used by classes in it's own package.
private methods and variables can only be used by methods of that class (In the same program). This may include bank information, passwords, etc.
static means memory allocation happens once. It contains a value shared by all instances of the class.
static variables can keep track of stats for objects, accumulate a total, provide a new identity number for objects, etc. - Methods
The header of a method contains the access specifier (public or private), return type (void, int, double, String...), name (usually main), and (parameter list in parenthesis separated by commas).
Constructors create objects in classes. for example:
public BankAccount(String acctPassword, double acctBalance) {
password = acctPassword;
balance = acctBalance;
}
Default constructors provides reasonable initial values for objects.
Accessors or Accessor Methods access a class object without altering the object.
public double getBalance()
{return balance}
Mutators change the state of objects by modifying variables.
public void deposit(String acctPassword, double amount) {
if (!acctPassword.equals(password))
/* throw an exception */
else {
balance -= amount;
if (balance < 0)
{balance -= Overdrawn_Penalty;}
}
@hansenator,
A great Java lesson is going on! You just reminds me my BSc level! Thanks friend!
Cheers~
Any time guru! Love to hear from you my friend!
#hansenator wow I love this . Ever since I met you ,your profile has been so educative . I love today's lesson . I'm not really good at Java programming but I'm good at "C " programming . So I was able to understand what's beeing thought . Except the control parameters used . But I really understood every lines and how the programme was executed .👍
Awesome rufans! Glad to bring you back into programming. I have absolutely no experience with "C". Haha. One day I might be forced to. We'll see.
I'm sure you can programme with C. Since you lecturered Java, "C" gonna look pretty easy for you . They are of same structure. that's the reason I was able to understand your tutorials .
Yeah. I'm sure I could too. Just takes practice like anything else. I may be teaching Java right now, but Steemit is helping me learn it, so I can teach it to my class. Haha.
You're 100% right about it ...we all learn while teaching . #hansenator you're so kind 👍
Congratulations @hansenator! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of comments received
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP