16
Unit2: Object-oriented programming Getting started with Java Jin Sa

Unit2: Object-oriented programming Getting started with Java

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Unit2: Object-oriented programming Getting started with Java

Unit2: Object-oriented programming

Getting started with JavaJin Sa

Page 2: Unit2: Object-oriented programming Getting started with Java

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

Page 3: Unit2: Object-oriented programming Getting started with Java

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.

Page 4: Unit2: Object-oriented programming Getting started with Java

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!");

}

}

Page 5: Unit2: Object-oriented programming Getting started with Java

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

Page 6: Unit2: Object-oriented programming Getting started with Java

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

Page 7: Unit2: Object-oriented programming Getting started with Java

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

Page 8: Unit2: Object-oriented programming Getting started with Java

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

Page 9: Unit2: Object-oriented programming Getting started with Java

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!");

}

}

Page 10: Unit2: Object-oriented programming Getting started with Java

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.

Page 11: Unit2: Object-oriented programming Getting started with Java

Input using Scannerpackage 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);

}

}

Page 12: Unit2: Object-oriented programming Getting started with Java

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

Page 13: Unit2: Object-oriented programming Getting started with Java

Array in Java

• Read unit 2 “Example 3: An Example with an array” in section D.5

• Complete Student Activity 2.5

Page 14: Unit2: Object-oriented programming Getting started with Java

An example of using methodspublic 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; }}

Page 15: Unit2: Object-oriented programming Getting started with Java

Using methods

• Read unit 2 “Example 4: An example with a method” in section D.6

• Complete Student Activity 2.6

Page 16: Unit2: Object-oriented programming Getting started with Java

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.