1 / 58

The Java Programming Language

The Java Programming Language. Simple – but abstract Safe Platform-independent ("write once, run anywhere") Has a Rich growing library Designed for the internet (applets and java scripts ). Primary slides from Horstmann. Becoming Familiar with your Computer. Login

menefer
Download Presentation

The Java Programming Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Java Programming Language • Simple – but abstract • Safe • Platform-independent ("write once, run anywhere") • Has a Rich growing library • Designed for the internet (applets and java scripts ) Primary slides from Horstmann

  2. Becoming Familiar with your Computer • Login • Locate the Java compiler - javac • Understand files and folders • Write a simple program (later) • Save your work

  3. An Integrated Development Environment

  4. File Hello.javasource code 1 public class Hello 2 { 3 public static void main(String[] args) // implies // application class 4 { 5 // display a greeting in the console window 6 System.out.println("Hello, World!"); 7 } 8 }

  5. A simple program • public class ClassName • public static void main(String[] args) • // comment • Method call object.methodName(parameters) • System class • System.out object • println method

  6. Syntax : Method Call object.methodName(parameters ) • Example: • System.out.println("Hello”); Purpose: • To invoke a method of an object and supply any additional parameters

  7. Compiling and Running • Type program into text editor • Save • Open command shell • Compile into byte codesjavac Hello.java • Execute byte codesjava Hello

  8. From Source Code to Running Program

  9. The Edit-Compile-Test Loop

  10. Objects and Classes • Object: entity that you can manipulate in your programs (by invoking methods) • Each instant object belongs to a class • Object Class: Set of objects with the same behavior – each object of the class is called an instance object

  11. Rectangle Class • Construct a rectangle: an instance of the Rectangle classnew Rectangle(5, 10, 20, 30)new Rectangle() • Use the constructed objectSystem.out.println(new Rectangle(5, 10, 20, 30));prints java.awt.Rectangle[x=5,y=10,width=20,height=30]

  12. Rectangle Shapes

  13. A Rectangle Object

  14. Syntax : Object Construction • new ClassName(parameters) • Example: • new Rectangle(5, 10, 20, 30) • new Car("BMW 540ti", 2004) • Purpose: • To construct a new object, initialize it with the construction parameters, and return a reference to the constructed object.

  15. Object Variables • Declare and optionally initialize:Rectangle cerealBox = new Rectangle(5, 10, 20, 30);Rectangle crispyCrunchy; • Apply methods:cerealBox.translate(15, 25); • Share objects:r = cerealBox;

  16. Uninitialized and Initialized Variables Uninitialized Initialized

  17. Two Object Variables Referring to the Same Object

  18. Syntax : Variable Definition TypeName variableName; TypeName variableName = expression; • Example: Rectangle cerealBox; String name ="Dave"; • Purpose: To define a new variable of a particular type and optionally supply an initial value

  19. Syntax : Importing a Class from a Package • Import packageName.ClassName ; • Example: • import java.awt.Rectangle; • Purpose: • To import a class from a package for use in a program

  20. A Simple Class public class Greeter { public String sayHello() { String message ="Hello,World!"; return message; } }

  21. Method Definition • access specifier (such as public) • return type (such as String or void) • method name (such as sayHello) • list of parameters (empty for sayHello) • method body in{ }

  22. Method Parameters public class Rectangle{ . . . public void translate(int x, int y) {method body} . . .}

  23. Syntax : Method Implementation • public class ClassName{ ... accessSpecifier returnType methodName(parameterType parameterName,...) { method body } ... } …Continue

  24. …Continue • Example: • public class Greeter { public String sayHello() { String message ="Hello,World!"; return message; } } • Purpose: • To define the behavior of a method A method definition specifies the method name, parameters, and the statements for carrying out the method's actions

  25. Testing a Class • Test class: a class with a main method that contains statements to test another class. • Typically carries out the following steps: • Construct one or more objects of the class that is being tested. • Invoke one or more methods. • Print out one or more results

  26. A Test Class for the Greeter Class public class GreeterTest { public static void main(String [] args)) { Greeter worldGreeter = new Greeter(); System.out.println(worldGreeter.sayHello()); } }

  27. Building a Test Program 1. Make a new subfolder for your program. 2. Make two files, one for each class. 3. Compile both files. 4. Run the test program.

  28. Testing with the SDK Tools • mkdir greeter • cd greeter • edit Greeter.java • edit GreeterTest.java • javac Greeter.java • javac GreeterTest.java • java GreeterTest

  29. Instance Fields public class Greeter { ...private String name; } • access specifier (such as private) • type of variable (such as String) • name of variable (such as name)

  30. Accessing Instance Fields • The sayHello method of the Greeter class can access the private instance field:public String sayHello(){String message = "Hello, " + name + "!";return message;}

  31. Other methods cannot:public class GreeterTest{public static void main(String[] args){. . .System.out.println(daveGreeter.name); // ERROR}} • Encapsulation = Hiding data and providing access through methods

  32. Syntax : Instance Field Declaration • accessSpecifier class ClassName{ ... accessSpecifier fieldType fieldName; ... }

  33. Example: • public class Greeter { ... private String name; ... } • Purpose: • To define a field that is present in every object of a class

  34. Constructors • A constructor initializes the instance variables • Constructor name = class name • public class Greeter(){ public Greeter(String aName) { name = aName; } . . .} • Invoked in new expressionnew Greeter("Dave")

  35. Syntax : Constructor Implementation • accessSpecifier classClassName{ ... accessSpecifier ClassName(parameterTypeparameterName ...) { constructor implementation } ... }

  36. Example: public class Greeter { ... public Greeter(String aName) { name = aName; } ...} • Purpose: • To define the behavior of a constructor, which is used to initialize the instance fields of newly created objects

  37. File Greeter.java 1public class Greeter 2 { 3 public Greeter(String aName) 4 { 5 name = aName; 6 } 7 8 public String sayHello() 9 {

  38. 10 String message = "Hello, " + name + "!"; 11 return message; 12 } 13 14 private String name; 15 }

  39. File GreeterTest.java 1public class GreeterTest 2 { 3 public static void main(String[] args) 4 { 5 Greeter worldGreeter = new Greeter("World"); 6System.out.println(worldGreeter.sayHello()); 7

  40. 8 Greeter daveGreeter = new Greeter("Dave"); 9System.out.println(daveGreeter.sayHello()); 10 } 11 }

  41. Designing the Public Interface Behavior of bank account: • deposit money • withdraw money • get balance Methods of BankAccount class: • deposit • withdraw • getBalance

  42. BankAccount Public Interface • public BankAccount() • public BankAccount(double initialBalance) • public void deposit(double amount) • public void withdraw(double amount) • public double getBalance()

  43. Using the Public Interface • Transfer balance • double amt = 500;momsSavings.withdraw(amt);harrysChecking.deposit(amt); • Add interest • double rate = 5; // 5%double amt = acct.getBalance() * rate / 100;acct.deposit(amt);

  44. Commenting the Public Interface • /** Withdraws money from the bank account. @param the amount to withdraw */ public void withdraw(double amount) { implementation filled in later }

  45. /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { implementation filled in later }

  46. Class Comment /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount { ...}

  47. Javadoc Method Summary

  48. Javadoc Method Detail

  49. BankAccount Class Implementation • Determine instance variables to hold object stateprivate double balance • Implement methods and constructors

  50. File BankAccount.java 1 /** 2 A bank account has a balance that can be changed by 3 deposits and withdrawals. 4 */ 5 public class BankAccount 6 {

More Related