diff --git a/ch03/Convert.java b/ch02/Convert.java similarity index 100% rename from ch03/Convert.java rename to ch02/Convert.java diff --git a/ch02/Date.java b/ch02/Date.java new file mode 100644 index 0000000..85b0395 --- /dev/null +++ b/ch02/Date.java @@ -0,0 +1,12 @@ + +public class Date { + public static void main(String [] args) { + String day = "Monday, "; + int date = 1; + String month = "March "; + int year = 2026; + + System.out.println("American format: " + day + month + date + ", " + year); + System.out.println("European format: " + day + date + " " + month + year); + } +} diff --git a/ch02/DeclareAssign.java b/ch02/DeclareAssign.java index 87a8e02..e701d68 100644 --- a/ch02/DeclareAssign.java +++ b/ch02/DeclareAssign.java @@ -12,7 +12,7 @@ public static void main(String[] args) { hour = 11; // assign the value 11 to hour minute = 59; // set minute to 59 - message = "123"; // legal + message = "123"; // legal // message = 123; // not legal String message2 = "Hello!"; diff --git a/ch03/Echo.java b/ch02/Echo.java similarity index 100% rename from ch03/Echo.java rename to ch02/Echo.java diff --git a/ch02/FloatingPoint.java b/ch02/FloatingPoint.java index b9afa1c..3ce28c3 100644 --- a/ch02/FloatingPoint.java +++ b/ch02/FloatingPoint.java @@ -4,10 +4,6 @@ public static void main(String[] args) { double pi; pi = 3.14159; - double minute3 = 59.0; - System.out.print("Fraction of the hour that has passed: "); - System.out.println(minute3 / 60.0); - double y = 1.0 / 3.0; // correct System.out.println(0.1 * 10); diff --git a/ch03/Formatting.java b/ch02/Formatting.java similarity index 100% rename from ch03/Formatting.java rename to ch02/Formatting.java diff --git a/ch03/Literals.java b/ch02/Literals.java similarity index 100% rename from ch03/Literals.java rename to ch02/Literals.java diff --git a/ch02/PrintingVars.java b/ch02/PrintingVars.java index 27a0f08..ed7cb0c 100644 --- a/ch02/PrintingVars.java +++ b/ch02/PrintingVars.java @@ -19,7 +19,7 @@ public static void main(String[] args) { System.out.println(hour * 60 + minute); System.out.print("Fraction of the hour that has passed: "); - System.out.println(minute / 60); + System.out.println(hour * 60); System.out.print("Percent of the hour that has passed: "); System.out.println(minute * 100 / 60); diff --git a/ch03/ScannerBug.java b/ch02/ScannerBug.java similarity index 100% rename from ch03/ScannerBug.java rename to ch02/ScannerBug.java diff --git a/ch02/Time.java b/ch02/Time.java new file mode 100644 index 0000000..68b1301 --- /dev/null +++ b/ch02/Time.java @@ -0,0 +1,16 @@ +public class Time { + public static void main(String[] args) { + int hour = 22; + int minute = 44; + int second = 59; + + int secondsSinceMidNight = (22 * 60 * 60) + 59; + System.out.println(secondsSinceMidNight); + + int secondsRemaining = (24 * 60 * 60) - secondsSinceMidNight; + System.out.println(secondsRemaining); + + double percentagePast = (secondsSinceMidNight * 100.0) / (24.0 * 60.0 * 60.0); + System.out.println(percentagePast); + } +} diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java index 64984df..df0fd85 100644 --- a/ch03/GuessStarter.java +++ b/ch03/GuessStarter.java @@ -1,4 +1,5 @@ import java.util.Random; +import java.util.Scanner; /** * Starter code for the "Guess My Number" exercise. @@ -9,7 +10,18 @@ public static void main(String[] args) { // pick a random number Random random = new Random(); int number = random.nextInt(100) + 1; - System.out.println(number); + int userGuess; + + System.out.println("I'm thinking of a number between 1 and 100"); + System.out.println("(including both). Can you guess what it is?"); + + Scanner scanner = new Scanner(System.in); + System.out.print("Type a number "); + userGuess = scanner.nextInt(); + + System.out.println("Your guess is: " + userGuess); + System.out.println("The number I was thinking of is: " + number); + System.out.println("You were off by: " + (number - userGuess)); } } diff --git a/ch03/SecondsConverter.java b/ch03/SecondsConverter.java new file mode 100644 index 0000000..ec613cb --- /dev/null +++ b/ch03/SecondsConverter.java @@ -0,0 +1,21 @@ +import java.util.Scanner; + +public class SecondsConverter { + public static void main(String[] args) { + int hours; + int minutes; + int seconds; + final int Seconds_In_Hour = 3600; + + Scanner scanner = new Scanner(System.in); + System.out.print("Enter how many seconds you want to calculate: "); + seconds = scanner.nextInt(); + scanner.close(); + + hours = seconds / Seconds_In_Hour; + minutes = (seconds % Seconds_In_Hour) / 60; + seconds = seconds % 60; + + System.out.printf("%d hours, %d minutes, %d seconds\n", hours, minutes, seconds); + } +} diff --git a/ch03/TempConverter.java b/ch03/TempConverter.java new file mode 100644 index 0000000..a82a12c --- /dev/null +++ b/ch03/TempConverter.java @@ -0,0 +1,14 @@ +import java.util.Scanner; + +public class TempConverter { + public static void main(String[] args) { + double celsius; + Scanner userInput = new Scanner(System.in); + + System.out.print("Enter a temperature in Celsius: "); + celsius = userInput.nextDouble(); + double Fahrenheit = (celsius * 9/5) + 32; + + System.out.printf("%.1f C = %.2f F\n", celsius, Fahrenheit); + } +} diff --git a/ch04/DateFromSecondChapter_4_1.java b/ch04/DateFromSecondChapter_4_1.java new file mode 100644 index 0000000..0dd4f1d --- /dev/null +++ b/ch04/DateFromSecondChapter_4_1.java @@ -0,0 +1,19 @@ +public class DateFromSecondChapter_4_1 { + public static void printAmerican(String day, int date, String month, int year) { + System.out.println("American format: " + day + ", " + month + " " + date + ", " + year); + } + + public static void printEuropean(String day, int date, String month, int year) { + System.out.println("European format: " + day + " " + date + ", " + month + " " + year); + } + + public static void main(String [] args) { + String day = "Monday"; + int date = 9; + String month = "March"; + int year = 2026; + + printAmerican(day, date, month, year); + printEuropean(day, date, month, year); + } +} diff --git a/ch04/Multadd.java b/ch04/Multadd.java new file mode 100644 index 0000000..7598448 --- /dev/null +++ b/ch04/Multadd.java @@ -0,0 +1,22 @@ +public class Multadd { + public static double multadd(double a, double b, double c) { + return a * b + c; + } + + public static double expSum(double x) { + return multadd(x, Math.exp(-x), Math.sqrt(1 - Math.exp(-x))); + } + + public static void main(String[] args) { + System.out.println(multadd(1.0, 2.0, 3.0)); + + double val1 = multadd(Math.sin(Math.PI / 4), 1.0, Math.cos(Math.PI / 4) / 2); + System.out.println(val1); + + double val2 = multadd(1.0, Math.log(10), Math.log(20)); + System.out.println(val2); + + System.out.println(expSum(1.0)); + System.out.println(expSum(2.0)); + } +} diff --git a/ch04/PrintTwice.java b/ch04/PrintTwice.java index 1180417..2515cfb 100644 --- a/ch04/PrintTwice.java +++ b/ch04/PrintTwice.java @@ -1,3 +1,4 @@ + public class PrintTwice { public static void printTwice(String s) { diff --git a/ch05/Ex_5_1.java b/ch05/Ex_5_1.java new file mode 100644 index 0000000..9890c15 --- /dev/null +++ b/ch05/Ex_5_1.java @@ -0,0 +1,12 @@ +import java.util.Scanner; + +public class Ex_5_1 { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int x = in.nextInt(); + + if (x > 0 && x < 10) { + System.out.println("positive single digit number"); + } + } +} \ No newline at end of file diff --git a/ch05/Ex_5_2.java b/ch05/Ex_5_2.java new file mode 100644 index 0000000..db9e32c --- /dev/null +++ b/ch05/Ex_5_2.java @@ -0,0 +1,37 @@ +import java.util.Random; +import java.util.Scanner; + +public class Ex_5_2 { + public static void main(String[] args) { + Random random = new Random(); + int number = random.nextInt(100) + 1; + int userGuess; + int userAttempts = 3; + + System.out.println("I'm thinking of a number between 1 and 100"); + System.out.println("(including both). Can you guess what it is?"); + + Scanner scanner = new Scanner(System.in); + + while(userAttempts != 0 ) { + System.out.print("Type a number "); + userGuess = scanner.nextInt(); + + if (userGuess < number) { + System.out.println("Your number is less than the number I'm thinking about"); + userAttempts--; + } else if (userGuess > number) { + System.out.println("Your number is bigger than the number I'm thinking about"); + userAttempts--; + } else { + System.out.println("Congratulations! You guessed right!"); + break; + } + + if (userAttempts == 0) { + System.out.println("I'm sorry you are out of attempts"); + System.out.println("The number I was thinking of is: " + number); + } + } + } +} diff --git a/ch05/Fermat.java b/ch05/Fermat.java new file mode 100644 index 0000000..032eee0 --- /dev/null +++ b/ch05/Fermat.java @@ -0,0 +1,28 @@ +import java.util.Scanner; + +public class Fermat { + public static void main(String[] args) { + int a; + int b; + int c; + int n; + + Scanner in = new Scanner(System.in); + System.out.print("What's your 'a' number? "); + a = in.nextInt(); + System.out.print("What's your 'b' number? "); + b = in.nextInt(); + System.out.print("What's your 'c' number? "); + c = in.nextInt(); + System.out.print("What's your 'n' number? "); + n = in.nextInt(); + + boolean result = Math.pow(a, n) + Math.pow(b, n) == Math.pow(c, n); + + if (n > 2 && result) { + System.out.println("Holy smokes, Fermat was wrong!"); + } else { + System.out.println("No, that doesn't work."); + } + } +} diff --git a/ch05/Quadratic.java b/ch05/Quadratic.java new file mode 100644 index 0000000..beb93ef --- /dev/null +++ b/ch05/Quadratic.java @@ -0,0 +1,45 @@ +import java.util.Scanner; + +public class Quadratic { + public static void main(String[] args) { + int a; + int b; + int c; + + Scanner in = new Scanner(System.in); + + System.out.print("Enter a number "); + if (!in.hasNextInt()) { + System.out.println("Error: \"" + in.next() + "\" is not a valid integer."); + return; + } + a = in.nextInt(); + + System.out.print("Enter a number "); + if (!in.hasNextInt()) { + System.out.println("Error: \"" + in.next() + "\" is not a valid integer."); + return; + } + b = in.nextInt(); + + System.out.print("Enter a number "); + if (!in.hasNextInt()) { + System.out.println("Error: \"" + in.next() + "\" is not a valid integer."); + return; + } + c = in.nextInt(); + + double discriminant = b * b - 4 * a * c; + + if (a == 0) { + System.out.println("The value of 'a' cannot be zero."); + } else if (discriminant < 0) { + System.out.println("No real solutions."); + } else { + double x1 = (-b + Math.sqrt(discriminant)) / (2 * a); + double x2 = (-b - Math.sqrt(discriminant)) / (2 * a); + System.out.println("x1 = " + x1); + System.out.println("x2 = " + x2); + } + } +} diff --git a/ch05/Triangle.java b/ch05/Triangle.java new file mode 100644 index 0000000..7c8fd1f --- /dev/null +++ b/ch05/Triangle.java @@ -0,0 +1,45 @@ +import java.util.Scanner; + +public class Triangle { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + System.out.print("Enter a number "); + if (!in.hasNextInt()) { + System.out.println("Error: \"" + in.next() + "\" is not a valid integer."); + return; + } + int a = in.nextInt(); + if (a <= 0) { + System.err.println("Error: " + a + " is less than or equal to 0."); + return; + } + + System.out.print("Enter a number "); + if (!in.hasNextInt()) { + System.out.println("Error: \"" + in.next() + "\" is not a valid integer."); + return; + } + int b = in.nextInt(); + if (b <= 0) { + System.err.println("Error: " + b + " is less than or equal to 0."); + return; + } + + System.out.print("Enter a number "); + if (!in.hasNextInt()) { + System.out.println("Error: \"" + in.next() + "\" is not a valid integer."); + return; + } + int c = in.nextInt(); + if (c <= 0) { + System.err.println("Error: " + c + " is less than or equal to 0."); + return; + } + + if (a + b > c && a + c > b && b + c > a) { + System.out.println("Yes, these sides can form a triangle."); + } else { + System.out.println("No, these sides cannot form a triangle."); + } + } +} diff --git a/ch06/Ex_6_2_SquareRoot.java b/ch06/Ex_6_2_SquareRoot.java new file mode 100644 index 0000000..f77a458 --- /dev/null +++ b/ch06/Ex_6_2_SquareRoot.java @@ -0,0 +1,21 @@ +public class Ex_6_2_SquareRoot { + public static void main(String[] a) { + System.out.println(squareRoot(9)); // should be ~3.0 + System.out.println(squareRoot(25)); // should be ~5.0 + System.out.println(squareRoot(2)); // should be ~1.4142 + } + + public static double squareRoot(double a) { + double x = a /2; + + while (true) { + double next = (x + a / x) / 2; + + if (Math.abs(next - x) < 0.0001) { + return next; + } + + x = next; + } + } +} diff --git a/ch06/isDoubloon.java b/ch06/isDoubloon.java new file mode 100644 index 0000000..a8e013a --- /dev/null +++ b/ch06/isDoubloon.java @@ -0,0 +1,27 @@ +public class isDoubloon { + public static boolean doubloon(String s) { + String lower = s.toLowerCase(); + int[] counts = new int[26]; + + for (int i = 0; i < s.length(); i++) { + char letter = lower.charAt(i); + int index = letter - 'a'; + counts[index]++; + } + + for (int count:counts) { + if (count != 0 && count != 2) { + return false; + } + } + + return true; + } + + public static void main(String[] args) { + System.out.println(doubloon("beriberi")); + System.out.println(doubloon("boob")); + System.out.println(doubloon("tit")); + System.out.println(doubloon("Caucasus")); + } +} diff --git a/ch07/Fruit.java b/ch07/Fruit.java index 50428c5..8a32c4a 100644 --- a/ch07/Fruit.java +++ b/ch07/Fruit.java @@ -3,9 +3,10 @@ */ public class Fruit { + // Multiplication of all elements public static int banana(int[] a) { - int kiwi = 1; - int i = 0; + int kiwi = 1; // counter + int i = 0; // beginner while (i < a.length) { kiwi = kiwi * a[i]; i++; @@ -13,6 +14,7 @@ public static int banana(int[] a) { return kiwi; } + // Looking for the number in the array; public static int grapefruit(int[] a, int grape) { for (int i = 0; i < a.length; i++) { if (a[i] == grape) { @@ -22,6 +24,7 @@ public static int grapefruit(int[] a, int grape) { return -1; } + // Counting how many number appears in the array that we are looking for; public static int pineapple(int[] a, int apple) { int pear = 0; for (int pine: a) { @@ -31,5 +34,4 @@ public static int pineapple(int[] a, int apple) { } return pear; } - } diff --git a/ch07/MakeDubMus.java b/ch07/MakeDubMus.java index 9c7e193..69023d8 100644 --- a/ch07/MakeDubMus.java +++ b/ch07/MakeDubMus.java @@ -11,12 +11,14 @@ public static int[] make(int n) { return a; } + // Multiplies each number to 2; public static void dub(int[] jub) { for (int i = 0; i < jub.length; i++) { - jub[i] *= 2; + jub[i] *= 2; // } } + // Addition of the array zoo; public static int mus(int[] zoo) { int fus = 0; for (int i = 0; i < zoo.length; i++) { @@ -26,9 +28,9 @@ public static int mus(int[] zoo) { } public static void main(String[] args) { - int[] bob = make(5); - dub(bob); - System.out.println(mus(bob)); + int[] bob = make(5); // [1, 2, 3, 4, 5] + dub(bob); // [2, 4, 8, 16, 32] + System.out.println(mus(bob)); // } } diff --git a/ch07/indexOfMax.java b/ch07/indexOfMax.java new file mode 100644 index 0000000..cd24b9c --- /dev/null +++ b/ch07/indexOfMax.java @@ -0,0 +1,21 @@ +public class indexOfMax { + public static int indexOfMaxMethod(int[] numbers) { + int result = 0; + int maxVal = numbers[0]; + + for (int i = 1; i < numbers.length; i++) { + if (numbers[i] > maxVal) { + maxVal = numbers[i]; + result = i; + } + } + + return result; + } + + public static void main(String[] args) { + int[] test = {3, 7, 1, 9, 12}; + + System.out.println(indexOfMaxMethod(test)); + } +}