Tag Archives: getting started
Python || Which Interpreter To Use?
Unlike compiled programming languages such as C, C++ or Java, Python is an interpreted language. Since this is the case, the type of interpreter you use will probably be the same no matter which programming environment you select. You can download the official Python interpreter here. Python version 3 will be featured throughout this site.
If you are using Windows, download the file named “Python 3.X.X Windows x86 MSI Installer“. If you are using Ubuntu, type the following command into the terminal:
sudo apt-get install python3
In terms of which IDE to use, there are a few choices, and your choice should probably be based on what you intend to write with it.
If you intend to program on Windows, the path of least resistance is to try the IDLE which comes bundled with the Windows installation of Python. This provides you with a straightforward IDE with (obviously) good integration with the Windows platform. This IDE is free, but is very basic and is almost like a simple text editor. So if you wish to go this route, there are other alternatives which are more beneficial and practical for use, such as Notepad++ or Gedit.
As far as IDE’s go, Eclipse grouped with PyDev is a popular alternative, as is PyScripter. Both of these will also give you a visual debugger and a build environment.
Ultimately, my opinion is, if you’re just starting out, go with Notepad++ or Gedit. Then when you’re more comfortable with what you’re doing – explore the alternatives.
Personally, I use PyScripter, and all the code that is posted on this site was created using that IDE
Assembly || Hello World!
This page will consist of creating the typical “hello world!” application. if you have never programmed in assembly before, this will be very interesting as the syntax is very different from most high level programming languages.
As noted on the introductory page, the assembly code presented on this site (X86-64) was assembled using The Netwide Assembler (NASM) under the Unix platform (Ubuntu) in association with C/C++ files. The purpose of combining Assembly code in association with C/C++ files is to demonstrate how each language “talks” to each other. Also, more importantly it is because today, it is unusual to create a stand alone program written completely in assembly language. Why is that the case? Because It is much easier and faster to program in a high level language than it is in assembly. So why should you learn assembly? Learning assembly can be most useful to help one gain a deeper understanding of how computers work, aswell as helping one to better understand how compilers and higher level languages like C work.
==== HELLO WORLD ====
All of the programs presented on this site will start with a simple C or C++ driver program like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
// =================================================================================== // Author: K Perkins // Date: Jul 9, 2012 // Program: Hello World // Taken From: http://programmingnotes.org/ // File: driver.c // // Purpose: This is the driver to the Hello World program. Driver.c only calls // helloWorld.asm. This file is used just as a "driver" and demostrates how to use a // C file along with an Assembly file to create one working program. Once the Assembly // file displays "Hello World" to the screen, control of the program is passed // back to the driver.c file, then the program closes // // ----- These are the commands to link all the files together ------- // // (1) Compile driver.c file: gcc -c -Wall -m64 -std=c99 -l driver.lis -o driver.o driver.c // (2) helloWorld.asm assembler file nasm -f elf64 -l helloWorld.lis -o helloWorld.o helloWorld.asm // (3) Link all files: gcc -m64 -o helloWorld.out driver.o helloWorld.o // (4) Execute in 64-bit protected mode: ./helloWorld.out // // ===== Begin code area ============================================================== #include <stdio.h> // external function prototype extern unsigned long int DisplayHelloWorld(); int main() { // declare variable unsigned long int returnCode = 1987; // display message to the screen printf("nnWelcome to My Programming Notes' Assembly Program.n"); printf("nControl will now be passed to the Assembly file...nn"); // here is a function call to the assembly file, where the asm file passes // back a return code to this current file, which will be displayed below returnCode = DisplayHelloWorld(); printf("nControl has now been passed back from the Assembly file to the C file!n"); printf("nThe return code is: %lu", returnCode); printf("nnBYE!n"); return returnCode; }// http://programmingnotes.org/ |
The “driver” file really only has one task, and that is simply to call the assembly function named ‘DisplayHelloWorld()’ as noted on line 39. This is a routine that will be present among all the code on this site.
There are several advantages in using the C driver routine. First, this lets the C system set up the program to run correctly in protected mode. All the segments and their corresponding segment registers will be initialized by C. The assembly code doesn’t need to worry about any of this. Secondly, the C library will also be available to be used by the assembly code.
The following shows a simple assembly program utilizing the C function “printf” to display ‘Hello World’ to the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
;===================================================================================== ; Author: K Perkins ; Date: Jul 9, 2012 ; Program: Hello World ; Taken From: http://programmingnotes.org/ ; File: helloWorld.asm ; ; Purpose: This is the helloWorld.asm file which demonstrates how to display text to ; the screen in assembly using C libraries. ; ;===== Begin code area =============================================================== extern printf ;External function printf taken ;from the C library which will be ;used to display output to the screen ; segment .data ;Place initialized data here ; ;======== Text declarations & variables which will be displayed to the user ========== displayMessage db "Hello World!",10, 0 specifierForStringData db "%s", 0 displayLineSeperator db "--------------------------------------------",10,0 ; ;;========= End of text declarations which will be displayed to the user ============= ; segment .bss ;Place un-initialized data here ; segment .text ;Place instruction code here ; global DisplayHelloWorld ;DisplayHelloWorld- the declaration ;that is visible for other programs ;to link to it ; DisplayHelloWorld: ;This is an entry point. Execution ;will begin here. ; ;============= Push registers to the stack =========================================== ;safe programming which pushes all registers to the stack so data doesnt get corrupted push rbp push rbx push rcx push rdx push rdi push rsi push rbp push r8 push r9 push r10 push r11 push r12 push r13 push r14 push r15 ; ;================ End of Push Registers ============================================== ; ; Left side: X86 instructions ;Right side: the narrative 'A.K.A' ;the story about this program. ; ;===== #1 Display Line Seperator ===================================================== mov qword rdi, specifierForStringData ;Prepare printf for string output mov qword rsi, displayLineSeperator ;The 'line seperator' is displayed mov qword rax, 0 ;No vector registers used call printf ;printf is going to output the data ; ;===== #2 Display Hello World ======================================================== mov qword rdi, specifierForStringData ;Prepare printf for string output mov qword rsi, displayMessage ;Hello World will be displayed mov qword rax, 0 ;No vector registers used call printf ;printf is going to output the data ; ;===== #3 Display Line Seperator ===================================================== mov qword rdi, specifierForStringData ;Prepare printf for string output mov qword rsi, displayLineSeperator ;The 'line seperator' is displayed mov qword rax, 0 ;No vector registers used call printf ;printf is going to output the data ; ; END CODE EXECUTION FOR HELLOWORLD.ASM ; ;====== pop the registers back from the stack in reverse order ======================== pop r15 pop r14 pop r13 pop r12 pop r11 pop r10 pop r9 pop r8 pop rbp pop rsi pop rdi pop rdx pop rcx pop rbx pop rbp ; ;===== END - RETURN TO CALLED FUNCTION =============================================== mov rax, 0 ;return 0 to the called function ret ;ret pops the stack taking away 8 bytes ; ; http://programmingnotes.org/ ; ;===== End of DisplayHelloWorld subprogram =========================================== |
Line 16 of the program defines a section that specifies memory to be stored in the data segment (whose name is .data). Only initialized data should be defined in this segment. On lines 19 to 21, several strings are declared. They will be printed with the C library, so they must be terminated with a null character (ASCII code 0). Remember there is a big difference between 0 and ’0’. Note, the number 10 is the ASCII code for a newline.
Uninitialized data should be declared in the .bss segment (named .bss on line 25). This segment gets its name from an early UNIX-based assembler operator that meant “block started by symbol.”
The code segment named .text is where instructions are placed. Note that if you are using Windows, the code label for the main routine (line 29 and 33) should have an underscore prefix, so it would be _DisplayHelloWorld. You would also need to do the same for printf (so it would be _printf). This is part of the C calling convention. This convention specifies the rules C uses when compiling code. It is very important to know this convention when interfacing C and assembly. (Note: This rule is specifically for DOS/Windows, the Linux C compiler does not prepend anything to C symbol names.)
The global directive on line 29 tells the assembler to make the asm main label global. Unlike in C, labels have internal scope by default. This means that only code in the same module can use the label. The global directive gives the specified label (or labels) external scope.
And there you have it! After you assemble the above code (see below), you should get this as your output
Welcome to My Programming Notes' Assembly Program.
Control will now be passed to the Assembly file...
--------------------------------------------
Hello World!
--------------------------------------------Control has now been passed back from the Assembly file to the C file!
The return code is: 0
BYE!
==== ASSEMBLING THE CODE ====
This can be achieved by simply opening the teminal, and doing a copy/paste of the commands listed on the ‘driver.c’ file, lines 16 thru 19. Make sure to compile them in order for the sake of continuity.
1 2 3 4 |
(1) Compile driver.c file: gcc -c -Wall -m64 -std=c99 -l driver.lis -o driver.o driver.c (2) helloWorld.asm assembler file: nasm -f elf64 -l helloWorld.lis -o helloWorld.o helloWorld.asm (3) Link all files: gcc -m64 -o helloWorld.out driver.o helloWorld.o (4) Execute in 64-bit protected mode: ./helloWorld.out |
Be advised, that the commands to assemble the code is designed to run in 64-bit mode. If you are not running a 64-bit machine, the commands will most likely fail to assemble.
If you are running a Windows computer and would like to assemble the code, look here or here for information.
You will need to change the 64-bit registers to 32-bit registers in the “helloWorld.asm” file, aswell as removing lines 38-52 and lines 80-94 respectively in order to run the program successfully.
Assembly || Which Assembler To Use?
A common question one may wonder is, which assembler should I use? There are a few choices, and your choice should probably be based on the type of platform you decide to operate with.
In terms of Assembly, there is a whole family of languages each specific to a different processor, and each language has several different names for a single language. These are the following designations which are often seen: IA-32, X86-32, X86-i386, 80×86, X86, X86-16, IA-64, X86-64, and so on. Some of those early languages are now obsolete. The extension of Assembly which will be presented on this site is the x86 instruction set, specifically being X86-64 (which is known as IA-64 in some documents). The X86-64 Assembly code will be assembled using the Unix platform (Ubuntu) in association with C/C++ files, demonstrating how each language “talks” to each other.
As far as which assembler to use, there are a few high level ones which are available to choose from. Some high level assemblers are Borland’s TASM, The Netwide Assembler’s NASM, Microsoft’s MASM, IBM’s HLASM (for z/Architecture systems), Alessandro Ghignola’s Linoleum, and Niklaus Wirth’s PL/360.
Of the assemblers listed above, The Netwide Assembler (NASM) is the recommended choice, as it is open source and free of charge. The Microsoft Macro Assembler (MASM), and The Borland Company’s Turbo Assembler (TASM) can also be used to learn Assembly Language. You may use other assemblers if you wish, though it is not guaranteed that the code presented on this site will work for assemblers other than NASM.
==== INSTALLING NASM ====
– On Windows –
(1) Visit the NASM homepage, and click on the tab which says "Downloads"
(2) Click on the link to the most current version of NASM, downloading the most recent archive for NASM (the zip file) located under the "win32" directory
(3) Once you've obtained the appropriate archive for NASM, nasm-XXX-dos.zip or nasm-XXX-win32.zip (where XXX denotes the version number of NASM contained in the archive), unpack it into its own directory (for example c:nasm).
Note: You can alternatively download the installer file located in the "win32" directory which will install NASM for you, forgoing the remaining steps.
(4) The archive will contain a set of executable files: the NASM executable file nasm.exe, the NDISASM executable file ndisasm.exe, and possibly additional utilities to handle the RDOFF file format.
(5) The only file NASM needs to run is its own executable, so copy nasm.exe to a directory on your PATH, or alternatively edit autoexec.bat to add the nasm directory to your PATH (to do that under Windows XP, go to Start > Control Panel > System > Advanced > Environment Variables; these instructions may work under other versions of Windows as well.)
(6) That's it - NASM is installed. You don't need the nasm directory to be present to run NASM (unless you've added it to your PATH), so you can delete it if you need to save space; however, you may want to keep the documentation or test programs.
– On Ubuntu –
NASM is currently located in the Ubuntu repository, so you can install it by simply opening the terminal window and entering the command:
sudo apt-get install nasm
– On Other Versions Of Unix –
(1) Visit the NASM homepage, and click on the tab which says "Downloads"
(2) Once you've obtained the Unix source archive for NASM, nasm-XXX.tar.gz (where XXX denotes the version number of NASM contained in the archive), unpack it into a directory such as /usr/local/src. The archive, when unpacked, will create its own subdirectory nasm-XXX.
(3) NASM is an auto-configuring package: once you've unpacked it, cd to the directory it's been unpacked into and type ./configure. This shell script will find the best C compiler to use for building NASM and set up Makefiles accordingly.
(4) Once NASM has auto-configured, you can type make to build the nasm and ndisasm binaries, and then make install to install them in /usr/local/bin and install the man pages nasm.1 and ndisasm.1 in /usr/local/man/man1. Alternatively, you can give options such as --prefix to the configure script (see the file INSTALL for more details), or install the programs yourself.
(5) NASM also comes with a set of utilities for handling the RDOFF custom object-file format, which are in the rdoff subdirectory of the NASM archive. You can build these with make rdf and install them with make rdf_install, if you want them.
Note: Instructions for installing NASM was taken from the official website located here. If you need further assistance installing NASM onto your computer, check out the help forums.
Java || Using If Statements, Char & String Variables
As previously mentioned, you can use the “int/float/double” data type to store numbers. But what if you want to store letters? Char and Strings help you do that.
===== SINGLE CHAR =====
This example will demonstrate a simple program using char, which checks to see if you entered the correctly predefined letter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Mar 30, 2021 // Taken From: http://programmingnotes.org/ // File: GuessALetter.java // Description: Demonstrates using char variables // ============================================================================ import java.util.Scanner; public class GuessALetter { // global variable declaration static Scanner cin = new Scanner(System.in); public static void main(String[] args) { // declare variables char userInput = ' '; System.out.print("Please try to guess the letter I am thinking of: "); // get a single character from the user data input userInput = cin.next().charAt(0); // Use an If Statement to check equality if (userInput == 'a' || userInput == 'A') { System.out.println("You have Guessed correctly!"); } else { System.out.println("Sorry, that was not the correct letter I was thinking of"); } }// end main }// http://programmingnotes.org/ |
Notice in line 19 I declare the char data type, naming it “userInput.” I also initialized it as an empty variable. In line 26 I used an “If/Else Statement” to determine if the user inputted value matches the predefined letter within the program. I also used the “OR” operator in line 26 to determine if the letter the user inputted was lower or uppercase. Try compiling the program simply using this
if (userInput == 'a')
as your if statement, and notice the difference.
The resulting code should give this as output
Please try to guess the letter I am thinking of: A
You have Guessed correctly!
===== CHECK IF LETTER IS UPPER CASE =====
This example is similar to the previous one, and will check if a letter is uppercase
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Mar 30, 2021 // Taken From: http://programmingnotes.org/ // File: CheckIfUppercase.java // Description: Demonstrates checking if a char variable is uppercase // ============================================================================ import java.util.Scanner; public class CheckIfUppercase { // global variable declaration static Scanner cin = new Scanner(System.in); public static void main(String[] args) { // declare variables char userInput = ' '; System.out.print("Please enter an UPPERCASE letter: "); // get a single character from the user data input userInput = cin.next().charAt(0); // Checks to see if inputted data falls between uppercase values if ((userInput >= 'A') && (userInput <= 'Z')) { System.out.println(userInput + " is an uppercase letter"); } else { System.out.println(userInput + " is not an uppercase letter"); } }// end main }// http://programmingnotes.org/ |
Notice in line 26, an If statement was used, which checked to see if the user inputted data fell between letter A and letter Z. We did that by using the “AND” operator. So that IF statement is basically saying (in plain english)
IF ('userInput' is equal to or greater than 'A') AND ('userInput' is equal to or less than 'Z')
THEN it is an uppercase letter
The resulting code should give this as output
Please enter an UPPERCASE letter: p
p is not an uppercase letter
===== CHECK IF LETTER IS A VOWEL =====
This example will utilize more if statements, checking to see if the user inputted data is a vowel or not. This will be very similar to the previous example, utilizing the OR operator once again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Mar 30, 2021 // Taken From: http://programmingnotes.org/ // File: CheckIfVowel.java // Description: Demonstrates checking if a char variable is a vowel // ============================================================================ import java.util.Scanner; public class CheckIfVowel { // global variable declaration static Scanner cin = new Scanner(System.in); public static void main(String[] args) { // declare variables char userInput = ' '; System.out.print("Please enter a vowel: "); // get a single character from the user data input userInput = cin.next().charAt(0); // Checks to see if entered data is A,E,I,O,U,Y if ((userInput == 'a')||(userInput == 'A')||(userInput == 'e')|| (userInput == 'E')||(userInput == 'i')||(userInput == 'I')|| (userInput == 'o')||(userInput == 'O')||(userInput == 'u')|| (userInput == 'U')||(userInput == 'y')||(userInput == 'Y')) { System.out.println("Correct, " + userInput + " is a vowel!"); } else { System.out.println("Sorry, " + userInput + " is not a vowel"); } }// end main }// http://programmingnotes.org/ |
This program should be very straight forward, and its basically checking to see if the user entered data is the letter A, E, I, O, U or Y.
The resulting code should give the following output
Please enter a vowel: o
Correct, o is a vowel!
===== HELLO WORLD v2 =====
This last example will demonstrate using the string data type to print the line “Hello World!” to the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Mar 30, 2021 // Taken From: http://programmingnotes.org/ // File: HelloWorldString.java // Description: Demonstrates using string variables // ============================================================================ import java.util.Scanner; public class HelloWorldString { // global variable declaration static Scanner cin = new Scanner(System.in); public static void main(String[] args) { // declare variables String userInput = ""; System.out.print("Please enter a sentence: "); // get a string from the user userInput = cin.nextLine(); // display message to user System.out.println("You entered: " + userInput); }// end main }// http://programmingnotes.org/ |
The resulting code should give following output
Please enter a sentence: Hello World!
You entered: Hello World!
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac YOUR_CLASS_NAME.java
*** To run the compiled program, simply type this command:
java YOUR_CLASS_NAME
Java || Simple Math Using Int & Double
This page will display the use of int and double data types.
==== ADDING TWO NUMBERS TOGETHER ====
To add two numbers together, you will have to first declare your variables by doing something like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: Mar 4, 2012 // Updated: Mar 7, 2021 // Taken From: http://programmingnotes.org/ // File: Add.java // Description: Demonstrates adding numbers together // ============================================================================ import java.util.Scanner; public class Add { public static void main(String[] args) { // declare variables int num1 = 0; int num2 = 0; int sum = 0; // prepare Scanner for integer data input Scanner cin = new Scanner(System.in); // get data from user System.out.print("Please enter the first number: "); num1 = cin.nextInt(); System.out.print("Please enter the second number: "); num2 = cin.nextInt(); // calculate the sum of the two numbers sum = num1 + num2; // display results to the user System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum); }// end main }// http://programmingnotes.org/ |
Notice in lines 14-16, I declared my variables, giving them a name. You can name your variables anything you want, with a rule of thumb as naming them something meaningful to your code (i.e avoid giving your variables arbitrary names like “x” or “y”). In line 29 the actual math process is taking place, storing the sum of “num1” and “num2” in a variable called “sum.” I also initialized my variables to zero. You should always initialize your variables.
I obtained data from the user by using the Scanner Class.
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac Add.java
*** To run the compiled program, simply type this command:
java Add
The above code should give you the following output:
Please enter the first number: 8
Please enter the second number: 24
The sum of 8 and 24 is: 32
==== SUBTRACTING TWO NUMBERS ====
Subtracting two ints works the same way as the above code, and we would only need to edit the above code in one place to achieve that. In line 29, replace the addition symbol with a subtraction sign, and you should have something like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: Mar 4, 2012 // Updated: Mar 7, 2021 // Taken From: http://programmingnotes.org/ // File: Subtract.java // Description: Demonstrates subtracting numbers together // ============================================================================ import java.util.Scanner; public class Subtract { public static void main(String[] args) { // declare variables int num1 = 0; double num2 = 0; double sum = 0; // prepare Scanner for integer data input Scanner cin = new Scanner(System.in); // get data from user System.out.print("Please enter the first number: "); num1 = cin.nextInt(); System.out.print("Please enter the second number: "); num2 = cin.nextDouble(); // calculate the difference of the two numbers sum = num1 - num2; // display results to the user System.out.println("The difference between " + num1 + " and " + num2 + " is: " + sum); }// end main }// http://programmingnotes.org/ |
Note: In the above example, “cin.nextDouble()” was used on line 26 in place of “nextInt.” The declaration “nextDouble” can be used in place of “nextInt” in case you want to obtain floating point data from the user.
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac Subtract.java
*** To run the compiled program, simply type this command:
java Subtract
The above code should give you the following output
Please enter the first number: 8
Please enter the second number: 23.99999
The difference between 8 and 23.99999 is: -15.99999
==== MULTIPLYING TWO NUMBERS ====
This can be achieved the same way as the 2 previous methods, simply by editing line 29, and replacing the designated math operator with the star symbol “*”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: Mar 4, 2012 // Updated: Mar 7, 2021 // Taken From: http://programmingnotes.org/ // File: Multiply.java // Description: Demonstrates multiplying numbers together // ============================================================================ import java.util.Scanner; public class Multiply { public static void main(String[] args) { // declare variables double num1 = 0; int num2 = 0; double sum = 0; // prepare Scanner for integer data input Scanner cin = new Scanner(System.in); // get data from user System.out.print("Please enter the first number: "); num1 = cin.nextDouble(); System.out.print("Please enter the second number: "); num2 = cin.nextInt(); // calculate the product of the two numbers sum = num1 * num2; // display results to the user System.out.println("The product of " + num1 + " and " + num2 + " is: " + sum); }// end main }// http://programmingnotes.org/ |
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac Multiply.java
*** To run the compiled program, simply type this command:
java Multiply
The above code should give you the following output
Please enter the first number: 7.999999
Please enter the second number: 24
The product of 7.999999 and 24 is: 191.99997
==== DIVIDING TWO NUMBERS TOGETHER ====
In division, when you divide numbers together, sometimes they end in decimals. Int data types can not store decimal data (try it yourself and see), so here is where the use of the double data type is mandatory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: Mar 4, 2012 // Updated: Mar 7, 2021 // Taken From: http://programmingnotes.org/ // File: Divide.java // Description: Demonstrates dividing numbers together // ============================================================================ import java.util.Scanner; public class Divide { public static void main(String[] args) { // declare variables double num1 = 0; double num2 = 0; double sum = 0; // prepare Scanner for integer data input Scanner cin = new Scanner(System.in); // get data from user System.out.print("Please enter the first number: "); num1 = cin.nextDouble(); System.out.print("Please enter the second number: "); num2 = cin.nextDouble(); // calculate the quotient of the two numbers sum = num1 / num2; // display results to the user System.out.println("The quotient of " + num1 + " and " + num2 + " is: " + sum); }// end main }// http://programmingnotes.org/ |
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac Divide.java
*** To run the compiled program, simply type this command:
java Divide
The above code should give the following output
Please enter the first number: 7.99999
Please enter the second number: 23.99999
The quotient of 7.99999 and 23.99999 is: 0.33333305
==== MODULUS ====
If you wanted to capture the remainder of the quotient you calculated from the above code, you would use the modulus operator (%).
From the above code, you would only need to edit line 29, from division, to modulus.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: Mar 4, 2012 // Updated: Mar 7, 2021 // Taken From: http://programmingnotes.org/ // File: Modulus.java // Description: Demonstrates performing modulus on numbers // ============================================================================ import java.util.Scanner; public class Modulus { public static void main(String[] args) { // declare variables double num1 = 0; int num2 = 0; double remainder = 0; // prepare Scanner for integer data input Scanner cin = new Scanner(System.in); // get data from user System.out.print("Please enter the first number: "); num1 = cin.nextDouble(); System.out.print("Please enter the second number: "); num2 = cin.nextInt(); // calculate the remainder of the two numbers remainder = num1 % num2; // display results to the user System.out.println("The remainder of " + num1 + " and " + num2 + " is: " + remainder); }// end main }// http://programmingnotes.org/ |
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac Modulus.java
*** To run the compiled program, simply type this command:
java Modulus
The above code should give the following output
Please enter the first number: 23.99999
Please enter the second number: 8
The remainder of 23.99999 and 8 is: 7.9999905
Java || Which Compiler To Use?
A common question one may wonder is, which compiler should I use? There are a few choices, and your choice should probably be based on what you intend to write with it.
If you intend to program on Windows, a common choice is to download one of the many (free) Integrated Development Environment’s (IDE) which are currently available on the internet. The IDE’s I recommend are:
Netbeans and Eclipse are the equivalent of Visual Studios and CodeBlocks for C++. (NOTE: Netbeans and Eclipse is also available for download on Linux).
You will also need to have Java installed on your computer in order to develop Java applications. If you are using Windows, then visit the Oracle website. and download and install either JDK7 or JDK7 with Netbeans. Don’t bother with JRE, as it merely runs Java programs, but does not compile them.
==== LINUX USERS ====
To install Java, open the terminal window and enter the command:
sudo apt-get install openjdk-7-jdk
If you want to add Netbeans to your computer, then use the same terminal window and enter the command below:
sudo apt-get install netbeans
Alternatively, if you want to add Eclipse to your computer, then use the same terminal window and enter the command below:
sudo apt-get install eclipse-platform
Personally, I use a simple text editor (gedit) and the terminal window in Ubuntu to program in Java, and all the source code which is presented on this site was created using that development environment.