Chapter 3

5 Pages
64 Views

Department
Information Technology Management
Course Code
ITM 200
Professor
Vikraman Baskaran

This preview shows pages 1 and half of page 2. Sign up to view the full 5 pages of the document.
Description
Khushi Sharma Tuesday May 8 , 2012 Chapter 3 Output and Calculations Structure of a Java Program: Comments: -comments are pieces of text that are ignored by the complier; they are usually included by the author of a program for 2 reasons:  Provide useful notes of explanations for a human reading the program  Prevent statements from being executed – maybe during lazy debugging or program development -comments begin with “//”, which tell the complier that all text from this point until the end of the line is to be ignored when translating the java program into byte code -as far as the complier is concerned, comments have no useful function -comments are included in order to explain the program to a human reader and well chosen comments are an important part of every program -you should always place a comment at the start of the program to state;  The purpose of the program  An outline of how it carries out its task  Who wrote the program and when  List of any changed to the program and when they were made  Anything else which might help a reader to understand the program -if you have to many comments they will obscure the program itself, and however too few comments will leave the reader guessing as to how the program works Importing (reusing) Code from other files -the line:  Import javax.swing.JOptionPane; -is an important statement, in order to use any of these API classes within your program you must instruct the Java complier to include them; this is done with the “import” statement -javax.swing is a package (collection of reusable Java Codes) which contain classes for providing graphical user interfaces for programs, and JOptionPane is a class within that package which provides facilities which enable one to display a message dialog box Classes -out simple program consists of a single class, defined as;  public class Hello1 { } -every Java program will contain at least one class defined by the programmer, the words of public class are Java keywords which must be included followed by the name you give to the class like in the example above “Hello1” -class name starts with a capital letter, also note that the name of the file containing your program must be the same as that of the class  Example: Hello1 class must be in a file called Hello1.java Khushi Sharma Tuesday May 8 , 2012 -file name must always be the class name spelt with the same mixture of upper and lower case letters followed by .java Coding Style Tip: -blank lines and extra spaces are generally ignored by the Java complier, you as the programmer have to read through the program to it makes sense to lay out the program statements in a neat and tidy fashion -remember opening and closing brackets should be aligned one above the other -the lines of text between them should be aligned with each other, and indented a few spaces from the position of the enclosing {and}  This is very important for producing programs which are easy to understand for the human reader Method: -methods are which implement each algorithm in your program is placed within the class definition -classes can have many methods (subprograms) -every java program must contain a main method (), this represents the main algorithm of the program -execution of the program will begin with the first statement in main () -every method name is followed by open and closed parentheses ‘(‘and’)’ between these parentheses there may or may not be parameters  Main method has parameter called ‘args’  This allows us to pass arguments (data) to the main method of the program on the command line Output Statement: -most basic sort of program implements a single sequence of instructions in the main () method each of which tells the computer what to do -remember the program instructions are also called statements -Example: JOptionPane.showMessageDislog(null, “Hello!”) System.exit (0); -the second statement: System.exit(0);  Calls a method exit (), which is found in a class called System, which is part of a package of classes called java.lang. -note that the java statement must always end with a semicolon, this tells the complier when the end of a statement has been reached -the showMessageDialog() method has 2 parameters  First parameter is null which is a reference value  Second parameter is the string of text that you wish to be displayed in the dialog in this case “Hello!” -to make the output move to a new line, you must include in the string the escape sequence ‘\n’ which represents the newline character -other escape characters include; ‘\t’ to print a tab and ‘\\’ to print backslash character Command Line Output: -you can also output in Java in the command line window using:  System.out.print()  System.out.println() Khushi Sharma Tuesday May 8 , 2012 -System.out is called the standard output object, it has methods print () or println () each of which displays a line of text on the screen -difference is that printlin () will place the output cursor at the start of the next line where as print (
More Less
Unlock Document

Only pages 1 and half of page 2 are available for preview. Some parts have been intentionally blurred.

Unlock Document
You're Reading a Preview

Unlock to view full version

Unlock Document

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit