D286: Java Fundamentals
Leave the first rating Students also studied Terms in this set (181) Western Governors UniversityD 315 Save
D286: Pre-Assessment
14 terms xm7gdfn4h4Preview C949 Data Structures and Algorithm...173 terms AidenOSprague Preview
WGU C952
239 terms njlasjdPreview
D385 P
39 terms Cyd Main()Where a program begins executing statements contained within braces {}.System.out.print()Outputs an item on the screen, starting at the screen cursor's present location, and moving the cursor after each output item.System.out.println()Prints an item and then moves the cursor to the next line (ie: newline).import java.util.Scanner;Specific import code placed at the top of a file to enable the program to get input Scanner scnr = new Scanner(System.in);The statement that instantiates a new scanner object in the variable "scnr" so system input can be parsed.scnr.nextInt();The statement that retrieves the system's next integer value input.It is the variable "scnr" calling this scanner class method to read integer values from input made possible after "scnr" is instantiated with a new scanner class object."desired text"Syntax for string literals, which is text in double quotes System.out.println("Wage is: " + wage);This example shows how the "+" symbol is used to output multiple items using one output statement.\nThis is the syntax for the newline character, an alternative to "println", though not used often for practical purposes.
// This is a commentThis is the syntax for writing a single-line comment that commonly appears after a statement on the same line./** This is a comment **/ This is the syntax for writing a multi-line comment, also called a block comment.WhitespaceThis is blank space (space and tab characters) between items within a statement and blank lines between statements (called newlines).A compiler ignores most of this and is used to make a program more readable.int numValue;This is an example syntax for declaring an integer variable with the identifier "numValue". In general, any identifier that is not a reserved word (ie: keyword) can be used to name a variable. Not how this example is using lowerCamelCase.numValue = 8;This is an example syntax for an assignment statement, which assigns an integer literal to a declared variable.int numValue = 100;This is an example syntax for initializing an integer variable, which assigns an
integer literal (ie: initial value) at the same time the variable is declared.
+, -, *, /Syntax for the addition, subtraction, multiplication, and division arithmetic operators, respectively.(), unary -, * / %, + -These four operator categories are listed first to last according to precedence rules for arithmetic operators.+=, -=, *=, /=, %=These are the five special operators known as compound operators, which all provide a shorthand way to update a variable double milesTravel;This is an example syntax for declaring a variable that can store a floating-point number.scnr.nextDouble();A scanner method that reads a floating-point value from input, which can be assigned to a double variable.Infinity, -InfinityJava outputs these when a user divides a nonzero floating-point number by zero.Not a number (NaN)If the dividend and divisor in floating-point division are both 0, the division results in this output.System.out.printf("%.2f", myFloat);This example shows the syntax for controlling the number of decimal places when printing a floating-point number. In this case, 'printf' for float, '.2' means to two decimal places, and 'myFloat' is an example variable where the decimal place manipulation is applied Math.PIJava provides this to hold the first 15 digits of the irrational mathematical constant pi (π).
6.02e23This is an example syntax of a floating-point literal using scientific notation where the e precedes the power-of-10 exponent.final double SPEED_OF_SOUND = 761.207;This is an example syntax for declaring a constant variable (ie: final variable). Note the use of 'final' to prevent modification and the use of upper cases & underscores for identifiability.Math.The syntax for invoking the Math class, a Java standard package that allows about 30 math methods to be called.sqrt(x), pow(x, y), abs(x)The syntax of three common math methods to invoke square root, power, and absolute value for a given value, respectively. These must be appended to 'Math.' to work.
- / 2 = 2 vs 5.0 / 2.0 = 2.5These two divisions show how the result depends on whether or not the involved
values are integers or floating-point. Integer division neglects any remainder.randNum This example yields a random number between 0 and 9, made possible by the modulo operator. Note that 0 is possible, not 10.(randNum ) + 20This example yields a random number between 20 and 30, made possible by the modulo operator and then adding 20 to the result.
0.504 * 316 = 159.264This example shows how calculating any integer with a floating-point number causes Java to invoke implicit conversion, a form of type conversion, to change the integer to a floating-point number prior to calculation.(int) vs (double)
Ex: myFloat = (double)numValue;
These two perform type casting, which invoke explicit conversion to change a value from double to integer or integer to double, respectively. These must be appended on the left side of a variable to work.char myChar;This is an example syntax to declare a variable that can store a single character literal. Any character being assigned to this variable type must be surrounded with single quotes, not double quotes. For example, myChar = 'm';.myChar = scnr.next().charAt(0);This example syntax shows how to get a character value from input. Note that next() gets the next sequence of non-whitespace characters (as a string), and charAt(0) gets the first character in that string.\n , \t , \' , \" , \\Thes are five common escape sequence characters, which are two-character sequences that start with \ and then the character the user wishes to represent.This allows the user to enter a newline, tab, single quote, double quote, and backslash, respectively.System.out.print("" + c1 + c2);This is an example syntax showing how a programmer can output multiple character variables with one statement. The initial "" tells the compiler to output a string of characters, and the +'s combine the subsequent characters into such a string. Without the "", the compiler will simply add the numerical values of c1 and c2, and output the resulting sum.
movieTitle = "The Martian";This example syntax is how to assign a string literal to a String variable. String variables can be assigned other String variables as well.String firstMonth = "January";This example syntax shows that String variables can be initialized during their declaration.scnr.next();This statement gets the next string literal from input, skipping any initial whitespace and stopping when the next whitespace is seen. For example, it can get "Hello" from " Hello World".scnr.nextLine()This statement gets the entire line from input, including all whitespace before, within, and after any characters on the line in question. Note that it removed the newline character at the end of the line, but does not include it within the string.byte, short, int, longThese are the four main integer numeric data types that can hold 8 bits, 16 bits, 32 bits, and 64 bits, respectively.float, doubleThese are the two main floating-point numeric data types that can hold 32 bits and 64 bits, respectively.import java.util.Random;This statement enables use of the Random class.Random randGen = new Random();This statement creates a new random number generator object named randGen.randGen.nextInt()After creating a new random number generator object named randGen, this method call can be used to get a random number integer ranging from "-2^31" to "2^31 - 1".randGen.nextInt(10)This example random number generator method call syntax shows a programmer restrictiing the possible outputs to 10 values ranging from 0 to 9.randGen.nextInt(30 - 18 + 1) + 18This example random number generator method call syntax shows a programmer restrictiing the possible outputs to 18 values ranging from 18 to 30.Random randGen = new Random(5);This statement creates a new random number generator object named randGen that also specifies a seed "5".randGen.setSeed(5);After creating a new random number generator object named randGen without a seed, this method call can be used to specify a seed, in this example "5", so each program run will yield the same sequence of pseudo-random numbers.import packageName.ClassName;This is the general syntax for any import statement. For example, for "import java.util.Scanner", 'java.util' is the package & subpackage name while 'Scanner' is the class name.Scanner(InputStream source)This constructor performs similarly to "new Scanner(System.in);" as they both are an InputStream object designed to take in input during program execution.However, This constructor can specify any input stream as the source, not just standard input.