Unit 2 (1/23)
Java Data Types
Whole numbers
byte - 1 byte
short - 2 bytes
int - 4 bytes
long - 8 bytes
Real numbers
float - 4 bytes
double - 8 bytes
Non-numeric
Unicode
char - 2 bytes
boolean - 1 byte
Arithmetic operators
Highest precedence
- (unary negation)
* / %
+ -
Associativity
- (unary negation) Right to left
* / % Left to right
+ - Left to right
Integer division
The result of the division operator depends on the type of its operands.
If one or both operands have a floating-point type, the result will be a floating-point number, allowing for decimal values in the outcome.
11 / 4 = 2
% Modulus (remainder from integer division)
Conversion
Highest rank to lowest
double
float
long
int
short
byte
Math Class
PI -
POW - returns the value of the first argument raised to the square root
area = Math.PI * Math.pow(radius, 2.0)
/**
* Hayley Pappas
*
* Sphere.java
*
* Given the radius (123.45) of a sphere, calculate & display its volume & surface area.
*
* Input: Sphere's radius
*
* Processing: 1. Assign radius = 123.45;
* 2. Calculate volume & surface area
* 3. Display results
*
* Output: Sphere's volume & surface area
*/
public class Sphere {
public static void main(String[] args)
{
// Variables
double radius = 123.45, volume, surfaceArea;
// Intro
System.out.println("\nSphere Geometry Calculator ...");
// Calculate volume & surface area
volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3.0);
surfaceArea = 4.0 * Math.PI * Math.pow(radius, 2);
// Display results
System.out.println("\nGiven a sphere with a radius of 123.45");
System.out.println("\tVolume:\t\t" + volume);
System.out.println("\tSurface Area:\t" + surfaceArea);
}
}