2 Computer Programming Module 10
2 Computer Programming Module 10
Pre-test: Find the error: Guess what will be the output: 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;
double result;
switch(operator)
{
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
Now that we already know what’s inside a Java program, let’s review again:
So what are these parts of the source code or program that I specified?
Built-in Packages – the Java API is a library of prewritten classes that are free to use.
User-defined packages – to create your own package, you need to understand that
Java uses a file system directory to store them.
Example: packagemypack;
Explanation: To better understood what is a package, let’s use the example of a Balikbayan
box, full of candies, chocolates, clothes, etc. A certain package should be sent to
the receiver in order for that receiver to use the contents of the box.
2. Class – a class is an object constructor, or a “blueprint” for creating objects. It can be private or
public.
Explanation: To better understood what is a class, let’s use the example of an anime drawing,
you cannot draw directly the character without using a blueprint or a guide. Same
goes to classes, you cannot proceed in what you want to code if there’s no class in
it.
MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS
3. Method – is a block of code which runs when it is called. Methods are used to perform certain
actions, and they are also known as functions.
4. Data types – data types specify the different sizes and values that can be stored in the variable.
Primitive data types – this include boolean, char, byte, short, int, long, float and
double.
Non-primitive data types – the non-primitive data types include classes, interfaces
and arrays.
5. Variable – is a piece of memory that can contain a data value. A variable thus has a data type.
Variables are typically used to store information which your java program needs to do its job.
Explanation: To better understood what are data types and variables, let’s use the example of a
glass container full of salt. The data type is the container, and the variable is the
salt inside the container.
6. Comments – are the statements that are not executed by the compiler and interpreter. The comments
can be used to provide information or explanation about the variable, method, class or any statement.
Comments are usually starts with either “/”, “//” or “/* */”.
7. Input and output –is an essential concept while working on Java programming. It consists of
elements such as input, output and stream.
Review 2 – 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
MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS
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
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
MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS
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'
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:
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:
MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS
Example:
Review 5 – Modifiers
The public keyword is an access modifier, meaning that it is used to set the access level for classes,
attributes, methods and constructors.
MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS
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.
Java, like all other programming languages, is equipped with specific statements that allow us to check a
condition and execute certain parts of code depending on whether the condition is true or false. Such statements
are called conditional, and are a form of composite statement.
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS
Example explained:
In the example above, time (20) is greater than 18, so the condition is false. Because of this, we
move on to the else condition and print to the screen "Good evening". If the time was less than
18, the program would print "Good day".
Use the else if statement to specify a new condition if the first condition is false.
Example explained:
In the example above, time (22) is greater than 10, so the first condition is false.
The next condition, in the else if statement, is also false, so we move on to the else condition
since condition1 and condition2 is both false - and print to the screen "Good evening".
However, if the time was 14, our program would print "Good day."
Evaluation: Using DCoder App, create a program using conditional statements: (Note: Make sure to
record your code using Screen recorder in your phone, if you have, and upload the video on the group
page comments on this topic.
MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS
Scenarios:
Output:
1. Name of student:
Grade and Strand:
Grades:
English:
Science:
Math:
ICT:
History:
Values Education:
GWA:
/* Indicate if:
You passed!
(or You failed.) */
Bibliography/References:
https://www.geeksforgeeks.org/the-complete-history-of-java-programming-language
https://www.onlinecollegeplan.com/computer-programming-languages
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
https://www.inf.unibz.it/~calvanese/teaching/04-05-ip/lecture-notes/uni05.pdf
https://www.w3schools.com/java/java_conditions.asp
https://www.w3schools.com/java/java_switch.asp