Recently, one of my BCB readers asked me a question that brought about this blog topic, he said “I have read through your several posts but how do I bring them all together and write a complete functional code?” Then I thought to myself that he was right, probably other readers might also have the same challenge bringing all the pieces together. Therefore, in this post and my next couple of posts, we will be taking a deep dive, referring back at every topic on Java we have previously dealt with on this blog in order to write a complete functional code. I hope you are ready to get your hands really dirty…lol.
Assumption: To start with, I assume that by now you have installed Java and you have java path set properly on your machine. If not, please refer to my previous post on this Lay your Bed this Christmas. Remember to direct all questions to me should you have any challenge following the steps. I will be using Netbeans IDE (Integrated Development Environment) for this example, you can use any IDE you are comfortable with to run this example. See list of possible IDEs and where to download them on this post Lay your Bed this Christmas.
Task: We will be writing a sample program to calculate the area of different basic shapes i.e. rectangle, square, circle etc..
Are you a passionate about science, technology, engineering, and math (STEM) ? Do you have some experience conducting scientific research (e.g., graduate students, postdoctoral fellows, university professors, or STEM professionals)? Do you have access to at least one communication device with internet capabilities? Are you passionate about engaging and inspiring the next generation of STEM innovators? Are you willing to give your voluntary support? If you answered yes to any of these questions, then Junior Academy is here for you!
During the last two posts, we looked at Java Statements where I explained the different types of statement and how they are categorized. We have also in previous posts been familiar with declarations of different Java keyword and we have seen their various syntax. So today, I will be shifting our attention to one of the things we have seen often in my sample codes, this is the Code Block. Block of codes in programming are similar in context to paragraphs in the English language. The illustration below explains this in more detail.
I started discussion on Java statements in my last post where I explained (i) the importance of Statements using algorithmic steps, (ii) classified the statements we have in Java and (iii) listed out the different types of statements in each category, click here if you missed it. In this post, I will continue the tutorial on these statements and also share sample codes on each of them, their syntax and the output of the sample codes. I hope by the end of this post you would have full understanding of Java Statement and will be able to make use of them appropriately when writing your own code.
A Java program is made up of classes, these classes are made up of methods, constructors and variable definitions, these methods and constructors are made up of set of instructions and these set of instructions are called statements in Java. Statements are tasks carried out by the computer to solve a problem, they are the building blocks of a Java Program. In order to have a good understanding of Java Statement it will first be beneficial to understand how to break a solution down into algorithmic steps. Each step of an algorithm can be represented as a Java statement or statements.
As promised in my last post, I will be sharing sample codes showing how Java operators behave. I have named the Java classes in the sample codes after the operator class and I also added the result of each operation on the variables.
Java operators are symbols used to command the computer to perform operations on operands. They follow either of the formats shown in the diagram below and are classified into eight different categories which includes Arithmetic, Relational, Logical, Unary, Bitwise and Bit Shift, Assignment Operators.