Tuesday 27 October 2015

Constants, Computation of Formulas and Concatenation in Java


You and I are Awesome!!!

I want to point out somethings with regards to my previous post which are:

1.   Variables have a naming convention that is to say you cannot name a variable anyhow you want. To name a variable the first letter should begin with a small letter and the next word should begin with a capslock for example: averageClass is correct. Averageclass is wrong, averageclass is wrong, AverageClass is wrong for naming a variable but correct for naming a class.

2.   The computer does not see mathematical calculations exactly the way we see it.
For example: Mathematically, 5 / 2 = 2.5
But in programming, 5 / 2 = 2

The reason is this, the computer sees the numerator and the denominator as integers (numbers that do not have a fraction or decimal point). Therefore, the computer gives its answer as an integer too and not a floating point number.

To tackle this problem, you have to make the computer see either the numerator or the denominator as a floating point number so that your answer can also be a floating point number.

For Example: In programming, 5.0 / 2 = 2.5 or 5 / 2.0 = 2.5

Another way to solve this problem is through declaration and initialization. See this chunk of code as an example:


// Making the numerator a floating point number
double numerator = 5;
// Leaving the denominator as an integer
int denominator = 2;
// Declaring and Initializing the answer as a floating point number
double answer = numerator / denominator

If you run this chunk of code you will get 2.5

With all these said let's move forward!

What is a constant in Java Programming?

A constant is just a variable that holds a data that never changes in the course of the program.

Take for example pi
pi is a mathematical constant that holds the value 22/7 and from now till the next 60 years it will still not change.
Therefore, it can be declared as a constant in a Java program.

A constant is not just declared like a variable. The declaration of a constant requires the use of the Java keyword final.

This is an example of how a constant is declared:

final datatype CONSTANT = value;

In the above assignment expression, you will observe that the word CONSTANT is all in capslock.

Good! That is to tell you that constant also has a naming convention.
In naming a constant USE CAPSLOCK ALL THROUGH. If you want to join more than one words to a constant use an underscore to separate them and still write them all in capslock.
For Example:
NICK_CONSTANT, MATH_PI, AVOGADRO_CONSTANT_VALUE

Now moving to the datatype. I am very sure I talked about datatypes in the previous post. So if you need a review you can go back there.

Making Pi a constant in our program here is how it goes:

final double PI = 22.0/7;

If you remember from the statements above if you write 22/7 it would return 3 as the answer because the computer would see the numerator, 22 and the denominator, 7 as integers and return 3 but the inclusion of 22.0 makes our answer to be 3.142.. a floating point number which is actually the correct answer and the datatype double is used because the value we are storing in the constant variable is a floating point number.

I HOPE YOU GOT THE GIST.

Now I would like to talk about concatentation

The symbol that is used for concatentation is the plus sign (+).

Concatentation is the process of joining a value to a string or a value to a value.

Let's take an example now.

Imaging you have a value for the area of a square with length 4 cm and you want the computer console to display the area of the square in this words.

The area of the square is 16 cm-squared.

Since 16 is a numeric value and you are not doing the computation so you do not know the area of the square.

You must have to join the variable holding the value of the area of the square the string.

Here is how it is done:

Chunk Of Code

// Declaring and Intializing Length
int length = 4;
// Finding Area Of A Square
int area = length * length;
// Displaying the Answer
System.out.println("The area of the square is "+area+" cm-squared.");

I hope you noticed the way I used plus to join the variable area to the sentence. If you did, that's concatentation.
We would do a lot of concatenation in Java so be ready.

If you run the program the computer would output this:

The area of the square is 16 cm-squared.

Let's Try Two Problems

1.   Write A Program That Calculates The Area Of A Circle Of Radius 3 cm.
Hint: declare and initialize the constant Pi
      formula:  πr²

Here is my own code:

package nicholas;
public class AreaCircle{
public static void main(String [] args){
// Declaring and Initializing the Constant Pi
final double PI = 22.0 / 7;
// Declaring and Initializing the radius
int radius = 3;
// Declaring, Initialization and computing the area, ANSWER
double area = PI * radius * radius;
// Displaying My Output
System.out.println("The area of the circle of radius "+radius+" cm is "+area+" cm-squared.");
}
}

CUSTOMIZING AND RUNNING....

Here is the screenshot of my code:

Here is the ouput in the console:

2.   Write A Program That Calculates The Volume Of A Cylinder Of Radius 4.7 m And Height 12 m.
Hint: declare and initialize the constant Pi
      formula:  πr²h

Here is my own code:

package nicholas;
public class VolumeCylinder{
public static void main(String [] args){
// Declaring and Initializing the constant Pi
final double PI = 22.0 / 7;
// Declaring and Initializing the radius
double radius = 4.7;
// Declaring and Initializing the height
int height = 12;
// Declaring and Computing the volume
double volume = PI * radius * radius * height;
// Displaying the Output
System.out.print("The volume of the cylinder of ");
System.out.print("radius "+radius+" m and height "+height+" m ");
System.out.println("is "+volume+" m-cube.");
}
}

CUSTOMIZE AND RUN...

I hope you still remember what I said one of my previous posts on
System.out.print(); - This puts the next output on the same line.
System.out.println(); - This moves the next output to the next line.

Here is the screenshot of my own code:

Here is the output:


That's all for now on this post.

I have got some assignments you can do. Try and do them because when you do and get them all right. You will definitely feel AWESOME.

1.   Write a program that calculates the area of a cone of radius 3.2 dm and slant height 12 dm.
2.   Write a program that calculates the hypotenuse of a right angled triangle with an opposite of 6 cm and adjacent of 8 cm.
3.   Write a program that calculates the perimeter of a rectangle of width 12 m and length 10 cm
Hint: Make sure you program your units to be the same before computation.

Peace!!

No comments:

Post a Comment