Skip to main content

Posts

Beginner Java Exercise: Method Overloading

Pretty self explanatory, so I won't go into too much of an explanation: just a quick example. Method overloading is allowed in Java enabling you to define different methods with the same name as long as their signatures are different. The Java compiler determines which method to use based on the signature.

Beginner Java Exercise: Creating Custom Methods

A method is normally created to execute some repetitive code. For example, lets say you want to perform the quadratic formula on 10 different instances; Instead of writing the quadratic formula 10 different times, you create a method, pass the necessary parameters (in this case a, b and c) and have the code method return the value. I honestly use it to make my code look cleaner and for re-usability across multiple classes. In the following example, we'll find the greatest common divisor by utilizing the following steps: Instantiate the Scanner class Prompt for first integer and store Prompt for second integer and store Call the gcd() method to calculate the greatest common divisor Return the value and display it to the screen

PHP Forms Class: All Tied Together

In the last few examples concerning the forms, I've gone through how to create the Forms class and how to create a method to utilize that class. To refresh your memory: http://dinocajic.blogspot.com/2015/06/php-forms-class.html http://dinocajic.blogspot.com/2015/06/php-forms-class-in-action.html This time, I'm going to show you how I tie it all in by creating a real life example. The following class was created as part of a larger web-application that inserts a purchase order into the database. It's very brief and the next class will add items to that purchase order. What we want to store is: PO # Date that the shipment is scheduled to arrive Which location is the shipment arriving to What's the current status of the shipment. If it's ordered from China, for example, there can be a few different types of statuses: Water Rail Truck or Hasn't Shipped Overview of the class AddPackingSlip I like to take the Java approach and import all o...

PHP Forms Class in Action

A couple of days ago I created a Forms class that I can re-use in most of my projects. I stated that I would show you an example of how I generate the form fields when they're needed. So here we go. To reference the Form Class: http://dinocajic.blogspot.com/2015/06/php-forms-class.html In the following example I'm creating a form and utilizing the forms class to help me separate most of the HTML from PHP. This is what the form will look like once the code is executed. To help guide you through the code: $_form is instantiated prior to the method call.  A random token is generated and set to $_SESSION['token'] to prevent Cross Site Request Forgery Also as an additional measure of security, we're only going to allow for the form to be valid for 5 minutes. So, we'll set $_SESSION['token_time'] = current time() The hidden field is generated with the random token as the value Input type of text is called to generate po_number....

Beginner Java Exercise: Break vs. Continue

There are a couple of ways to interrupt a loop: one is to use the keyword break and the other is to use the keyword continue . In the following example, a loop-continuation-condition is set to test if the number is less than 20. If true, the number variable will increment by one and the current value of the variable number will be added to the total as is defined by the variable sum. So if we let the following loop continue without any sort of interruption, the number will be 20 and the sum will be 210. However, adding the keyword break after the test is performed within the while loop will cause the loop to stop if the sum is at or larger than 100. The output for the number variable will be 14 and for the sum variable, 105. The next example deals with the keyword continue. We start the loop by testing whether the number is less than 20. If so, the number variable is incremented by 1. The next condition that it checks is whether the number variable is set to 10 or 11. ...

Beginner Java Exercise: Sentinel Values and Do-While Loops

In my previous post on while loops, we used a loop-continuation-condition to test the arguments. In this example, we'll loop at a sentinel-controlled loop. The sentinel value is a special input value that tests the condition within the while loop. To jump right to it, we'll test if an int variable is not equal to 0. The data != 0 within the while (data != 0) { ... } is the sentinel-controlled-condition. In the following example, we'll keep adding an integer to itself until the user enters 0. Once the user enters 0, the loop will break and the user will be displayed with the sum of all of the integers that he/she has entered. As you can see from the code above, the code is somewhat redundant. It asks the user to enter an integer twice: Once before the loop begins, and an x amount of times within the loop (until the user enters 0). A better approach would be through a do-while loop. In a do-while loop, you "do" something "while" the condition...

PHP Forms Class

Here's another attempt to create a usable PHP forms class. I know everyone has their favorites. I had to create my own to get the functionality that I needed. Hopefully it helps someone out there. Read the comments above each method for clarification. I'll create an example of how I use the forms class at a later date. P.S. I see that Github moved some stuff around not keeping the white-spacing that I wanted. Appologies on their behalf. Ha.