Chapter 2 (Using Data) - Discover Dalton State

Download Report

Transcript Chapter 2 (Using Data) - Discover Dalton State

Using Data Within a Program
Chapter 2





Classes
Methods
Statements
Modifiers
Identifiers

Classes



Class {
main {
Methods


The class is the essential Java construct.
A class is a template or blueprint for
objects.
Main method is a collection of
statements that performs a sequence of
operations
Statements


Represents an action or a sequence of
actions
Every statement in Java ends with a
semicolon (;)
Statements;
}
}


Java uses certain reserved words called
modifiers that specify the properties of the data,
methods, and classes and how they can be used.
Common modifiers:

public and static
public static void main {

Other modifiers:

private, final, abstract, and protected.
4
Three types of comments in Java
1. Line comment: A line comment is preceded by two
slashes (//) in a line
2. Paragraph comment: A paragraph comment is
enclosed between /* and */ in one or multiple lines
3. javadoc comment: javadoc comments begin with /**
and end with */. They are used for documenting
classes, data, and methods. They can be extracted into
an HTML file using JDK's javadoc command.



Used to represent a piece of data
Actually represents memory location where
data is stored
Identifier used to name variable
•
•
•

Should begin with lower case letter
Subsequent words use upper case
Should be meaningful
Example:
salesTaxRate

Constant


Cannot be changed while program is running
Variable
Named memory location
 Use to store value
 Can hold only one value at a time
 Value can change


Data type
Type of data that
can be stored
 How much
memory item
occupies
 What types of
operations can
be performed on
data


Type int
Store integers, or whole numbers
 Value from –2,147,483,648 to +2,147,483,647


Variations of the integer type
byte
 short
 long


Floating-point number


Floating-point data types



Contains decimal positions
float
double
Significant digits

Refers to mathematical accuracy

Boolean logic


Based on true-or-false comparisons
Boolean variable
Can hold only one of two values
 true or false

boolean isItPayday = false;

Relational operator

Compares two items

char data type


Holds any single character
Place constant character values within single
quotation marks
char myMiddleInitial = 'M';

String



Built-in class
Store and manipulate character strings
String constants written between double
quotation marks
String myName = “Tim”;

Arithmetic with variables or constants of
same type


Arithmetic operations with operands of
unlike types


Result of arithmetic retains same type
Java chooses unifying type for result
Unifying type

Type to which all operands in expression are
converted for compatibility

Every variable in a Java program must be declared
before it is used




A variable declaration tells the compiler what kind of
data (type) will be stored in the variable
The type of the variable is followed by one or more
variable names separated by commas, and terminated
with a semicolon
Variables are typically declared just before they are
used or at the start of a block
Basic types in Java are called primitive types
int numberofdays;
double totalWeight;



Variable given no particular value when
declared
Assignment gives it a value
Syntax
variable = expression ;
•
•
Expression evaluated
Value stored in memory location specified by
variable

Declare multiple variables in separate
statements on different lines
int myAge = 25;
int yourAge = 19;
16

To find the area of a circle, we use the
formula:

Area = (Pi) * (radius)2
radius
A=πr2
public class ComputeArea {
public static void main(String[ ] args) {
double radius;
double area;
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
radius
area
allocate memory
for radius and area
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
no value
no value
assign value to radius
public class ComputeArea {
public static void main(String[ ] args) {
double radius;
double area;
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
radius
area
compute area and assign
it to variable area
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
20
1256.636
Print message
to console


System.out is an object that is part of the
Java language
println is a method invoked by the
System.out object that can be used for
console output



The data to be output is given as an argument in
parentheses
A plus sign is used to connect more than one item
Every invocation of println ends a line of output
System.out.println("The answer is " + 42);


Another method that can be invoked by the
System.out object is print
The print method is like println, except
that it does not end a line
With println, the next output goes on a new
line
 With print, the next output goes on the same
line


Libraries in Java are called packages



A package is a collection of classes that is stored in a
manner that makes it easily accessible to any program
In order to use a class that belongs to a package, the
class must be brought into a program using an import
statement
Use dialog box to display:
import javax.swing.JOptionPane;
JOptionPane.showMessageDialog()
import javax.swing.*;
public class YourNameDialog{
public static void main(String[ ] args){
String result;
result = JOptionPane.showInputDialog
(null, "What is your name? ");
JOptionPane.showMessageDialog
(null, "Hello " + result);
}
}

Input data through the keyboard by using the Scanner
class in the utility library
import java.util.Scanner;

Create a Scanner object
Scanner input = new Scanner(System.in);

To obtain the value use the methods:








next()
for string data type
nextByte()
for byte
nextShort()
for short integer
nextInt() for integer
nextLong()
for long integer
nextFloat()
for floating point
nextDouble()
for double floating point
nextBoolean()
for boolean value
import java.util.Scanner;
public class ComputeArea {
public static void main(String[ ] args) {
double radius;
double area;
// Assign a radius
System.out.print("Enter your radius: ");
Scanner input = new Scanner(System.in);
radius = input.nextDouble();
// Compute area
area = radius * radius * 3.14159;
}
}
// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
import javax.swing.*;
public class ComputeArea {
public static void main(String[ ] args) {
double radius;
double area;
String input;
// Assign a radius
input = JOptionPane.showInputDialog
(null, "Enter the radius ");
radius = Double.parseDouble(input);
// Compute area
area = radius * radius * 3.14159;
}
}
// Display results
JOptionPane.showMessageDialog
(null, "The area for the circle of radius " +
radius + " is " + area);
Dialog Box
only uses
String data
Converts the
string to a
double format

A program should always prompt the user
when he or she needs to input some data:
System.out.println(
"Enter a whole number or decimal number: ");

Always echo all input that a program
receives from the keyboard

In this way a user can check that he or she has
entered the input correctly