Blocks and overloading – Java Tutorial

Blocks and overloading – Java Tutorial

The coding for any method or class that is between “{” and “}” is called a block. Think of a block of code as children’s building blocks. No matter how hard you try, you cannot make the two blocks equal one. You can make them equal one shape (or in our case, one class or method), but never one block.

For example:

public class First
{
public static void main(String[] args)
{
System.out.println("This is my first Java Program!!!");
}
}

has two blocks within the program. The first block is:

public class First
{
}

and the second block is:

public static void main(String[] args)
{
System.out.println("This is my first Java Program!!!");
}

This second block is called a nested block because it is inside the first block. The scope, or area you can reference in, of this block is called local scope, meaning only this block can use the included data. Anything outside of this block would be considered global, meaning that any method or block after the declaration of the global variable can use that variable. You can use nested blocks to write programs inside of programs, or programs that bypass other programs data. This is called overloading. Here is a form of overloading a method:

The Blocks and overloading

public class DemoOverload
{
public static void main(String[] args)
{
int month = 6, day = 24, year = 2003;
overloadDate(month);
overloadDate(month,day);
overloadDate(month,day,year);
}
public static void overloadDate(int mm)
{
System.out.println("Event date " + mm + "/1/2001");
}
public static void overloadDate(int mm, int dd)
{
System.out.println("Event date " + mm + "/" + dd + "/2001");
}
public static void overloadDate(int mm, int dd, int yy)
{
System.out.println("Event date " + mm + "/" + dd + "/" + yy);
}
}

There are other more advanced methods you can use to create desired outputs. This next program involves using the Math class.

public class DemoMath
{
public static void main(String[] args)
{
double val = 26.9;
System.out.print("Absolute value of val is ");
System.out.println(Math.abs(val));
System.out.print("Absolute value of -val is ");
System.out.println(Math.abs(-val));
System.out.print("The square root of val is ");
System.out.println(Math.sqrt(val));
System.out.print("Val rounded is ");
System.out.println(Math.round(val));
}
}

Another advanced method is the import java.util.*. This whole class, which is included in the JDK when you download the program to your computer, will import a library of classes and methods for you to use without having to write out every method you wish to use. In future lessons, you will learn more about other class packages that are included with the Java JDK. These packages make it so you don’t have to “reinvent the wheel”. The following is an example of this class:

import java.util.*;
public class DemoDate
{
public static void main(String[] args)
{
Date startTime = new Date();
Date classStart = new Date(99,7,25);
System.out.println("The current date is " + startTime);
System.out.println("The class started on " + classStart);
Date endTime = new Date();
System.out.print("Time elapsed is ");
System.out.print(endTime.getTime() - startTime.getTime());
System.out.println(" milliseconds");
}
}

You cannot type import java.* to use all the packages. You have to access each package individually. Although the word “import” is at the front of the call, “import” simply tells the program that you will be referring to some of the data and method names that are part of the class you are importing. Below is a program that computes three commission rates. The first method computes the sales and the double rate. The second method computes the sales and the integer rate by converting the integer to a double. The third method does the same as the second, only it contains its own rate.

public class Commission
{
   //main() sends the values to each of the methods
  public static void main(String[] args)
  {
    double salesFigure = 865.42;
    double commRateOne = 0.25;
    int commRateTwo = 30;
    computeCommission(salesFigure,commRateOne);
    computeCommission(salesFigure,commRateTwo);
    computeCommission(salesFigure);
  }

   //First computeCommission() computes the two double variables
  public static void computeCommission(double sales, double rateOne)
  {
    double commission;
    commission = sales * rateOne;
    System.out.println("Your first commission is " + commission);
  }

   //Second computeCommission() computes the integer and double variables
  public static void computeCommission(double sales, int rateTwo)
  {
    double commission, rateAsPercent;
    rateAsPercent = rateTwo / 100.0;
    commission = sales * rateAsPercent;
    System.out.println("Your second commission is " + commission);
  }

   //Third computeCommission() computes the integer with a new rate
  public static void computeCommission(double sales)
  {
    double commission, rate = 7.5, rateAsPercent;
    rateAsPercent = rate / 100.0;
    commission = sales * rateAsPercent;
    System.out.println("Your third commission is " + commission);
  }
}

Question: I came to this site to learn Java. I don’t want to learn some programming language. When are you going to teach me how to do things relating to HTML?

Answer: On the contrary, you ARE learning Java because Java IS a programming language. As far as HTML goes, you’ll be learning how to incorporate Java with HTML before you know it.