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,43 +11,81 @@ public class MethodExercises {
// TODO: 1 - Create a method called greet that takes a String parameter 'name'
// and prints "Hello, {name}!"
// Hint: public static void greet(String name) { ... }

public static void greet(String name) {
System.out.printf("Hello %s",name);
System.out.println();
}

// TODO: 2 - Create a method called add that takes two int parameters (a, b)
// and returns their sum
// Hint: public static int add(int a, int b) { ... }
public static int add(int a, int b) {
return a + b;
}


// TODO: 3 - Create a method called isEven that takes an int parameter 'number'
// and returns true if the number is even, false otherwise
// Hint: Use the modulus operator (%)

public static boolean isEven(int number) {
return number % 2 == 0;
}

// TODO: 4 - Create a method called max that takes two int parameters (a, b)
// and returns the larger of the two
// Hint: Use an if statement or the ternary operator

public static int max(int a, int b) {
return Math.max(a, b);
}

// TODO: 5 - Create a method called factorial that takes an int parameter 'n'
// and returns n! (n factorial) using a loop
// Hint: 5! = 5 * 4 * 3 * 2 * 1 = 120. Use a long return type for larger values.
public static int factorial(int n) {
int factorialPlaceholder = 1;
for (int num = 1; num <= n; num++) {
factorialPlaceholder *= num;
}
return factorialPlaceholder;
}


// TODO: 6 - Create two overloaded methods called multiply:
// - One that takes 2 int parameters and returns their product
// - One that takes 3 int parameters and returns their product
// Overloading means having multiple methods with the same name but different parameters.
public static int multiply(int a, int b) {
return a * b;
}

public static int multiply(int a, int b, int c) {
return a * b * c;
}


public static void main(String[] args) {

// TODO: 7 - Call all the methods above and print their results
// - Call greet with your name
greet("Jose");
// - Call add with two numbers and print the result
int result = add(6, 7);
System.out.println(result);
// - Call isEven with a number and print whether it is even
boolean isEvenResult = isEven(5);
System.out.println(isEvenResult);
// - Call max with two numbers and print the larger one
int maxNumber = max(6, 9);
System.out.println(maxNumber);
// - Call factorial with 5 and print the result
int factorialRes = factorial(7);
System.out.println(factorialRes);
// - Call both multiply methods and print their results
int multiply1 = multiply(5, 5);
int multiply2 = multiply(5, 5, 5);

System.out.println("multiply1 result: " + multiply1);
System.out.println("multiply2 result: " + multiply2);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,90 @@ public static void main(String[] args) {
// TODO: 1 - Use arithmetic operators (+, -, *, /) on two int variables and print the results
// Declare two int variables (e.g., a = 10, b = 3)
// Print the result of a + b, a - b, a * b, and a / b
System.out.println("TODO 1");
int num1 = 10;
int num2 = 3;

System.out.println("Addition: " + (num1 + num2));
System.out.println("Subtraction: " + (num1 - num2));
System.out.println("Multiplication: " + (num1 * num2));
System.out.println("Division: " + (num1 /+ num2));
System.out.println();

// TODO: 2 - Use the modulus operator (%) to check if a number is even
// Declare an int variable called number with any value.
// Print the result of number % 2
// Print whether the number is even (result is 0) or odd (result is 1)

System.out.println("TODO 2");
System.out.println("------------------------");
int variable = 78;
int isEven = variable % 2 == 0 ? 0 : 1;
System.out.println(isEven);

// TODO: 3 - Use increment (++) and decrement (--) operators
// Declare an int variable called counter, set it to 5
// Use counter++ and print the result, then use counter-- and print the result

System.out.println("TODO 3");
System.out.println("------------------------");
int counter = 5;
counter++;
System.out.println(counter);
counter--;
System.out.println(counter);

// TODO: 4 - Use compound assignment operators (+=, -=, *=)
// Declare an int variable called score, set it to 10
// Use +=, -=, and *= on score, printing after each operation
System.out.println("TODO 4");
System.out.println("------------------------");
int score = 10;
score += 5;
System.out.println(score);
score -= 5;
System.out.println(score);
score *= 5;
System.out.println(score);



// TODO: 5 - Use comparison operators (==, !=, >, <, >=, <=) and print the boolean results
// Declare two int variables (e.g., x = 5, y = 10)
// Print the result of each comparison, e.g.: System.out.println("x == y: " + (x == y));
System.out.println("TODO 5");
System.out.println("------------------------");
int x = 20;
int y = 40;
System.out.println("x == y: " + ( x == y));
System.out.println("x >= y: " + (x >= y));
System.out.println("x <= y: " + (x <= y));
System.out.println("x > y: " + (x > y));
System.out.println("x < y: " + (x < y));
System.out.println("x != y: " + (x != y));


// TODO: 6 - Use logical operators (&&, ||, !) to combine conditions
// Declare two boolean variables (e.g., hasLicense = true, hasInsurance = false)
// Print the result of: hasLicense && hasInsurance
// Print the result of: hasLicense || hasInsurance
// Print the result of: !hasLicense
System.out.println("TODO 6");
System.out.println("------------------------");
boolean hasLicense = true;
boolean hasInsurance = false;
System.out.println("hasLicense && hasInsurance: " + (hasLicense && hasInsurance));
System.out.println("hasLicense || hasInsurance: " + (hasLicense || hasInsurance));
System.out.println("!hasLicense: " + (!hasLicense));


// TODO: 7 - Use the ternary operator to assign "adult" or "minor" based on age
// Declare an int variable called age with any value
// Use the ternary operator: String status = (condition) ? "adult" : "minor";
// Print the status
System.out.println("TODO 6");
System.out.println("------------------------");
int age = 36;
String status = age >= 18 ? "adult" : "minor";
System.out.println("Are you an adult?: " + status);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,62 @@ public static void main(String[] args) {
// TODO: 1 - Widen an int to a double (implicit casting)
// Declare an int variable with any value, then assign it to a double variable.
// Print both variables to see the result.

System.out.println("Widen an int to a double (implicit casting)");
int number = 50;
double numberCastedDouble = number;
System.out.println("number: " + number);
System.out.println("numberDouble: " + numberCastedDouble);

// TODO: 2 - Narrow a double to an int (explicit casting)
// Declare a double variable (e.g., 9.78), then cast it to an int.
// Print both variables to see what happens to the decimal part.

System.out.println("---------------------------------------------");
System.out.println("Narrow a double to an int (explicit casting)");
double todoTwoDouble = 9.78;
int todoTwoInt = (int) todoTwoDouble;
System.out.println("todoTwoDouble: " + todoTwoDouble);
System.out.println("todoTwoInt: " + todoTwoInt);

// TODO: 3 - Cast an int to a char to get the character it represents
// Hint: int value 65 corresponds to 'A' in ASCII
// Print the resulting char.
System.out.println("---------------------------------------------");
System.out.println("Cast an int to a char to get the character it represents");
int todoThreeInt = 65;
char todoThreeChar = (char) todoThreeInt;
System.out.println("todoThreeInt: " + todoThreeInt);
System.out.println("todoThreeChar: " + todoThreeChar);


// TODO: 4 - Cast a char to an int to get its ASCII value
// Hint: char 'Z' has an ASCII value of 90
// Print the resulting int.
System.out.println("---------------------------------------------");
System.out.println("Cast a char to an int to get its ASCII value");
char todoFourChar = 'Z';
int todoFourInt = todoFourChar;
System.out.println("todoFourChar: " + todoFourChar);
System.out.println("todoFourInt: " + todoFourInt);



// TODO: 5 - Convert a String "42" to an int using Integer.parseInt()
// Declare a String variable with the value "42", then parse it to an int.
// Print the result.
System.out.println("---------------------------------------------");
System.out.println("Convert a String \"42\" to an int using Integer.parseInt()");
String todoFiveString = "42";
System.out.println(Integer.parseInt(todoFiveString));


// TODO: 6 - Convert an int 42 to a String using String.valueOf()
// Declare an int variable with the value 42, then convert it to a String.
// Print the result.
System.out.println("---------------------------------------------");
System.out.println("Convert an int 42 to a String using String.valueOf()");
int todoSixInt = 42;
String todoSixIntString = String.valueOf(todoSixInt);
System.out.println(todoSixIntString);

}
}
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);

}
}