Java syntax

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

System.out.Printf("%3d", i*j)

% /number of spaces taken up/ Integer, variable

str.equals("test")

(boolean) returns true if the string "test" matches string str, false otherwise

for (typeName variable: array/collection) { sum = sum + variable; }

enhanced for loop for arrays; adds each element of array to sum (declared before loop)

array.length

gives the length of the array (no parentheses!)

public static double cubeVolume (double sideLength)

header for method

if (condition) { statements1 } else { statements2 }

if statement (else not required in all cases)

import java.util.Scanner

imports Scanner library

++/--

increment/decrement; add 1 to/subtract 1 from the variable

/* */

long comments

final int VOLUME = 8

names constant integer

String input = in.next();

next() reads the next string; returns any charas that are not white space

Integer.parseInt or Double.parseDouble

obtains the integer value of a string containing the digits

System.out.print

prints statement

System.out.println

prints statement and creates new line

Scanner in = new Scanner(new File("input.txt");

reads input from a file (also declared in same line)

java SentinelDemo < numbers.txt

redirects input

==,!=,>,<,>=,<=

relational operators; equal, not equal, greater than, less than, greater than/equal to, less than/equal to

in.useDelimiter("[^A-Za-z]+");

restricts input to only letters, removes punctuation and numbers

return volume;

return statement that returns the result of a method and terminates the method call

str.charAt(pos)

returns the character (char) at position pos (int) in the given string

str.length()

returns the number of characters in the given string (int)

str.indexOf(text)

returns the position (int) of the first occurrence of string (text) in string str (output -1 if not found)

str.substring(start, end)

returns the string starting at position (start) and ending at position (end-1) in the given string

char newChar = (char)(mychr + x)

shift character by x numbers (use ASCII table values)

//comment

short comments

Math.sin(x)

sine of x (x in radians)

Math.sqrt(x)

square root of x

Character.isDigit(ch)

statement (boolean), ch (char); allows you to check if ch is a digit or not

Character.isLetter(ch); Character.isUpperCase(ch); Character.isLowerCase(ch); Character.isWhiteSpace(ch);

statement (boolean), ch (char); allows you to check if ch is a letter/uppercase/lowercase/whitespace

while (condition) { statements }

statements = body of while loop; braces not needed for a single statement

/** Computes the volume of a cube. @param sideLength the side length of the cube @return the volume */

structure for commenting before methods (javadoc)

Math.tan(x)

tangent of x

throws FileNotFoundException

terminates main if FileNotFoundException occurs

for (initialization; condition; update) { statements }

terminating loop

double blah = (int) (blah + 3)

typecast; change variable type

string Var = "Hello"

variable Var = Hello (*double quotes!)

char answer = 'y'

variable answer = y (*single quotes!)

public static void boxString(String contents)

void methods return no value, but can produce output

PrintWriter out = new PrintWriter("output.txt");

writes output to a file "output.txt"

Math.pow(x,y)

x^y (x>0, or x=0 and y>0, or x<0 and y is an integer)

ArrayList<String> friends = new ArrayList<String>();

ArrayList<typeParameter> variableName = new ArrayList<typeParameter>(); typeParameter cannot be a primitive type

- Left alignment 0 show leading zeroes + show a plus sign for positive numbers ( enclose negative numbers in parentheses , show decimal separators ^ convert letters to uppercase

Format flags (printf)

d decimal integer f fixed floating-point e exponential floating-point (very large or very small values) g general floating-point s string

Format types (printf)

Arrays.copyOf(values, n)

The call Arrays.copyOf(values, n) allocates an array of length n, copies the frst n elements of values (or the entire values array if n > values.length) into it, and returns the new array.

string1.compareTo (string2) < 0

The compareTo method compares strings in lexicographic order.

countryName = countryName.trim();

The trim method returns the string with all white space at the beginning and end removed.

values[i] = 0;

access element of array at position i (count starts from 0)

friends.add (i, "Cindy"); String name = friends.get(i); friends.set(i, "Harry"); friends.remove(0);

adds element to arrayList at pos i (w/out i will add element to end); obtains element at i; redefines element at i as "Harry"; removes element

double[] values = new double[10]; double[] moreValues = {32, 54, 67.5};

array declaration and initialization

{ double volume = sideLength * sideLength * sideLength; return volume; }

body of method

&&, ||, !

boolean operators; and, or, not

\

char that comes after is exempted from compiler (ie printing "")

in.hasNextDouble()

checks for numerical input; false if the next input is not a floating-point number

if (string1.equals(string2))...

compares equality of 2 strings

Math.toRadians(x)

convert x degrees to radians (ie, returns x* p/180)

Math.toDegrees(x)

convert x radians to degrees (ie, returns x*180/p)

Math.cos(x)

cosine of x

Scanner in = new Scanner(System.in);

declares Scanner "in"

String s = in.nextLine();

declares and inputs a line (a string of characters)

double d = in.nextDouble();

declares and inputs a real value (floating point #)

int i = in.nextInt();

declares and inputs integer value

do { i++; //more code }while (i<max)

do/while loop

Math.exp(x)

e^x

Arrays.toString(values) = [4,7,9...]

elements separated by commas with brackets


Ensembles d'études connexes

Early Literature including Indian, Sanskrit, Chinese, Persian and Faith-based Literature

View Set

100 Rules of Will Construction in Texas

View Set

Lord of the Flies Chapter 6 Questions & Answers

View Set