Nothing Special   »   [go: up one dir, main page]

Notes On JAVA 10.2005.3: Syntax of Commands

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Notes on JAVA 10.2005.

3: Syntax of Commands:
Program Header: Class Name: Use the New Application Boilerplate/Template in the File Menu. Erase only the // Place your code here and begin programming right there. The class should be named to indicate the purpose or function of the program.

Basic (also called Primitive) Types: Characters char 16 bit stored in Unicode Logical boolean 1 bit true or false (uses one byte) Number Types Integer types (whole numbers) byte 8 bit (1 byte) -128 (27) to +127 (27-1) // 28 different numbers short 16 bit -32768 (215) to +32767 (215-1) // 216 different numbers int 32 bit -2147483648 (231) to +2147483647 (231-1) //232 dif nos long 64 bit -263 to +263-1 // 264 different numbers Floating point types (with decimals) float 32 bit in IEEE 754 format double 64 bit in IEEE 754 format NB: All primitive types are written with lower-case letters. Strings: Strings are limited to 255 characters. Strings are a class (therefore there are several methods associated with strings). NB: Class names are always started with a capital letter. The variables should be named to indicate their purpose or function! NB: Variable names are always written with lower-case letters! int x; char initial; String firstName;

Variable Names: Declaration of Variables: Assignment Statements:

x = 24; // if the variable has been declared already float y = 12.34; // else declaration and assignment in one step float y = x + z / 3 + 5.2; int z = x + 2 * y - y / 4; NB: Assignment is NOT equality, but works from right to left! x = x + 1; means x x + 1; or x + 1 is stored in x, overwriting the old value of x. Whenever you write two or more lines of code that you want to be executed together, it must be blocked with curling brackets: { Statements; } NB: The initial curling brackets are supplied automatically with the Application Boilerplate/Template. Do not erase them! System.out.println (Here follows text, and here follows a + variableName); System.out.print (Here the cursor stays on the same line.); Input is the most difficult part of the Java program. Because a careless user can create input type errors (type as in data type, not as in typing), Java is anticipating such problems and wants to be programmed accordingly. 1. As input in Java is quite difficult to handle, Holt Software Associates has written a package with simplified input methods and bundled it with the Ready IDE. To make use of these, hsa.Stdin or hsa.* has to be imported (at the top, before public class). 2. Having made hsa.Stdin available, you can use Stdin.readInt () to read integer input. Similarly, the following can be used: String name = Stdin.readLine (); float myNumber = Stdin.readFloat (); double num = Stdin.readDouble (); char initial = Stdin.readChar ();

Program Block:

Output Statements: Input Statements:

Notes on Java 10.2005.3: Syntax of Commands:

Page 2 of 3

Reserved words:

Ready shows a lot of words in bold automatically, i.e. int, import, public, void, while, if, for, do, char, double, etc. These are reserved words in the Java language, i.e. they have a very specific meaning in Java and you may not use them as class or variable names, or for any other purpose. It is good programming practice to comment on aspects of the program, to mark sections, etc. If these comments are marked as such, the compiler will ignore them. In Java the marker is the double forward slash //. If this is placed at the beginning of the line, the whole line is treated as a comment (or if placed later in a line, the rest of the line is a comment). For longer passages a start signal /* starts a comment. This then has to be followed by the stop signal */. Another use of the // comment signal is to temporarily disable a line. You then dont have to erase (and later rewrite) it. if..else == is equal to != is not equal to > is greater than < is smaller than >= is greater or equal than<= is smaller or equal than e.g. // Evaluating a number if (x < 0) System.out.println ("This is a negative number."); else if (x == 0) System.out.println ("You have entered zero(0)."); else System.out.println ("This is a positive number.");

Comments:

Decision Structures:

Repetition Structures:

for-loop e.g. for (int i = 1 ; i <= x ; i++) { for (int j = 1 ; j <= x ; j++) System.out.print ("* "); System.out.println (); } do loop System.out.print (Please enter a positive number: ); int num = 0; e.g. do { num = Stdin.readInt (); } while (num < 0);

NB: See the attached program. Good luck with your own programming efforts!

_______________________________________ mb 23 April, 2013

Notes on Java 10.2005.3: Syntax of Commands:

Page 3 of 3

Remember: The program layout should make for easy reading, look neat and uniform. Of course, the Indent button (or F2) of the Ready IDE will be of great help // The "DemonstratingBasics" class. // Created by M Brock, 3 May 2001, updated 9 February 2004 import hsa.Stdin; public class DemonstratingBasics { /* This is a sample program to illustrate basic input/output routines, decision structures, good programming style, and simple loop structures. */ public static void main (String [] args) { //Getting input System.out.print ("Please enter your name: "); String name = Stdin.readLine (); System.out.print ("Hello, " + name + ", please enter a number: "); int num = 0; do { num = Stdin.readInt (); // Evaluating the number if (num < 0) { System.out.println ("This is a negative number."); System.out.println ("Please try again."); System.out.println (); } else if (num == 0) System.out.println ("You have entered zero(0)."); else { System.out.println ("This is a positive number."); System.out.println ("Press ENTER to continue."); char nothing = Stdin.readChar (); } //End of if..else statement } // end of do loop while (num < 0); System.out.println (); // Empty line, to separate square pattern from rest // Square pattern for (int i = 1 ; i <= num ; i++) { for (int j = 1 ; j <= num ; j++) System.out.print ("* "); System.out.println (); } // end of for loop // ending off System.out.println ("\nEnd of program."); } // main method } // DemonstratingBasics class

You might also like