Saturday, October 27, 2018

Learning Java on a Mac

Farrell states it is a tradition that programmers write their first program in a particular language that produces the expression “Hello, world!” (2016). Performing such a tradition for the language of Java will be the second time I have had the pleasure of undertaking in the world of programming. The first time was in 2003 at Penn State, Berks Campus, where I created machine code for Motorola's 68000 microprocessor in the Associate Electrical Engineering Technology program. In this discussion, I will explore some basic knowledge on how to get set up using Java on a Mac and will demonstrate my first Java program that computes a cylinder's area and volume based on a user's prompt for radius and length.

The process of writing a computer program begins with the use of a simple text application such as Text Editor to formulate statements, called source code, using certain rules of the Java language. The text file is saved as a .java file. The whole point of creating source code is to prepare for a smooth interface between the human and computer languages. The Java Compiler, provided by a Java Developer Kit (JDK), converts the source code in to a binary format called bytecode and creates a new .class file. When the program is run, the Java Virtual Machine (JVM) performs a security analysis of the .class file and translates it into machine code, which is a set of instructions that acts upon the computer's operating system to produce a desired result.

The three types of errors that may be encountered in program construction are compile-time, run-time, and logic. A compile-time error is the result of the improper use of the Java language syntax rules. Because of this violation, source code is not able to be converted into bytecode. A run-time error occurs when machine code is created successfully but the program fails to execute. A logic error, the most difficult to resolve, occurs when the machine code is created successfully and the program runs but the execution produces undesired results. For example, consider the programmer who wants to add two numbers together. The programmer would realize a logical error has occured when discovering that the program was actually multiplying the two numbers instead of adding them.

I use a MacBook (13-inch, Mid 2010, Model Identifier 7,1), with a Core 2 Duo 64-bit processor that runs on the 64-bit MacOS High Sierra Version 10.13.6. To write my source code for calculating the area and volume of a cylinder, I used the application Text Editor, which comes with the operating system (OS). When finished writing, I saved the file as a .java file, which can be done by viewing the file extension in the File Save window and replacing .txt with .java.  A JDK is required to compile and execute the program. Therefore, since I have Java Version 8 installed, I downloaded Java JDK Version 8 from Java's website. I used Terminal, which comes with the OS, to compile the source code and have JVM run the program. The command to compile is javac followed by a space and complete file name for the source code. If compiled successfully without any errors, a new file is created with the same file name but with .class file extension. The command to run is java followed by a space and just the file name without an extension. JVM is actually loading and running the bytecode in the .class file.

Using Text Editor and Terminal is a very simplistic, bare bones way to writing programs but it does give you an appreciation for the basic requirements and a greater awareness of the process steps. Some other commands in Terminal are date (display time and date), ls (list the directory contents), pwd (show current directory), and cd (change directory). It is important to note that a PC uses a backward slash ( \ ) for file paths and a Mac uses a forward slash ( / ). To improve and speed up the process of writing sourcode, I downloaded a 32-bit application called Text Wrangler. This application color codes the different functions of the source code but you still have to use Terminal to compile and check for syntax errors. The ultimate experience is to download an Integrated Development Environment (IDE) application. An IDE will automatically check for syntax errors as you type and automatically compiles the code behind the scenes when you choose to run the program. I opted for NetBeans.

The purpose of the blog is just to show how to get started with running a Java program on a Mac. All that is needed next is a good textbook on the topic in order to learn the many tools that are available.

References:

Farrell, J. (2016). Java programming (8th ed.). Boston, MA: Cengage Learning.

The good and the bad of Java programming (2018, August 9). Altexsoft. Retrieved from: https://www.altexsoft.com/blog/engineering/pros-and-cons-of-java-programming/