Class Notes (837,435)
Canada (510,273)
ITM 200 (22)
Vikirmin (5)
Lecture

Chapter 3 200.docx

8 Pages
65 Views
Unlock Document

Department
Information Technology Management
Course
ITM 200
Professor
Vikirmin
Semester
Winter

Description
Chapter 3- Outputs and Caculations Variables are containers which hold information:  // Compute the first area  radius = 1.0;  area = radius * radius * 3.14159;  System.out.println("The area is “ + area + " for radius "+radius);  // Compute the second area  radius = 2.0;  area = radius * radius * 3.14159;  System.out.println("The area is “ + area + " for radius "+radius); Constants: final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3; Comments: are pieces of text that are ignored by the compiler. They are usually included by the programmer as provider of useful notes and prevent statements from being executing.  Comments begin with // which tells the compiler that all text from this point until the end of the line is to be ignored when translating the java program into byte code  Should always put the comment at the start of the program to state the purpose, who wrote the program and when, any list of changes, etc  Another form of comments is one that starts with /* and ends with */ The line import javax.swing.JOptionPane; is an import statement. The java has a library of predefined programs (API) that can be used within out programs. These program chunks are called classes and related classes are grouped within the API into so called packages. Javax.swingpackage(a collection of reusable pre written classes) JOptionPane is a class within that package (dialog box) Methods: implement each algorithm in your program within the class. Classes have many methods which are basically subprograms. Every java program must contain a method called main(). Example: Public static void main(String[] args) Void: is a keyword which indicates that the main method does not return a value  Every method name is followed by open and closed parentheses. Between the parentheses there may be parameters. Example above, the parameter is args  Args= allows us to pass arguments (data) to the main method of the program on the command lineOutput Statements Statements: program instructions Example: 2 statements 1) JOptionPane.showMessageDialog(null, “Hello”!”); 2) System.exit(0);  found in class classed System, which is part of a package of classes called java.lang  Java statements always end with a semicolon ;  2 parameters in the example above: Null: a reference value String of text that you want to enter in To make dialog box pop up a few times than: //Print greeting on the screen in 3 dialog boxes Import javax.swing.JOptionPane; Public class Hello2 { Public static void main(String[] args] { JOptionPane.showMessageDialog(null, “Hello!”); JOptionPane.showMessageDialog(null, “How nice to see you!”); JOptionPane.showMessageDialog(null, “How are you?”); System.exit(0); } } To make sentences in one dialog box:  Have to add “\n” //Print greeting on the screen on three lines //in a single dialog box Import javax.swing.JoptionPane; Public class Hello3 { Public static void main(Strings[] args) { JOptionPane.showMessageDialog(null, “Hello!\nHow are you?\nIt’s nice to see you!”); System.exit(0); } } Command Line Output  Can do output in Java in the command line window using methods  System.out.print() and System.out.println()  System.out is called the standard output object  The method is print() and println()  displays a line of text on the screen  Print()= will leave the cursor on the same line after the printing text  Println()= will place the output cursor at the start of the next line Subprograms -The name of the main method (algorithm) is always main(); but you can implement other algorithms as named methods and invoke them by using names as instructions in main(). Example, 2 methods spling() and sponge()  public static void splong() ..refer to page 46 Variables Int = integer which are whole numbers no decimals or fractions Char= letter or characters, character value is indicated by single quotes ex. Letter= ‘A’ Double= distance; allows you to place double precision floating point numbers (decimal points) up to 15 Float= variables which store floating point numbers in 7 digits  Declares variable: age, distance, equipment  Type: int, char, double, float Example: Int age=21; Char letter=’Z’; Double distance=12.75; Declaring Variables: int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable; Identifiers: Identifiers are names given to variables, constants, classes and methods  An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($)  An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit  An identifier cannot be a reserved word  An identifier cannot be true, false, or null  An identifier can be of any length Declarations of more than one variable of the same type can be combined as follows: -Int age, weight, height; Each variable name is separated by a comma, with a final semicolon.  Always give variables meaningful name example int temperature;  Name of a variable is known as an identifier  It must start with a letter, underscore, or a $ sign  This may be followed by letters, digits, underscores, or $  Note that uppercase and lowercase characters are treated as different characters Note: The left hand side of the = sign is always the name of a variable. The right hand side of the = sign is an expression of the same type as the variable on the left The order of events when the computer executes an assignment statement is 1) Evaluate the expression of the RHS of the = sign 2) Store the resultant value of expression in the variable on the LHS of the = sign Assignment Statements: x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a; Declaring and Initializing in One Step • int x = 1; • double d = 1.4; Example: Int timePassed; timePassed = 5; age=16+8; //16+8=24 stored in age Age=16+timePassed; //16+5= 21 stored in age; previous value in age is overwritten Age= age+6; //21+6=27 stored in age Age=age+timePassed; //27+5=32 stored in age Assignments work:  Each time a new value is stored in a variable the old one is overwritten (a variable can only hold one value at a time) Note: If you do not assign a value to a variable declared in main() (between the { and } associated with main(); also known as the body of the method) before it is used in output then the variable will not compile. If the variable is declared in the class (between the { and } associated with the class) but not within a method body, then Java will automatically assign the variable a default value of zero.Note:  The methods print() a
More Less

Related notes for ITM 200

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