Constants public class Car { //This class produces cars with 18 MPG private String color; private...

Preview:

Citation preview

Constantspublic class Car {

//This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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; }}

public class Car {

private final double MPG = 18.0; //value cannot change //adds readablility to code private String make; private String model; private double gas;

public Car (String col, String mk, String mod, double galofgas) { …….. }

public void moveForward(double miles) { gas = gas - miles/MPG; }

public void moveBackward(double miles) { gas = gas - miles/MPG; }}

Static MethodsIf 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.)

When do you use a static instance variable?? * when information needs to be shared between objects and a method calls does not make design sense

*class constants should be static. A constant memory location cannot be changed, so it makes sense to just allocate one

Class Constants should be static

 In a method: final typeName variableName= expression ;

In a class: accessSpecifier static final typeName variableName = expression;

Why??

Because, each object of class does not need it’s own copy of a constant

(each Car object does not need it’s own memory location to store MPG)

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) ; }

 public class Game{ public static void main (String [] args){ Player player1, player2, player3; player1 = new Player (); player2 = new Player (); player2 = new Player ();

while ( Player.winner() == false) {

//code to play game } }}

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

val1 = val1 % 4; //assigns 3

Mathematical 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)

sine, cosine, tangent (x in radian)

Math.round(x) closest integer to x

The Math class is a class which contains static methods whichperform mathmatical functions.

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; //discards fractional part

• int ww = Math.round(ww); //rounds to 6.0, then //truncates to 6

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;

When using Integer.parseInt (or Double.parseDouble):

Conversion throws an exception if user doesn't supply a number

(ie. Program will crash if user provides non numerical input)

JOptionPane also provides a method for output JOptionPane.showMessageDialog(null,”output string”);

AddSystem.exit(0)to the end of any main method of any program that uses JOptionPane method

Recommended