API Lab
- Yahvin Gali
- Feb 9, 2021
- 2 min read
A review of JAVA that involves writing methods that serve the same function as the basic system methods used in Java.
At the end of the previous post concerning the Annual Fuel Use project, I mentioned the print format method. The printf() method is an inherent java system method, meaning that it is part of the java package.
The java package contains a host of useful system methods that make your job as a programmer easier
Purpose
This projects specifically addresses a few commonly used system methods in java. To be exact, the project helps teach the importance of said methods in making writing programs a whole lot easier, as well as coaching on how to better use them.
Commonly Used Packages
Those who have programmed with JAVA before would recognize some of the java packages I will be mentioning. For example, the java.util and java.io packages are both used quite frequently; any program that utilizes complex functions, such as an internal timer, a GUI graph, a scanner that accepts user input, or a file reader, etc. uses these two packages.
The official mentions of each package by Oracle is linked above.
Common System Methods
As shown in the project, methods such as
Math.pow and Math.sqrt: used in mathematical operations of squaring and rooting
compareTo() and equals(): used to lexigraphically compare two phrases
and length() and indexOf(): used to find the length of a string, or the index of a character
are all utilized in order to create other, more complex methods. This is because each of the mentioned methods are system methods, and are used quite often when doing anything concerning numbers or words.
Created Methods
Some methods which were declared and created in the project compare values (such as max, min) can compare both numbers and strings. This is because two different types of methods are created, even though both have the same name. This is thanks to a technique called overriding, which allows the creation of another max method that only accepts Strings, while still preserving and allowing the use of another max method that only accepts numbers. The entire list of methods can be found here, on the Github page of the project.
Final Thoughts
This project was quite beneficial, as I learnt how to better utilize basic JAVA system methods. I also got a better grasp of what output thee system methods produce, which will aid me in future JAVA endeavors. I did have a few difficulties with the creation of a some methods, such as getLongestString() and getShortestString(). However, I was ecstatic when I managed to resolve the issues.
Comments