/* Object-Oriented Programming This program illustrates the idea that most things can be thought of as objects. Objects contain: * Information or "attributes" * Methods or functions Alternatively, the two points also represent the interior (information) and exterior (operations) view of objects. Examples of objects include bank accounts, football teams, etc... Objects don't have to represent real world objects. The components of a graphical element on a computer screen is an object. Objects are sometimes used to group together related parts of a progam - i.e. they're sometimes used for purely organizational purposes. Information is defined by "variables" of various types. Objects can contain other objects as part of its information. Methods or functions can be of two principal types: * Methods that performs some operation * Methods that performs some operation and returns some piece of information it has computed. The syntax of invoking a method on an object is (in general format) objectname.methodname( parameters ) The parameters are information passed to the methods Some methods merely sets attributes, and some merely return some piece of information contained in the objects. Other methods will need to perform complex computations before doing so. Objects are categorized by classes. For example, "jets" and "giants" are objects classified by "footballteam". We say that each object is an "instance" of its class. The following program illustrates these basic points. ( don't worry too much about how the "bodies" of the methods are defined at first ) */ class footballteam { // information private String quarterback; private int w = 0; // number of wins, initially zero private int l = 0; // number of losses // functions // records that team has won a game: public void win() { w = w+1; } // records that team has lost a game: public void loose() { l = l+1; } // set or change the quarterback of the team: public void setQB(String newqb) { quarterback = newqb; } // calculate and return the winning percentage of the team. public double percentage() { double gamesPlayed; gamesPlayed = w + l; // total number of games played if (gamesPlayed == 0) return 0; else return w/gamesPlayed; } // compares winning percentage of "this" team with another team public boolean betterthan(footballteam otherteam) { if (this.percentage() > otherteam.percentage()) return true; else return false; } } // end of class footballteam /* Note that the information is preceeded by the word "private" and the methods are preceeded by "public". This corresponds to the idea that information corresponds to the internal state of the object, while the functions provides the means for accessing the object externally. Let's take a more abstract view of the external interface of a footballteam object: void win() void loose() void setQB(String newqb) double percentage() boolean betterthan(footballteam otherteam) As you might have guessed, "void" means the functions will not return any values. These functions are invoked as statements. */ /* Every stand-alone java program must contain a "public class" with a "static" method called "main". The name of the public class must match the file name. */ public class sep8 { public static void main(String[] args) { // create two footballteam objects: footballteam jets = new footballteam(); footballteam giants = new footballteam(); // call methods jets.setQB("vinnie"); giants.setQB("vinnie's friend"); giants.win(); giants.loose(); giants.win(); giants.win(); jets.loose(); jets.loose(); jets.loose(); jets.setQB("somebody better than vinnie"); // a call like jets.percentage() would not be valid, since // it returns a piece of information that needs to be processed: System.out.println("Giants' percentatge: " + giants.percentage()); System.out.println("Jets' percentage: " + jets.percentage()); if (jets.betterthan(giants)) System.out.println("the jets are better than the giants"); else System.out.println("the giants are better"); System.exit(0); } } // end of public class