DEV Community

Cover image for Stage (1/30): Intro to Computer programming & Java
Khalid
Khalid

Posted on

Stage (1/30): Intro to Computer programming & Java

Java is a powerful platform independent computer programming language that can run on any type of computer. It can be used to create larger applications or smaller programs. These programs can be part of a website.

Question: What is a computer?

Answer: A computer is a machine that is capable of following instructions given to it by a program.

● Program: A set or collection of instructions that determines actions that a computer will take.

  • Programmers create software that serves as a specialized tool that performs a specific task.

● Software: A program or collection of programs that are built into the computers hardware.

  • As a general rule of thumb, computer systems are composed of similar hardware devices and software components/parts.

● Hardware: The physical parts of a computer.

  • Hardware can be thought of as a physical device. However, for a computer to function properly, the system of individual devices must operate in conjunction with one another. For example, a single instrument serves a single function, but when used in conjunction with other instruments they form an orchestra. Another example would be how a car is made up of individual parts (eg. engine, transmission, wheels, etc…), but all these parts are used in conjunction so the car can operate.

Computer System Organization

A typical computer system is made up of three organized components which have two parts each.

1) CPU (Central Processing Unit)

A) Control Unit

B) Arithmetic & Logic Unit (ALU)

2) Memory

A) Internal Memory

B) External Memory

3) Input/Output Devices (I/O Devices)

A) Input Devices

B) Output Devices

CPU

1) The CPU* has two functions or purposes. The first is to retrieve instructions and second is to follow those instructions that were retrieved:

a) Control Unit: The control unit serves as a coordinator for all of the computer’s operations. It is responsible for retrieving all of the computers instructions and also, to determine where these instructions can be found. It is also a regulator that uses control signals.

b) Arithmetic & Logic Unit (ALU): This unit is the part of the CPU that is designed to perform mathematical operations.

  • The CPU is engaged in a process known as the FDE* cycle, which is an acronym for (Fetch, Decode, Execute).

● Fetch: This is the first part in the FDE cycle. In this part of the cycle, the CPU fetches / retrieves the next instruction from main memory. This is done based on the way the instructions are given.

● Decode: This is the second part in the FDE cycle. In this part of the cycle, the instructions are fetched / retrieved from main memory main memory get decoded. The instructions were fetched / retrieved are encoded in the form of a number. After the Control Unit decodes these instructions, it will generate an electronic signal.

● Execute: This is the third part in the FDE* cycle. In this part of the cycle, an electronic signal gets routed or sent to the appropriate part of the computer. (eg. ALU, Disk Drive, etc..). This electronic signal will make that part of the computer perform an operation.

Memory

2) Memory* is divided into two parts:

a) Internal Memory: Also known as Random Access Memory (RAM), is a device that holds information.

b) External Memory: Also known as secondary storage, this is a type of memory that can hold on to data for long periods of time, even if there is no power source. Programs that are used often get stored in secondary storage and are loaded into main memory as needed.

Side note about memory: Memory is split up into sections that hold an equal amount of data. Sections are made of 8 “switches” that can be either on or off. When a switch is on it is denoted by the number 1, and off it’s denoted by the number 0. Data is stored by setting switches in a memory location to a pattern which represents a character or a number. 8 switches is known as a bit. A section of memory that is a collection of 8 bits is known as a byte. Each of these bytes gets assigned its own unique number known as an address. These addresses are in order from lowest to highest. (When the computer is off, the information inside RAM is erased.)

Input/Output Devices

3) I/O Devices* are input and output devices:

a) Input Devices: Input is any data that the computer takes from outside. The device that takes in data from outside and gives it to the computer is known as the input device. (eg. Disk Drives, Mouse, Keyboard, etc…)

b) Output Device: Output is any data that the computer gives the outside. The device that send data outside is known as the output device. Data is sent to an output device that formats it and presents it. (eg. Monitors, Printers, Audio Recorders, etc…)

Software

There are two types of Software* generally speaking, which are Operating Systems* & Application Software*.

● Operating Systems: A collection of programs that are in charge of computers hardware devices and controls the process.

● Application Software: The programs that make the computer of use to the user.

Algorithms

In order for a program to function it must follow it Algorithm* sequentially (i.e. in order).

● Algorithm: A series of clearly defined steps used to perform a task or solve a problem.

The CPU of a computer is only able to process instructions when they are written in Machine Language.

● Machine Language: A stream of Binary language written in 0’s and 1’s.

The CPU interprets machine language as commands. Every different type of CPU has its own machine language. Suppose you had two different computers named computer X and computer Y. If a machine language program is written for computer X and is then run on computer Y, the program would have to be rewritten in computer Y’s language in order to run successfully.

To ease the task of programming, languages were created to use words instead of numbers. After a programming language is written using words, it is then translated to machine language. Programmers use software to perform this translation.

Java Applications & Applets

The two types of programs that can be created using Java are Applications* & Applets*.

● Application: A stand alone program that runs on a computer. (eg. Word processors, Spreadsheets, Graphics Programs, etc…)

● Applet: A small application designed to be transmitted over the internet from a web server which is then executed in a web browser. (Applets are important because of how effective they are in extending a web pages capabilities.)

Side note about apps and applets: A web page is normally written in HTML (Hypertext MarkUp Language). It’s limited due to the fact that it only describes the content and layout of a webpage. It doesn’t have the capability of performing math calculations and interacting with the user. Web Designers write Java applets to perform operations which are normally performed by an application, and then embed it into a website. Upon visiting a website, an applet gets downloaded to the visitors browser then executed.

The Java Programming Language

There are certain elements that all programming languages have in common with one another. Below is a list of definitions of the different elements of a programming language.

● Keywords: Also known as “reserved” words. These are words that carry a special meaning in a programming language that may only be used for their intended purpose.

● Lexicon: A set of basic elements that are allowed.

● Operators: Words or symbols which perform one or more operations on operands. Operands are usually referring to one more items of data such as numbers.

● Punctuation: The use of punctuation characters is required by most programming languages. These characters are used to serve particular purposes such as separating items on a list or, marking the beginning or ending of a statement.

● Programmer — Defined Names: These are the keywords that have been by the programmer which are unlike keywords that have already been preset by the programmer. These names are used to identify storage locations in memory as well as parts of the program that the programmer created. These names are often called identifiers.

● Syntax: The rules for combining the basic elements into programs. The syntax decides how the operators and keywords can be used, as well as the place the punctuation symbols have to appear. These are the rules that must be followed when writing the program. These rules are built up from basic elements (Lexicon) and you have a syntax which allows you to do so. This also helps you understand what the “allowable” instructions are.

● Variables: A named storage location in the computer’s memory. This is the most fundamental and basic way a java program stores an item of data. (Data stored in a variable can change while the program is running).

The Compiler & the Java Virtual Machine

When writing a java program the program must be typed and saved to a file in the computer. This can be done through a text editor which is similar to a word processing program. This information that’s written is called the source code* and is saved in a source file*. (Note: — These java source files will end with a .java extension.)

● Source Code: The programming statements written by the programmer.

● Source File: The file where the source code is saved.

● Compiler: A program that translates source code into a form that can be executed.

● Syntax Error: Mistakes that violate the rules of the programming language. (Note: In order for the compiler to translate the source code these syntax errors must be corrected.)

  • After a source code is saved to a file, the programmer runs it on the java compiler. When translating the program finds any syntax errors that can be detected in the program. After making sure there are no syntax errors then the compiler will be able to create another file that holds the translated instructions. Most programming language compilers take the source code and translate it into files that are composed of machine language instructions. Such files are known as executable files because they can be directly executed by the computer’s CPU.

JVM (Java Virtual Machine)

Java is different than any other programs. The java compiler will translate a java source file into a file that contains byte code instructions rather than machine code instructions. Since they are byte code instructions and not machine language instructions, they cannot be directly executed by the CPU. They are instead executed by what is known as the JVM* (Java Virtual Machine).

● Java Virtual Machine: Also known as the JVM, this is a program which reads Java byte code instructions and executes them as they are read. (Note: The JVM is often called the interpreter. Which is why java is sometimes referred to as an interpreted language.)

  • Since java byte code cannot be machine language for the CPU, we can consider it as sort of a machine language for the JVM. In this respect, we can think of the JVM as a program that acts as a computer whose language is java byte code.

Portability

Since java byte code is the same on all computers, programs which have been compiled are highly Portable*. Any computer that has a JVM can run a compiled java program.

● Portable: The ability of a program to be written on one type of computer and the run on a variety of computer, with little to no modification necessary.

  • In different language portability can be achieved by creating a compiler for each different computer that language runs on.

Java Software Editions

The JDK (Java Development Kit) is the software which is used to create Java programs. The Java compiler will translate to a Java source file into a file that contains byte code instructions rather than machine code instructions. Since they are byte code instructions and not machine language instructions, they cannot be directly executed by the CPU. They are instead executed by what is known as the JVM (Java Virtual Machine)*

● Java Virtual Machine (JVM): Also known as the JVM, this is a program which reads Java byte code as they are read

  • (Note: The JVM is often called the interpreter. Which is why Java is sometimes referred to as an interpreted language.)

The JDK is also referred to as the SDK(Software Development Kit). Below are JDK editions that are available for use:

● Java SE: Standard edition provides all essential tools for creating java applications and applets.

● Java EE: Enterprise edition provides tool to create large business apps that employ servers and provide services over the web.

● Java ME: The Java micro edition provides a small, highly optimized runtime environment for consumer products,

Compiling and Running a Java Program

Once the JDK has been installed go to your operating systems command prompt. (In windows type in command prompt in the search bar).

Note; Make sure you are in the same directory or folder where the Java program you wish to compile is. The use the javac command, in the following form,

Javac Filename

The name of the file that contains the Java source code is Filename,

(Remember: Remember this file has a .java extension).

This command will run the compiler, Should any syntax errors occur one or more error messages may appear which will cause the compiler to not translate the file into byte code.Once the program has been cleared of syntax errors, the compiler will then be able to translate it into byte code . This byte code is then stored in a file with the .class extension, thus the .java file will be stated in .class, which will be in the same directory or folder as the source file.

Note; In order to run the Java program you must use the following form: java ClassFilename

This is the name of the .class file that you will use in order to execute your file.You do not type the ,class extension. You simply type java ClassFilename.

Example: java Payroll or java Account; Where Payroll and Account are examples of ClassFilename. This command will allow the JVM to execute the program.

Integrated Development Environment(IDE)

Along with command prompt programs, IDE’s can also be found alongside. These environments consist of a text editor, debugger, compiler, along with other utilities which are part of a package that contains a single set of menus. Computation is done by choosing an item from the menu or with the simple click of a button.

The Programming Process

The programming process is made up of several processes which involve, creating, tsting, and debugging. Programming can be a very difficult task, especially for students who just started, Getting started is usually where most students have the most trouble. The following steps may help you deal with these issues:

  1. Define what you want your program to do clearly,

  2. Picture how your program will look when its running on a computer.

  3. Create a model for your program using design tools.

  4. Look over your model carefully and check for logic errors.

  5. Enter your code and compile it.

  6. If you find any errors after you compile your program, go back and correct them. 7. Using test data for input, run the program.

  7. If you detect any runtime errors while running your program, go back and correct them. 9. Make sure the results of your program are valid.

Debugging

There are several types of Bugs which can appear when writing a program. These bugs do not allow the program to run properly and cause Errors . Below are a list of the different types of Errors that may appear in a program.

● Compilation Errors (Syntax Errors): These are errors in the code which are caused by syntax. A few examples are:

  • Misspelled Keywords

  • Wrong assignment symbols

  • Missing symbols that are essential in the code

  • Misplacement of statements

● Execution Errors (Run-Time Error): This is an error that is detected while the program is running.

● Logical Errors: These are often the most difficult errors to catch. The program will run without any Execution Errors, but the result that we want is going to be wrong, Example: You want to write a program that prints the numbers 1 through 100. The program compiles and runs but the result prints out 1 through 50.

Finally programming can be extremely stressful.If you find yourself struggling with a difficult bug, you may feel angry, distressed, or despondent. Remember that not all is lost!

Design For Any Real — World Program

Any real — world program follows a certain design. This makes the process much easier to deal with. When you envision your program design you will see that by properly taking the right steps you will make a seemingly difficult task, much simpler. The key is to work smarter not harder. Below is a layout for any real world design which will help you accomplish your task,

➔ A logical flow of instructions sets the groundwork for accomplishing the task at hand.

➔ Follow the mathematical procedures in the layout that you created.

➔ Create a clear layout of the programming statements that will be used in your code.

➔ Try to envision how this will appear on the screen.

➔ Think about how the presentation will appear to the user of your program.

➔ Remember that your objective as a programmer is to make it user friendly.

➔ Functions such as manuals, help systems, and other forms of documentation help the user navigate through your program.

Software Engineering

Software engineering encompasses the entire process creating computer software.This process involves designing, writing, testing, debugging, documenting modifying, and maintaining intricate software development tasks and projects.Much like regular engineers, software engineers use the tools at their disposal to solve complex issues. Below are some examples:

  1. Specifications for programs.

  2. Diagrams and charts to present screen output.

  3. Diagrams which represent the flow of data from components of the program.

  4. Pseudocode which is simplified language that will help the programmer.

  5. Demonstration of expected input and output.

  6. Specialized software whose function is to test programs.

The majority of programs are large and complex. Therefore, it requires a team of programmers to develop them.

Object Oriented Programming (OOP)

Java is an OOP (Object Oriented Programming) language. This is a method of software development which includes its own practices, concepts, and vocabulary. The two types of programming languages that are used today are Procedural & Object Oriented. The earliest languages were procedural in nature. This means that the programs were made of one or more Procedures*.

● Procedure: A set of programming statements that perform a certain task.

  • Usually procedures operate on data items. These items are separate from procedures. Items of data in procedural programs are passed from one to another. The separation of data are passed from one procedure to another. Problems arise when the code that operates on the data is separated from it. This is why programmers have shifted to OOP due to the fact that the programming is centered around creating Objects* rather than creating procedures.

● Object: A software entity that contains both data and procedures.

  • Data that is contained in objects is known as the objects attributes. The procedures that are performed by an object are the objects methods. The way in which an object addresses the issue of separation between code and data is through Encapsulation* & Data Hiding*. Normally, an object hides its data. However, the methods have access to the data so that it can perform operations on it and modify it as needed.

● Encapsulation: The combination of code and data into a single object.

● Data Hiding: The ability of an object to hide its data from code that is outside the object.

This will be the first in a 30 Part series in which I will continue to cover Java from the basics all the way to the advanced Topics. If you like this post please follow me! Thanks!

Top comments (0)