1 / 16

Unit2: Object-oriented programming Getting started with Java

Unit2: Object-oriented programming Getting started with Java. Jin Sa. Objectives of this unit. To understand relevant terminologies To know the basic syntax of a Java program and methods To be able to write, compile and run simple Java programs To know how to group classes into packages

ian-reeves
Download Presentation

Unit2: Object-oriented programming Getting started with Java

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. Unit2: Object-oriented programmingGetting started with Java Jin Sa

  2. Objectives of this unit • To understand relevant terminologies • To know the basic syntax of a Java program and methods • To be able to write, compile and run simple Java programs • To know how to group classes into packages • To know how to use the Scanner class • To know how to create, compile and run Java programs using NetBeans

  3. Terminologies • Java 2 Platform, Standard Edition (J2SE) • Java 2 Platform, Enterprise Edition (J2EE) • Java Development Toolkit (JDK), set of tools such as compiler (javac), documentation generator (javadoc) and debugger • Java API: predefined classes and interfaces for developing java programs. • Integrated development environment (IDE): integrate tools such as compiler and debugger to provide facilities for programmers to develop software, e.g. NetBeans incorporates the development tools of the JDK into one convenient GUI-based program.

  4. A Simple Java program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args){ System.out.println(“Welcome to Java!"); } }

  5. Develop and run the Java program without IDE • Name of the file must be the same as the name of the class with .java, • e.g. Welcome.java • Convention: class name starts with capital letter • To compile • javac Welcome.java • Generates a class file called Welcome.class • To run • java Welcome

  6. Develop and run Java programs using IDE (NetBeans) • incorporates the Java compiler, the Java interpreter and other tools together with file and project management for developing and executing Java programs. • Student activity 2.1. in section C.3. Following NetBeans tutorial on how to create and run a java program using NetBeans • Student activity 2.2 in section C.3, create the Welcome program using NetBeans

  7. Some basic language features • Referenece: Sun’s tutorial on Java (Language basics http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html • Package • Scanner • Loop • Switch • Array • Method

  8. Package and import • Everything in java is part of a class. • Packages are used to group classes • Package can contain other packages • Java expects a one-to-one mapping of the package name and the file system directory structure • Every class in Java belongs to a package, to put a class in a package, include this line as the first line • package packagename; • If not, the class is in a default unnamed package

  9. Welcome class in the startjava package package startjava; //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println(“Welcome to Java!"); } }

  10. Use classes from other packages • Use the fully qualified name of the class. E.g. the fully qualified name for the Scanner class is • java.util.Scanner. • Or use the “import” statement. For example, to import the Scanner class from the java.util package, you use • import java.util.Scanner; You can also import all the classes in a package, e.g. • import java.util.*; The import statement tells the compiler where to locate the classes.

  11. Input using Scanner package startjava; import java.util.Scanner; //This program prints Welcome to Java!+the name public class Welcome { public static void main(String[] args) { String name=""; Scanner scan = new Scanner(System.in); System.out.println("Enter your name:"); name=scan.nextLine(); System.out.println("Welcome to Java! "+name); } }

  12. Loop and switch in Java • Read unit 2 “Example 1: Calculating total – using loop” in section D.3 • Complete Student Activity 2.3 • Read unit 2 Example 2: Option menu – using switch in section D.4 • Complete Student Activity 2.4

  13. Array in Java • Read unit 2 “Example 3: An Example with an array” in section D.5 • Complete Student Activity 2.5

  14. An example of using methods public class Welcome1 { public static void main(String[] args) { prompt(); String name=getName(); System.out.println(name); } public static void prompt(){ System.out.println(“Enter your name”); } public static String getName(){ Scanner in=new Scanner(System.in); String nm=in.nextLine(); return nm; } }

  15. Using methods • Read unit 2 “Example 4: An example with a method” in section D.6 • Complete Student Activity 2.6

  16. Summary In this unit, we have • introduced some well known terminology related to programming in Java • explained how to use NetBeans • demonstrated how to write simple Java programs using NetBeans • introduced the concept of package • illustrated how to use the Scanner class for input • covered some basic program concepts including array, loop, conditional statement and methods.

More Related