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(8261)
Test-Driven Development with Java by Alan Mellor(6395)
Data Augmentation with Python by Duc Haba(6294)
Principles of Data Fabric by Sonia Mezzetta(6072)
Hadoop in Practice by Alex Holmes(5941)
Learn Blender Simulations the Right Way by Stephen Pearson(5930)
Microservices with Spring Boot 3 and Spring Cloud by Magnus Larsson(5817)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5786)
RPA Solution Architect's Handbook by Sachin Sahgal(5211)
Big Data Analysis with Python by Ivan Marin(5181)
Life 3.0: Being Human in the Age of Artificial Intelligence by Tegmark Max(5108)
The Infinite Retina by Robert Scoble Irena Cronin(4903)
Pretrain Vision and Large Language Models in Python by Emily Webber(4157)
Functional Programming in JavaScript by Mantyla Dan(4021)
The Age of Surveillance Capitalism by Shoshana Zuboff(3917)
Infrastructure as Code for Beginners by Russ McKendrick(3915)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3619)
Embracing Microservices Design by Ovais Mehboob Ahmed Khan Nabil Siddiqui and Timothy Oleson(3431)
Applied Machine Learning for Healthcare and Life Sciences Using AWS by Ujjwal Ratan(3404)
