Using Data: Instructor: Mr. Neil A. Basabe, MIT
Using Data: Instructor: Mr. Neil A. Basabe, MIT
Camel Case
- the first word is in small letters, next word or abbreviation
begins with a capital letter
- use for methods, variables, objects
Upper Case
- use for constants
Lower Case
- use for
Declaring and Using Constants
and Variables
• Constant
Data type
– Cannot be changed while program A type of data that can be
is running stored
• Literal constant How much memory an item
occupies
– Value taken literally at each use
What types of operations
• Numeric constant can be performed on data
– As opposed to a literal constant
Primitive type
• Unnamed constant
A simple data type
– No identifier is associated with it
• Variable Reference types
More complex data types
– A named memory location
– Used to store a value
– Can hold only one value at a time
– Its value can change
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Declaring Named Constants
A named constant:
• Should not change during program execution
• Has a data type, name, and value
• Has a data type preceded by the keyword final
• Can be assigned a value only once
• Conventionally is given identifiers using all uppercase
letters
ClassName.methodName();
objectName.methodName();
class Foo{
int i=5;
static void p(){
int j=1;//wrong
}
}
The Scope of Variables
double radius = 1;
}
Note: The data properties and methods are the members of
the class. There is no order among them. Therefore, they
can be declared in any order in a class.
The scope of a local variable starts from its declaration and
continues to the end of the block that contains the variable. A local
variable must be declared before it can be used.
You can declare a variable only once as a class member (instance
or class variable), but you can declare the same variable in a method
multiple times in different non-nesting blocks.
class Foo{
int x=0;int y=0;//instance variable
Foo(){}
void p(){
int x=1;//local variable
System.out.println(“x = ”+x);
System.out.println(“y = ”+y);
}
}
Using the Scanner Class to Accept Keyboard Input
• System.in object
• Standard input device
• Normally the keyboard
• Access using the Scanner class
• Scanner object
• Breaks input into units called tokens
Pitfall: Using nextLine() Following One of the Other Scanner Input Methods
There is a problem when using one numeric Scanner class retrieval method or
next()method before using the nextLine()method
Keyboard buffer
Location in memory that stores all keystrokes, including Enter
To avoid issues, add an extra nextLine()method call to retrieve the abandoned
Enter key character after numeric or next() inputs
Using the JOptionPane Class to Accept GUI Input
Dialog boxes used to accept user input:
Input dialog box
Confirm dialog box
Type-wrapper classes
Each primitive type has a corresponding class contained in the java.lang
package
Include methods to process primitive type values
Integer.parseInt()
Double.parseDouble()
Using Confirm Dialog Boxes
Confirm dialog box
Displays the options Yes, No, and Cancel
showConfirmDialog() method in JOptionPane class
Four overloaded versions are available
Returns integer containing either:
JOptionPane.YES_OPTION
JOptionPane.NO_OPTION
JOptionPane.CANCEL_OPTION
You can create a confirm dialog box with five arguments:
Parent component
Prompt message
Title
Integer that indicates which option button to show
Integer that describes the kind of dialog box