Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
public Car (String col, String mk, String mod, double galofgas)
..
}
public void moveForward(double miles) {
gas = gas - miles/18.0;
}
public void moveBackward(double miles) {
gas = gas - miles/18.0;
Static Methods
If a method is declared as static, it may be called without using an object of the
class.
For example: we called all Car class methods in the form
CarObject.methodName
because none of these methods were static
If a method is declared as static, it may be called without an object
ClassName.methodName
Example:
Math.sqrt(4)
The sqrt method is a static method defined in the Math class.
Static Variables
If an instance variable is declared as static, it is shared by ALL
OBJECTS of the class.
(eg.
Each object created from that class DOES NOT have its own storage location
allocated for the variable all the objects use the one variable location.)
Simple example
public class Player{
private static int topScore = 0;
private int myScore = 0;
private static final int winScore = 21;
public Player() { .}
public void gotPoint () {
myScore = myScore + 1;
if (myScore > topScore)
topScore = myScore;
}
public static boolean winner( ) {
return (topScore >= winScore) ;
}
Arithmetic Operators
+, - , * are used for addition, subtraction and
multiplication
/ is the division operator
If both arguments are integers, the result is an
integer. The remainder is discarded
For example:
int val1 = 7;
double value = 7.0;
value = value / 4; //assigns 1.75
val1 = val1 / 4;
//assignes 1
value = val1 / 4; //assigns 1.0
Arithmetic Operators
Get the remainder with % (pronounced
"modulo")
For example:
int val1 = 7;
val1 = val1 / 4;
//assigns 1
Mathematical Functions
The Math class is a class which contains static methods which
perform mathmatical functions.
Math.sqrt(x)
square root
Math.pow(x, y)
power xy
Math.exp(x)
ex
Math.log(x)
natural log
Math.sin(x), Math.cos(x),
Math.tan(x)
Math.round(x)
closest integer to x
Type Conversion
Java will only allow an assignment if NO VALUE
will be lost and types are compatible
double total = "a lot"; //not compatible
int ww = 5.67;
//value would be lost
Use cast to force conversion from one type to
another (if it is possible):
int ww = (int) 5.67;
Reading Input
The simplest (and prettiest) way to read input into a Java
program is to use a method provided by the JOptionPane
class (java.lang package). The call:
JOptionPane.showInputDialog(prompt)
causes a dialog box to be displayed with the prompt and a
textbox for the user to type his/her response.
Whatever the user types into the box is returned to the
program (in String form).
Note: showInputDialog is a static method of the JOptionPane class
An Input Dialog
int val;
String in;
in = JOptionPane.showInputDialog(Enter a positive number);
Suppose we want to add the user input to the value 45.
we cannot say:
val = in + 45;
because in is NOT of a numeric type (such as int or double).
We must convert our string to type int. The Integer class
provides a static method called parseInt which accepts a String,
and returns its int equivalent.
int size = Integer.parseInt(in);
val = size + 45;
Add
System.exit(0)
to the end of any main method of any program that uses
JOptionPane method