/* A SUPERQUICK INTRODUCTION TO PROGRAMMING IN JAVA By Chuck Liang February 15th, 2009 As I've indicated at the beginning of this course, I expect you to have basic knowledge of Java and C. Although that's still true, I'd like to offer a "superquick" introduction to help you become comfortable and to get started. There are also numerous tutorials and references, both online and in print, that you may consult. One reference I always use is the "Java API Documentation", which can be found under "online resources" on blackboard. You might already have a Java development environment setup, but I recommend jdk 1.6 from sun. Download the "update" from the link in "online resources", then add the path to the jdk1.6.../bin directory, which contains the compiler javac.exe, to your windows systems path just like we did when installing Ada. The program implements objects representing bank accounts and the usual methods such as deposit, withdraw, etc. For this program we first define the "interface" of the kind of objects we wish to create. In C, the interface is placed in the .h file. */ import java.util.*; // needed for ArrayList datastructure, used in main // interface defines services offered by type bankaccount. interface bankaccount { void withdraw(double amt); void deposit(double amt); double inquiry(); void addinterest(); } /* A class "implements" interfaces. Sometimes classes are defined without interfaces but to take advantage of advanced object-oriented features, you will need to. */ class account implements bankaccount { private double balance; // balance of this account, automatically set to 0 public String name; // name of the owner of the account private static double interest = 0.01; // interest rate, .01=1% // constructor initializes balance and sets name: public account(String n, double b) { name=n; balance=b; } /* Some comments before we go on. I find that a lot of people think that the keyword "static" means that the value of a variable cannot change. That is wrong. The word "final" means it can't change. "static" means that the variable is shared by all instances of the class. The interest variable belongs to the class, not just to any individual bank account. All accounts have the same interest rate. But it can certainly be changed. balance and name are called "instance variables" while interest is a "class variable". The constructor, which is implicitly called when you invoke "new account(...)" usually just initializes the internal variables of the object. Note the special syntax of the contructor: there's no return type and the name is identical to that of the class. Now we write the methods that can be invoked on bank account objects: */ public void deposit(double amt) { if (amt>0) balance += amt;} public void withdraw(double amt) { if (amt<=balance) balance -= amt; } public double inquiry() { return balance; } // balance inquiry // The above methods are all invoked on individual account instances, // but the next one is invoked on the entire class: public static void changeinterest(double R) // set new interest rate { interest = R; } // note that this method is also static. A static method can only // reference other static elements of the class. e.g, it cannot // refer to balance. // The following method cannot be called static: why? public void addinterest() // adds interest to balance { balance += balance*interest; } // The following method merges the balance of another account with // "this" account. public void mergeaccount(account B) // merge this with B { this.balance += B.balance; // add B's balance to mine. B.balance = 0; // valid even though balance is private, since // B and "this" are instances of the same class. B.name = B.name+" merged"; // + is also string concactenation. } /* Be aware of the following points: 1. The keyword "this" always refers to the current object. When I refer to balance, for example, I really mean "this.balance". Sometimes, "this" is explicitly needed to pass to a method a pointer "to yourself". 2. Except for primitive numeric types such as double and int, all parameters in Java are passed by reference. That is, we don't actually pass an entire account object to the mergeaccount method, only a pointer to it. The same is true of all complex types including all strings and arrays. */ }// account class /* Now to test the account class we write the "main" function, which also needs to be inside a class. In fact, it must be inside a "public" class with a name that matches the name of the file. Each file can contain only one public class. */ public class superquick // must match filename "superquick.java" { public static void main(String[] args) throws Exception // what a mouthful! { // create instances of account, i.e, account objects: account myaccount = new account("prof", 2000); account youraccount = new account("student", 3000); myaccount.withdraw(50); youraccount.deposit(400); System.out.println("your balance is now "+youraccount.inquiry()); // change the interest rate for all accounts: account.changeinterest(0.005); //"account" used to refer to class. myaccount.addinterest(); myaccount.mergeaccount(youraccount); // sorry System.out.printf("my balance is now %.2f\n",myaccount.inquiry()); // The printf call from C was added to Java recently - praise heaven! /* So far this has been a rather ordinary program. We haven't really made use of the interface bankaccount. However, suppose that "youraccount" is declared to not be of type account but bankaccount, the name of the interface: bankaccount youraccount = new account("student",3000); Then the line myaccount.mergeaccount(youraccount); would have resulted in a compiler error, because mergeaccount is expecting a parameter of type account, not bankaccount. We could make the call the following way: myaccount.mergeaccount( (account)youraccount ); That is, we "downcast" the type of youraccount from the abstract "bankaccount" to the concrete class "account". So why would we want to use the interface anyway? Because there could be multiple classes that implement the same interface. This gives us a way to writing code that's more generic or "polymorphic". --- To end this superquick tutorial, let me show you how to use the built-in ArrayList class, which are actually linked lists. Along the way, I will also illustrate the use of type parameters. Note the "import" line at the top of this program is needed to use arraylists. Consult the Java API documentation for all the ways to use the ArrayList class. I can create a list of account objects as follows: */ ArrayList AL = new ArrayList(); AL.add(myaccount); // add to end of arraylist AL.add(youraccount); AL.add(new account("rich guy",10000000)); // ArrayList elements can be accessed by indexing, starting at 0: System.out.println("The third account belongs to "+AL.get(2).name); // ArrayLists elements can likewise be changed using set: AL.set(2,new account("somebody",5000)); System.out.println("The size of the list is now "+AL.size()); AL.remove(1); // removes the second element of the list. // A "foreach" loop to print everybody's balance: for (account a : AL) System.out.printf("%s's balance is %.2f\n",a.name,a.inquiry()); System.exit(0); // usually this is not needed, but if you have // threads still running this will kill'em all :) }//main }//superquick /* To run this program compile it with javac superquick.java Run it with java superquick The output for this program is: your balance is now 3400.0 my balance is now 5359.75 The third account belongs to rich guy The size of the list is now 3 prof's balance is 5359.75 somebody's balance is 5000.00 */ // Hope this has been helpful.