- Locate examples the following terms in the Java code below:
- Identifier
- Variables
- Variable declarations
- Variable initialization
- Data type
- Primitive data type
- Keywords
- Reserved literal
- Assignment statement
- Literal
- Operator
- Multi-line comment
- Single line comment
import java.util.Scanner;
public class ProbabiltySameBirthday {
public static void main (String[] args) {
final int NUM_DAYS_YEAR = 366;
Scanner stdinScanner = new Scanner(System.in);
int numPeople;
System.out.print("Please enter the number of people in the room: ");
numPeople = stdinScanner.nextInt();
double numerator = 1;
for( int i = NUM_DAYS_YEAR; i > (NUM_DAYS_YEAR - numPeople); --i){
numerator *= i;
}
double denominator;
denominator = Math.pow( NUM_DAYS_YEAR, numPeople);
double probability = (1.0 - numerator / denominator) * 100.0;
System.out.println("For " + numPeople + ", there's a " + probability + "% chance that two or more people share a birthday!");
}
}
- What are the two rules for naming identifiers (for example, variables)?
-
-
- List three examples of valid identifiers:
- List three examples of invalid identifiers:
- Java (currently) has 51 (active) keywords and 3 reserved words.
We will be covering the underlined ones in this course.
Research 3 keywords that we're going to use this semester and explain how they're used to someone else.
Keywords:
abstract
assert
boolean
break
byte
case
catch
char
class
continue
default
do
double
else
enum
exports
extends
final
finally
float
for
if
implements
import
instanceof
int
interface
long
module
native
new
package
private
protected
public
requires
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
Reserved literals:
false
null
true
- In general, there are two style guidelines for naming identifiers:
- words_separated_by_underscores
- wordsWithFirstLetterCapitalized
Tell someone which one are you going to use.
- List the five compound assignment operators in Java:
- In the following code, identify invalid code (syntax errors):
Valid and invalid code used for an exercise to identify proper and improper coding syntax and guidelines.
@author
@version
public class VariablesExercise {
public static void main (String[] args) {
int peopleInAmerica = 325,782,231;
double gross_domestic_product = 19.39e12;
double unemploymentRate = 4.0%;
int gdppc = gross_domestic_product/peopleInAmerica;
System.out.println("The gross domestic product per person (in the United States of America) is " + gdppc);
}
}
- Which of the following primitive data types store their contents as a number:
- Discuss with someone have many bytes does an int and a double require in Java. Namely, does it vary depending on the machine's architecture or is it predictable and fixed?
- What is the range of possible values for the following primitive data types:
- boolean
- char
- int
- long
- float
- double
- Body Mass Index (BMI) is calculated as 703 * weight_in_lbs / height_in_inches2. What does the following code display and why?
final int BMI_SCALAR = 703;
int weight = 10;
int height = 20;
int calculation = weight / (height * height);
double bmi = BMI_SCALAR * calculation;
System.out.println( "BMI: " + bmi );
- In Java, what's a promotion? What are the rules for promotion?
- What does the following display and why?
int counter = 0;
if( counter++ != 1 ){
System.out.println("counter: " + counter );
}else{
System.out.println("increment operators are cool!");
}
- Write the last two statements as an equivalent single Java statement:
Scanner stdin = new Scanner( System.in );
int j = stdin.nextInt();
int k = stdin.nextInt();
k = k + 1;
j = k;
- Write the last two statements as an equivalent single Java statement:
Scanner stdin = new Scanner( System.in );
int j = stdin.nextInt();
int k = stdin.nextInt();
j = k;
k = k + 1;
- Declare a constant (primitive) variable with the value of 100 (using common coding conventions).