ITM 200 Lecture Notes - Dialog Box, Dollar Sign, Double-Precision Floating-Point Format

36 views8 pages

For unlimited access to Class Notes, a Class+ subscription is required.

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.swingpackage(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
line
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in
Output 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()
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in
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;
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
1 Booster Class
Class+
$8 USD/m
Billed $96 USD annually
Homework Help
Class Notes
Textbook Notes
30 Verified Answers
Study Guides
1 Booster Class