//Author: ACS Cobham Computer Science�//Date: September 2021�//Description: A demonstration on code coding style.���//this class contains the name of the program�public class ReviewOfPrintln�{� //every program needs this first line. We call this the MAIN METHOD.� public static void main(String args[])� {� //We use "print line" to output text on the screen.� //System is a facility which provided standard output (STDout)� System.out.println("Examples of coding style to output of text");� System.out.println("==========================================");� � //A blank line. Notice it has no parameters inside the brackets.� System.out.println(); � � System.out.println(); �� System.out.println("This is the end");� }�} |
Review of standard output
Learning Intentions
Primitive Data Types
byte short int long float double char boolean
a data type provided by a programming language as a basic building block
How to use the int data type
//Author: ACS Cobham Computer Science�//Date: September 2021�//Description: A demonstration on code coding style and the use of data types.���//this class providec the name of the program "UsingInt"�public class UsingInt�{� //every program needs this first line. We call this the main method.� public static void main(String args[])� {� //We use "print line" to output text on the screen.� //System is a facility which provided standard output (STDout)� System.out.println("Examples of Using Different Data Types");� System.out.println("======================================");� � //A blank line. Notice it has no parameters.� System.out.println(); � � int x = 10;� System.out.println("The value of x is ");� System.out.println(x);� � System.out.println(); ��� int y = 5;� System.out.println("The value of x is " + x);� System.out.println("The value of y is " + y);�� }�} |
//Author: ACS Cobham Computer Science�//Date: September 2021�//Description: A demonstration on code coding style and the use of data types.��//this class providec the name of the program "IntegerMath"�public class IntegerMath�{� //every program needs this first line. We call this the main method.� public static void main(String args[])� {� System.out.println("Examples of Using Different Data Types");� System.out.println("======================================");� � //A blank line. Notice it has no parameters.� System.out.println(); � � int x = 10;� int y = 3;�� System.out.println("The value of x/y is ");� System.out.println(x/y);� System.out.println(); �� int result = x/y;� System.out.println("The result = " + result);� }�} |
How to use the int data type
The result of integer math is always in integer!
How to use the int data type
The result of integer math is always in integer!
public class MoreIntegerMath�{� public static void main(String args[])� {� System.out.println("Examples of Using Different Data Types");� System.out.println("======================================");� System.out.println(); � � int x = 10;� int y = 3;� � int sum = x + y;� int product = x * y;� int division = x/y;� int formula = 3*(x+y)/2;� � System.out.println("The sum is " + sum);� System.out.println("The product is " + product);� System.out.println("The division is " + division);� System.out.println("The formula is " + formula);� }�} |
Naming Conventions
Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', character '_', and character '$'. ��A name can not contain the space character.��Do not start with a digit. ��A name can be any length. ��Upper and lower case count as different characters. �So SUM and Sum are different names.��A name can not be a reserved word. ��A name must not already be in use in this part of the program.
Problem
Modify the code to add two integers together using
sum = first_number + second_number;
How to use the double data type
Double is used for numbers that need decimal precision
public class UsingDouble�{� public static void main(String args[])� {� System.out.println("Examples of Using Different Data Types");� System.out.println("======================================");� System.out.println(); � � double x = 10.0;� double y = 3.0;�� System.out.println("The value of x/y is ");� System.out.println(x/y);� System.out.println(); �� double result = x/y;� System.out.println("The result = " + result);� }�} |
How to use the double data type
Double is used for numbers that need decimal precision
public class UsingDouble�{� public static void main(String args[])� {� System.out.println("Examples of Using Different Data Types");� System.out.println("======================================");� System.out.println(); � � double x = 10;� double y = 3;�� System.out.println("The value of x/y is ");� System.out.println(x/y);� System.out.println(); �� double result = x/y;� System.out.println("The result = " + result);� }�} |
How to use the float data type
float is used for numbers that need LESS decimal precision
public class UsingFloat�{� public static void main(String args[])� {� System.out.println("Examples of Using Different Data Types");� System.out.println("======================================");� System.out.println(); � � float x = 10.0;� float y = 3.0;�� System.out.println("The value of x/y is ");� System.out.println(x/y);� System.out.println(); �� float result = x/y;� System.out.println("The result = " + result);� }�} |
How to use the float data type
float is used for numbers that need LESS decimal precision
public class UsingFloat�{� public static void main(String args[])� {� System.out.println("Examples of Using Different Data Types");� System.out.println("======================================");� System.out.println(); � � float x = 10;� float y = 3;�� System.out.println("The value of x/y is ");� System.out.println(x/y);� System.out.println(); �� float result = x/y;� System.out.println("The result = " + result);� }�} |
We can convert numeric data types by casting them into another type
(int)
(double)
(float)
public class UsingCasts�{� public static void main(String args[])� {� System.out.println("Examples of Using Different Data Types");� System.out.println("======================================");� System.out.println(); � � int x = 10;� int y = 3;�� double result1 = x / y;� System.out.println("Result1 = " + result1);� � //We can convert from integer to double and float� double result2 = (double)x / (double)y;� System.out.println("Result2 = " + result2);� � float result3 = (float)x/ (float)y;� System.out.println("Result3 = " + result3);�� }�} |
EXERCISE - Using & Applying Data Types
Question 1
Write a Java program to convert temperature from Fahrenheit to Celsius degree
Test Data
Input a degree in Fahrenheit: 212
Expected Output:
212.0 degree Fahrenheit is equal to 100.0 in Celsius
Question 2
Write a Java program that reads a number in inches, converts it to meters.
Note: One inch is 0.0254 meter.
Test Data
Input a value for inch: 1000
Expected Output :
1000.0 inch is 25.4 meters
Question 3
Write a Java program to convert minutes into a number of years and days.
Test Data
Input the number of minutes: 3456789
Expected Output :
3456789 minutes is approximately 6 years and 210 days
Question 4
Write a Java program that reads a number and display the square, cube, and fourth power.
Expected Output:
Square: 9
Cube: .27
Fourth power: 81
public class ConvertFtoD�{� public static void main(String args[])� {� System.out.println("Convert from from Fahrenheit to Celsius degree");� System.out.println("==============================================");� System.out.println(); � � int f = 212;� double fd = (double)f;� double c = (5 * ( fd - 32) ) / 9;�� System.out.println(fd + " degree Fahrenheit is equal to " + c + " in Celsius");� }�} |
HELP - Question 1
HELP - Question 1 - What your PDF should look like?
Declaring Variables
dataType variableName;
dataType variableName = initialValue ;
dataType variableNameOne, variableNameTwo ;
dataType variableNameOne = initialValueOne,
variableNameTwo = initialValueTwo ;
A variable declaration may be made in several ways
Task
Write a program which stores the three variables
firstName, telNumber and studentAge.
Your program should then output these details in a single sentence.
Controlling Text Output
using escape sequences
public class ShowEscapeSequences
{
public static void main(String[] args)
{
System.out.print("I really like\n\t\"CIS355A\"Business Application Programming with Lab using JAVA\"");
}
Working with Escape Sequences
tabulating your output
Task
Read and explore the code snippets.
Change the output so that it appears as required.
Working with Escape Sequences
tabulating your output
Task
Modify your firstName, telNumber and studentAge program so that it has a tabular layout.
Include column headings, like the example table below, together with a program heading and the appropriate use of empty lines and escape sequences such as \t.
Boolean in Programming and How to use boolean data type
Boolean in programming
Boolean algebra is used frequently in computer programming. A Boolean expression is any expression that has a Boolean value. For example, the comparisons 3 < 5, x < 5, x < y and Age < 16 are Boolean expressions.
Boolean expressions will be covered in detail when we explore the next coding component and when we investigate “Branching and Decision Making” in Java.
class usingChar �{� public static void main (String args[])� {� char charA = 'H'; //observe the single quote ' and not a double quote for strings "" � char charB = 'e';� char charC = 'l';� char charD = 'l';� char charE = 'o';�� System.out.println("valA\tvalB\tvalC\tValD\tvalE");� System.out.println(charA + "\t" + charB + "\t" + charC + "\t" + charD + "\t" + charE);� � String stringA = Character.toString(charA);� String stringB = Character.toString(charB);� String stringC = Character.toString(charC);� String stringD = Character.toString(charD);� String stringE = Character.toString(charE);�� System.out.println();� � String aSentance = stringA + stringB + stringC + stringD + stringE;� System.out.println(aSentance);� }�} |
How to use char data type
How to convert char into String
Task
Explore the impact of removing the toString()functions that convert char to String.
Tinker with the code and try the following. Discuss happens and explain why?.
String stringA = charA;
Boolean in Programming and How to use boolean data type
class usingBooleanAlgebra �{� public static void main (String args[])� {� boolean valA = true; � boolean valB = false;� � System.out.println("the status of valA = " + valA);� System.out.println("the status of valB = " + valB);� � boolean valC = 3>6; �� System.out.println("is 3 > 6? The computers says: " + valC);� � boolean valD = 3<6; � � System.out.println("is 3 < 6? The computers says: " + valD);�� }�} |
Boolean in programming
Boolean algebra is used frequently in computer programming. A Boolean expression is any expression that has a Boolean value. For example, the comparisons 3 < 5, x < 5, x < y and Age < 16 are Boolean expressions.
Boolean expressions will be covered in detail when we explore the next coding component and when we investigate “Branching and Decision Making” in Java.
Range of Values for
Numerical Data Types
Floating Point Primitive Data Types
Type
Size
Range
float
32 bits
-3.4E+38 to +3.4E+38
double
64 bits
-1.7E+308 to 1.7E+308
Integer Primitive Data Types
Type
Size
Range
byte
8 bits
-128 to +127
short
16 bits
-32,768 to +32,767
int
32 bits
(about)-2 billion to +2 billion
long
64 bits
(about)-10E18 to +10E18
Usually you should pick a data type that has a range much greater than the range of numbers you expect to deal with.
Declaring Doubles
Because Java chooses Double by default the following code is the same outcome
Task:
Code both programs and test your output.
Why is their no difference?
Precision of Doubles - Example
accuracy of floating point numbers using 64 bits
That is because some numbers -- such as 0.1 -- cannot be represented exactly in binary floating-point.
Task:
Code both programs and test your output.
Why is the output strange? Discuss the read the next slide to understand. Discuss again.
Float and Double - Accuracy
Impact on Accuracy
Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.
What is the impact on Currency?
That is because some numbers -- such as 0.1 -- cannot be represented exactly in binary floating-point.
There is no way you can altogether "avoid" floating point arithmetic errors. The number of bits used in representing a number will always be finite.
Try the following example and count the decimal digits
float: 32 bits (4 bytes) has 6 to 9 decimal digits with about 7 on average
double: 64 bits (8 bytes) has 15 to 17 decimal digits, about 16 on average
Example 3
accuracy of floating point numbers using 64 bits