O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Unit of competency

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
Chapter 2.1
Chapter 2.1
Carregando em…3
×

Confira estes a seguir

1 de 8 Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (20)

Semelhante a Unit of competency (20)

Anúncio

Mais recentes (20)

Unit of competency

  1. 1. UNIT OF COMPETENCY: PERFORM OBJECT-ORIENTED ANALYSIS AND DESIGN IN JAVA TECHNOLOGY ELEMENT PERFORMANCE CRITERIA Italicized terms are elaborated in the Range of Variables 1 Apply Basics of Java language 1.1 Executable Java applications are created in accordance with Java framework 1.2 Java packages are imported to make them accessible in the code 1.3 Working with Java Data types is demonstrated in accordance with Java framework 1.4 Using Operators and Decision Constructs is demonstrated in accordance with Java framework 1.5 Creating and Using Arrays is demonstrated in accordance with Java framework 1.6 Using Loop Constructs is demonstrated in accordance with Java framework VARIABLE RANGE 1. Executable Java applications  Hello World  Hello with name  Hello with name and date 1. Working with Java Data Types  Declare and initialize variables  Differentiate between object references and primitive variables  Read and write to object fields  Explain an object’s lifecycle (creation, dereference, and garbage collection)  Call methods on objects  Manipulate data using StringBuilder class and its methods  Create and manipulate Strings 2. Using Operators and Decision Constructs  Use Java operators  Use parenthesis to override operator precedence  Test equality between strings and other objects using == and equals()  Create and use if-else constructs  Use a switch statement
  2. 2. 3. Creating and Using Arrays  Declare, initialize, and use a one-dimensional array  Declare, initialize, and use a multi-dimensional array  Declare and use an ArrayList 4. Using Loop Constructs  Create and use while loops  Create and use for loops including the enhanced for loop  Create and use do-while loops  Compare loop constructs  Use break and continue Unit of Competency Learning Outcome Methodology Assessment Approach 1. Perform object- oriented analysis and design in Java technology 1.1 Apply basics of Java language  Lecture/ Discussion  Hands on  Exercises  Demonstration  Practical exam  Interviews/ questioning
  3. 3. Objectives: At the end of the lesson the student should be able to: Identify the basic parts of a Java Program Execute basic Java Application Writing a Java hello world program 1 2 3 4 5 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } } Save the file as HelloWorld.java (note that the extension is .java) under a directory, Every Java program starts from the main() method. This program simply prints “Hello world” to screen. Description The java command starts a Java application. It does this by starting the Java Runtime Environment (JRE), loading the specified class, and calling that class's main() method. The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter.
  4. 4. Compiling it Now let’s compile our first program in the HelloWorld.java file using javac tool. Type the following command to change the current directory to the one where the source file is stored: cd C:Java And type the following command: javac HelloWorld.java 5. Running it It’s now ready to run our first Java program. Type the following command: java HelloWorld That invokes the Java Virtual Machine to run the program called HelloWorld (note that there is no .java or .class extension). You would see the following output: What we have learnt so far Throughout this tutorial you have learnt the following things:  o JDK is the Java SE Development Kit that contains tools and libraries for Java development. o JRE is the Java Runtime Environment that enables running Java programs on your computer. o JVM is the Java Virtual Machine that actually executes Java programs. With JVM, programs written in Java can run on multi-platforms (thus Java is called cross-platform language). o How to install JDK and configure environment variables. o javac.exe is the Java compiler. It translates Java source code into bytecode. o java.exe is the JVM launcher which we use to run our program. o Every Java program starts from the main() method. o When compiling, the compiler generates a .class file from a .java file. http://www.codejava.net/java-core/how-to-write-compile-and-run-a-hello-world-java-program-for- beginners?utm_campaign=javatipseveryday&utm_medium=email&utm_source=getresponse
  5. 5. Configuring Sublime Text 2 editor to compile and run Java programs 1. Create a batch script file called runJava.bat with the following content: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @ECHO OFF cd %~dp1 ECHO Compiling %~nx1....... IF EXIST %~n1.class ( DEL %~n1.class ) javac %~nx1 IF EXIST %~n1.class ( ECHO -----------OUTPUT----------- java %~n1 ) The purpose of this script is to call Java compiler (javac) to compile the source file. Then if the compilation succeeds (identified by checking if the .class file generated), call the Java launcher (java) to run the program. 2. Save the runJava.bat under JDK’s bin directory, e.g. c:Program FilesJavajdk1.8.0bin. 3. Locate and open the JavaC.sublime-build file under this directory: 4.Replace the text “javac” by “runJava.bat”: 5.Then press Ctrl + B again, you would see the following result:
  6. 6. Dissecting my first Java program public class Hello { /** * My first java program */ public static void main(String[] args) { //prints the string "Hello world" on screen System.out.println("Hello world!"); } } public class Hello indicates the name of the class which is Hello. In Java, all code should be placed inside a class declaration. We do this by using the class keyword. In addition, the class uses an access specifier public, which indicates that our class in accessible to other classes from other packages (packages are a collection of classes). a curly brace { indicates the start of a block. Java comment. A comment is something used to document a part of a code. It is not part of the program itself but used for documentation purposes. It is good programming practice to add comments to your code. A comment is indicated by the delimiters “/*” and “*/”. Anything within these delimiters are ignored by the Java compiler, and are treated as comments.
  7. 7. public static void main(String[] args) { indicates the name of one method in Hello which is the main method. The main method is the starting point of a Java program. All programs except Applets written in Java start with the main method. Make sure to follow the exact signature. The next line is also a Java comment, //prints the string "Hello world" on screen Now, we learned two ways of creating comments. The first one is by placing the comment inside /* and */, and the other one is by writing // at the start of the comment. System.out.println("Hello world!"); prints the text “Hello World!” on screen. The command System.out.println(), prints the text enclosed by quotation on the screen. The last two lines which contains the two curly braces is used to close the main method and class respectively. Coding Guidelines: 1. Your Java programs should always end with the .java extension. 2. Filenames should match the name of your public class. So for example, if the name of your public class is Hello, you should save it in a file called Hello.java. 3. You should write comments in your code explaining what a certain class does, orwhat a certain method do. Java Comments Comments are notes written to a code for documentation purposes. Those text are not part of the program and does not affect the flow of the program. Java supports three types of comments: C++-style single line comments, C-style multiline comments and special javadoc comments. C++-Style Comments C++ Style comments starts with //. All the text after // are treated as comments. For example, // This is a C++ style or single line comments C-Style Comments C-style comments or also called multiline comments starts with a /* and ends with a */. All text in between the two delimeters are treated as comments. Unlike C++ style comments, it can span multiple lines. For example, /* this is an example of a C style or multiline comments */
  8. 8. Java Statements and blocks A statement is one or more lines of code terminated by a semicolon. An example of a single statement is,System.out.println(“Hello world”); A block is one or more statements bounded by an opening and closing curly braces that groups the statements as one unit. Block statements can be nested indefinitely. Any amount of white space is allowed. An example of a block is, public static void main( String[] args ){ System.out.println("Hello"); System.out.println("world"); } Coding Guidelines: 1. In creating blocks, you can place the opening curly brace in line with the statement, like for example, public static void main( String[] args ){ or you can place the curly brace on the next line, like, public static void main( String[] args ) { 2. You should indent the next statements after the start of a block,for example, public static void main( String[] args ){ System.out.println("Hello"); System.out.println("world"); }

×