Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,33 @@ public class Variables {
public static void main(String[] args) {

// TODO: 1 - Declare an int variable called age and assign it the value 25

int age = 25;

// TODO: 2 - Declare a double variable called price and assign it the value 9.99

double price = 9.99;

// TODO: 3 - Declare a boolean variable called isJavaFun and assign it the value true

boolean isJavaFun = true;

// TODO: 4 - Declare a String variable called name and assign it your name

String name = "Jose";

// TODO: 5 - Declare a char variable called grade and assign it the value 'A'

char grade = 'A';

// TODO: 6 - Print all the variables above using System.out.println
// Hint: You can print each variable on its own line, e.g.:
// System.out.println("Age: " + age);

System.out.println("Age: " + age);
System.out.println("Price: " + price);
System.out.println("isJavaFun: " + isJavaFun);
System.out.println("Name: " + name);
System.out.println("Grade: " + grade);

// TODO: 7 - Declare a final (constant) variable called MAX_SCORE, set it to 100, and print it
// Hint: Use the 'final' keyword before the type to make a constant
final int MAX_SCORE = 100;
System.out.println("MAX_SCORE: " + MAX_SCORE);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static BigDecimal createFromString(String value) {
// TODO: 1 - Create and return a BigDecimal using new BigDecimal(value).
// The String constructor preserves the exact decimal value.
// NEVER use new BigDecimal(0.1) — see TODO 6 for why.
return null;
return new BigDecimal(value);
}

/**
Expand All @@ -36,7 +36,7 @@ public static BigDecimal createFromString(String value) {
public static BigDecimal addValues(BigDecimal a, BigDecimal b) {
// TODO: 2 - Use the add() method to add a and b. Return the result.
// Remember: BigDecimal is immutable, so add() returns a NEW BigDecimal.
return null;
return a.add(b);
}

/**
Expand All @@ -48,7 +48,7 @@ public static BigDecimal addValues(BigDecimal a, BigDecimal b) {
*/
public static BigDecimal multiplyValues(BigDecimal a, BigDecimal b) {
// TODO: 3 - Use the multiply() method to multiply a and b. Return the result.
return null;
return a.multiply(b);
}

/**
Expand All @@ -63,7 +63,7 @@ public static BigDecimal divideValues(BigDecimal a, BigDecimal b, int scale) {
// TODO: 4 - Use a.divide(b, scale, RoundingMode.HALF_UP) to divide.
// You MUST specify a rounding mode for division, otherwise you may get
// an ArithmeticException for non-terminating decimals (like 1/3).
return null;
return a.divide(b, scale, RoundingMode.HALF_UP);
}

/**
Expand All @@ -80,7 +80,7 @@ public static int compareValues(BigDecimal a, BigDecimal b) {
// TODO: 5 - Use a.compareTo(b) to compare the two values.
// Return the result (which will be -1, 0, or 1).
// Do NOT use equals() for numeric comparison!
return 0;
return a.compareTo(b);
}

/**
Expand All @@ -94,6 +94,12 @@ public static void demonstrateDoubleProblem() {
// Print both values. Notice that fromDouble is NOT exactly 0.1!
// It will show something like 0.1000000000000000055511151231257827021181583404541015625
// Print a message explaining why the String constructor should be preferred.
System.out.println("The string constructor should be preferred because passing in a double\n " +
"causes the 0.1 to actually be 0.1000000000000000055511151231257827021181583404541015625 because\n" +
"it is essentially a floating point number based on binary fractions (base 2).\n" +
"Passing it through the BigDecimal constructor will allow it to do it's job exactly as it's meant to\n" +
"which is storing that exact long number into memory.");


}

Expand Down