Learn Java with Math by Ron Dai
Author:Ron Dai
Language: eng
Format: mobi, epub, azw3
ISBN: 9781484252093
Publisher: Apress
Published: 0101-01-01T00:00:00+00:00
© Ron Dai 2019
R. DaiLearn Java with Math
https://doi.org/10.1007/978-1-4842-5209-3_20
20. Tracing Moving Objects
Ron Dai1
(1)
Seattle, WA, USA
Java provides a basic coding framework, such as for or while loops and if or switch conditional statements. We can make use of them to keep track of moving objects versus its times. First, we’ll work with a popular math problem - bouncing ball scenario.
Math: Bouncing Ball
In a pure math approach, we’d build a table to record the height after each bounce. It is not that hard, but if we change the height value in the original problem setting, we will have to recalculate the values in the same table by hand.
Example
A ball is dropped from a height of 3 meters. On its first bounce it rises to a height of 2 meters. It keeps falling and bouncing to 2/3 of the height it reached in the previous bounce. On which bounce will it rise to a height less than 0.5 meters? This problem is selected from past AMC 8 (American Mathematics competitions for up to 8th grade)
Answer
The programming approach will largely reduce repetitive manual work.
public static void main(String[] args) {
System.out.println(ballBouncing(3.0));
}
private static int ballBouncing(double originalHeight) {
double currentHeight = originalHeight;
int count = 0;
while(currentHeight > 0.5) {
currentHeight = currentHeight * 2 / 3;
count++;
System.out.println("Bounce No=" + count +
"; current height=" + currentHeight);
}
return count;
}
When you execute it, the output shows the current height after each bounce.
Bounce No=1; current height=2.0
Bounce No=2; current height=1.3333333333333333
Bounce No=3; current height=0.8888888888888888
Bounce No=4; current height=0.5925925925925926
Bounce No=5; current height=0.3950617283950617
5
Changing the parameter originalHeight and re-executing the same program will promptly output the detailed result. This is much more efficient than solving it on paper in a traditional mathematical approach.
Example
A snail tries to get out of a well. Each day it climbs up the side of the well 4 feet and each night it slides down the well 2 feet and 6 inches. If the snail starts 40 feet down inside in the morning, how many days will it take the snail take to get out of the well? This problem is selected from a MathIsCool competition.
Answer
In order to keep using an integer value, we convert feet to inches by calculation. Notice that we set the depth of the well as a constant variable by utilizing the keyword final. We check if the snail has reached the top of the well, after it climbs up every day, and before it slides down.
private static void snail() {
final int DEPTH = 12 * 40;
int currentHeight = 0;
int numOfDays = 0;
while (currentHeight < DEPTH) {
currentHeight += 12 * 4;
numOfDays++;
if (currentHeight >= DEPTH) {
break;
}
currentHeight -= 12 * 2 + 6;
System.out.println("No. " + numOfDays + " day - " +
(DEPTH - currentHeight) + " inches to the top.");
}
System.out.println("No. " + numOfDays + " day - at the top.");
}
This is partial output from the program runtime:
............
No. 1 day - 462 inches to the top.
No. 2 day - 444 inches to the top.
No. 3 day - 426 inches to the top.
No. 4 day - 408 inches to the top.
No. 5 day - 390 inches to the top.
No. 6 day - 372 inches to the top.
Download
Learn Java with Math by Ron Dai.epub
Learn Java with Math by Ron Dai.azw3
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Computer Vision & Pattern Recognition | Expert Systems |
Intelligence & Semantics | Machine Theory |
Natural Language Processing | Neural Networks |
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7846)
Hadoop in Practice by Alex Holmes(5656)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5510)
Life 3.0: Being Human in the Age of Artificial Intelligence by Tegmark Max(4497)
Functional Programming in JavaScript by Mantyla Dan(3720)
The Age of Surveillance Capitalism by Shoshana Zuboff(3413)
Big Data Analysis with Python by Ivan Marin(2969)
Blockchain Basics by Daniel Drescher(2884)
The Rosie Effect by Graeme Simsion(2704)
WordPress Plugin Development Cookbook by Yannick Lefebvre(2583)
Hands-On Machine Learning for Algorithmic Trading by Stefan Jansen(2493)
Applied Predictive Modeling by Max Kuhn & Kjell Johnson(2475)
Dawn of the New Everything by Jaron Lanier(2433)
Test-Driven Development with Java by Alan Mellor(2304)
The Art Of Deception by Kevin Mitnick(2295)
Rapid Viz: A New Method for the Rapid Visualization of Ideas by Kurt Hanks & Larry Belliston(2190)
Human Dynamics Research in Smart and Connected Communities by Shih-Lung Shaw & Daniel Sui(2175)
Data Augmentation with Python by Duc Haba(2148)
Once Upon an Algorithm by Martin Erwig(2143)