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

15
Constants public 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; }

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

Page 1: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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

Page 2: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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

Page 3: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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.

Page 4: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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

Page 5: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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)

Page 6: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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

Page 7: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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

Page 8: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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

Page 9: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

Arithmetic Operators• Get the remainder with % (pronounced

"modulo")

• For example: int val1 = 7; val1 = val1 / 4; //assigns 1

val1 = val1 % 4; //assigns 3

Page 10: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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.

Page 11: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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

Page 12: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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

Page 13: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

An Input Dialog

Page 14: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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;

Page 15: Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

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