Computer Programming - Week 3

You might also like

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

SESSION 1

Data Types

What are data types?


 Used in Java to determine the values a variable may contain.
 Determines the operations that can be performed on the variable.

Primary Kinds of Data Types in Java


 Primitive Data Types are basic, built-in data types commonly found on most programming languages.
 Constructed Data Types are data types created by the programmer using primitive data types, data structures
and abstract data types.

Classes of Data Types


 Numeric Data Types are used for numbers or digits; these data types also have corresponding types for
numbers that have specific characteristics, such as having exponential or decimal places, as well as how large
a numerical value it can hold.
 Byte is an 8-bit non-decimal data type that can hold a small numerical value; it also helps in clarifying code by
designating a corresponding use for its size limitation, such as indexes for data structures like arrays.
 Short is a 16-bit data type that can hold values larger than byte but smaller than int. It is still considered a
compact data type because it occupies a small amount of memory despite being able to hold numbers larger
than byte; this allows it to be used for very large data structures with indexes exceeding values of what
byte can hold.
 Int (short for Integer) is the 32-bit non-decimal data type with a limit for its numerical value exceeding that of
short, but smaller than long; it is the most commonly known and used numeric data type.
 Long is a 64-bit data type that offers the largest limit for non-decimal numerical values; it serves as the
final numeric data type to use if a program requires very large numbers or values.
 Float is a 32-bit single precision data type that can save memory due to its being smaller than double. It is
also the most commonly used data type for numbers that have decimal or exponential values.
 Double is a 64-bit double precision data type for large numbers with decimal or exponential values. It also
offers the largest limit for decimal numbers.

 Character Data Types are used for characters, marks and symbols; it uses the keyword char (short for
character), and then the literal that represents the variable, usually enclosed in single quotes (‘’) .
 The Boolean Data Type is a 1-bit data type used for conditional statements, or statements that return a value of
either True or False; it uses the keyword boolean and has a default value of False.

Sample code implementing different Data Types

public class DatatypeSample


{
public static void main(String[] args)
{
String name = "Joshua F. Madera";
char gender = 'M';
int age = 18;
double average = 95.75;

System.out.println("This is a String: " + name);


System.out.println("This is a Character: " + gender);
System.out.println("This is an Integer: " + age);
System.out.println("This is a Double: " + average);
}
}

Computer Programming (JAVA) – Week 3 Page 1 of 4


Output:

This is a String: Joshua F. Madera


This is a Character: M
This is an Integer: 18
This is a Double: 95.75

SESSION 2
Variables
 Also called Fields in Java.
 Items of data used to store the state of objects.

Components of Variables
 The data type indicates the type of value that the variable will hold.
 The identifier serves as the label for the variable; it can be called for use on the program’s classes on methods .

What are the guidelines when using variables in Java?


1. Always initialize variables as you declare them.
2. Boolean variables do not need declared values by default.
3. Always use descriptive and unique names for your variables. Don’t use vague identifiers that are confusing
especially if the program is using variables of similar nature.
4. Declare only one variable per line of code to eliminate confusion.

Methods to display variables in Java


1. System.out.print() is used if the program does not need to append a line at the end of each displayed output.
2. System.out.println() is used when a program needs to automatically append to a new line at the end of the
output.

Types of Variables
 Primitive Variables are composed of primitive data types stored in its actual memory location; primitive
variables are usually located within methods, and cannot be used across other methods.
 Reference Variables are variables that store an address in its actual memory location instead of the data itself;
the address points the program to the data located in another location. Reference variables are usually used for
classes so that a class and its corresponding methods and values can be used.

Sample code implementing different types of Variables

public class VariableSample


{
public static void main(String []args)
{
String name = "Juan Dela Cruz";
int age = 16;
double bodyTemp = 38.50;

System.out.println("The value of the variable name is: " + name);


System.out.println("The value of the variable age is: " + age);
System.out.println("The value of the variable bodyTemp is: " + bodyTemp);
}
}

Computer Programming (JAVA) – Week 3 Page 2 of 4


Output:

The value of the variable name is: Juan Dela Cruz


The value of the variable age is: 16
The value of the variable bodyTemp is: 38.5

SESSION 3
Constants
 Used to represent values that never change (such as the number of days of the week, number of days in a year,
the value of pi, etc.).
 Especially important if the value that is being represented is constantly being used across the program’s different
classes and methods.
 Uses the final keyword for the variable’s declaration.

Sample code implementing Java Constant

Sample #1:

public class Circumference


{
public static void main(String []args)
{
double pi = 3.1416; //pi holds the value of the constant 3.1416
int r = 5; //the value of radius represented by variable r is 5
double circ = 2 * pi * r; //c represents the variable of circumference
System.out.println("The circumference of the circle is: " + circ);
}
}

Output:

The circumference of the circle is: 31.416

Sample #2:

public class TemperatureConversion


{
public static void main(String []args)
{
/*
Formula of Temperature Conversion
Fahrenheit to Celsius: (F - 32) * 5/9
Celsius to Fahrenheit: (C * 9/5) + 32
*/

//Declare the needed variables together with their values


double C = 37;
double F = 45;

//use the formula given above to solve the problem

Computer Programming (JAVA) – Week 3 Page 3 of 4


double celsius = (F - 32) * 5/9; //given fahrenheit minus 32 multiply by 5
divided by 9

double fahrenheit = (C * 9/5) + 32; //given celsius multiply by 9 divided


by 5 add 32

System.out.println("The answer in Celsius is: " + celsius);


System.out.println("The answer in Fahrenhiet is: " + fahrenheit);

//this output implements concatenation


System.out.println("The Celsius is: " + celsius + " and the Fahrenheit is:
" + fahrenheit);
}
}

Output:

The answer in Celsius is: 7.222222222222222


The answer in Fahrenhiet is: 98.6
The Celsius is: 7.222222222222222 and the Fahrenheit is: 98.6

References

Deitel, H., & Deitel, P. (2004). Java: How to program (early objects). Prentice Hall.

Lambert, K., Osborne, M. (2011). Fundamentals of Java. Cengage Learning Asia Pte Ltd.

Computer Programming (JAVA) – Week 3 Page 4 of 4

You might also like