2 Computer Programming Module 3
2 Computer Programming Module 3
Pre-test: Find the error: As the instruction stated, find the error and write the correct line on a one-half cross-
wised sheet of paper or element for the said program stated below: (10 points)
import java.util.Scanner
public class AddTwo Numbers2 {
sc.close();
sum = num1 + num2
System.out.println("Sum of these numbers: ",sum);
}
}
MODULE 3 - PACKAGES, PROGRAM CODES, INPUT AND OUTPUT, AND MODIFIERS
Lesson 1 – Packages
A package in Java is used to group related classes. Think of it as a folder in a file directory. We use
packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories:
Built-in Packages
The Java API is a library of prewritten classes, that are free to use, included in the Java Development
Environment.
The library contains components for managing input, database programming, and much much more.
The library is divided into packages and classes. Meaning you can either import a single class (along with its
methods and attributes), or a whole package that contain all the classes that belong to the specified package.
To use a class or a package from the library, you need to use the import keyword
Import a Class
If you find a class you want to use, for example, the Scanner class, which is used to get user input.
User-defined Packages
MODULE 3 - PACKAGES, PROGRAM CODES, INPUT AND OUTPUT, AND MODIFIERS
To create your own package, you need to understand that Java uses a file system directory to
store them.
As explained in the previous module, a variable in Java must be a specified data type:
A primitive data type specifies the size and type of variable values, and it has no additional
methods.
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
Integer types stores whole numbers, positive or negative (such as 123 or -456), without
decimals. Valid types are byte, short, int and long. Which type you should use, depends on the
numeric value.
Floating point types represents numbers with a fractional part, containing one or more decimals.
There are two types: float and double.
Since we are only using int, float, double and char, so let's check out how do they work:
The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that
you should end the value with an "f"
The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that
you should end the value with a "d"
The char data type is used to store a single character. The character must be surrounded
by single quotes, like 'A' or 'c'
MODULE 3 - PACKAGES, PROGRAM CODES, INPUT AND OUTPUT, AND MODIFIERS
Primitive types are predefined (already defined) in Java. Non-primitive types are created by the
programmer and is not defined by Java (except for String).
Non-primitive types can be used to call methods to perform certain operations, while primitive types
cannot.
A primitive type has always a value, while non-primitive types can be null.
A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
The size of a primitive type depends on the data type, while non-primitive types have all the same size.
Java input and output - is an essential concept while working on Java programming.
The output is the data what we receive from the program in the form of result.
Stream represents flow of data or the sequence of data. To give input we use the input stream and to
give output we use the output stream.
To use the Scanner class, create an object of the class and use any of the available methods found in
the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read
Strings.
Example:
MODULE 3 - PACKAGES, PROGRAM CODES, INPUT AND OUTPUT, AND MODIFIERS
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
Input Types
In the example above, we used the nextLine() method, which is used to read Strings. To
read other types, look at the table below:
Example:
Lesson 4 - Modifiers
MODULE 3 - PACKAGES, PROGRAM CODES, INPUT AND OUTPUT, AND MODIFIERS
The public keyword is an access modifier, meaning that it is used to set the access level for classes,
attributes, methods and constructors.
Evaluation: Create a Program: As it is stated on the title, Using your DCoder app, create a program based on
the scenario(s):
MODULE 3 - PACKAGES, PROGRAM CODES, INPUT AND OUTPUT, AND MODIFIERS
Some Notes:
1. Post-test will be recorded and the instructor will collect your answers.
2. Activities will also be recorded and the instructor will collect your exercises.
Bibliography/References:
https://www.w3schools.com/java/java_packages.asp
https://www.w3schools.com/java/java_data_types.asp
https://www.w3schools.com/java/java_user_input.asp
https://www.w3schools.com/java/java_modifiers.asp