0% found this document useful (0 votes)
2K views

Java Cheat Sheet

This document provides a cheat sheet overview of key Java concepts and methods including: 1) Input/output methods like JOptionPane, if/else statements, for, while and do-while loops. 2) Common Java data types like integers, doubles, chars, booleans, and strings. 3) Useful math methods from the Math class. 4) Operators for assignment, comparison, increment/decrement. 5) Switch and case statements. 6) Examples of string methods and escape characters.

Uploaded by

api-3744988
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Java Cheat Sheet

This document provides a cheat sheet overview of key Java concepts and methods including: 1) Input/output methods like JOptionPane, if/else statements, for, while and do-while loops. 2) Common Java data types like integers, doubles, chars, booleans, and strings. 3) Useful math methods from the Math class. 4) Operators for assignment, comparison, increment/decrement. 5) Switch and case statements. 6) Examples of string methods and escape characters.

Uploaded by

api-3744988
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

HP JAVA Cheat Sheet

INPUT with JOptionPane if with multiple actions (braces)


import javax.swing.*; 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
System.out.println("Buy the cookie!" );
else
baseString = JOptionPane.showInputDialog("Enter base:");
System.out.println("Buy the apple!" );
heightString = JOptionPane.showInputDialog("Enter height:");

base = Double.parseDouble( baseString ); if


height = Double.parseDouble( heightString ); if ( (hunger + look + smell ) > 15 )
area = (.5)*base*height; // Compute the area. System.out.println("Buy the cookie!" );
else if (hunger + look + smell)>20
JOptionPane.showMessageDialog(null, "The base was "+ base System.out.println("Buy the cake!" );
+ "The height was "+ height else
+ "The area of the triangle is " + area); System.out.println("Buy the apple!" );
} // end of main()

} // end of class TriangleArea


for
for (countDown=3;countDown>=0;countDown--)
{
Java Primitives/Variables System.out.println(countDown);
System.out.println("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: System.out.println(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>
System.out.print(“Enter the score”); (same line) </body>
System.out.println(“Try again”); (new line) </html>
System.out.println(“The answer is “+ 42);
APPLET
Branching Structures import javax.swing.*;
“and” is represented in Java as && import java.awt.*;
“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 ) canvas.drawOval(100, 50, 200, 200);
addition = -5; canvas.fillOval(155, 100, 10, 20);
canvas.fillOval(230, 100, 10, 20);
canvas.drawArc(150, 160, 100, 50, 180, 180);
}
}
HP JAVA Cheat Sheet

Useful Math Class Methods Result


Math.abs(-7)
Math.ceil(6.2)
7
7
Increment and Decrement Operators
count++; //increase the value of count by 1
Math.floor(6.8) 6
count--; // decrease the value of count by 1
Math.max(5,6) 6
count+2; // increase the value of count by 2
Math.min(5,6) 5
count-2 // decrease the value of count by 2
Math.pow(2.0,3.0) 8.0
Math.random() number between 0 less than 1
Math.round(6.5) 7
Math.sqrt(4.0) 2.0 Switch and Case Statements
int month = 8;
switch (month) {
Assignment Statements case 1: System.out.println("January"); break;
double pi; case 2: System.out.println("February"); break;
int x = 1; case 3: System.out.println("March"); break;
String empty = ""; case 4: System.out.println("April"); break;
double pi = 3.14159; case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
x == y // x equals y case 8: System.out.println("August"); break;
x != y // x is not equal to y case 9: System.out.println("September"); break;
x>y // x is greater than y case 10: System.out.println("October"); break;
x<y // x is less than y case 11: System.out.println("November"); break;
x >= y // x is greater than or equal to y case 12: System.out.println("December"); break;
x <= y // x is less than or equal to default: System.out.println("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 stringVariable.length();
\t horizontal tab
equals stringVariable.equals(otherStringVariable);
\r position cursor to beginning of current line
stringVariable.equalsIgnoreCase(otherStringVariable);
\\ backslash – prints a backslash
lowercase stringVariable.toLowerCase();
\” prints a double quote
uppercase stringVariable.toUpperCase();
trim stringVariable.trim()
System.out.println("Hello\nnext\nLine");
charAt(Position) stringvariable.charAt(Position);

Simple OOP example


class BoxTester
{
public static void main ( String[] args )
{
Box cube = new Box( 2.0, 2.0, 2.0 ) ;
System.out.println( "Area: " + cube.area() );
System.out.println(“Volume:” +cube.volume());
}
}

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 = console.readInt();
} discountRate = console.readDouble();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy