Java
INPUT with JOptionPane import javax.swing.*; public class TriangleArea { public static void main(String[] args) { String baseString,heightString; double base; // base of triangle. double height; // height of the triangle double area; // area of triangle baseString = JOptionPane.showInputDialog("Enter base:"); heightString = JOptionPane.showInputDialog("Enter height:"); base = Double.parseDouble( baseString ); height = Double.parseDouble( heightString ); area = (.5)*base*height; // Compute the area. JOptionPane.showMessageDialog(null, "The base was "+ base + "The height was "+ height + "The area of the triangle is " + area); } // end of main() } // end of class TriangleArea
if with multiple actions (braces) if ( xCoord > 800 ) { xAddition = -5; forwards = true; //Boolean variable }
if with else if ( hungry) //hunger is Boolean variable – must be true System.out.println("Buy the cookie!" ); else System.out.println("Buy the apple!" );
if if ( (hunger + look + smell ) > 15 ) System.out.println("Buy the cookie!" ); else if (hunger + look + smell)>20 System.out.println("Buy the cake!" ); else System.out.println("Buy the apple!" );
for for (countDown=3;countDown>=0;countDown--) { System.out.println(countDown); System.out.println("and counting."); }
Java Primitives/Variables
Integers
int= -2,147,483,648 to 2,147,483,647
Real Numbers:
while while ( dollars < 1000000.00 ) { dollars = dollars*1.05; year++; }
Double = +1038 (15 significant figures)
Single Characters:
Char letter=’A’;
Boolean values (true or false)
Boolean isPrime= true;
do looping do { System.out.println(x); } while (x>10);
String Values:
String name = “Erin”;
Arrays (see Java Array Class): int theArray[] = new int[10]; 1 dimensional int theArray[][]=new int[6][6]; 2 dimensional
HTML for APPPLET
Standout Output Methods
System.out.print(“Enter the score”); (same line) System.out.println(“Try again”); (new line)