SlideShare a Scribd company logo
1 of 24
Lecture#6+7
Overloading Methods
Example 4.3 Overloading the max Method
public static double max(double num1, double
num2) {
if (num1 > num2)
return num1;
else
return num2;
}
Type Casting
 If x is a float and y is an int, what will be the data type of
the following expression?
 x * y
 The answer is float.
 The above expression is called a mixed expression.
 The data types of the operands in mixed expressions are
converted based on the promotion rules. The promotion
rules ensure that the data type of the expression will be the
same as the data type of an operand whose type has the
highest precision.
Explicit Type Casting
 Explicit type conversion is a type conversion which is
explicitly defined within a program (instead of being done
by a compiler for implicit type conversion).
 Instead of relying on the promotion rules, we can make an
explicit type cast by prefixing the operand with the data
type using the following syntax:
( <data type> ) <expression>
 Example
(float) x / 3
(int) (x / y * 3.0)
Type case x to float and
then divide it by 3.
Type cast the result of the
expression x / y * 3.0 to
int.
Constants
 We can change the value of a variable. If we want the value to remain
the same, we use a constant.
final double PI = 3.14159;
final int MONTH_IN_YEAR = 12;
final short FARADAY_CONSTANT = 23060;
These are constants,
also called named
constant.
The reserved word
final is used to
declare constants.
These are called
literal constant.
Sample Code Fragment
//code fragment to input radius and output
//area and circumference
final double PI = 3.14159;
double radius, area, circumference;
//compute area and circumference
area = PI * radius * radius;
circumference = 2.0 * PI * radius;
System.out.println("Given Radius: " + radius);
System.out.println("Area: " + area);
System.out.println(" Circumference: “ + circumference);
String Input
 Using a Scanner object is a simple way to input data from the standard input
System.in, which accepts input from the keyboard.
 First we need to associate a Scanner object to System.in as follows:
 After the Scanner object is set up, we can read data.
 The following inputs the first name (String):
import java.util.Scanner;
Scanner scanner;
scanner = new Scanner(System.in);
System.out.print (“Enter your first name: ”);
String firstName = scanner.next();
System.out.println(“Nice to meet you, ” +
firstName + “.”);
Numerical Input
 We can use the same Scanner class to input numerical
values
Scanner scanner = new Scanner(System.in);
int age;
System.out.print( “Enter your age: “ );
age = scanner.nextInt();
Scanner Methods
Method Example
nextByte( ) byte b = scanner.nextByte( );
nextDouble( ) double d = scanner.nextDouble( );
nextFloat( ) float f = scanner.nextFloat( );
nextInt( ) int i = scanner.nextInt( );
nextLong( ) long l = scanner.nextLong( );
nextShort( ) short s = scanner.nextShort( );
next() String str = scanner.next();
The Math class
 The Math class in the java.lang package contains class methods for
commonly used mathematical functions.
 Class methods:
 Trigonometric Methods
 Exponent Methods
 Rounding Methods
 min, max, abs, and random Methods
double num, x, y;
x = …;
y = …;
num = Math.sqrt(Math.max(x, y) + 12.4);
Some commonly use Math Class
Methods
Method Description
exp(a) Natural number e raised to the power of a.
log(a) Natural logarithm (base e) of a.
floor(a) The largest whole number less than or
equal to a.
max(a,b) The larger of a and b.
pow(a,b) The number a raised to the power of b.
sqrt(a) The square root of a.
sin(a) The sine of a. (Note: all trigonometric
functions are computed in radians)
Trigonometric Methods
 sin(double a)
 cos(double a)
 tan(double a)
 acos(double a)
 asin(double a)
 atan(double a)
Exponent Methods
 exp(double a)
Returns e raised to the power of a.
 log(double a)
Returns the natural logarithm of a.
 pow(double a, double b)
Returns a raised to the power of b.
 sqrt(double a)
Returns the square root of a.
Rounding Methods
 double ceil(double x)
x rounded up to its nearest integer. This integer is returned as a double value.
 double floor(double x)
x is rounded down to its nearest integer. This integer is returned as a double value.
 double rint(double x)
x is rounded to its nearest integer. If x is equally close to two integers, the even one
is returned as a double.
 int round(float x)
Return (int)Math.floor(x+0.5).
 long round(double x)
Return (long)Math.floor(x+0.5).
min, max, abs, and random
 max(a, b)and min(a, b)
Returns the maximum or minimum of two parameters.
 abs(a)
Returns the absolute value of the parameter.
 random()
Returns a random double value
in the range [0.0, 1.0).
Chapter 5 Arrays
 Introducing Arrays
 Declaring Array Variables, Creating Arrays, and
Initializing Arrays
 Passing Arrays to Methods
Introducing Arrays
Array is a data structure that represents a collection of
the same types of data.
myList[0]
myList[1]
myList[2]
myList[3]
myList[4]
myList[5]
myList[6]
myList[7]
myList[8]
myList[9]
double[] myList = new double[10];
myList reference
An Array of 10
Elements
of type double
Declaring Array Variables
 datatype[] arrayname;
Example:
double[] myList;
 datatype arrayname[];
Example:
double myList[];
Creating Arrays
arrayName = new datatype[arraySize];
Example:
myList = new double[10];
myList[0] references the first element in the array.
myList[9] references the last element in the array.
Declaring and Creating
in One Step
 datatype[] arrayname = new
datatype[arraySize];
double[] myList = new double[10];
 datatype arrayname[] = new
datatype[arraySize];
double myList[] = new double[10];
The Length of Arrays
Once an array is created, its size is fixed. It
cannot be changed. You can find its size
using
arrayVariable.length
For example,
myList.length returns 10
Initializing Arrays
 Using a loop:
for (int i = 0; i < myList.length; i++)
myList[i] = i;
 Declaring, creating, initializing in one step:
double[] myList = {1.9, 2.9, 3.4, 3.5};
This shorthand syntax must be in one statement.
Declaring, creating, initializing Using the
Shorthand Notation
double[] myList = {1.9, 2.9, 3.4, 3.5};
This shorthand notation is equivalent to the following
statements:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
CAUTION
Using the shorthand notation,
you have to declare, create,
and initialize the array all
in one statement. Splitting it
would cause a syntax error.
For example, the following is
wrong:
double[] myList;
myList = {1.9, 2.9, 3.4, 3.5};
Passing Arrays to Methods
Java uses pass by value to pass
parameters to a method. There are
important differences between passing
a value of variables of primitive data
types and passing arrays.
 For a parameter of a primitive type
value, the actual value is passed.
Changing the value of the local
parameter inside the method does not
affect the value of the variable
outside the method.
 For a parameter of an array type, the
value of the parameter contains a
reference to an array; this reference
is passed to the method. Any changes
to the array that occur inside the
THE END

More Related Content

What's hot

What's hot (20)

Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Arrays
ArraysArrays
Arrays
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Arrays
ArraysArrays
Arrays
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
 
Arrays
ArraysArrays
Arrays
 
Day 4b iteration and functions for-loops.pptx
Day 4b   iteration and functions  for-loops.pptxDay 4b   iteration and functions  for-loops.pptx
Day 4b iteration and functions for-loops.pptx
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Day 4a iteration and functions.pptx
Day 4a   iteration and functions.pptxDay 4a   iteration and functions.pptx
Day 4a iteration and functions.pptx
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Arrays
ArraysArrays
Arrays
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 

Viewers also liked

Content Marketing Strategy Attracts New Business
Content Marketing Strategy Attracts New BusinessContent Marketing Strategy Attracts New Business
Content Marketing Strategy Attracts New BusinessSRIMedia
 
Taller de mediación en el colegio Europa de Montequinto
Taller de mediación en el colegio Europa de MontequintoTaller de mediación en el colegio Europa de Montequinto
Taller de mediación en el colegio Europa de MontequintoCEIP Europa
 
Oracle Service Cloud Benefits for you
Oracle Service Cloud Benefits for youOracle Service Cloud Benefits for you
Oracle Service Cloud Benefits for youVincent van Huizen
 
Reunión de socios PMI Madrid Spain Chapter - 29-octubre-2013
Reunión de socios PMI Madrid Spain Chapter - 29-octubre-2013Reunión de socios PMI Madrid Spain Chapter - 29-octubre-2013
Reunión de socios PMI Madrid Spain Chapter - 29-octubre-2013Jesús Vázquez González
 
Menú San Valentín 2014 Restaurante Manolín Valladolid
Menú San Valentín 2014 Restaurante Manolín ValladolidMenú San Valentín 2014 Restaurante Manolín Valladolid
Menú San Valentín 2014 Restaurante Manolín ValladolidRestaurante Manolín Valladolid
 
PresentacióN Smh Red.Es V.1.2
PresentacióN Smh   Red.Es V.1.2PresentacióN Smh   Red.Es V.1.2
PresentacióN Smh Red.Es V.1.2muriel sebas
 
Stermedia Profile und Portfolio
Stermedia Profile und PortfolioStermedia Profile und Portfolio
Stermedia Profile und Portfoliostermedia
 
Manual de Etiqueta Sustentável
Manual de Etiqueta SustentávelManual de Etiqueta Sustentável
Manual de Etiqueta SustentávelCOOAMPLA
 
Donation For Anna Hazare Movement in 2010 12
Donation For Anna Hazare Movement in 2010 12Donation For Anna Hazare Movement in 2010 12
Donation For Anna Hazare Movement in 2010 12Akash Agrawal
 
Keynote capitals india morning note 07 november-12
Keynote capitals india morning note 07 november-12Keynote capitals india morning note 07 november-12
Keynote capitals india morning note 07 november-12Keynote Capitals Ltd.
 
Plantilla para evaluar recursos digitales asmed
Plantilla para evaluar recursos digitales asmedPlantilla para evaluar recursos digitales asmed
Plantilla para evaluar recursos digitales asmedAsmed Trujillo
 
Sig t01-modelos de datos-dominios-representacion
Sig t01-modelos de datos-dominios-representacionSig t01-modelos de datos-dominios-representacion
Sig t01-modelos de datos-dominios-representacionGabriel Parodi
 
OpenStack Nova Network to Neutron Migration: Survey Results
OpenStack Nova Network to Neutron Migration: Survey ResultsOpenStack Nova Network to Neutron Migration: Survey Results
OpenStack Nova Network to Neutron Migration: Survey ResultsJu Lim
 
"La ciudad de los niños" (ideas)
"La ciudad de los niños" (ideas)"La ciudad de los niños" (ideas)
"La ciudad de los niños" (ideas)Sara Alonso Diez
 
sistema de riego para cultivo
sistema de riego para cultivosistema de riego para cultivo
sistema de riego para cultivobaTakoOscar
 

Viewers also liked (20)

Content Marketing Strategy Attracts New Business
Content Marketing Strategy Attracts New BusinessContent Marketing Strategy Attracts New Business
Content Marketing Strategy Attracts New Business
 
Taller de mediación en el colegio Europa de Montequinto
Taller de mediación en el colegio Europa de MontequintoTaller de mediación en el colegio Europa de Montequinto
Taller de mediación en el colegio Europa de Montequinto
 
Andele del siglo xxi mayo-junio10
Andele del siglo xxi   mayo-junio10Andele del siglo xxi   mayo-junio10
Andele del siglo xxi mayo-junio10
 
Oracle Service Cloud Benefits for you
Oracle Service Cloud Benefits for youOracle Service Cloud Benefits for you
Oracle Service Cloud Benefits for you
 
Reunión de socios PMI Madrid Spain Chapter - 29-octubre-2013
Reunión de socios PMI Madrid Spain Chapter - 29-octubre-2013Reunión de socios PMI Madrid Spain Chapter - 29-octubre-2013
Reunión de socios PMI Madrid Spain Chapter - 29-octubre-2013
 
Netiquette
NetiquetteNetiquette
Netiquette
 
Menú San Valentín 2014 Restaurante Manolín Valladolid
Menú San Valentín 2014 Restaurante Manolín ValladolidMenú San Valentín 2014 Restaurante Manolín Valladolid
Menú San Valentín 2014 Restaurante Manolín Valladolid
 
PresentacióN Smh Red.Es V.1.2
PresentacióN Smh   Red.Es V.1.2PresentacióN Smh   Red.Es V.1.2
PresentacióN Smh Red.Es V.1.2
 
Stermedia Profile und Portfolio
Stermedia Profile und PortfolioStermedia Profile und Portfolio
Stermedia Profile und Portfolio
 
Manual de Etiqueta Sustentável
Manual de Etiqueta SustentávelManual de Etiqueta Sustentável
Manual de Etiqueta Sustentável
 
Donation For Anna Hazare Movement in 2010 12
Donation For Anna Hazare Movement in 2010 12Donation For Anna Hazare Movement in 2010 12
Donation For Anna Hazare Movement in 2010 12
 
Keynote capitals india morning note 07 november-12
Keynote capitals india morning note 07 november-12Keynote capitals india morning note 07 november-12
Keynote capitals india morning note 07 november-12
 
Plantilla para evaluar recursos digitales asmed
Plantilla para evaluar recursos digitales asmedPlantilla para evaluar recursos digitales asmed
Plantilla para evaluar recursos digitales asmed
 
Bondia Lleida 13042012
Bondia Lleida 13042012Bondia Lleida 13042012
Bondia Lleida 13042012
 
Sig t01-modelos de datos-dominios-representacion
Sig t01-modelos de datos-dominios-representacionSig t01-modelos de datos-dominios-representacion
Sig t01-modelos de datos-dominios-representacion
 
Perfil Biográfico: Ángel Páez
Perfil Biográfico: Ángel PáezPerfil Biográfico: Ángel Páez
Perfil Biográfico: Ángel Páez
 
Color
ColorColor
Color
 
OpenStack Nova Network to Neutron Migration: Survey Results
OpenStack Nova Network to Neutron Migration: Survey ResultsOpenStack Nova Network to Neutron Migration: Survey Results
OpenStack Nova Network to Neutron Migration: Survey Results
 
"La ciudad de los niños" (ideas)
"La ciudad de los niños" (ideas)"La ciudad de los niños" (ideas)
"La ciudad de los niños" (ideas)
 
sistema de riego para cultivo
sistema de riego para cultivosistema de riego para cultivo
sistema de riego para cultivo
 

Similar to Overloading Methods, Type Casting, Arrays, and Math Class

Similar to Overloading Methods, Type Casting, Arrays, and Math Class (20)

Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
 
Arrays
ArraysArrays
Arrays
 
Built-in Classes in JAVA
Built-in Classes in JAVABuilt-in Classes in JAVA
Built-in Classes in JAVA
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
R Basics
R BasicsR Basics
R Basics
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
C# p9
C# p9C# p9
C# p9
 
6.array
6.array6.array
6.array
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Array
ArrayArray
Array
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 

More from Tanzeel Ahmad

Signal and systems solution manual 2ed a v oppenheim a s willsky - prentic...
Signal and systems  solution manual 2ed   a v oppenheim a s willsky - prentic...Signal and systems  solution manual 2ed   a v oppenheim a s willsky - prentic...
Signal and systems solution manual 2ed a v oppenheim a s willsky - prentic...Tanzeel Ahmad
 
Simulation of sinosoidal pulse width modulation
Simulation of sinosoidal pulse width modulationSimulation of sinosoidal pulse width modulation
Simulation of sinosoidal pulse width modulationTanzeel Ahmad
 
Discrete Time Signal Processing Oppenhm book 2nd
Discrete Time Signal Processing Oppenhm book 2nd Discrete Time Signal Processing Oppenhm book 2nd
Discrete Time Signal Processing Oppenhm book 2nd Tanzeel Ahmad
 
Automobile cruise control
Automobile cruise controlAutomobile cruise control
Automobile cruise controlTanzeel Ahmad
 
indus valley civilization
indus valley civilizationindus valley civilization
indus valley civilizationTanzeel Ahmad
 

More from Tanzeel Ahmad (11)

Signal and systems solution manual 2ed a v oppenheim a s willsky - prentic...
Signal and systems  solution manual 2ed   a v oppenheim a s willsky - prentic...Signal and systems  solution manual 2ed   a v oppenheim a s willsky - prentic...
Signal and systems solution manual 2ed a v oppenheim a s willsky - prentic...
 
Simulation of sinosoidal pulse width modulation
Simulation of sinosoidal pulse width modulationSimulation of sinosoidal pulse width modulation
Simulation of sinosoidal pulse width modulation
 
Discrete Time Signal Processing Oppenhm book 2nd
Discrete Time Signal Processing Oppenhm book 2nd Discrete Time Signal Processing Oppenhm book 2nd
Discrete Time Signal Processing Oppenhm book 2nd
 
Automobile cruise control
Automobile cruise controlAutomobile cruise control
Automobile cruise control
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 
130706266060138191
130706266060138191130706266060138191
130706266060138191
 
130704798265658191
130704798265658191130704798265658191
130704798265658191
 
130700548484460000
130700548484460000130700548484460000
130700548484460000
 
indus valley civilization
indus valley civilizationindus valley civilization
indus valley civilization
 
130553713037500000
130553713037500000130553713037500000
130553713037500000
 
130553704223906250
130553704223906250130553704223906250
130553704223906250
 

Recently uploaded

Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 

Overloading Methods, Type Casting, Arrays, and Math Class

  • 2. Overloading Methods Example 4.3 Overloading the max Method public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; }
  • 3. Type Casting  If x is a float and y is an int, what will be the data type of the following expression?  x * y  The answer is float.  The above expression is called a mixed expression.  The data types of the operands in mixed expressions are converted based on the promotion rules. The promotion rules ensure that the data type of the expression will be the same as the data type of an operand whose type has the highest precision.
  • 4. Explicit Type Casting  Explicit type conversion is a type conversion which is explicitly defined within a program (instead of being done by a compiler for implicit type conversion).  Instead of relying on the promotion rules, we can make an explicit type cast by prefixing the operand with the data type using the following syntax: ( <data type> ) <expression>  Example (float) x / 3 (int) (x / y * 3.0) Type case x to float and then divide it by 3. Type cast the result of the expression x / y * 3.0 to int.
  • 5. Constants  We can change the value of a variable. If we want the value to remain the same, we use a constant. final double PI = 3.14159; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060; These are constants, also called named constant. The reserved word final is used to declare constants. These are called literal constant.
  • 6. Sample Code Fragment //code fragment to input radius and output //area and circumference final double PI = 3.14159; double radius, area, circumference; //compute area and circumference area = PI * radius * radius; circumference = 2.0 * PI * radius; System.out.println("Given Radius: " + radius); System.out.println("Area: " + area); System.out.println(" Circumference: “ + circumference);
  • 7. String Input  Using a Scanner object is a simple way to input data from the standard input System.in, which accepts input from the keyboard.  First we need to associate a Scanner object to System.in as follows:  After the Scanner object is set up, we can read data.  The following inputs the first name (String): import java.util.Scanner; Scanner scanner; scanner = new Scanner(System.in); System.out.print (“Enter your first name: ”); String firstName = scanner.next(); System.out.println(“Nice to meet you, ” + firstName + “.”);
  • 8. Numerical Input  We can use the same Scanner class to input numerical values Scanner scanner = new Scanner(System.in); int age; System.out.print( “Enter your age: “ ); age = scanner.nextInt();
  • 9. Scanner Methods Method Example nextByte( ) byte b = scanner.nextByte( ); nextDouble( ) double d = scanner.nextDouble( ); nextFloat( ) float f = scanner.nextFloat( ); nextInt( ) int i = scanner.nextInt( ); nextLong( ) long l = scanner.nextLong( ); nextShort( ) short s = scanner.nextShort( ); next() String str = scanner.next();
  • 10. The Math class  The Math class in the java.lang package contains class methods for commonly used mathematical functions.  Class methods:  Trigonometric Methods  Exponent Methods  Rounding Methods  min, max, abs, and random Methods double num, x, y; x = …; y = …; num = Math.sqrt(Math.max(x, y) + 12.4);
  • 11. Some commonly use Math Class Methods Method Description exp(a) Natural number e raised to the power of a. log(a) Natural logarithm (base e) of a. floor(a) The largest whole number less than or equal to a. max(a,b) The larger of a and b. pow(a,b) The number a raised to the power of b. sqrt(a) The square root of a. sin(a) The sine of a. (Note: all trigonometric functions are computed in radians)
  • 12. Trigonometric Methods  sin(double a)  cos(double a)  tan(double a)  acos(double a)  asin(double a)  atan(double a) Exponent Methods  exp(double a) Returns e raised to the power of a.  log(double a) Returns the natural logarithm of a.  pow(double a, double b) Returns a raised to the power of b.  sqrt(double a) Returns the square root of a.
  • 13. Rounding Methods  double ceil(double x) x rounded up to its nearest integer. This integer is returned as a double value.  double floor(double x) x is rounded down to its nearest integer. This integer is returned as a double value.  double rint(double x) x is rounded to its nearest integer. If x is equally close to two integers, the even one is returned as a double.  int round(float x) Return (int)Math.floor(x+0.5).  long round(double x) Return (long)Math.floor(x+0.5). min, max, abs, and random  max(a, b)and min(a, b) Returns the maximum or minimum of two parameters.  abs(a) Returns the absolute value of the parameter.  random() Returns a random double value in the range [0.0, 1.0).
  • 14. Chapter 5 Arrays  Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Passing Arrays to Methods
  • 15. Introducing Arrays Array is a data structure that represents a collection of the same types of data. myList[0] myList[1] myList[2] myList[3] myList[4] myList[5] myList[6] myList[7] myList[8] myList[9] double[] myList = new double[10]; myList reference An Array of 10 Elements of type double
  • 16. Declaring Array Variables  datatype[] arrayname; Example: double[] myList;  datatype arrayname[]; Example: double myList[];
  • 17. Creating Arrays arrayName = new datatype[arraySize]; Example: myList = new double[10]; myList[0] references the first element in the array. myList[9] references the last element in the array.
  • 18. Declaring and Creating in One Step  datatype[] arrayname = new datatype[arraySize]; double[] myList = new double[10];  datatype arrayname[] = new datatype[arraySize]; double myList[] = new double[10];
  • 19. The Length of Arrays Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayVariable.length For example, myList.length returns 10
  • 20. Initializing Arrays  Using a loop: for (int i = 0; i < myList.length; i++) myList[i] = i;  Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement.
  • 21. Declaring, creating, initializing Using the Shorthand Notation double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;
  • 22. CAUTION Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double[] myList; myList = {1.9, 2.9, 3.4, 3.5};
  • 23. Passing Arrays to Methods Java uses pass by value to pass parameters to a method. There are important differences between passing a value of variables of primitive data types and passing arrays.  For a parameter of a primitive type value, the actual value is passed. Changing the value of the local parameter inside the method does not affect the value of the variable outside the method.  For a parameter of an array type, the value of the parameter contains a reference to an array; this reference is passed to the method. Any changes to the array that occur inside the