Java Review

//***************************************
// File: Camus.java
// Author: Mary Eberlein
//
// A simple first Java program
//***************************************

public class Camus
{
   // print a Camus quote

   public static void main(String[] args)
   {
      System.out.println("Camus said: ");
      System.out.println("Some people talk in their sleep. \nLecturers talk               while other people sleep.");
   }
}


Output

Camus said:
Some people talk in their sleep.
Lecturers talk while other people sleep.




What's In Our Program?









Nuts and Bolts for Java Programs



Printing made nicer:
Escape Sequences - Two character sequences that represent other characters


Exercise: Re-do the Camus class so that the quote is contained in double quotes.





Identifiers and Keywords






Data Types

Java has 8 primitive types - 6 are number types (4 integer and 2 floating point types), the character type char, and the boolean type.

Java integer types

type                                storage                                range
int                                    4 bytes                                approx -2 billion to 2 billion
short                                2 bytes                                 -32,768 to 32, 768
long                                 8 bytes                               approx -9.2x10^18 to 9.2x10^18
byte                                 1 byte                                 -128 to 127

Usually int is most practical.
When to use long?
When to use short or byte?



Floating point types

Numbers with fractional parts

type                              storage                                range
float                                4 bytes                              approx -3.4x10^38 to 3.4x10^38
                                                                                    6-7 significant decimal digits
double                            8 bytes                              approx -1.8x10^308 to 1.8 x 10^308
                                                                                     15 significant decimal digits


Usually use double.

Suffix F --> type float
Ex: 23.5F

Ex: 23.5 is type double




The Character Type - char

Note: "a" is a string of length 1, NOT a char.

We can compare characters using < and >. The ordering on digit and letter characters is:
1-9  A-Z  a-z

Ex: '1' < '2' < '3'...
       'A' < 'a' < 'z'


Pre-defined Character Methods for us to use:
char Character.toLowerCase(char ch) - returns the lowercase version of the character that was passed in
char Character.toUpperCase(char ch) - returns the uppercase version of the character that was passed in

Ex: System.out.println(Character.toLowerCase('A'));
        Output to screen: a





The boolean type

A boolean variable has only 2 valid values: true and false






Variables





Declaring a Variable

The general form for a variable declaration is:

dataType variableName;

Examples:
int count;
double bankAccountBalance;
char myFavLetter;





Assignment - Assigning a value to a variable

General form:
variableName = expression;

Or you can declare a variable and assign it a value at the same time:
dataType variableName = expression;

Examples:
int count = 0;
double area = 15.74;
int length = 5;
int width = 6;
int perimeterRectangle = length + length + width + width;



Operators

+ - * /
% <-- integer remainder

Examples:
15 + 2 is 17
3 - 1 is 2
15/4 is 3  <----- If you divide one integer by another, the result is an integer (the remainder is lost).
11.0/4 is 2.75
15%2 is 1
25%5 is 0
21% 6 is 3

Example: Write a program that assigns values to 2 variables that represent the length and width of a rectangle, and then prints the rectangle's area.

Example: Write a program that assigns integer values to 3 variables that represent test scores, and then computes and prints the average score to the screen.




Conversions between Numeric Types

For binary operations on numeric values of different types:

If any operand is a double, other will be converted to double.
Otherwise if any operand is float, other will be converted to float.
Otherwise if any operand is long, other will be converted to long.
Otherwise if any operand is int, other will be converted to int.
Otherwise if any operand is short, other will be converted to short.

These conversions are made automatically since no information is lost.
Ex: int n = 5;
       double x = n;  // No information is lost when the int is stored in a variable of type double.

To convert a double to an int, you must use an explicit cast, since information may be lost.


Ex: double x = 5.8993;
       int n = (int) x;   // n has the value 5 - the fraction was discarded.



To round a float or double to the nearest int, use the Math.round method:

Ex: double x = 5.8993;
       int n = (int) Math.round(x);   // the value of n is 6. Note that the return type of Math.round() is long.




Constants

General form:
final dataType constantName = expression;


Examples:
final int MAX_OCCUPANCY_PAI314 = 62;  // the max occupancy of classroom PAI 3.14
final double UNLEADED_PRICE = 2.09;


Example:

public class InToCm
{
   // This program converts measurements in inches to centimeters

   public static void main(String[] args)
   {
      final double CM_PER_INCH = 2.54;

      // Convert 1.55 inches to centimeters
      System.out.println("Length of 1.55 in in centimeters: " + 1.55 * CM_PER_INCH);
   }
}


Useful Methods and Constants from java.lang.Math

Constants

static double E                (approx. 2.71...)
static double PI                (approx 3.14159...)

Methods

abs(x)  - returns the absolute value of a numeric type

exp(x)  - returns e^x. argument and return type: double

max(x, y)

min(x, y)

pow(x, a) - returns x^a. argument and return type: double






Exercise: Write a program that uses a variable to store the number of gallons of gas purchased by a driver. Use a constant to store the price of unleaded gasoline. Print the amount the driver spent on gas.

Exercise: Write a program that calculates the area of a circle. Use the Math.PI constant in java.lang.Math.