Sunteți pe pagina 1din 14

MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS

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;

public class Calculator {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);


System.out.print("Enter two numbers: ");

// nextDouble() reads the next double from the keyboard


double first = reader.nextDouble();
double second = reader.nextDouble();

System.out.print("Enter an operator (+, -, *, /): ");


char operator = reader.next().charAt(0);

double result;

switch(operator)
{
case '+':
result = first + second;
break;

case '-':
result = first - second;
break;

case '*':
result = first * second;
break;

case '/':
result = first / second;
break;

// operator doesn't match any case constant (+, -, *, /)


default:
System.out.printf("Error! operator is not correct");
return;
}

System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);


}
}
MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS

Lesson 1 – Review: Activity-driven Topics:

Review 1 – What’s inside a Java Program?

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?

1. Import package (or package) – is used to group related classes.

Why use packages?


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.

Example: import package.name.Class;

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

Why use methods?


Simple, to reuse code: define the code once, and use it many times.

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.

4. Data types – data types specify the different sizes and values that can be stored in the variable.

There are two types of data types in Java:

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

Why put comments?


To identify what is inside the source code.

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

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

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:

Review 3 – Java Data Types


MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS

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

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
MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS

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'

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.


MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS

Review 4 – 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:

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:
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

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

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.

Review 6 – Conditional Statements

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.

In Java, there are two forms of conditional statements:

• the if-else statement, to choose between two alternatives;


• the switch statement, to choose between multiple alternatives.

Java supports the usual logical conditions from mathematics:

o Less than: a < b


o Less than or equal to: a <= b
o Greater than: a > b
o Greater than or equal to: a >= b
o Equal to a == b
o Not Equal to: a != b

Java has the following conditional statements:

 Use if to specify a block of code to be executed, if a specified condition is true


 Use else to specify a block of code to be executed, if the same condition is false
 Use else if to specify a new condition to test, if the first condition is false
 Use switch to specify many alternative blocks of code to be executed

1.1 - The if Statement

Use the if statement to specify a block of Java code to be executed if a condition is true.

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS

1.2 - The else statement

Use the else statement to specify a block of code to be executed if the condition is false.

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

1.3 - The else-if statement


MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS

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

1.4 - Switch-case statement


MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS

Use the switch statement to select one of many code blocks to be executed.

This is how it works:

o The switch expression is evaluated once.


o The value of the expression is compared with the values of each case.
o If there is a match, the associated block of code is executed.
o The break and default keywords are optional, and will be described later in this chapter

1.5 - The break Keyword


MODULE 10 – REVIEW: ACTIVITY-DRIVEN TOPICS

When Java reaches a break keyword, it breaks out of the switch block.


This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing.

1.6 - The default Keyword

The default keyword specifies some code to run if there is no case match:

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:

1. Make a calculator using if-else statement


2. Make a sari-sari store order using switch-case statement
3. Create a program for circumference, radius and diameter

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

S-ar putea să vă placă și