Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

College for Research & Technology of

Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

Lesson Title: Getting to know Java and Object-Oriented Programming

Lesson Objectives:
At the end of this module, you should be able to:
1. Explain object-oriented programming (OOP’s) general principles and key terms
2. Demonstrate familiarity and understanding of basic structures, code, and syntax of the Java
programming language.
3. Identify the different components of the prescribed IDE for java, including useful word.
4. Evaluate simple Java programs.

Reference: https://www.tutorialspoint.com/java/index.htm

Productivity Tip: Turn off Distractions! One of the major productivity killers is the distraction of constant
interruptions: emails, phone calls, and people appearing at your door.

A. LESSON PREVIEW/REVIEW

1. Introduction

- Java is a high-level programming language originally developed by Sun Microsystems and


released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the
various versions of UNIX. This tutorial gives a complete understanding of Java. This
reference will take you through simple and practical approaches while learning Java
Programming language.

2. Activity 1: What I Know Chart (PART 1)

For today’s lesson try answering the questions below fill out the first column of the table. Write
your idea about the questions being asked in “What I know” part. It’s okay to write what came
out to your mind after reading the questions.

What I Know Questions: What I Learned (Activity 4)


1. What are the areas of
application of Java?
College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

2. What are the reserved words in


Java?

B. MAIN LESSON

1. Activity 2: Content Notes


Java programming language was originally developed by Sun Microsystems which was initiated
by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform
(Java 1.0 [J2SE]). The latest release of the Java Standard Edition is Java SE 8. With the advancement
of Java and its widespread popularity, multiple configurations were built to suit various types of
platforms. For example: J2EE for Enterprise Applications, J2ME for Mobile Applications. The new
J2 versions were renamed as Java SE, Java EE, and Java ME respectively. Java is guaranteed to
be Write Once, Run Anywhere.
Java is −
• Object Oriented − In Java, everything is an Object. Java can be easily extended since it is based on the
Object model.
• Platform Independent − Unlike many other programming languages including C and C++, when Java
is compiled, it is not compiled into platform specific machine, rather into platform independent byte
code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on
whichever platform it is being run on.
• Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would
be easy to master.
• Secure − With Java's secure feature it enables to develop virus-free, tamper-free systems.
Authentication techniques are based on public-key encryption.
• Architecture-neutral − Java compiler generates an architecture-neutral object file format, which
makes the compiled code executable on many processors, with the presence of Java runtime system.
• Portable − Being architecture-neutral and having no implementation dependent aspects of the
specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability
boundary, which is a POSIX subset.
• Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly on compile
time error checking and runtime checking.
• Multithreaded − With Java's multithreaded feature it is possible to write programs that can perform
many tasks simultaneously. This design feature allows the developers to construct interactive
applications that can run smoothly.
• Interpreted − Java byte code is translated on the fly to native machine instructions and is not stored
anywhere. The development process is more rapid and analytical since the linking is an incremental
and light-weight process.
• High Performance − With the use of Just-In-Time compilers, Java enables high performance.
• Distributed − Java is designed for the distributed environment of the internet.
College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

• Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to an
evolving environment. Java programs can carry extensive amount of run-time information that can
be used to verify and resolve accesses to objects on run-time.

AREAS OF APPLICATION
1. World Wide Web Applets
2. Cross-Platform Application Development
3. Other Network Application

SYNTAX
The syntax of Java is largely derived from C++. Unlike C++, which combines the syntax for structured,
generic, and object-oriented programming. Java was built almost exclusively as an object-oriented language.
All code is written inside a class, and everything is an object. with the exception of the primitive data types
(e.g. integers, floating-point numbers, Boolean values, and characters). which are not classes for performance
reasons.
Unlike C++. Java does not support operator overloading or multiple inheritance for classes. This
simplifies the language and aids in preventing potential errors and anti-pattern design. Java uses similar
commenting methods to C++. There are three different styles of comments: a single line style marked with
two slashes (//), a multiple line style opened with /* and closed with *, and the Javadoc commenting style
opened with /** and closed with *. The Javadoc style of commenting allows the user to run the Javadoc
executable to compile documentation for the program.

RESERVED WORDS:

abstract assert boolean break

byte case catch char

class const continue default

do double else enum

extends final finally float

for goto if implements

import instanceof int interface

long native new package

private protected public return

short static strictfp super


College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

switch synchronized this throw

throws transient try void

volatile while

SAMPLE PROGRAM
Note: Class name should start in uppercase.

class HelloWorld
{
public static void main(String []args)
{
System.out.println("Hello World!!!");
}
}

OUTPUT: Hello World!!!

Understanding the Statement;


public class Welcome
• public is an access specifier
• Welcome is the name of the class

System.out.printIn("First Java Application");

• The dots or periods are used to separate the name of the components in the statement
• System is a class.
• out is an object defined in the System class.
• PrintIn() is a method. Method names are always followed by parentheses.
• First Java Application is a literal string that is the argument to the print in© method.
• ;(semicolon) is used to end a Java statement.

GUI Output using JOptionPane


import javax.swing.JOptionPane;
public class GUISamplel
{
public static void main(String args[])
{
JOptionPane.show MessageDialog(null,
"Welcome to Java Programming );
}
College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

System.exit(0);
}

OUTPUT: Welcome to Java Programming

Besides allowing you to use the System class to produce command window output. Java provides
built-in classes that produce GUI output on dialog boxes. A dialog box is a GUI object resembling a window
which you can place messages you want to display.
You use an import statement when you want to access a built-in Java class that is contained in a
group of classes called package. To use the JOptionPane class, you must import the package named
javax.swing. JoptionPane.
In older versions of Java, any application that use a dialog was required to end with System exit(0);
statement or the application would not terminate. You can add this statement to your programs, and they will
work correctly, but it is not necessary.

Scanner class Methods


Method Description
nextDouble() Retrieves input as double
nextInt() Retrieves input as an int
nextLine() Retrieves the next line of data and returns it as a String
next() Retrieves the next complete token as a String

Example: (Assume that the name is Juan Dela Cruz)

import javax.swing.JOptionPane;
public class InputDialogDemo
{
public static void main(String[] args)
{
String name;
name = JOptionPane.showInputDialog("Enter Your Name");
JOptionPane.showMessageDialog(null, "Your name is " + name );
}

OUTPUT:
Enter Your Name Juan Dela Cruz
Your name is Juan Dela Cuz

2. Activity 3: Skill- Building Activities

Let’s Practice! What is the output of the following Java codes?


College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

Code Output
1 class Welcome1
{
public static void main(String arg[])
{
System.out.printIn(“Welcome to Computer
Programming 2”);
}
}
2 class Welcome2
{
public static void main(String arg[])
{
System.out.print(“Welcome to”);
System.out.print(“Computer Programming 2”);
}
}
3 class Welcome3
{
public static void main(String arg[])
{
System.out.printIn(“Welcome to”);
System.out.print(“Computer Programming 2”);
}
}
4 class Welcome4
{
public static void main(String arg[])
{
System.out.print(“Welcome to ” + Computer
Programming 2”);
}
}
5 class Welcome4
{
public static void main(String arg[])
{
System.out.print(“Welcome to ” +/n
Computer Programming 2”);
}
}
College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

3. Activity 4: What I Know Chart

Based on what you have learn in this course overview about Computer Programming 2, answer
the third column in the table found at the page 1 of this module.

4. Activity 5: Check for Understanding


What is the output of the following codes?

Code Output
1 Class Sample
{
Public static void main(String args[])
{
Int age=18;
String name=”Juan Dela Cruz”;
System.out.print(“I am” + name + “,” + age + “years
old.”;
}
}
2 import java.util.Scanner;
public class GetUserInput
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter your name>>");
String name = input.nextLine();
System.out.print("Enter your age>>");
int age = input.nextInt();

JOptionPane.showMessageDiaglog(null, "your
name"+name+ "and you are"+age+"years old");
}
}

FAQ’S
1. Is there a case sensitive in Java Programming?
• Yes, java is case sensitive, which means identifier Hello and hello would have a
different meaning in java.
2. What is the rule in setting classmate name?
College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

• For all class name, the first letter should be in Upper Case. If several words are used
to form the class’s name, each inner words first letter should be in Upper Case.
3. What is the rule in setting method names and program file name?
• All method names should start with a Lower-Case letter. If several words are used to
form the methods name, then each inner words first letter should be in Upper Case
while the program files name should exactly match the class name.

You might also like