Welcome to AP Computer Science JAVA Programming in
Objective: To identify the components of an object-oriented program written in java.
What is JAVA? Java is a language designed for object-oriented programming, abbreviated OOP. Object-oriented programming is a programming methodology that views a program as a set of objects that interact with each other by means of actions.
What are Objects? An object is a program construction that has data associated with it and that can perform certain actions. For example, if we wanted to write a program to simulate brewing and selling coffee at a coffee shop. One object in our program could be for the different types of coffee that we are brewing and selling.
What other objects could our Coffee Shop program contain?
Each object has certain attributes. An object’s attributes are the information/data associated with it. For example, our coffee object would have attributes such as coffeeType, price, ingredients, temperature, etc. An object’s attributes are a major part of what makes each object unique.
Along with attributes, each object has a set of methods. Methods are the actions associated with the object. Our coffee object could have methods such as mixIngrediants(), brewCoffee(), checkTemperature().
Objects of the same type are said to be from the same Class. In JAVA, we create a class which is basically the definition of an object. The attributes and methods of each individual object are set up in a class. All objects in the same class have the same kind of attributes and methods. To create an object of a certain class, we say we create an instance of the class.
How do we set up a class? First, we must design our class. Class Name:Coffee Attributes (data): Methods (actions): coffee type price special ingredients mix ingredients brew coffee
Once we have finished designing our class, we can then start to code it. Class Name:Coffee Attributes (data): Methods (actions): coffee type price special ingredients mix ingredients brew coffee public class Coffee { private String sCoffeeType; private double dPrice; private String sSpecIngredients; public void mixIngredients( ) { // method definition here } public void brewCoffee( ) { // method definition here } }
In object-oriented programs, we first define classes, and while the program is running, we create objects from these classes to accomplish tasks. // Define Caffe Latte object of type class Coffee Coffee CaffèLatte = new Coffee( ); // Define Caffe Mocha object of type class Coffee Coffee CaffèMocha = new Cofee( );
Coffee CaffèLatte = new Coffee( ); Class Name Object Name Instantiates a new object Constructor ClassName ObjectName = new Constructor( ); Creating and Instantiating an Object
To perform methods (actions) on an object, we must call the object along with its method. //The method that mixes the ingredients is called for the CaffeLatte object (of type coffee). CaffèLatte.mixIngredients( ); //The method that mixes the ingredients is called for the CaffeMocha object (of type coffee). CaffèMocha.mixIngredients( );
CaffèLatte. mixIngredients( ); Object Name Method To have an object perform an action we must have the specific object call the method using the dot operator. Dot operator
Let’s write a simple program. HelloWorld