Professional Documents
Culture Documents
Week ULO
1
Discuss the program structure and the basic elements of Java
2
programming and apply them appropriately in a program.
3
4 Use Control structures to efficiently implement branch and
5 looping algorithm in programs
6
7 Explore user-defined methods and ways of manipulating
8 records and to correctly apply all concepts in a self-developed
9 application
Discuss the program structure and the basic elements of Java programming and apply them
appropriately in a program.
METALANGUAGE
Programming Language are languages that people us which are similar to human
language, which will be translated into machine code for the computer to understand.
Comments are denoted by // for a single line comment and /* at the start of a block of
statements to comment and */ to close the block of statements that you want to comment.
Data types specify the different sizes and values that can be stored in the variable.
Primitive type variables directly store data into their memory space.
Reference variables store the address of the object containing the data.
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
ESSENTIAL KNOWLEDGE
When a reference variable of a class type is declared, memory space is allocated for
the data and an object of that class type is instantiated. The address of the object is
stored in a reference variable.
From the above diagram, the primitive type variables directly store data into their
memory space. However, the reference variables store the address of the object
containing the data.
An object is an instance of a class, and the operator new is used to instantiate an
object.
3. Class String
23 7
• Example
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
JOptionPane Example
• Next, you use the object inFile to input data from the file prog.dat
just the way you used the object console to input data from the
standard input device using the methods next, nextInt, nextDouble,
and so on
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
• To store the output to a file of a program in a file, you use the class
PrintWriter
• Import java.io to use the PrintWriter class
• Declare a PrintWriter variable and associate this variable with the
destination
• Suppose the output is to be stored in the file prog.out
• Consider the following statement:
• PrintWriter outFile = new PrintWriter("prog.out");
• This statement creates the PrintWriter object outFile and associates
it with the file prog.out
• You can now use the methods print, println, and printf with outFile
just the same way they have been used with the object System.out
• The statement:
outFile.println("The paycheck is: $" + pay);
stores the output—The paycheck is: $565.78—in the file prog.out
• This statement assumes that the value of the variable pay is 565.78
• Closing the file; you close the input and output files by using the
method close
inFile.close();
outFile.close();
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
• Closing the output file ensures that the buffer holding the output will
be emptied; that is, the entire output generated by the program will
be sent to the output file
• If an input file does not exist, the program throws a
FileNotFoundException
• If an output file cannot be created or accessed, the program throws
a FileNotFoundException
• The method main does not need to handle the
FileNotFoundException exception, it will be included in a command
in the heading of the method main to throw the
FileNotFoundException exception
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
SELF-HELP
Take for eg. you want to find out the methods that are available for Scanner class.
At the hyperlink, click on java.util. This will bring you to the package java.util and the
description of the package. Under the Class Summary, click on Scanner. You will see the
details of the Class Scanner. It also has the descriptions of all the methods available under
this class.
You will need to use IDE (Integrated Development Environment) – either Eclipse or
NetBeans for doing the programming activities. Please refer to Self-Help section under
Week 2 if you have not already installed your IDE.
Using Eclipse
Launch Eclipse
Click File -> New -> Java Project
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
While Selecting your project from the package explorer window on the left,
click on File -> New -> Class
The following window will appear. Check the source folder and make sure it is the project
you have created. Type the name of your Java Class. Note: No spaces and special symbols.
Also, check the public static void main(String[] args) box in the method stubs.
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
You will see the screen below if you have successfully created your Java class.
Once you have completed your program, click on Run -> Run or click on Ctrl+F11.
Using NetBeans
Launch NetBeans.
Click on File -> New Project
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
Click Next
You will see this when you are done and you are ready to get going for your lab activities.
Once you have completed your program, click on Run -> Run File or click on Shift+F6.