Unit1: Object-oriented design and programming

farrightΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 4 μήνες)

242 εμφανίσεις

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


To know how to use the Scanner class


To know how to create, compile and run Java
programs using NetBeans

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.

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


}

}


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


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


Some basic language features


Referenece: Sun’s tutorial on Java (Language
basics
http://java.sun.com/docs/books/tutorial/java/nu
tsandbolts/index.html


Package


Scanner


Loop


Switch


Array


Method

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


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


}

}


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.


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


}

}

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

Array in Java


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


Complete Student Activity 2.5


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;


}

}

Using methods


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


Complete Student Activity 2.6

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.