HP JAVA Cheat Sheet
INPUT with JOptionPane if with multiple actions (braces)
import [Link].*; if ( xCoord > 800 )
{
public class TriangleArea xAddition = -5;
{ forwards = true; //Boolean variable
public static void main(String[] args) }
{
String baseString,heightString;
double base; // base of triangle.
double height; // height of the triangle
if with else
if ( hungry) //hunger is Boolean variable – must be true
double area; // area of triangle
[Link]("Buy the cookie!" );
else
baseString = [Link]("Enter base:");
[Link]("Buy the apple!" );
heightString = [Link]("Enter height:");
base = [Link]( baseString ); if
height = [Link]( heightString ); if ( (hunger + look + smell ) > 15 )
area = (.5)*base*height; // Compute the area. [Link]("Buy the cookie!" );
else if (hunger + look + smell)>20
[Link](null, "The base was "+ base [Link]("Buy the cake!" );
+ "The height was "+ height else
+ "The area of the triangle is " + area); [Link]("Buy the apple!" );
} // end of main()
} // end of class TriangleArea
for
for (countDown=3;countDown>=0;countDown--)
{
Java Primitives/Variables [Link](countDown);
[Link]("and counting.");
Integers
}
int= -2,147,483,648 to 2,147,483,647
Real Numbers: while
Double = +1038 (15 significant figures) while ( dollars < 1000000.00 )
{
dollars = dollars*1.05;
Single Characters: year++;
Char letter=’A’; }
Boolean values (true or false) do looping
Boolean isPrime= true;
do
{
String Values: [Link](x);
String name = “Erin”; }
while (x>10);
Arrays (see Java Array Class):
int theArray[] = new int[10]; 1 dimensional HTML for APPPLET
int theArray[][]=new int[6][6]; 2 dimensional <html>
<body>
<applet code="HappyFace" width="800" height="600">
Standout Output Methods </applet>
[Link](“Enter the score”); (same line) </body>
[Link](“Try again”); (new line) </html>
[Link](“The answer is “+ 42);
APPLET
Branching Structures import [Link].*;
“and” is represented in Java as && import [Link].*;
“or” is represented in Java as | |
“not” is represented in Java as ! public class HappyFace extends JApplet
{
public void paint(Graphics canvas)
if {
if ( xCoord > 800 ) [Link](100, 50, 200, 200);
addition = -5; [Link](155, 100, 10, 20);
[Link](230, 100, 10, 20);
[Link](150, 160, 100, 50, 180, 180);
}
}
HP JAVA Cheat Sheet
Useful Math Class Methods Result
[Link](-7)
[Link](6.2)
7
7
Increment and Decrement Operators
count++; //increase the value of count by 1
[Link](6.8) 6
count--; // decrease the value of count by 1
[Link](5,6) 6
count+2; // increase the value of count by 2
[Link](5,6) 5
count-2 // decrease the value of count by 2
[Link](2.0,3.0) 8.0
[Link]() number between 0 less than 1
[Link](6.5) 7
[Link](4.0) 2.0 Switch and Case Statements
int month = 8;
switch (month) {
Assignment Statements case 1: [Link]("January"); break;
double pi; case 2: [Link]("February"); break;
int x = 1; case 3: [Link]("March"); break;
String empty = ""; case 4: [Link]("April"); break;
double pi = 3.14159; case 5: [Link]("May"); break;
case 6: [Link]("June"); break;
case 7: [Link]("July"); break;
x == y // x equals y case 8: [Link]("August"); break;
x != y // x is not equal to y case 9: [Link]("September"); break;
x>y // x is greater than y case 10: [Link]("October"); break;
x<y // x is less than y case 11: [Link]("November"); break;
x >= y // x is greater than or equal to y case 12: [Link]("December"); break;
x <= y // x is less than or equal to default: [Link]("Not a month!");break;
}
String Methods
(assume stringVariable is name) Java Escape Characters
\n new line – position cursor to the beginning of next line
length [Link]();
\t horizontal tab
equals [Link](otherStringVariable);
\r position cursor to beginning of current line
[Link](otherStringVariable);
\\ backslash – prints a backslash
lowercase [Link]();
\” prints a double quote
uppercase [Link]();
trim [Link]()
[Link]("Hello\nnext\nLine");
charAt(Position) [Link](Position);
Simple OOP example
class BoxTester
{
public static void main ( String[] args )
{
Box cube = new Box( 2.0, 2.0, 2.0 ) ;
[Link]( "Area: " + [Link]() );
[Link](“Volume:” +[Link]());
}
}
class Box
{
private double width, height, length;
//constructors
Box( double W, double H, double L)
{ height = H ;
width = W ; Input with EasyReader
length = L ;
} // console is an EasyReader Object you create below
// methods EasyReader console = new EasyReader();
double volume()
{ //EasyReader Method Calls are below
return length*width*height ; change = [Link]();
} discountRate = [Link]();