0% found this document useful (0 votes)
43 views8 pages

2 Computer Programming Module 3

This document discusses Java packages, data types, input/output, and modifiers. It covers built-in vs user-defined packages, primitive vs non-primitive data types like int and String, using Scanner for user input, and access modifiers like public. Examples are provided for importing classes, declaring variables, reading input, and setting access levels. Students are assigned to create simple programs to practice circumference calculation and name input/output.

Uploaded by

joel lacay
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
43 views8 pages

2 Computer Programming Module 3

This document discusses Java packages, data types, input/output, and modifiers. It covers built-in vs user-defined packages, primitive vs non-primitive data types like int and String, using Scanner for user input, and access modifiers like public. Examples are provided for importing classes, declaring variables, reading input, and setting access levels. Students are assigned to create simple programs to practice circumference calculation and name input/output.

Uploaded by

joel lacay
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

MODULE 3 - PACKAGES, PROGRAM CODES, INPUT AND OUTPUT, AND MODIFIERS

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 {

public static void main(String args) {

int num1, num1, sum;


Scan sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = sc.nextInt;

System.out.println("Enter Second Number: );


num = sc.nextInt();

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 (packages from the Java API)


 User-defined Packages (create your own packages)

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.

In the example above, java.util is a package, while Scanner is a class of the java.util package.

Try to code this:

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.

To create a package, use the package keyword:

Lesson 2 - Java Data Types

As explained in the previous module, a variable in Java must be a specified data type:

Data types are divided into two groups:

 Primitive data types- includes byte, short, int, long, float, double, boolean and char


 Non-primitive data types - such as String, Arrays and Classes 

Primitive Data Types

A primitive data type specifies the size and type of variable values, and it has no additional
methods.

There are eight primitive data types in Java:

Data Type Size Description

byte 1 byte Stores whole numbers from -128 to 127

short 2 bytes Stores whole numbers from -32,768 to 32,767


MODULE 3 - PACKAGES, PROGRAM CODES, INPUT AND OUTPUT, AND MODIFIERS

int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647

long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to


9,223,372,036,854,775,807

float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal


digits

double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits

boolean 1 bit Stores true or false values

char 2 bytes Stores a single character/letter or ASCII values

Primitive number types are divided into two groups:

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 int data type can store whole numbers from -2147483648 to 2147483647.

 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

Non-Primitive Data Types

Non-primitive data types are called reference types because they refer to objects.

The main difference between primitive and non-primitive data types are:

 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.

Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.

Lesson 3 - Input and Output

Java input and output - is an essential concept while working on Java programming.

It consists of elements such as input, output and stream. 

The input is the data that we give to the program.

The “System.in” represents the keyboard.

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.

The Scanner class is used to get user input, and it is found in the java.util package.

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

import java.util.Scanner; // Import the Scanner class

class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");

String userName = myObj.nextLine(); // Read user input


System.out.println("Username is: " + userName); // Output user input
}
}

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.

We divide modifiers into two groups:

o Access Modifiers - controls the access level


o Non-Access Modifiers - do not control access level, but provides other functionality

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

1. Create a simple circumference program


2. Create an input and output program that has an output like this:

Enter First Name:


Enter Last Name:

Your Complete Name is:

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

You might also like