Post on 29-Jun-2015
description
Numerical DataGroup 3
Adewumi ezekiel Computer science
Group Members
Fa juko M ichae lAdewumi Ezek ie l
E tukudo AndyAfi a Kennedy
INTRODUCTION
This report provides concept of classes thatwill assist us in writing arithmetic
expressions in Java, using and manipulating date information, converting input string values into numerical data, and, inputting
and outputting of numerical data.
ORDER OF PRESENTATION ETUKUDO ANDY-will introduce the project and talk on the order in which the project will be presented and also give a brief knowledge about his contribution in the group for five (5) minutes.ADEWUMI EZEKIEL- will present the project titled “NUMERICAL DATA” and also give a brief knowledge about his contribution in the group for seven (7) minutes.FAJUKO MICHEAL-will run the program that concerns the project and also give a brief knowledge about his contribution to the group for seven (7) minutes.Finally, AFIA KENNEDY-will talk on the conclusion and how the project relates to our previous lectures and also give a brief knowledge about his contribution to the group for five (5) minutes.
Numerical Data
Arithmetic expressions in Java
byEtukudo Andy
Arithmetic expression is a valid arrangement of variables, constants , operators ,operands and parentheses.
e.g This an expression=x²-(4bc)
where x=variablesB and c=constants-sign =operators
()sign=parenthesis 4=operands
There are five (5) operations in arithmetic expression that can be written in java the arithmetic operators are:
Addition(+)Subtraction(-)
Multiplication(*)Division(/)
Modulo division (%)
Example
The Evaluation of the Arithmetic Expression in java is shown below;•10+25=35•50-15=35•6*3=18•25/5=5•9%2=1
Precedence Rules
Precedence Rule
When two or more operators are present in an expression, we determine the order of evaluation by following the precedence rules.
Order of Precedence
Bracket( )Unary operators such as minors sign (-) and plus sign (+)Multiplication operator such as multiplication (*), Division (/) and modulo division (%)Additive operator such as minors(-) and addition(+)I call it BUMA
Precedence Examples
Example 16 + ((37 % 8) / 5) = 6 + ( 5 / 5) = 7
Evaluate
(7 * (10 - 5) % 3) * 4 + 9 (7 * 5 % 3) * 4 + 9 (35 % 3) * 4 + 9
2 * 4 + 9 8 + 9 17
Steps for Creating & Running a Program
1. Write the source code in a .java file
2. Compile the source code into byte code creating a .class file
3. JVM interprets the byte code into machine language as 1s and
0s and the computer then executes the instructions
The Math ClassBy Adewumi
Ezek ie l
The Math class is a type of standard class in the java.lang package. The Math class contains methods that perform various mathematical functions.
Example: Square Roota = Math.sqrt(9.0)
a would receive the value of 3.
Example: Exponentsa = Math.pow(4.0, 2.0)
a would receive the value of 16. where Math is the class, pow is the method and the rest are the parameters.
Mathematical Syntax Operation Java Description
Math.sqrt(x) square root of x
Math.pow(x, y) x raised to the exponent y
Math.abs(x) absolute value of x
Some Math Class Methods
16
Example
( ( ( Math.pow( ( ( 1 + Math.sqrt( 5 ) ) / 2 ),fibonacciNum ) ) - ( Math.pow( ( ( 1- Math.sqrt( 5 ) ) / 2 ), fibonacciNum ) ) ) / Math.sqrt( 5 ) );
Write a formula with the use of mathclass method that accepts N and displays FN.
DecimalFormat class
The DecimalFormat class (which is part of the java.text package) is one way to formatnumbers.
To format a number using the DecimalFormat class, a number of steps are required. In the
followingexample, we will format a number to two decimal
places.
1. Import the java.text.DecimalFormat class.import java.text.DecimalFormat;
2. Now you must create a DecimalFormat object to format the text according to a pattern that you specify.// Declare and initialize DecimalFormat objectDecimalFormat df = new DecimalFormat(“.00”);df is the name given to the DecimalFormat object we have created# is a placeholder object that will be removed if there is not a digit at that location0 is a placeholder that will show up as zero if a digit is not found at that location
3. Now we can use the DecimalFormat object to format your number.System.out.println(df.format(234.5678));The above expression outputs 234.57.The format pattern #.00 asks that a number be converted into four characters – i.e. one digit to the left of the decimal separator, a decimal separator, and two digits on the right.
21
Constructor
DecimalFormat decFor = new DecimalFormat(String pattern);
If I wanted to format the number to zero decimal places, my DecimalFormat object would be created asfollows:DecimalFormat df = new DecimalFormat(“#”);System.out.println(df.format(234.5678));The above program would output the number 234.5678 would be outputted as 235.
class
parameterVariable name
EXAMPLEimport java.text.DecimalFormat; class Deci {public static void main(String args[]) {//formatting numbers up to 2 decimal places in JavaDecimalFormat df = new DecimalFormat("######.00");System.out.println(df.format(364565.14));//formatting numbers up to 3 decimal places in Javadf = new DecimalFormat("######.000");System.out.println(df.format(364565.14));}}
It will display something like this;
Convert input string values to numerical data
by Fajuko Micheal
Converting Strings to Integers
•The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns “123”. To obtain the input as a
number, you have to convert a string into a number.
•To convert a string into an int value, you can use the static parseInt method in the Integer
class as follows: int intValue = Integer.parseInt(intString);
•where intString is a numeric string such as
“123”.
Converting Strings to Doubles
To convert a string into a double value, you can use the static parseDouble method in the
Double class as follows:
double doubleValue =Double.parseDouble(doubleString);
where doubleString is a numeric string such as
“123.45”.
Input numerical data by using system.in
and output by system.out
Input numerical data by using system.in
It is an open code and ready to supply input data. Typically it corresponds to keyboard input
or another input source specified by the user. System.in means by inputting the attributes and
method or data.NOTE -The data collected by the system.in object is character data, even if they are
entered as numeric digits. Therefore if the application performs any calculation, the input
character, it must first be converted to primitive data type before it is used.
Output a numerical data by using system.out
It is an open and ready to accept output data. Typically it corresponds to display output or another output
destination specified by the user. Writing character to the system.out
using method print() or print() displays these character on your
screen.
Examples of running programs usingInput numerical data by using system.in and output by system.out
GregorianCalendarby Afia Kennedy
32
GregorianCalendar class is a type of standard class used for manipulating calendar information such as year, month, day, hour, minute and second from a Date object..
Firstly, identifying the package in which a class belongs is very important
Therefore (Java.util.Gregorian-Calendar is a concrete
subclass of Calendar);
33
However they are constant defined in the calendar class for retrieving different fragment of calendar/time which are;
Parameters and Constant defined
year - the value used to set the YEAR time field in the calendar.month - the value used to set the MONTH time field in the calendar. Month value is 0-based. e.g., 0 for January.WEEK-the constant value for the days of week.date - the value used to set the DATE time field in the calendar.hour - the value used to set the HOUR_OF_DAY time field in the calendar.
How to Constructs a GregorianCalendar with given date and time set for the default time
GregorianCalendar today = new GregorianCalendar( );public GregorianCalendar (int year, int month, int date, int hour, int minute, int second)
And it is possible to create a new GregorianCalendar object that represents today asOr specifically presenting the day as January7, 2014, by passing year, month, and day as the parameters
e.g. GregorianCalendar independenceDay = new
GregorianCalendar (2014, 3, 7); The value of 3 in the second parameter
means April because the value of the January is 0; and the value of February which is the second month of the year is 1 so therefore; the, value of April is 3
Example of gregorian Calender class showing the
current time and date
Memory allocation for object and primitive data
value
38
Java Data Types
object
array interface class
primitive
integral boolean
byte char short int long
floating point
float double
Memory allocation to primitive data values
When a new Data values is created, Java allocates space from a pool of
memory called the stack to the variables
Java Primitive Data Types
Primitive data value is also known as (numerical data value) used to compute the sum and the difference of variables (x and y) in a Java program
we must first declare what kind of data will be assigned to them after assigning values to them, then computing their sum and difference is made possible, to declare that the type of data assigned to them is an integer, we write
int x, y;
After declaration is made, memory is allocated to store data values for x and y. These memory locations are called
variables, and x and y are the names we associate with the memory locations. When declaration is made, we can assign only integers to x and y but not real numbers.
However there are six numerical
data types in Java: byte, short, int, long, float, and double.
The difference among these six numerical data types are their range of values they can represent
Also a data type with a larger range of values is said to have a higher precision data type.
Double has a higher precision than the data type float.
Memory size for data typesEvery data type has some memory size defined. This enables that whenever a variable is declared, the memory size is automatically defined
Default value:Every primitive data type has default values defined. When the programmer does not declare to assign any values to the variables, these default values will be assigned by the Virtual machine during the object instantiation. Range of values the data types can representIt is extremely important to understand what are the min and max range of values a data type can be able to hold.
This is syntax for declaring primitive data value
<data type> <variables>;
num1 38
num2 96Before:
num2 = num1;
num1 38
num2 38After:
Int number1,number2;
number1 = 138;
number2 = number1
When number2 is declared as a variable it overwrite the reference in number1
Object Data type These are also variables (they can be change) but the only difference is the content because a variable in an object contains an address where the object is stored. Without executing a new command, no new object is created, two variables can refer to the same object as the object having two distinct names
Before a memory is allocated to an object in a program, it must be declared and initialized
An object declaration simply declares the name (identifier) that is used to refer to an object. Where <object names> is a sequence of object names separated by commas and <Class name> is the name of a class to which these objects belongs. The name of an object and the class to which the object belongs
This is the syntax for object declaration:<Class name> <object names>;
When an address of an object is executed and memory space is allocated
When the class of an object is executed there’s a space memory allocated to it
Example of object data value For object
name2 = name1;
name1
name2Before:
"Steve Jobs"
"Steve Wozniak"
name1
name2After:
"Steve Jobs"
Past lecturesFrom the open book test, we have been drilled to
read and understand the concept of java in chapter two of McGraw introduction to java. From
the first topic, we learnt about the six programming phases which are; understand the program, Plan the logic, Code the program, Test
the program etc and this was used while practicing and working out the java program for each of the bullet point in question. Class is also
the template in object-oriented programming and it can also be related to pseudocode.
Conclusion
At the end of this group task we have learnt about the concepts of
numerical data value. We’ve carried out different researches
and have learnt how to solve different problems just by
collaborating with one another