Mobile Development Workshop JAVA REFRESHER
Overview Programming languages Object Oriented Programming with Java
First up… Create a new project in Netbeans ◦ Call it Bank
Running a Program
Programming Languages High-level languages are relatively easy to use ◦ Java, C#, C++, Visual Basic, Python, Ruby. Unfortunately, computer hardware does not understand high-level languages. ◦ Therefore, a high-level language program must be translated into a low-level language.
Java Byte-Code The Java compiler does not translate a Java program into assembly language or machine language for a particular computer. Instead, it translates a Java program into byte-code . ◦ Byte-code is the machine language for a hypothetical computer (or interpreter ) called the Java Virtual Machine.
The Java Language Java is an Object Oriented Language ◦ Everything is represented as an object Objects can have data and actions Data is stored in variables Actions are defined in methods
OOP Terminology Objects of the same kind have the same type and belong to the same class. ◦ Objects within a class have a common set of methods and the same kinds of data ◦ but each object can have it’s own data values.
Introduction to Encapsulation The data and methods associated with any particular class are encapsulated (“put together in a capsule”), but only part of the contents is made accessible. ◦ Encapsulation provides a means of using the class, but it omits the details of how the class works. ◦ Encapsulation often is called information hiding.
Example An automobile consists of several parts and pieces and is capable of doing many useful things. ◦ Awareness of the accelerator pedal, the brake pedal, and the steering wheel is important to the driver. ◦ Awareness of the fuel injectors, the automatic braking control system, and the power steering pump is not important to the driver.
Back to your program… Create a new class called BankAccount Give it the following properties ◦ String ownerName ◦ double balance Give it a constructor that takes an ownerName and starting balance to set up the object Project a getter and setter for the ownerName property
Back to your program… Create two new methods ◦ deposit(double) – this will increase the balance of the account ◦ withdraw() – this will reduce the balance of the account
Introduction to Inheritance Classes can be organized using inheritance. A class at lower levels inherits all the characteristics of classes above it in the hierarchy. At each level, classifications become more specialized by adding other characteristics. Higher classes are more inclusive; lower classes are less inclusive.
Introduction to Inheritance
Back to your program… Create two new calsses ◦ CheckingAccount ◦ SavingsAccount Both classes should inherit from the BankAccount class
Flow of Control Flow of control is the order in which a program performs actions. A branching statement chooses between two or more possible actions. A loop statement repeats an action until a stopping condition occurs.
The if-else Statement A branching statement that chooses between two possible actions. Syntax if (Boolean_Expression) Statement_1 else Statement_2
Semantics of the if-else Statement
Back to your program… In the BankAccount class ◦ Prevent the deposit method in the BankAccount class from accepting a deposit amount less than or equal to zero ◦ Prevent the withdrawal method from leaving a negative balance
Overriding a method When a subclass functionality from a base class, it can discard the functionality it doesn’t want by overriding the previous implementation The older version is still there, and can be accessed using the super keyword
Back to your program Override the withdraw() method in your CheckingAccount class to allow overdrafts up to $100
Compound Boolean Expressions Boolean expressions can be combined using the "and" (&&) operator. Example if ((score > 0) && (score <= 100)) ... Not allowed if (0 < score <= 100)
Java Logical Operators
Recommend
More recommend