├── CodingTime ├── R01_VAT_CalculatorProgra ├── R02_perimeterExplorationProgram ├── R03_Calculator ├── R04_UserLogin ├── R05_NotOverMeasures └── R06_ticketPrice ├── Projects ├── Calculator └── Temperature_Converter ├── README.md ├── TeacherGPT ├── R01_isEmpty_isBlank ├── R02_null ├── R03_replace ├── R04_replaceAll ├── R05_toUpperCase ├── R06_lenght ├── R07_substring ├── R08_substring_indexOf ├── R09_startsWith └── R10_contains ├── forBegginers ├── r01_IfStatements ├── r02_nested_IfStatements ├── r03_nested_IfStatements_01 ├── r04_TernaryOperator ├── r05_SwitchStatements ├── r06_Methods ├── r07_Methods_01 ├── r08_Methods_02 ├── r09_lastIndexOf ├── r10_isEmpty_isBlank ├── r11_forLoop ├── r12_forLoopNotes ├── r13_forLoop ├── r14_forLoop ├── r15_forLoop ├── r16_factorial ├── r17_sum_of_figures ├── r18_FizzBuzz ├── r19_StringReversal ├── r20_nestedForLoop ├── r21_nestedForLoop ├── r22_nestedForLoop ├── r23_nestedForLoop ├── r24_nestedForLoop ├── r25_nestedForLoop ├── r26_MethodCreation ├── r27_MethodCreation ├── r28_MethodCreation ├── r29_UsingMethodFromAnotherClass ├── r29_methodOverloading ├── r30_methodCreation ├── r31_methodOlusturma ├── r32_MethodOverloading ├── r33_whileLoop ├── r34_whileLoop ├── r35_whileLoop_DoWhileLoop ├── r36_whileLoop_doWhileLoop ├── r37_whileLoop_DoWhileLoop ├── r38_whileLoop_DoWhileLoop ├── r39_Scope ├── r40_Scope ├── r41_Scope ├── r42_Arrays └── r43_Arrays └── homework ├── R01_contains ├── R02_parse ├── R03_replace_substring ├── R04_! └── R05_@email /CodingTime/R01_VAT_CalculatorProgra: -------------------------------------------------------------------------------- 1 | package CodingTime; 2 | 3 | import java.util.Scanner; 4 | 5 | public class R01_VAT_CalculatorProgram { 6 | public static void main(String[] args) { 7 | double amount , VATPrice , vat = 0.20; 8 | Scanner scan = new Scanner(System.in); 9 | System.out.println("Enter the amount of the product"); 10 | amount = scan.nextDouble(); 11 | VATPrice = amount + (amount * vat); 12 | System.out.println("Price with VAT: " + VATPrice); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CodingTime/R02_perimeterExplorationProgram: -------------------------------------------------------------------------------- 1 | package CodingTime; 2 | 3 | import java.util.Scanner; 4 | 5 | public class R02_perimeterExplorationProgram { 6 | public static void main(String[] args) { 7 | Scanner scan = new Scanner(System.in); 8 | 9 | int range; 10 | double area, volume, pi = 3.14; 11 | 12 | System.out.println("Please enter the circle radius: "); 13 | range = scan.nextInt(); 14 | area = 2 * pi * range; 15 | volume = pi * (range * range); 16 | System.out.println("Area of the apartment: " + area); 17 | System.out.println("Volume of the apartment: " + volume); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CodingTime/R03_Calculator: -------------------------------------------------------------------------------- 1 | package CodingTime; 2 | 3 | import java.util.Scanner; 4 | 5 | public class R03_Calculator { 6 | public static void main(String[] args) { 7 | 8 | Scanner scan = new Scanner(System.in); 9 | double num1, num2, action; 10 | System.out.print("Please enter first number: "); 11 | num1 = scan.nextDouble(); 12 | System.out.print("\nPlease enter second number: "); 13 | num2 = scan.nextDouble(); 14 | System.out.println("\nPlease select the action you want to take"); 15 | System.out.println("1- Collection\n2- Subtraction\n3- Multiplication\n4- Division"); 16 | System.out.print("Your action: "); 17 | action = scan.nextDouble(); 18 | 19 | if (action == 1) { 20 | System.out.println("Collection: " + (num1 + num2)); 21 | } else if (action == 2) { 22 | System.out.println("Subtraction: " + (num1 - num2)); 23 | } else if (action == 3) { 24 | System.out.println("Multiplication: " + num1 * num2); 25 | } else if (action == 4) { 26 | if (num2 == 0) { // Division by zero error check 27 | System.out.println("Cannot divide by zero"); 28 | } else { 29 | System.out.println("Division: " + num1 / num2); 30 | } 31 | } else { 32 | System.out.println("Exiting program... The number entered is an invalid transaction: " 33 | + action); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /CodingTime/R04_UserLogin: -------------------------------------------------------------------------------- 1 | package CodingTime; 2 | 3 | import java.util.Scanner; 4 | 5 | public class R04_UserLogin { 6 | public static void main(String[] args) { 7 | 8 | // userName: Java 9 | // password: adminjava 10 | 11 | Scanner scan = new Scanner(System.in); 12 | String userName , password; 13 | System.out.print("Please enter user name: "); 14 | userName = scan.nextLine(); 15 | System.out.print("Please enter password: "); 16 | password = scan.nextLine(); 17 | 18 | if (userName.equals("java") && password.equals("adminjava")){ 19 | System.out.println("You made a successful entry."); 20 | } else System.out.println("Invalid username or password"); 21 | 22 | 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /CodingTime/R05_NotOverMeasures: -------------------------------------------------------------------------------- 1 | package CodingTime; 2 | 3 | import java.util.Scanner; 4 | 5 | public class R05_NotOverMeasures { 6 | 7 | // Enter grades for Math, Science, Social Studies, Physical Education and History. 8 | // Calculate the average of the grades. 9 | // If the average is less than 50, fail the class, if not, pass the class. 10 | 11 | public static void main(String[] args) { 12 | 13 | int mat, science, socialKnowledge, physicalEducation, history; 14 | double GPA; 15 | 16 | Scanner scan = new Scanner(System.in); 17 | 18 | System.out.print("Please enter your Math grade: "); 19 | mat = scan.nextInt(); 20 | 21 | System.out.print("Please enter your Science grade: "); 22 | science = scan.nextInt(); 23 | 24 | System.out.print("Please enter your Social Studies grade: "); 25 | socialKnowledge = scan.nextInt(); 26 | 27 | System.out.print("Please enter your Physical Education grade: "); 28 | physicalEducation = scan.nextInt(); 29 | 30 | System.out.print("Please enter your date grade: "); 31 | history = scan.nextInt(); 32 | 33 | GPA = (mat + science + socialKnowledge + physicalEducation + history) / 5; 34 | 35 | if (GPA >= 101){ 36 | System.out.println("\nInvalid grade information entered. Note: Your exam grades cannot be more than 100."); 37 | } else if (GPA <= -1){ 38 | System.out.println("\nYour exam grades cannot be below 0."); 39 | } else if (GPA >= 50){ 40 | System.out.println("\nYou passed the class, your GPA: " + GPA); 41 | } else System.out.println("\nYou failed, your GPA: " + GPA); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /CodingTime/R06_ticketPrice: -------------------------------------------------------------------------------- 1 | package CodingTime; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class R06_ticketPrice { 7 | 8 | // Unit price per KM: $0.10 9 | // 50% discount on the total price if the age is less than 12 10 | // 10% discount on the total price if the age is between 12 and 24 11 | // 30% discount on the total price if the age is greater than or equal to 65 12 | // 20% discount on the total price for a round trip 13 | // A program that calculates flight ticket prices according to these conditions. 14 | public static void main(String[] args) { 15 | 16 | Scanner scan = new Scanner(System.in); 17 | int km, age, type; 18 | double basePrice = 0 , ageDiscount = 0, typeDiscount = 0; 19 | System.out.print("Enter the distance (in km): "); 20 | km = scan.nextInt(); 21 | System.out.print("Enter your age: "); 22 | age = scan.nextInt(); 23 | System.out.print("Select trip type (1- One-way, 2- Round trip): "); 24 | type = scan.nextInt(); 25 | 26 | if (km > 0 && age > 0 && (type == 1 || type == 2)) { 27 | basePrice = km * 0.10; 28 | if (age < 12) { 29 | ageDiscount = basePrice * 0.5; 30 | } else if (age >= 12 && age <= 24) { 31 | ageDiscount = basePrice * 0.1; 32 | } else if (age >= 65) { 33 | ageDiscount = basePrice * 0.3; 34 | } else { 35 | ageDiscount = 0; 36 | } 37 | basePrice -= ageDiscount; 38 | if (type == 2) { 39 | typeDiscount = basePrice * 0.2; 40 | basePrice = (basePrice * 2) - typeDiscount; 41 | } 42 | System.out.println("Ticket Price: $" + basePrice); 43 | } else { 44 | System.out.println("Invalid input."); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Projects/Calculator: -------------------------------------------------------------------------------- 1 | package Java_Projects; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Calculator { 6 | public static void main(String[] args) { 7 | 8 | Scanner scan = new Scanner(System.in); 9 | while (true) { 10 | System.out.println("1- Addition"); 11 | System.out.println("2- Subtraction"); 12 | System.out.println("3- Multiplication"); 13 | System.out.println("4- Division"); 14 | System.out.println("0- Exit"); 15 | 16 | System.out.println("Please select the operation you want to perform:"); 17 | 18 | int choice = scan.nextInt(); 19 | 20 | if (choice == 0) { 21 | System.out.println("Program is terminating..."); 22 | break; 23 | } 24 | 25 | System.out.print("Enter the first number: "); 26 | double num1 = scan.nextDouble(); 27 | System.out.print("Enter the second number: "); 28 | double num2 = scan.nextDouble(); 29 | double result = 0; 30 | 31 | if (choice == 1) { 32 | result = num1 + num2; 33 | } else if (choice == 2) { 34 | result = num1 - num2; 35 | } else if (choice == 3) { 36 | result = num1 * num2; 37 | } else if (choice == 4) { 38 | result = num1 / num2; 39 | } else { 40 | System.out.println("Invalid choice!"); 41 | continue; 42 | } 43 | 44 | System.out.println("Result: " + result); 45 | } 46 | 47 | scan.close(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Projects/Temperature_Converter: -------------------------------------------------------------------------------- 1 | package Java_Projects; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Temperature_Converter { 6 | public static void main(String[] args) { 7 | 8 | Scanner scan = new Scanner(System.in); 9 | System.out.println("Please enter a temperature in Fahrenheit or Celsius (e.g. 32F or 20C):"); 10 | String input = scan.nextLine().toUpperCase(); // Convert input to uppercase 11 | 12 | double temperature; 13 | String unit; 14 | 15 | if (input.endsWith("F")){ 16 | // User entered temperature in Fahrenheit 17 | temperature = Double.parseDouble(input.substring(0, input.length() - 1)); 18 | unit = "Fahrenheit"; 19 | 20 | // Convert Fahrenheit to Celsius 21 | temperature = (temperature - 32) * 5 / 9; 22 | unit = "Celsius"; 23 | } else if (input.endsWith("C")) { 24 | // User entered temperature in Celsius 25 | temperature = Double.parseDouble(input.substring(0, input.length() - 1)); 26 | unit = "Celsius"; 27 | 28 | // Convert Celsius to Fahrenheit 29 | temperature = temperature * 9 / 5 + 32; 30 | unit = "Fahrenheit"; 31 | } else { 32 | // Invalid input 33 | System.out.println("Invalid input. Please enter a temperature followed by 'F' or 'C'."); 34 | return; 35 | } 36 | 37 | // Output the converted temperature 38 | System.out.println("The temperature in " + unit + " is " + temperature + "."); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java Projects 2 | 3 | This repository contains a collection of Java projects for a tester who is just starting to work on Java. Each project is designed to demonstrate a specific concept or technique in Java programming. 4 | 5 | There are entry-level projects. 6 | 7 | ## Projects 8 | 9 | ### Project 1: [Temperature Converter](https://github.com/ruesandora/Java/blob/main/Projects/Temperature_Converter) 10 | 11 | A simple temperature converter that allows the user to convert temperatures between Fahrenheit and Celsius. 12 | 13 | ### Project 2: [Calculator](https://github.com/ruesandora/Java/blob/main/Projects/Calculator) 14 | 15 | A basic calculator that can perform arithmetic operations such as addition, subtraction, multiplication, and division. 16 | 17 | ### Project 3: Tic-Tac-Toe 18 | 19 | A simple game of Tic-Tac-Toe that can be played between two human players. 20 | 21 | ## Getting Started 22 | 23 | To get started with these projects, simply clone this repository to your local machine and import the project into your favorite Java IDE. Each project contains its own README.md file with specific instructions on how to run the project. 24 | 25 | ## Usage 26 | 27 | The projects in this repository are designed to be simple and easy to use. Each project has a specific purpose and provides a useful example of a particular Java programming technique or concept. Feel free to use these projects as a starting point for your own Java projects. 28 | 29 | ## Contributing 30 | 31 | If you find a bug in one of the projects or have an idea for a new feature, please submit a pull request or create an issue in the GitHub repository. I am always looking for ways to improve these projects and welcome any contributions or feedback. 32 | 33 | ## Contact 34 | 35 | If you have any questions or feedback about these projects, please feel free to contact me at my email address: [ruesforum@gmail.com](mailto:ruesforum@gmail.com). You can also follow me on Twitter [@ruesandora0](https://twitter.com/ruesandora0) for updates and news about my Java projects. 36 | -------------------------------------------------------------------------------- /TeacherGPT/R01_isEmpty_isBlank: -------------------------------------------------------------------------------- 1 | package TeacherGPT; 2 | 3 | public class R01_isEmpty_isBlank { 4 | 5 | // Define three String variables: 6 | //str1: Empty, containing only "" 7 | //str2: Contain only spaces (" ") 8 | //str3: Let it be a normal String ("Hello") 9 | 10 | // For each String variable, use the .isEmpty() and .isBlank() 11 | // methods to check if it is empty and print the result. 12 | 13 | // The output should look like this: 14 | // is str1 empty? true 15 | // is str2 empty? true 16 | // str3 empty? false 17 | public static void main(String[] args) { 18 | 19 | String str1 = ""; 20 | String str2 = " "; 21 | String str3 = "Hello"; 22 | 23 | System.out.println("Is str1 empty? " + str1.isEmpty()); // true 24 | System.out.println("Is str2 empty? " + str2.isBlank()); // true 25 | System.out.println("Is str3 empty? " +str3.isEmpty()); // false 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /TeacherGPT/R02_null: -------------------------------------------------------------------------------- 1 | package TeacherGPT; 2 | 3 | public class R02_null { 4 | public static void main(String[] args) { 5 | 6 | String str1; 7 | String str2; 8 | 9 | str1 = "Hello"; 10 | str2 = null; 11 | 12 | if (str1 == null){ 13 | System.out.println("Null variable"); 14 | } 15 | 16 | if (str2 == null){ 17 | System.out.println("Null variable"); 18 | } 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /TeacherGPT/R03_replace: -------------------------------------------------------------------------------- 1 | package TeacherGPT; 2 | 3 | public class R03_replace { 4 | public static void main(String[] args) { 5 | // Create a String called text with the value 6 | // "The quick brown fox jumps over the lazy dog." 7 | 8 | // Use .replace() to: 9 | 10 | //Replace "quick" with "slow" 11 | //Replace "fox" with "cat" 12 | //Replace "dog" with "bear" 13 | 14 | String text = "The quick brown fox jumps over the lazy dog"; 15 | 16 | text = text.replace("quick" , "slow"); 17 | text = text.replace("fox" , "cat"); 18 | text = text.replace("dog" , "bear"); 19 | 20 | System.out.println(text); // The slow brown cat jumps over the lazy bear. 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /TeacherGPT/R04_replaceAll: -------------------------------------------------------------------------------- 1 | package TeacherGPT; 2 | 3 | public class R04_replaceAll { 4 | public static void main(String[] args) { 5 | 6 | String text = "There are many colours in the rainbow " + 7 | "like red, green, blue, orange and yellow."; 8 | 9 | text = text.replaceAll("red|green|blue|orange|yellow" , 10 | "colour"); 11 | 12 | System.out.println(text); 13 | } 14 | } 15 | 16 | class R05_replaceAll_01{ 17 | public static void main(String[] args) { 18 | 19 | String text = "The quick brown fox jumps over the lazy dog."; 20 | 21 | text = text.replaceAll("\\s" , "-"); 22 | 23 | System.out.println(text); 24 | } 25 | } 26 | 27 | class R05_replaceAll_02{ 28 | public static void main(String[] args) { 29 | 30 | String str = "Hello World!"; 31 | str = str.replaceAll("o", "x"); 32 | System.out.println(str); 33 | 34 | } 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /TeacherGPT/R05_toUpperCase: -------------------------------------------------------------------------------- 1 | package TeacherGPT; 2 | 3 | import java.util.Scanner; 4 | 5 | public class R07_toUpperCase { 6 | 7 | // Write a Java program that asks the user to enter 8 | // a name and prints it on the screen in uppercase letters. 9 | // You can use the toUpperCase() method. 10 | public static void main(String[] args) { 11 | 12 | Scanner scan = new Scanner(System.in); 13 | System.out.print("Please enter a name: "); 14 | String name = scan.nextLine(); 15 | 16 | System.out.println(name.toUpperCase()); 17 | 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /TeacherGPT/R06_lenght: -------------------------------------------------------------------------------- 1 | package TeacherGPT; 2 | 3 | import java.util.Scanner; 4 | 5 | public class R08_lenght { 6 | 7 | 8 | public static void main(String[] args) { 9 | 10 | // Write a Java program that asks the user to enter a 11 | // sentence and prints the length of the sentence on the screen. 12 | // You can use the length() method. 13 | 14 | Scanner scan = new Scanner(System.in); 15 | System.out.print("Please enter a word: "); 16 | String input = scan.next(); 17 | 18 | System.out.println(input.length()); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /TeacherGPT/R07_substring: -------------------------------------------------------------------------------- 1 | package TeacherGPT; 2 | 3 | import java.util.Scanner; 4 | 5 | public class R09_substring_length { 6 | 7 | // Write a Java program that asks the user to enter a word 8 | // and prints the last character of the word on the screen. 9 | // You can use the substring() and length() methods. 10 | public static void main(String[] args) { 11 | 12 | Scanner scan = new Scanner(System.in); 13 | System.out.print("Please enter a word: "); 14 | String input = scan.next(); 15 | 16 | int length = input.length(); 17 | String lastCharacter = input.substring(length - 1); 18 | 19 | System.out.println("Last character: " + lastCharacter); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /TeacherGPT/R08_substring_indexOf: -------------------------------------------------------------------------------- 1 | package TeacherGPT; 2 | 3 | import java.util.Scanner; 4 | 5 | public class R10_substring_indexOf { 6 | 7 | // Write a Java program that asks the user to enter a 8 | // sentence and prints the first word of the sentence on the screen. 9 | // You can use substring() and indexOf() methods. 10 | public static void main(String[] args) { 11 | Scanner scan = new Scanner(System.in); 12 | System.out.print("Enter a sentence: "); 13 | String input = scan.nextLine(); 14 | 15 | int gapIndex = input.indexOf(" "); 16 | String firstWord = input.substring(0, gapIndex); 17 | 18 | System.out.println("First sentence: " + firstWord); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /TeacherGPT/R09_startsWith: -------------------------------------------------------------------------------- 1 | package TeacherGPT; 2 | 3 | import java.util.Scanner; 4 | 5 | public class R12_startsWith { 6 | public static void main(String[] args) { 7 | 8 | Scanner scan = new Scanner(System.in); 9 | System.out.print("Lütfen bir URL giriniz: "); 10 | String url = scan.next(); 11 | 12 | if (url.startsWith("https://")){ 13 | System.out.println(true); 14 | } else { 15 | System.out.println(false); 16 | } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /TeacherGPT/R10_contains: -------------------------------------------------------------------------------- 1 | package TeacherGPT; 2 | 3 | import java.util.Scanner; 4 | 5 | public class R13_contains { 6 | public static void main(String[] args) { 7 | Scanner scan = new Scanner(System.in); 8 | System.out.println("İnsan neyle yaşar kitabının yazarı kimdir?: "); 9 | String input = scan.nextLine(); 10 | 11 | System.out.println(input.contains("tolstoy")); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /forBegginers/r01_IfStatements: -------------------------------------------------------------------------------- 1 | package day01_IfStatements; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C01_Homework { 6 | public static void main(String[] args) { 7 | // 1- Homework: Ask the user for their age, if they are 65 or older "You can retire" 8 | // make them write it down, otherwise make them write down the number of years they have to work to retire. 9 | 10 | Scanner scan = new Scanner(System.in); 11 | System.out.println("Please enter your age:"); 12 | int age = scan.nextInt(); 13 | 14 | if (age >= 65) System.out.println("You Can Retire"); 15 | else System.out.println("For you to retire " + (65 - age) + " you have to work for another year"); 16 | } 17 | } 18 | 19 | class C01_Homework1 { 20 | public static void main(String[] args) { 21 | // 2- Homework: Ask the user to enter a character, the character entered must be greater than 22 | // write whether there is a letter or not. 23 | 24 | Scanner scan = new Scanner(System.in); 25 | System.out.println("Please enter a character"); 26 | char charecter = scan.next().charAt(0); 27 | 28 | if (charecter >= 'A' && charecter <= 'Z') { 29 | System.out.println("The character entered is uppercase."); 30 | } else { 31 | System.out.println("The character entered is not uppercase."); 32 | } 33 | } 34 | } 35 | 36 | class C01_Homework2{ 37 | public static void main(String[] args) { 38 | // 3- Prompt the user for a letter, if the character entered is a lowercase letter, you can change it to uppercase. 39 | // Make it type, otherwise make it type the entered letter. 40 | 41 | Scanner scan = new Scanner(System.in); 42 | System.out.println("Please enter a letter"); 43 | char karakter = scan.next().charAt(0); 44 | 45 | if (Character.isLowerCase(karakter)){ 46 | char buyukHarf = Character.toUpperCase(karakter); 47 | System.out.println("Capitalization of the entered character: " + buyukHarf); 48 | } else { 49 | System.out.println("Entered character: " + karakter); 50 | } 51 | 52 | } 53 | } 54 | 55 | class C01_Homework3 { 56 | public static void main(String[] args) { 57 | // AA if the student's grade is 85 and above, 58 | // BB if 65 and above, 59 | // CC if 50 and above 60 | // Remaining DD 61 | 62 | Scanner scan = new Scanner(System.in); 63 | System.out.println("Please enter your exam grade"); 64 | double grade = scan.nextDouble(); 65 | 66 | if (grade > 100 || grade < 0){ 67 | System.out.println("Invalid Grade"); 68 | } else if (grade >= 85) { 69 | System.out.println("Pretty Good: AA"); 70 | } else if (grade >= 65) { 71 | System.out.println("Good: BB"); 72 | } else if (grade >= 50) { 73 | System.out.println("Middle: CC"); 74 | } else { 75 | System.out.println("Bad: DD"); 76 | } 77 | } 78 | } 79 | 80 | class C01_Homework4 { 81 | public static void main(String[] args) { 82 | // Request the user's weight (kg) and height (cm) 83 | // calculate body mass index (cbm) (weight*10000 / (height * height)) 84 | // obese if the body mass index is greater than 30, overweight if it is between 25-30, 85 | // If between 20-25, write normal, if less than 20, write weak. 86 | 87 | Scanner scan = new Scanner(System.in); 88 | System.out.println("Please enter your weight"); 89 | double weight = scan.nextDouble(); 90 | System.out.println("Please enter your height in CM"); 91 | double height = scan.nextDouble(); 92 | double cbm = weight*10000 / (height * height); 93 | 94 | if (cbm >= 30){ 95 | System.out.println("Obese"); 96 | } else if (cbm >= 25) { 97 | System.out.println("Overweight"); 98 | } else if (cbm >= 20) { 99 | System.out.println("Normal"); 100 | } else { 101 | System.out.println("Weak"); 102 | } 103 | 104 | System.out.println("Your Body Mass Index: " + cbm); 105 | } 106 | } 107 | 108 | class C01_Homework5 { 109 | public static void main(String[] args) { 110 | 111 | // Homework: Get a note entry from the user and 112 | // determine the letter grade according to the following rules and print it on the screen: 113 | //90 and above: AA 114 | //80 to 89: BA 115 | //70 to 79: BB 116 | //60 to 69: CB 117 | //50 to 59: CC 118 | //40 to 49: DC 119 | //30 to 39: DD 120 | //0 to 29: FF 121 | 122 | Scanner scan = new Scanner(System.in); 123 | System.out.println("Please enter your grade"); 124 | double grade = scan.nextDouble(); 125 | 126 | if (grade >= 90){ 127 | System.out.println("Perfect: AA"); 128 | } else if (grade >= 80) { 129 | System.out.println("Pretty good: BA"); 130 | } else if (grade >= 70) { 131 | System.out.println("good: BD"); 132 | } else if (grade >= 60) { 133 | System.out.println("Cumhurbaskanı: CB"); 134 | } else if (grade >= 50) { 135 | System.out.println("M'ddle: CC" ); 136 | } else if (grade >= 40) { 137 | System.out.println("You have to work harder: DC"); 138 | } else if (grade >= 30) { 139 | System.out.println("Bad: DD"); 140 | } else { 141 | System.out.println("You failed: FF"); 142 | } 143 | 144 | } 145 | } 146 | 147 | class C01_Homework6 { 148 | public static void main(String[] args) { 149 | // Homework: Get an integer input from the user and 150 | // Calculate the factorial of the number according to the following rules and print it on the screen: 151 | 152 | // If the number is negative, give the message "Invalid input!". 153 | // If the number is zero, accept its factorial as 1. 154 | // If the number is positive, calculate the factorial and print it to the screen. 155 | 156 | Scanner scan = new Scanner(System.in); 157 | System.out.println("Please enter an integer"); 158 | int num = scan.nextInt(); 159 | 160 | if (num < 0) { 161 | System.out.println("Invalid login!"); 162 | } else { 163 | int factorial = 1; 164 | for (int i = 1; i <= num; i++) { 165 | factorial *= i; 166 | } 167 | System.out.println(num + " factorial of the number: " + factorial); 168 | } 169 | } 170 | 171 | } 172 | -------------------------------------------------------------------------------- /forBegginers/r02_nested_IfStatements: -------------------------------------------------------------------------------- 1 | package day08_Nested_If_Else; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C05_IfElseIfStatements { 6 | public static void main(String[] args) { 7 | 8 | // Ask the user for an integer. 9 | // If the number is divisible by 3, a multiple of 3 10 | // If the number is divided by 5, a multiple of 5 11 | // supernumerary if divided by both 12 | // if not divisible by both, write a naughty number 13 | 14 | Scanner scan = new Scanner(System.in); 15 | System.out.println("Please enter an integer"); 16 | int num = scan.nextInt(); 17 | 18 | if (num % 5 == 0 && num % 3 == 0){ 19 | System.out.println("Super Number"); 20 | } else if (num % 5 == 0){ 21 | System.out.println("Multiple of 5"); 22 | } else if (num % 3 == 0) { 23 | System.out.println("Multiple of 3"); 24 | } else { 25 | System.out.println("Naughty Number"); 26 | } 27 | } 28 | } 29 | 30 | class C05_IfElseIfStatements1 { 31 | public static void main(String[] args) { 32 | // Get the number of items purchased from the user and the list price, 33 | // ask the user if they have a customer card. 34 | // If they have a customer card, 20% if they buy more than 10 items, 35 | // if not available, 15% discount, 15% if the customer buys more than 10 items without a card, 36 | // if not available, give 10% discount 37 | 38 | Scanner scanner = new Scanner(System.in); 39 | System.out.println("Please enter your product quantity"); 40 | int product = scanner.nextInt(); 41 | System.out.println("Please enter the price of your products"); 42 | double price = scanner.nextDouble(); 43 | System.out.println("Do you have a customer card? Y: Yes, N: No" ); 44 | char card = scanner.next().toUpperCase().charAt(0); 45 | 46 | if (card == 'Y' && product >= 10){ 47 | double cardDiscounted = price - price * 0.2; 48 | System.out.println("20% discounted amount you have to pay: " + cardDiscounted); 49 | } else if (card == 'Y' && product < 10) { 50 | double cardDiscounted = price - price * 0.15; 51 | System.out.println("15% discounted amount you have to pay: " + cardDiscounted); 52 | } else if (card == 'N' && product >= 10) { 53 | double cardDiscounted = price - price * 0.15; 54 | System.out.println("15% discounted amount you have to pay: " + cardDiscounted); 55 | } else if (card == 'N' && product < 10) { 56 | double cardDiscounted = price - price * 0.10; 57 | System.out.println("10% discounted amount you have to pay: " + cardDiscounted); 58 | } else { 59 | System.out.println("Invalid option"); 60 | } 61 | 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /forBegginers/r03_nested_IfStatements_01: -------------------------------------------------------------------------------- 1 | package day08_Nested_If_Else; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C01_Odev { 6 | public static void main(String[] args) { 7 | 8 | // Assignment: You need to write a Java program that asks a user to enter his/her grade. 9 | // The program should give feedback to the user about his/her grade based on the following criteria: 10 | // If the grade is 90 or higher, print the message "Great job! Your grade is A". 11 | // If the grade is in the range 80-89, print the message "Good job! Your grade is B". 12 | // If the grade is between 70-79, print the message "Average job! Your grade is C". 13 | // If the grade is between 60-69, print the message "You need to work more! Your grade is D". 14 | // If the grade is less than 60, print the message "Sorry, you failed. Your grade is F". 15 | // Create the program and get a value from the user for grade input. 16 | // Then print the appropriate feedback based on the criteria above. 17 | 18 | Scanner scan = new Scanner(System.in); 19 | System.out.println("Please enter your grade"); 20 | double grade = scan.nextDouble(); 21 | 22 | if (grade > 100){ 23 | System.out.println("Invalid grade"); 24 | } else if (grade > 90){ 25 | System.out.println("Great job! Your grade is A"); 26 | } else if (grade > 80) { 27 | System.out.println("Good job! Your grade is B"); 28 | } else if (grade > 70) { 29 | System.out.println("Average job! Your grade is C"); 30 | } else if (grade > 60) { 31 | System.out.println("You have to study more! Your grade is D"); 32 | } else { 33 | System.out.println("I'm sorry, you failed. Your grade is F"); 34 | } 35 | } 36 | } 37 | 38 | class C03_Odev3 { 39 | public static void main(String[] args) { 40 | 41 | // Write a program that checks whether a number is positive, negative or zero. 42 | // Your program asks the user to enter a number. 43 | // Then check whether the entered number is positive, negative or zero and print the result on the screen. 44 | // Hint: Assign the number received from the user to a variable. 45 | // Then use the if-else if-else construct to check whether the number is positive, negative or zero and print the result on the screen. 46 | 47 | Scanner scan = new Scanner(System.in); 48 | System.out.println("Please enter a number"); 49 | double num = scan.nextDouble(); 50 | 51 | if (num == 0){ 52 | System.out.println("Your entered number is zero"); 53 | } else if (num > 0) { 54 | System.out.println("Your entered number is positive"); 55 | } else { 56 | System.out.println("Your entered number is negative"); 57 | } 58 | 59 | } 60 | } 61 | 62 | class C04_Odev4 { 63 | public static void main(String[] args) { 64 | 65 | // Homework: You need to write a program to take food orders in a restaurant. 66 | // The program will get the price of the food the user wants to order and the payment method. 67 | // Then, it will apply a discount based on the payment method and print the total amount on the screen. 68 | 69 | // Rules: 70 | 71 | // A 10% discount will be applied when paying by credit card. 72 | // A 5% discount will be applied for cash payment. 73 | // No discount will be applied for other payment methods. 74 | // You can follow the steps below while writing your program: 75 | 76 | // Get the meal price and payment method from the user. 77 | // Using the nested if-else structure, calculate the discount amount according to the payment method and determine the total payment amount. 78 | // Print the total payment amount on the screen. 79 | 80 | Scanner scan = new Scanner(System.in); 81 | 82 | System.out.println("Please enter the price of the meal"); 83 | double price = scan.nextDouble(); 84 | 85 | scan.nextLine(); 86 | System.out.println("Please enter your payment method (Cash or Credit Card)"); 87 | String payment = scan.nextLine(); 88 | 89 | 90 | if (price < 0){ 91 | System.out.println("Invalid Payment Amount"); 92 | } else if (!(payment.equalsIgnoreCase("Cash") || oayment.equalsIgnoreCase("Credit Card"))){ 93 | System.out.println("Invalid Payment Amount"); 94 | } else if (payment.equalsIgnoreCase("Cash")){ 95 | double discountAmount = price * 0.05; 96 | double totalPayment = price - discountAmount; 97 | System.out.println("Food price: " + price + "\nPayment Method: Cash" + "\nTotal Payment Amount (5% Discount Applied): " + totalPayment); 98 | } else if (payment.equalsIgnoreCase("Credit Card")){ 99 | double discountAmount = price * 0.1; 100 | double totalPayment = price - discountAmount; 101 | System.out.println("Price of food: " + price + "\nPayment Method: Credit Card" + "\nTotal Payment Amount (10% Discount Applied): " + toplamOdeme); 102 | } 103 | } 104 | } 105 | 106 | class C04_Odev5 { 107 | public static void main(String[] args) { 108 | 109 | // Homework: Get a decimal input from the user. 110 | // Then print the result on the screen by following the steps below: 111 | // Convert the number to an integer (casting) and assign it to a new variable. 112 | // Increase the integer value by 1. 113 | // Concatenate the incremented value with some text. 114 | // If the incremented value is greater than or equal to 10, 115 | // Print the message "The result is greater than or equal to 10". 116 | // Otherwise, print the message "The result is less than 10". 117 | 118 | Scanner scan = new Scanner(System.in); 119 | System.out.println("Please enter a decimal number"); 120 | double num = scan.nextDouble(); 121 | int num1 = (int) num; 122 | num1++; 123 | 124 | if (num1 >= 10){ 125 | System.out.println("The result is greater than or equal to 10"); 126 | } else { 127 | System.out.println("The result is less than 10"); 128 | } 129 | } 130 | } 131 | 132 | class C05_Odev6 { 133 | public static void main(String[] args) { 134 | 135 | // take a number from the user and divide it by 2 136 | // We can write a program that calculates the remainder. 137 | // If the remainder is 0, we can print "The number is even", otherwise we can print "The number is odd". 138 | 139 | Scanner scan = new Scanner(System.in); 140 | System.out.println("Please enter a number"); 141 | double num = scan.nextDouble(); 142 | 143 | if (num % 2 == 0){ 144 | System.out.println("Number is Even"); 145 | } else { 146 | System.out.println("Number is odd"); 147 | } 148 | } 149 | } 150 | 151 | class C06_Odev7 { 152 | public static void main(String[] args) { 153 | 154 | // Add two integers and store the result in a variable named "sum" 155 | // increments "sum" by 10 156 | // concatenate "sum" with a string of your choice and store the result in a variable called "output" 157 | // Check if the "total" value is greater than or equal to 50. 158 | // If it is, print the "output" value in upper case. 159 | // If not, print the "output" value in lower case. 160 | 161 | Scanner scan = new Scanner(System.in); 162 | System.out.println("Enter the first integer"); 163 | int num1 = scan.nextInt(); 164 | System.out.println("Enter the second iteger"); 165 | int num2 = scan.nextInt(); 166 | 167 | int sum = num1 + num2; 168 | sum += 10; 169 | String output = sum + " is the sum plus 10"; 170 | 171 | if (sum >= 50){ 172 | System.out.println(output.toUpperCase()); 173 | } else { 174 | System.out.println(output.toLowerCase()); 175 | } 176 | 177 | 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /forBegginers/r04_TernaryOperator: -------------------------------------------------------------------------------- 1 | package day09_ternaryOperator; 2 | 3 | import java.util.Scanner; 4 | import java.util.SortedMap; 5 | 6 | public class C01_Ternary { 7 | public static void main(String[] args) { 8 | // Prompt the user for a letter, the character entered 9 | // If it is a lowercase letter, change it to uppercase 10 | // print, otherwise print the entered letter. 11 | 12 | // A letter from the user 13 | Scanner scan = new Scanner(System.in); 14 | System.out.println("Please entar a letter"); 15 | char letter = scan.next().charAt(0); 16 | System.out.println((letter >= 'a' && letter <= 'z' ? Character.toUpperCase(letter) : letter)); 17 | } 18 | } 19 | 20 | class C01_Ternary3 { 21 | public static void main(String[] args) { 22 | 23 | // Get the user's grade If 50 or higher "Passed the class", if less than 50 24 | // Make it say "I'm afraid you failed." 25 | 26 | Scanner scan = new Scanner(System.in); 27 | System.out.println("Please enter your grade"); 28 | double num = scan.nextDouble(); 29 | 30 | System.out.println(num >= 50 ? "You passed the class" : "Unfortunately, you failed the class."); 31 | } 32 | } 33 | 34 | class C02_Ternary1 { 35 | public static void main(String[] args) { 36 | int a = 10; 37 | int b = 20; 38 | 39 | System.out.println(b < a ? b > 0 ? b + a : b - a : a < 10 ? a * 5 : b/a); 40 | 41 | int x = 10; 42 | String result = x > 0 ? "Positive" : x == 0 ? "Zero" : "Negative"; 43 | System.out.println(result); 44 | } 45 | } 46 | 47 | class C02_Ternary2 { 48 | public static void main(String[] args) { 49 | Scanner scan = new Scanner(System.in); 50 | System.out.println("Please enter first number."); 51 | double num = scan.nextDouble(); 52 | System.out.println("Please enter second number"); 53 | double num1 = scan.nextDouble(); 54 | 55 | System.out.println(num > num1 ? "The first number is greater than the second number" : 56 | num < num1 ? "The first number is less that the second number" : "The first number is equal to the second number" 57 | ); 58 | } 59 | } 60 | 61 | class C02_Ternary4 { 62 | public static void main(String[] args) { 63 | // Get a positive integer from the user 64 | // print whether the number is odd or even 65 | 66 | Scanner scanner = new Scanner(System.in); 67 | System.out.println("Please enter a positive integer"); 68 | int num = scanner.nextInt(); 69 | 70 | System.out.println(num % 2 == 0 ? "The number entered is an even number" : "The entered number is an odd number"); 71 | } 72 | } 73 | 74 | class C02_Ternary5{ 75 | public static void main(String[] args) { 76 | // "even number" if the given input is an even number, 77 | // if input is an odd number, write a ternary that returns 2 * input. 78 | 79 | Scanner scanner = new Scanner(System.in); 80 | System.out.println("Please enter a number"); 81 | int input = scanner.nextInt(); 82 | 83 | System.out.println(input %2 == 0 ? "Even Number" : input * 2); 84 | 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /forBegginers/r05_SwitchStatements: -------------------------------------------------------------------------------- 1 | package day10_switchStatements; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C01_Switch { 6 | public static void main(String[] args) { 7 | // Get the month number from the user and write the month. 8 | 9 | Scanner scan = new Scanner(System.in); 10 | System.out.println("Please enter the month number:"); 11 | int month = scan.nextInt(); 12 | 13 | switch (month){ 14 | case 1: 15 | System.out.println("January"); 16 | break; 17 | case 2: 18 | System.out.println("February"); 19 | break; 20 | case 3: 21 | System.out.println("March"); 22 | break; 23 | case 4: 24 | System.out.println("April"); 25 | break; 26 | case 5: 27 | System.out.println("May"); 28 | break; 29 | case 6: 30 | System.out.println("June"); 31 | break; 32 | case 7: 33 | System.out.println("July"); 34 | break; 35 | case 8: 36 | System.out.println("August"); 37 | break; 38 | case 9: 39 | System.out.println("September"); 40 | break; 41 | case 10: 42 | System.out.println("October"); 43 | break; 44 | case 11: 45 | System.out.println("November"); 46 | break; 47 | case 12: 48 | System.out.println("December"); 49 | break; 50 | default: 51 | System.out.println("Invalid number"); 52 | } 53 | } 54 | } 55 | 56 | class C02_Switch_01 { 57 | public static void main(String[] args) { 58 | // Get the month number from the user and type the season. 59 | 60 | Scanner scan = new Scanner(System.in); 61 | System.out.println("Please enter the month number"); 62 | int monthNumber = scan.nextInt(); 63 | 64 | switch (monthNumber){ 65 | case 12: 66 | case 1: 67 | case 2: 68 | System.out.println("Winter"); 69 | break; 70 | case 3: 71 | case 4: 72 | case 5: 73 | System.out.println("First Spring"); 74 | break; 75 | case 6: 76 | case 7: 77 | case 8: 78 | System.out.println("Summer"); 79 | break; 80 | case 9: 81 | case 10: 82 | case 11: 83 | System.out.println("Last Spring"); 84 | break; 85 | default: 86 | System.out.println("Invalid Number"); 87 | } 88 | } 89 | } 90 | 91 | class C03_Switch_02 { 92 | public static void main(String[] args) { 93 | // Write a Java program that prompts the user to enter a number 94 | // uses a switch statement to determine whether the number corresponds 95 | // to a weekday or a weekend day. If the number corresponds to a weekday, 96 | // the program should print "It's a weekday". If the number corresponds to 97 | // a weekend day, the program should print "It's a weekend day". 98 | // If the number is not valid (i.e., is less than 1 or greater than 7), 99 | // the program should print "Invalid day". 100 | 101 | Scanner scanner = new Scanner(System.in); 102 | System.out.println("Enter a number (1-7"); 103 | int day = scanner.nextInt(); 104 | 105 | switch (day){ 106 | case 1: 107 | case 2: 108 | case 3: 109 | case 4: 110 | case 5: 111 | System.out.println("It's a weekday"); 112 | break; 113 | case 6: 114 | case 7: 115 | System.out.println("It's a weekend day"); 116 | break; 117 | default: 118 | System.out.println("Invalid day"); 119 | } 120 | } 121 | } 122 | 123 | class C04_Switch_03 { 124 | public static void main(String[] args) { 125 | Scanner scan = new Scanner(System.in); 126 | System.out.println("Enter a letter grade (A, B, C, D, or F):"); 127 | String letterGrade = scan.next().toUpperCase(); 128 | 129 | switch (letterGrade){ 130 | case "A": 131 | System.out.println("4.0"); 132 | break; 133 | case "B": 134 | System.out.println("3.0"); 135 | break; 136 | case "C": 137 | System.out.println("2.0"); 138 | break; 139 | case "D": 140 | System.out.println("1.0"); 141 | break; 142 | case "F": 143 | System.out.println("0.0"); 144 | break; 145 | default: 146 | System.out.println("Invalid letter grade. Please try again."); 147 | } 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /forBegginers/r06_Methods: -------------------------------------------------------------------------------- 1 | package day11_StringManipulations; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C02_Methods { 6 | public static void main(String[] args) { 7 | 8 | // Write a Java program and ask the user to enter a word. 9 | // Then convert the word the user enters into both uppercase 10 | // and lowercase letters and print these two new words on the screen. 11 | // Then ask the user to enter another word and compare it with the first word. 12 | // Check if the two words are equal with no difference in case and print 13 | // the result on the screen. 14 | 15 | Scanner scan = new Scanner(System.in); 16 | System.out.println("Please enter a word"); 17 | String word = scan.next(); 18 | 19 | System.out.println("The output of your entered word in upper and lower case: " + 20 | word.toLowerCase() + " " + word.toUpperCase()); 21 | 22 | System.out.println("Please enter another word"); 23 | String word2 = scan.next(); 24 | 25 | System.out.println(word2.equals(word)); 26 | } 27 | } 28 | 29 | class C02_Methods2 { 30 | public static void main(String[] args) { 31 | // Write a Java program and ask the user to enter two words. 32 | // Check if the two words are equal regardless of case and 33 | // print the result on the screen. 34 | // 35 | // In this assignment, you need to check for case-independent 36 | // equality between two String objects using the equalsIgnoreCase() 37 | // or equalsIgnoreCase() methods. 38 | 39 | Scanner scan = new Scanner(System.in); 40 | System.out.println("Please enter two word"); 41 | System.out.println("Your first word:"); 42 | String word1 = scan.next(); 43 | System.out.println("Your second word:"); 44 | String word2 = scan.next(); 45 | 46 | if (word2.equalsIgnoreCase(word1)){ 47 | System.out.println("Your two words are the same"); 48 | } else System.out.println("Your two words are not the same"); 49 | 50 | } 51 | } 52 | 53 | 54 | -------------------------------------------------------------------------------- /forBegginers/r07_Methods_01: -------------------------------------------------------------------------------- 1 | package day11_StringManipulations; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C03_length { 6 | public static void main(String[] args) { 7 | Scanner scan = new Scanner(System.in); 8 | System.out.print("Please enter your name: "); 9 | String name = scan.nextLine(); 10 | System.out.print("The letters in the middle of your name: " + 11 | name.charAt(name.length()/2 - 1) + name.charAt(name.length() / 2)); 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /forBegginers/r08_Methods_02: -------------------------------------------------------------------------------- 1 | package day11_StringManipulations; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C04_methods { 6 | public static void main(String[] args) { 7 | // receive an email from the user - "invalid mail" if the mail does not contain @ 8 | // - if mail does not contain @gmail.com, "mail must be gmail"- 9 | // if the mail does not end with @gmail.com, make it say "there is a typo in the mail". 10 | 11 | Scanner scan = new Scanner(System.in); 12 | System.out.print("Please enter your mailing address: "); 13 | String mail = scan.nextLine(); 14 | 15 | if (!mail.contains("@")){ 16 | System.out.print("Invalid mail"); 17 | } else if (!mail.contains("@gmail.com")) { 18 | System.out.print("Mail must be gmail); 19 | } else if (!mail.endsWith("@gmail.com")){ 20 | System.out.print("There is a typo in the mail"); 21 | } else System.out.println("Your mail was received successfully."); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /forBegginers/r09_lastIndexOf: -------------------------------------------------------------------------------- 1 | package day11_StringManipulations; 2 | 3 | public class C06_lastIndexOf { 4 | public static void main(String[] args) { 5 | 6 | String str = "We will learn Java, there is no other way"; 7 | 8 | // Print the first occurrence index of a letter 9 | System.out.println(str.indexOf("a")); // 1 10 | 11 | // Print the last occurrence index of a letter 12 | System.out.println(str.lastIndexOf("a")); // 24 13 | 14 | // Print the first index of J 15 | System.out.println(str.indexOf("J")); // 0 16 | 17 | // Print the last index of J 18 | System.out.println(str.lastIndexOf("J")); // 0 19 | 20 | // Check if the e letter is used 2 or more times without looking at the text 21 | 22 | int firstIndex = str.indexOf("e"); 23 | int lastIndex = str.lastIndexOf("e"); 24 | 25 | if (firstIndex == -1){ 26 | System.out.println("No e letter is used"); 27 | } else if (firstIndex == lastIndex) { 28 | System.out.println("E letter is used only 1 time"); 29 | } else { 30 | System.out.println("E letter is used more than once."); 31 | } 32 | 33 | // Print the last index of o 34 | System.out.println(str.lastIndexOf("o")); // 32 35 | 36 | // Print the index of previous occurrence of o before its last use 37 | System.out.println(str.lastIndexOf("o" , 31)); // 27 38 | } 39 | -------------------------------------------------------------------------------- /forBegginers/r10_isEmpty_isBlank: -------------------------------------------------------------------------------- 1 | package day11_StringManipulations; 2 | 3 | public class C07_isEmpty_isBlank { 4 | public static void main(String[] args) { 5 | 6 | String str1 = ""; 7 | String str2 = " "; 8 | String str3 = "."; 9 | 10 | System.out.println(str1.isEmpty()); // true 11 | System.out.println(str2.isEmpty()); // false 12 | 13 | System.out.println(str1.isBlank()); // true 14 | System.out.println(str2.isBlank()); // true 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /forBegginers/r11_forLoop: -------------------------------------------------------------------------------- 1 | package day13_stringManipulations_forLoop; 2 | 3 | public class C05_forLoop { 4 | public static void main(String[] args) { 5 | // Print 10 consecutive integers starting from the user's input value 6 | int input = 8; 7 | for (int i = input; i < input+10 ; i++) { 8 | System.out.print(i + " "); 9 | } 10 | System.out.println(""); 11 | 12 | // Start from the user's input value, 13 | // and print integers up to 100 (including 100) in increments of 5 14 | for (int i = input; i <= 100 ; i+=5) { 15 | System.out.print(i + " "); 16 | } 17 | System.out.println(""); 18 | 19 | // Start from 100 and go backwards to 1 20 | // Print the numbers that are multiples of 7 21 | for (int i = 100; i >= 1 ; i--) { 22 | if (i % 7 == 0){ 23 | System.out.print(i + " "); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /forBegginers/r12_forLoopNotes: -------------------------------------------------------------------------------- 1 | package day13_stringManipulations_forLoop; 2 | public class C06_ForLoopNotes { 3 | public static void main(String[] args) { 4 | // If the starting value we give does not meet the end condition, 5 | // the for loop will run, BUT the for loop body will NOT be executed 6 | for (int i = 0; i >10 ; i++) { 7 | System.out.println(i); 8 | } 9 | 10 | // If the starting value and the way it changes never reaches the end value, 11 | // an infinite loop is created 12 | /* 13 | for (int i = 0; i >=0 ; i++) { 14 | System.out.print(i +" "); 15 | } 16 | */ 17 | 18 | // If there is a number that can be evenly divided by the desired number 19 | // between the starting and ending values given by the user (including these values) 20 | // print "there is a number that can be evenly divided by the desired number" 21 | int start = 201; 22 | int end = 237; 23 | int desiredNumber = 43; 24 | for (int i = start; i <= end ; i++) { 25 | System.out.println(i); 26 | if (i % desiredNumber == 0){ 27 | System.out.println("there is a number that can be evenly divided by the desired number"); 28 | break; 29 | } 30 | } 31 | /* 32 | While a loop is running, 33 | if we achieve what we want at a certain value 34 | and we don't need the rest of the loop anymore 35 | we can use break there 36 | When Java sees the break; command, it stops running the loop 37 | */ 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /forBegginers/r13_forLoop: -------------------------------------------------------------------------------- 1 | package day14_forLoops; 2 | 3 | import java.util.Scanner; 4 | public class C02_forLoop { 5 | public static void main(String[] args) { 6 | // Get positive integers from the user as starting and ending values 7 | // Print the sum of all numbers between them, including the limits. 8 | // If the ending value is less than the starting value, print a warning and terminate the operation. 9 | 10 | Scanner scanner = new Scanner(System.in); 11 | System.out.println("Please enter a positive integer for the starting value:"); 12 | int start = scanner.nextInt(); 13 | System.out.println("Please enter a positive integer for the ending value:"); 14 | int end = scanner.nextInt(); 15 | 16 | if (end < start){ 17 | System.out.println("The starting value cannot be greater than the ending value."); 18 | } else { 19 | int sum = 0; 20 | for (int i = start; i <= end ; i++) { 21 | // sum = sum + i; 22 | sum += i; 23 | } 24 | System.out.println("The sum of the integers between the limits: " + sum); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /forBegginers/r14_forLoop: -------------------------------------------------------------------------------- 1 | package day14_forLoops; 2 | import java.util.Scanner; 3 | public class C03_ForLoop { 4 | public static void main(String[] args) { 5 | // Get positive integers from the user as starting and ending values 6 | // Print the sum of all numbers between them, including the limits. 7 | // Even if the ending value is less than the starting value, the program should still run. 8 | 9 | Scanner scanner = new Scanner(System.in); 10 | System.out.println("Please enter a positive integer for the starting value:"); 11 | int start = scanner.nextInt(); 12 | System.out.println("Please enter a positive integer for the ending value:"); 13 | int end = scanner.nextInt(); 14 | 15 | int sum = 0; 16 | if (start < end){ 17 | for (int i = start; i <= end ; i++) { 18 | sum += i; 19 | } 20 | } else { 21 | for (int i = end; i <= start ; i++) { 22 | sum += i ; 23 | } 24 | } 25 | System.out.println("The sum of the integers between the limits: " + sum); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /forBegginers/r15_forLoop: -------------------------------------------------------------------------------- 1 | package day14_forLoops; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C01_SifreKontrolu { 6 | public static void main(String[] args) { 7 | // Get a password from the user and check the following conditions: 8 | // - The first character should be a lowercase letter. 9 | // - The last character should be a number. 10 | // - The password should not contain any spaces. 11 | // - The length of the password should be at least 10 characters. 12 | // If the password meets all the requirements, print "Password successfully saved." 13 | // Use the flag method. Set the flag to true at the beginning and assign false value in every invalid condition. 14 | 15 | boolean flag = true; 16 | Scanner scanner = new Scanner(System.in); 17 | System.out.println("Please enter your password:"); 18 | String password = scanner.nextLine(); 19 | 20 | // Check if the first character is a lowercase letter. 21 | char firstChar = password.charAt(0); 22 | if (!(firstChar >= 'a' && firstChar <= 'z')){ 23 | System.out.println("The first character should be a lowercase letter."); 24 | flag = false; 25 | } 26 | 27 | // Check if the last character is a number. 28 | char lastChar = password.charAt(password.length()-1); 29 | if (!(lastChar >= '0' && lastChar <= '9')){ 30 | System.out.println("The last character should be a number."); 31 | flag = false; 32 | } 33 | 34 | // Check if the password contains any spaces. 35 | if (password.contains(" ")){ 36 | System.out.println("The password should not contain any spaces."); 37 | flag = false; 38 | } 39 | 40 | // Check if the length of the password is at least 10 characters. 41 | if (!(password.length() >= 10)){ 42 | System.out.println("The length of the password should be at least 10 characters."); 43 | flag = false; 44 | } 45 | 46 | // If the flag is still true, print "Password successfully saved." 47 | if (flag){ 48 | System.out.println("Password successfully saved."); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /forBegginers/r16_factorial: -------------------------------------------------------------------------------- 1 | package day14_forLoops; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C04_Faktoryel { 6 | public static void main(String[] args) { 7 | // Get a number less than 20 from the user and calculate its factorial. 8 | // Example: 6! = 6 * 5 * 4 * 3 * 2 * 1 = 720 9 | 10 | Scanner scanner = new Scanner(System.in); 11 | System.out.println("Please enter a positive integer less than 20:"); 12 | int number = scanner.nextInt(); 13 | int product = 1; 14 | 15 | for (int i = 1; i <= number ; i++) { 16 | product *= i ; 17 | } 18 | 19 | System.out.println(number + "! = " + product); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /forBegginers/r17_sum_of_figures: -------------------------------------------------------------------------------- 1 | package day14_forLoops; 2 | import java.util.Scanner; 3 | public class C05_RakamlarToplami { 4 | public static void main(String[] args) { 5 | // Get a positive integer from the user and print the sum of its digits. 6 | 7 | Scanner scanner = new Scanner(System.in); 8 | System.out.println("Please enter a positive integer:"); 9 | int enteredNumber = scanner.nextInt(); 10 | int number = enteredNumber; 11 | int onesPlace = 0; 12 | int sumOfDigits = 0; 13 | int numberOfDigits = (number+"").length(); 14 | 15 | for (int i = 1; i <= numberOfDigits ; i++) { 16 | onesPlace = number % 10; 17 | sumOfDigits += onesPlace; 18 | number /= 10; 19 | } 20 | 21 | System.out.println("The sum of the digits of the " + enteredNumber +" is : " + sumOfDigits); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /forBegginers/r18_FizzBuzz: -------------------------------------------------------------------------------- 1 | package day14_forLoops; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C06_FizzBuzz { 6 | public static void main(String[] args) { 7 | // Take a positive integer from the user, 8 | // print all integers from 1 to the given number, and 9 | // - if a number is divisible by 3, print "fizz" instead of the number 10 | // - if a number is divisible by 5, print "buzz" instead of the number 11 | // - if a number is divisible by both 3 and 5, print "fizzBuzz" instead of the number 12 | Scanner scanner = new Scanner(System.in); 13 | System.out.println("Please enter a positive integer:"); 14 | int number = scanner.nextInt(); 15 | for (int i = 1; i <= number; i++) { 16 | if (i % 3 == 0 && i % 5 == 0) { 17 | System.out.print("fizzBuzz\n"); 18 | } else if (i % 3 == 0) { 19 | System.out.print("fizz "); 20 | } else if (i % 5 == 0) { 21 | System.out.print("buzz "); 22 | } else { 23 | System.out.print(i + " "); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /forBegginers/r19_StringReversal: -------------------------------------------------------------------------------- 1 | package day14_forLoops; 2 | import java.util.Scanner; 3 | 4 | public class C07_StringReversal { 5 | public static void main(String[] args) { 6 | Scanner scanner = new Scanner(System.in); 7 | System.out.println("Please enter the text you want to reverse:"); 8 | String str = scanner.nextLine(); 9 | // Java is Awesome 10 | // emosewA si avaJ 11 | // loop through the characters of the string in reverse order and print them 12 | for (int i = str.length()-1; i >=0 ; i--) { 13 | System.out.print(str.charAt(i)); 14 | } 15 | System.out.println(""); 16 | // store the reversed string in a new variable 17 | String reversedStr = ""; 18 | for (int i = str.length()-1; i >=0 ; i--) { 19 | reversedStr += str.charAt(i); 20 | } 21 | System.out.println("Reversed string: " + reversedStr); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /forBegginers/r20_nestedForLoop: -------------------------------------------------------------------------------- 1 | package day15_nestedForLoop_MethodOlusturma; 2 | 3 | public class C01_NestedForLoop { 4 | public static void main(String[] args) { 5 | /* 6 | Print the following table on the console: 7 | 1 2 3 4 8 | 2 4 6 8 9 | 3 6 9 12 10 | 11 | If the shape to be printed is rectangular, we can use nested for loops to create it. 12 | */ 13 | for (int i = 1; i <=4 ; i++) { 14 | System.out.print(i*1 + " "); 15 | } 16 | System.out.println(""); 17 | for (int i = 1; i <=4 ; i++) { 18 | System.out.print(i*2 + " "); 19 | } 20 | System.out.println(""); 21 | for (int i = 1; i <=4 ; i++) { 22 | System.out.print(i*3 + " "); 23 | } 24 | System.out.println("=================="); 25 | for (int j = 1; j <=3 ; j++) { 26 | for (int i = 1; i <=4 ; i++) { 27 | System.out.print(i*j +" "); 28 | } 29 | System.out.println(""); 30 | } 31 | -------------------------------------------------------------------------------- /forBegginers/r21_nestedForLoop: -------------------------------------------------------------------------------- 1 | package day15_nestedForLoop_MethodOlusturma; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C02_NestedForLoop { 6 | public static void main(String[] args) { 7 | /* 8 | Take two integers from the user: the first one is the number of rows and 9 | the second one is the number of columns. Then draw the following shape: 10 | 11 | * * * * * 12 | * * * * * 13 | * * * * * 14 | * * * * * 15 | */ 16 | Scanner scanner = new Scanner(System.in); 17 | System.out.println("Enter the number of rows:"); 18 | int rows = scanner.nextInt(); 19 | System.out.println("Enter the number of columns:"); 20 | int columns = scanner.nextInt(); 21 | for (int i = 1; i <= rows; i++) { // loops through the rows 22 | for (int j = 1; j <= columns; j++) { // loops through the columns 23 | System.out.print("* "); 24 | } 25 | System.out.println(""); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /forBegginers/r22_nestedForLoop: -------------------------------------------------------------------------------- 1 | package day15_nestedForLoop_MethodOlusturma; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C04_NestedForLoop { 6 | public static void main(String[] args) { 7 | /* 8 | Take an integer as input from the user, and draw the following shape: 9 | * 10 | * * 11 | * * * 12 | * * * * 13 | * * * * * 14 | */ 15 | Scanner scanner = new Scanner(System.in); 16 | System.out.println("Enter the number of rows:"); 17 | int rows = scanner.nextInt(); 18 | for (int i = 1; i <= rows; i++) { // loops through the rows 19 | for (int j = 1; j <= i; j++) { // loops through the columns 20 | System.out.print("* "); 21 | } 22 | System.out.println(""); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /forBegginers/r23_nestedForLoop: -------------------------------------------------------------------------------- 1 | package day15_nestedForLoop_MethodOlusturma; 2 | 3 | public class C05_NestedForLoop { 4 | public static void main(String[] args) { 5 | int rows = 5; 6 | int columns = 7; 7 | 8 | // Print row and column numbers in a rectangular shape 9 | for (int i = 1; i <= rows; i++) { // loops through the rows 10 | for (int j = 1; j <= columns; j++) { // loops through the columns 11 | System.out.print(i + "," + j + " "); 12 | } 13 | System.out.println(""); 14 | } 15 | 16 | // Print row and column numbers in a triangular shape 17 | for (int i = 1; i <= rows; i++) { // loops through the rows 18 | for (int j = 1; j <= i; j++) { // loops through the columns 19 | System.out.print(i + "," + j + " "); 20 | } 21 | System.out.println(""); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /forBegginers/r24_nestedForLoop: -------------------------------------------------------------------------------- 1 | package day15_nestedForLoop_MethodOlusturma; 2 | 3 | public class C03_NestedForLoop { 4 | public static void main(String[] args) { 5 | /* 6 | Print the following pattern: 7 | 1 8 | 1 2 9 | 1 2 3 10 | 1 2 3 4 11 | */ 12 | for (int i = 1; i <= 4; i++) { // loops through the rows 13 | for (int j = 1; j <= i; j++) { // loops through the columns 14 | System.out.print(j + " "); 15 | } 16 | System.out.println(""); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /forBegginers/r25_nestedForLoop: -------------------------------------------------------------------------------- 1 | package day15_nestedForLoop_MethodOlusturma; 2 | 3 | public class C06_KendiSubStringimiz { 4 | public static void main(String[] args) { 5 | String str = "Java Candir"; 6 | int start = 5; 7 | int end = 7; 8 | 9 | /* 10 | Given a string and start and end indices, do the following: 11 | 1- If the end index is less than the start index, print an error message. 12 | 2- If the start or end indices are out of bounds, print an error message. 13 | 3- Otherwise, print the characters from the start index (inclusive) 14 | to the end index (exclusive). 15 | */ 16 | if (end < start) { 17 | System.out.println("The end index cannot be less than the start index."); 18 | } else if (start < 0 || end < 0 || start >= str.length() || end >= str.length()) { 19 | System.out.println("The indices are out of bounds."); 20 | } else { 21 | for (int i = start; i < end; i++) { 22 | System.out.print(str.charAt(i)); 23 | } 24 | } 25 | 26 | System.out.println(str.substring(start, end)); 27 | 28 | /* 29 | The substring() method returns a part of a string, starting from the specified index. 30 | For example, str.substring(5) returns "Candir" from the string "Java Candir". 31 | However, if we don't print or assign the result to a variable, it is useless. 32 | */ 33 | 34 | String desiredSubstring = str.substring(5); // saves the result but does not print it 35 | System.out.println(str.substring(5)); // prints the result but does not save it 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /forBegginers/r26_MethodCreation: -------------------------------------------------------------------------------- 1 | package day16_methodCreation_Usage; 2 | public class C01_MethodCreation { 3 | // Question 1- Create a method that takes a String input from the user and prints the letters between 4 | // the starting and ending indexes, including the starting index but excluding the ending index. 5 | // 6 | // - If the user enters a starting value greater than the ending value, give an error message. 7 | // - If the user enters an index greater than the indexes in 'str', give an error message. 8 | public static void main(String[] args) { 9 | altString("Java is beautiful",3,7); //a is 10 | altString("Please have some patience",5,4); 11 | // The starting index cannot be greater than the ending index 12 | altString("Is this not a goal ?" , 11,33); 13 | // The given index is out of bounds 14 | String str= "Let's not spoil it"; 15 | altString(str,5,10); // not sp 16 | } // end of main method 17 | public static void altString(String text, int startIndex, int endIndex ){ 18 | if (startIndex>endIndex){ 19 | System.out.println("The starting index cannot be greater than the ending index"); 20 | } else if (startIndex>=text.length() || endIndex>=text.length()) { 21 | System.out.println("The given index is out of bounds"); 22 | }else{ 23 | for (int i = startIndex; i ='a' && firstChar<='z')){ 29 | System.out.println("The first character must be a lowercase letter."); 30 | count++; 31 | } 32 | // - Last character must be a number 33 | char lastChar = password.charAt(password.length()-1); 34 | if (!(lastChar>='0' && lastChar<='9')){ 35 | System.out.println("The last character must be a number."); 36 | count++; 37 | } 38 | // - Password cannot contain spaces 39 | if (password.contains(" ")){ 40 | System.out.println("The password cannot contain spaces."); 41 | count++; 42 | } 43 | // - Password must have at least 10 characters 44 | if (password.length() < 10){ 45 | System.out.println("The password must have at least 10 characters."); 46 | count++; 47 | } 48 | if (count == 0){ 49 | return true; 50 | }else{ 51 | return false; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /forBegginers/r35_whileLoop_DoWhileLoop: -------------------------------------------------------------------------------- 1 | package day18_whileLoop_DoWhileLoop; 2 | import java.util.Scanner; 3 | 4 | public class C01_SifreKontrolu { 5 | public static void main(String[] args) { 6 | 7 | // Question 4: Ask the user for a password in the main method 8 | // Check the password in a separate method with the following conditions: 9 | // - First character must be a lowercase letter 10 | // - Last character must be a number 11 | // - Password cannot contain spaces 12 | // - Password must have at least 10 characters 13 | // Return true or false from the method and ask for input again until all conditions are met 14 | // When the conditions are met, print "Password successfully saved" and exit the loop 15 | 16 | /* 17 | We solved this question in day17, but for more professional coding in Java, 18 | we will make two improvements: 19 | 1- 20 | The inside of the while loop's parentheses runs as long as it is true. 21 | In this question, the variable "result" also contains a boolean value, 22 | but in terms of the logic of the question, 23 | the loop should run as long as "result" is false. 24 | 2- 25 | Every time the while loop runs, 26 | Scanner scanner = new Scanner(System.in); 27 | String password = scanner.nextLine(); 28 | runs and a new object is created. 29 | The better way is to create the object before the loop 30 | and only assign a value inside the loop. 31 | */ 32 | 33 | boolean result = false; 34 | Scanner scanner = new Scanner(System.in); 35 | String password; 36 | while (!result){ // the result of the operation "result == false" 37 | System.out.println("Please enter your password:"); 38 | password = scanner.nextLine(); 39 | result = checkPassword(password); 40 | } 41 | System.out.println("Password successfully saved."); 42 | } 43 | 44 | public static boolean checkPassword(String password){ 45 | int count = 0; // count the errors 46 | // - First character must be a lowercase letter 47 | char firstChar = password.charAt(0); 48 | if (!(firstChar>='a' && firstChar<='z')){ 49 | System.out.println("The first character must be a lowercase letter."); 50 | count++; 51 | } 52 | // - Last character must be a number 53 | char lastChar = password.charAt(password.length()-1); 54 | if (!(lastChar>='0' && lastChar<='9')){ 55 | System.out.println("The last character must be a number."); 56 | count++; 57 | } 58 | // - Password cannot contain spaces 59 | if (password.contains(" ")){ 60 | System.out.println("The password cannot contain spaces."); 61 | count++; 62 | } 63 | // - Password must have at least 10 characters 64 | if (password.length() < 10){ 65 | System.out.println("The password must have at least 10 characters."); 66 | count++; 67 | } 68 | if (count == 0){ 69 | return true; 70 | }else{ 71 | return false; 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /forBegginers/r36_whileLoop_doWhileLoop: -------------------------------------------------------------------------------- 1 | package day18_whileLoop_DoWhileLoop; 2 | import java.util.Scanner; 3 | 4 | public class C02_GirilenSayilariToplama { 5 | public static void main(String[] args) { 6 | // Ask the user for numbers to be added together 7 | // If the sum of the entered numbers exceeds 500, 8 | // say "This is enough numbers, the sum of the entered numbers is ...", and end the process. 9 | // If the number of entered numbers is 10 or more, 10 | // say "You cannot enter more than 10 numbers, the sum of the entered numbers is ...", and end the process. 11 | Scanner scanner; 12 | int count = 0; 13 | int sum = 0; 14 | int number; 15 | while (count<=10 && sum<500) { // The inside of the while loop will continue to run as long as it is true 16 | scanner = new Scanner(System.in); 17 | System.out.println("Please enter a number to be added:"); 18 | number = scanner.nextInt(); 19 | sum += number; 20 | count++; 21 | } 22 | // If the while loop has ended, 23 | // one of the conditions has not been met 24 | if (count >= 10){ 25 | System.out.println("You cannot enter more than 10 numbers, the sum of the entered numbers is "+sum+"."); 26 | } 27 | if(sum>500){ 28 | System.out.println("This is enough numbers, the sum of the entered numbers is "+sum+"."); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /forBegginers/r37_whileLoop_DoWhileLoop: -------------------------------------------------------------------------------- 1 | package day18_whileLoop_DoWhileLoop; 2 | public class C03_StringiTerseCevirme { 3 | public static void main(String[] args) { 4 | // Create a method that reverses a given string using a while loop, 5 | // and returns the reversed string. 6 | System.out.println(reverseString("This is a test.")); 7 | System.out.println(reverseString("Java is fun!")); 8 | } 9 | public static String reverseString(String str){ 10 | String reversedStr = ""; 11 | int index = str.length()-1; 12 | while(index >= 0){ 13 | reversedStr += str.charAt(index); 14 | index--; 15 | } 16 | return reversedStr; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /forBegginers/r38_whileLoop_DoWhileLoop: -------------------------------------------------------------------------------- 1 | package day18_whileLoop_DoWhileLoop; 2 | 3 | import java.util.Scanner; 4 | 5 | public class C04_RakamlarToplaminiBulma { 6 | public static void main(String[] args) { 7 | // Create a program that takes a number from the user and finds the sum of its digits using a while loop. 8 | Scanner scanner = new Scanner(System.in); 9 | System.out.println("Please enter a number to find the sum of its digits:"); 10 | int enteredNumber = scanner.nextInt(); 11 | int number = enteredNumber; 12 | int onesDigit = 0; 13 | int sumOfDigits = 0; 14 | while (number>0){ 15 | onesDigit = number%10; 16 | sumOfDigits += onesDigit; 17 | number /= 10; 18 | } 19 | System.out.println("The sum of the digits of " + enteredNumber + " is: " + sumOfDigits + "."); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /forBegginers/r39_Scope: -------------------------------------------------------------------------------- 1 | package day19_scope; 2 | public class C01_Scope { 3 | static int classLevelStatic = 12; 4 | String strClassLevelStaticOlmayan = "Java Guzeldir"; 5 | public static void main(String[] args) { 6 | int sayiMain = 20; 7 | System.out.println(classLevelStatic); 8 | // System.out.println(strClassLevelStaticOlmayan); 9 | for (int i = 0; i <10 ; i++) { 10 | int sayiForLoop=5; 11 | System.out.println(classLevelStatic); 12 | } 13 | } 14 | public static void staticMethod(){ 15 | String strStaticMethod = "Java Candir"; 16 | System.out.println(classLevelStatic); 17 | // System.out.println(strClassLevelStaticOlmayan); 18 | } 19 | public void staticOlmayanMethod(){ 20 | boolean blStaticOlmayanMethod = true; 21 | classLevelStatic = 40; 22 | System.out.println(strClassLevelStaticOlmayan); 23 | } 24 | /* 25 | Scope temelde 2'ye ayrilir 26 | 1- Local variable'lar 27 | A - scope'lari icinde olusturulduklari kod blogudur 28 | bir method'da olusturulan variable, baska method'dan KULLANILAMAZ 29 | B - Loop Scope'u 30 | bir loop icerisinde olusturulan variable 31 | sadece o loop icerisinde kullanilabilir 32 | olusturuldugu method'da loop'un disinda da KULLANILAMAZ 33 | Not: local variable'lar deger verilmeden olusturulabilir 34 | ama deger atanmadan KULLANILAMAZ 35 | 2- Class Level Variable'lar 36 | Class level variable'lar method'larin ve kod bloklarinin disinda olusturulur 37 | ve scope'lari TUM CLASS'dir. 38 | Class level variable'larin scope'u tum class olsa da 39 | static keyword de variable'larin kullanimina etki eder 40 | 41 | hastane ismi, hastane adresi, bashekim adi gibi variable'lar 42 | herkes icin ortak olmalidir. 43 | bu tur variable'lar static olarak isaretlenir 44 | 45 | personel ismi, personel adresi, personel telefonu gibi variable'lar ise 46 | tum personel icin tanimli olmakla birlikte 47 | bu variable'lardaki degisim herkesi ETKILEMEZ 48 | sadece o personeli etkiler 49 | bu tur varianble'lari ise static yapmayiz 50 | static olmayan class level'daki variable'lara INSTANCE variable'lar denir 51 | 52 | C - class level static variable'lar 53 | bu variable'lar static klubune uye olduklari icin 54 | her yere gidebilirler 55 | her method'dan kullanilabilirler 56 | D - class level instance variable'lar 57 | variable static olmayinca ustunlugu olmaz 58 | bu durumda secici olan method olur 59 | 60 | static method'lar instance variable'larin girmesine izin vermez 61 | ama static olmayan method'lar, 62 | static olmayan(instance) variable'lari kabul eder 63 | */ 64 | } 65 | -------------------------------------------------------------------------------- /forBegginers/r40_Scope: -------------------------------------------------------------------------------- 1 | package day19_scope; 2 | public class C02_ClassLevelVariablelar { 3 | static boolean bls; 4 | boolean bli; 5 | static String strs ="Java"; 6 | String stri; 7 | static int sayis; 8 | int sayii = 23; 9 | static char chrs = 'a'; 10 | char chri; 11 | public static void main(String[] args) { 12 | System.out.println(bls); // false 13 | System.out.println(strs); // Java 14 | System.out.println(sayis); // 0 15 | System.out.println(chrs); // a 16 | C02_ClassLevelVariablelar obj = new C02_ClassLevelVariablelar(); 17 | System.out.println(obj.bli); // false 18 | System.out.println(obj.stri); // null 19 | System.out.println(obj.sayii); // 23 20 | System.out.println(obj.chri); // '' 21 | } 22 | /* 23 | Class level kurallar 24 | 1- class level variable'lara deger atanmasa da 25 | hem olusturulabilir, hem de kullanilabilir. 26 | deger atamasi yapmadiysak, java onlara default olan degerleri atar 27 | boolean ==> false 28 | sayisal variabler ==> 0 29 | non-primitive variable'lar ==> null 30 | char ==> '' char olarak hiclik 31 | 2- instance variable'lara static method'lar icinden direk ULASILAMAZ 32 | Eger static method icinde, instance variable kullanmamiz gerekirse 33 | O class'dan bir obje olusturup 34 | o obje uzerinden instance variable'lara ulasabiliriz 35 | 3- Baska class'daki class level variable'lara ulasmak istersek 36 | static variable'lar icin classIsmi.staticVariableIsmi 37 | yazarak ulasabiliriz 38 | instance variable'lara ulasmak icin ise 39 | variable'larin oldugu class'dan obje olusturmaliyiz 40 | eger static bir variable'a obje uzerinden ulasmak isterseniz 41 | Java otomatik olarak getirmez 42 | ama elle yazarsaniz kabullenir 43 | intellij static variable'a insatance gibi obje uzerinden ulasirsaniz 44 | kodu sariya boyayarak sizi uyarir 45 | */ 46 | } 47 | -------------------------------------------------------------------------------- /forBegginers/r41_Scope: -------------------------------------------------------------------------------- 1 | package day19_scope; 2 | public class C03_baskaClassdanClasslevelvariablelaraErisim { 3 | public static void main(String[] args) { 4 | System.out.println(C02_ClassLevelVariablelar.bls); // false 5 | System.out.println(C02_ClassLevelVariablelar.strs); // Java 6 | System.out.println(C02_ClassLevelVariablelar.sayis); // 0 7 | System.out.println(C02_ClassLevelVariablelar.chrs); // a 8 | C02_ClassLevelVariablelar obj = new C02_ClassLevelVariablelar(); 9 | System.out.println(obj.bli); // false 10 | System.out.println(obj.stri); // null 11 | System.out.println(obj.sayii); // 23 12 | System.out.println(obj.chri); // '' 13 | System.out.println(obj.bls); 14 | System.out.println(obj.strs); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /forBegginers/r42_Arrays: -------------------------------------------------------------------------------- 1 | package day20_arrays; 2 | import java.util.Arrays; 3 | public class C01_ArrayOlusturma { 4 | public static void main(String[] args) { 5 | int[] sayilar = {2,4,6,7,8}; 6 | String harfler[] = {"a","f","e"}; 7 | System.out.println(sayilar[1]); // 4 8 | System.out.println(harfler[2]); // e 9 | String[] ogrenciList = {"Burhan","Ramazan","Samet","Senol"}; 10 | System.out.println(ogrenciList[1]); // Ramazan 11 | // 5 kisilik bir sinif olusturun 12 | String[] sinifListesi1 = {null , null, null, null, null}; 13 | String[] sinifListesi2 = new String[5]; 14 | /* 15 | Bir array olusturulurken 2 sey mutlaka belirtilmelidir 16 | 1- icine konulacak datalarin turu 17 | 2- Array'in uzunlugu (Bir array'e olusturulurken yazilan uzunlugundan 18 | daha fazla element konulamaz) 19 | */ 20 | // bir array'in tumunu nasil yazdirabiliriz ? 21 | System.out.println(sayilar); // [I@2752f6e2 22 | // dongu ile yazdiralim 23 | for (int i = 0; i < sayilar.length ; i++) { 24 | System.out.print(sayilar[i] + " "); 25 | } // 2 4 6 7 8 26 | System.out.println(""); 27 | // Array'in tamamini yazdirmak isterseniz 28 | // Arrays class'indan hazir method kullaniriz 29 | System.out.println(Arrays.toString(sayilar)); // [2, 4, 6, 7, 8] 30 | // for loop ile elementleri yazdiririz. 31 | // Arrays.toString() ise bize array'in kendisini yazdirir 32 | System.out.println(Arrays.toString(harfler)); // [a, f, e] 33 | System.out.println(Arrays.toString(sinifListesi1)); // [null, null, null, null, null] 34 | System.out.println(Arrays.toString(sinifListesi2)); // [null, null, null, null, null] 35 | int[] notlar = new int[4]; 36 | System.out.println(Arrays.toString(notlar)); // [0, 0, 0, 0] 37 | boolean[] blList = new boolean[8]; 38 | System.out.println(Arrays.toString(blList)); // [false, false, false, false, false, false, false, false] 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /forBegginers/r43_Arrays: -------------------------------------------------------------------------------- 1 | package day20_arrays; 2 | import java.util.Arrays; 3 | public class C02_ArrayElementleriniGuncelleme { 4 | public static void main(String[] args) { 5 | int[] sayilar = new int[6]; 6 | System.out.println(Arrays.toString(sayilar)); // [0, 0, 0, 0, 0, 0] 7 | // 2.indexdeki elementi 5 yapin 8 | sayilar[2] = 5 ; 9 | System.out.println(Arrays.toString(sayilar)); // [0, 0, 5, 0, 0, 0] 10 | // 2.index'deki sayiyi 7 yapin 11 | sayilar[2] = 7 ; 12 | System.out.println(Arrays.toString(sayilar)); // [0, 0, 7, 0, 0, 0] 13 | // index kullanmadan bir element'e atama yapmak mumkun degildir 14 | // 5.index'deki elementi 8 yapin 15 | sayilar[5] = 8; 16 | System.out.println(Arrays.toString(sayilar)); // [0, 0, 7, 0, 0, 8] 17 | // 6.index'e yeni bir element ekleyelim 18 | sayilar[6] = 20; // ArrayIndexOutOfBoundsException 19 | // run dedikten sonra bu hata olustugu icin 20 | // buna Run Time Error denilir 21 | 22 | /* 23 | Uzunlugu verilen bir array'de 24 | var olan elementler index ile update edilebilir 25 | ANCAK 26 | yeni element ekleyip uzunlugunu degistirmek isterseniz 27 | RunTimeError olusur 28 | */ 29 | 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /homework/R01_contains: -------------------------------------------------------------------------------- 1 | package day11_StringManipulations; 2 | 3 | import java.util.Scanner; 4 | 5 | public class homework { 6 | public static void main(String[] args) { 7 | 8 | /* 1: 9 | 10 | Get a sentence from the user 11 | 12 | - If home is in the sentence, make it say "home home sweet home". 13 | - If work is in the sentence, write "work is good". 14 | - if it contains both "" 15 | - If it contains none of the above, make it say "you have to work hard" 16 | */ 17 | 18 | Scanner scan = new Scanner(System.in); 19 | System.out.print("Please enter a text for your home: "); 20 | String input = scan.nextLine(); 21 | 22 | if (input.contains("home") && input.contains("job")){ 23 | System.out.println("you need a home and a job"); 24 | } else if (input.contains("job")) { 25 | System.out.println("it's good to work"); 26 | } else if (input.contains("home")) { 27 | System.out.println("home home sweet home"); 28 | } else System.out.println("You have to work hard"); 29 | 30 | // We can use the contains() String method to check if a text contains the word or character we are looking for. 31 | // we can check that it doesn't exist, but what if I want to search for two words or characters? 32 | 33 | // the answer is input.contains("home") && input.contains("job") 34 | 35 | -------------------------------------------------------------------------------- /homework/R02_parse: -------------------------------------------------------------------------------- 1 | package day11_StringManipulations; 2 | 3 | import java.util.Scanner; 4 | 5 | public class homework_01 { 6 | public static void main(String[] args) { 7 | /* 2: 8 | 9 | Write a collection of String prices given by the user in a specific format. 10 | 11 | Example: 12 | - input1: "15.30$" , input2: "11.40$" 13 | - output: 26.70$ 14 | */ 15 | 16 | Scanner scan = new Scanner(System.in); 17 | System.out.print("What is the price of your first product?: "); 18 | String input1 = scan.nextLine(); 19 | System.out.print("What is the price of your second product?: "); 20 | String input2 = scan.nextLine(); 21 | 22 | double num1 = Double.parseDouble(input1); 23 | double num2 = Double.parseDouble(input2); 24 | double output = (num2 + num1); 25 | 26 | System.out.println(output); 27 | 28 | 29 | // How do I calculate two String numeric input? 30 | // dataType.parseDataType(input) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /homework/R03_replace_substring: -------------------------------------------------------------------------------- 1 | package day11_StringManipulations; 2 | 3 | import java.util.Scanner; 4 | 5 | public class homework_02 { 6 | public static void main(String[] args) { 7 | /* 3: 8 | Write a program that removes unwanted numbers and special characters from the text entered by the user, 9 | and makes only the first letter uppercase and the rest lowercase. 10 | 11 | input: java1 ogRe2@nMek3 #ne +Gu=zel 12 | output: Java ogrenmek ne guzel. 13 | */ 14 | 15 | Scanner scan = new Scanner(System.in); 16 | System.out.print("Please enter a text: "); 17 | String input = scan.nextLine(); 18 | String output; 19 | 20 | input = input.replaceAll("\\d", ""); // Removes digits from the text 21 | input = input.replaceAll("\\s", "1"); // Preserves space characters 22 | input = input.replaceAll("\\W", ""); // Removes special characters from the text 23 | input = input.replace('1', ' '); // Replaces any remaining space characters 24 | 25 | String firstLetter = input.substring(0, 1).toUpperCase(); // Converts the first letter to uppercase 26 | String remainingLetters = input.substring(1).toLowerCase(); // Converts the remaining letters to lowercase 27 | output = firstLetter + remainingLetters; // Combines the first letter and remaining letters 28 | 29 | System.out.println(output); // Prints the result to the console 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /homework/R04_!: -------------------------------------------------------------------------------- 1 | package day11_StringManipulations; 2 | 3 | import java.util.Scanner; 4 | 5 | public class homework_03 { 6 | public static void main(String[] args) { 7 | /* 4: 8 | 9 | Ask the user for a password and check the following conditions 10 | then tell the user all the missing parts they need to correct, 11 | if all conditions are met print "password successfully saved" 12 | 13 | - First character should be a lowercase letter 14 | - Last character should be a number 15 | - Password should not contain spaces 16 | - Length should be at least 10 characters 17 | */ 18 | Scanner scanner = new Scanner(System.in); 19 | 20 | System.out.print("Please enter a password: "); 21 | String password = scanner.nextLine(); 22 | 23 | boolean hasMissingParts = false; // variable to check if there are missing parts or not 24 | 25 | if (!Character.isLowerCase(password.charAt(0))) { 26 | System.out.println("The first character of the password should be a lowercase letter."); 27 | hasMissingParts = true; 28 | } 29 | 30 | if (!Character.isDigit(password.charAt(password.length() - 1))) { 31 | System.out.println("The last character of the password should be a number."); 32 | hasMissingParts = true; 33 | } 34 | 35 | if (password.contains(" ")) { 36 | System.out.println("The password cannot contain space characters."); 37 | hasMissingParts = true; 38 | } 39 | 40 | if (password.length() < 10) { 41 | System.out.println("The password should be at least 10 characters long."); 42 | hasMissingParts = true; 43 | } 44 | 45 | if (!hasMissingParts) { 46 | System.out.println("Password successfully saved."); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /homework/R05_@email: -------------------------------------------------------------------------------- 1 | package day11_StringManipulations; 2 | 3 | import java.util.Scanner; 4 | 5 | public class homework_05 { 6 | public static void main(String[] args) { 7 | // Ask the user to enter an email address and validate it. 8 | // Check whether the email address is in the correct format. 9 | 10 | // The correct format is as follows: 11 | 12 | // Must contain the @ symbol 13 | // Must have at least 2 characters before the @ symbol 14 | // Must have at least 2 characters after the @ symbol 15 | // Must contain a dot (.) 16 | // Must have at least 2 characters after the dot 17 | // Not case sensitive 18 | 19 | // If the email is in the correct format, print "You entered a valid email address." 20 | // Otherwise, print an appropriate error message indicating which rule was violated. 21 | 22 | // abc@example.com 23 | 24 | Scanner scan = new Scanner(System.in); 25 | System.out.print("Please validate your e-mail: "); 26 | String input = scan.nextLine(); 27 | boolean hasErrors = false; // variable to check if there are any errors. 28 | 29 | if (!input.contains("@")){ 30 | System.out.println("Error: E-mail must contain the @ symbol."); 31 | hasErrors = true; 32 | } 33 | 34 | int atIndex = input.indexOf("@"); // atIndex = 3 35 | String beforeAt = input.substring(0, atIndex - 2); // beforeAt 36 | if (beforeAt.length() < 2){ 37 | System.out.println("Error: There must be at least 2 characters before the @ symbol."); 38 | hasErrors = true; 39 | } 40 | beforeAt = input.substring(0, atIndex + 2); 41 | if (beforeAt.length() < 2){ 42 | System.out.println("Error: There must be at least 2 characters before and after the @ symbol."); 43 | hasErrors = true; 44 | } 45 | 46 | if (!input.contains(".")){ 47 | System.out.println("Error: E-mail must contain a dot (.)"); 48 | hasErrors = true; 49 | } else { 50 | int dotIndex = input.lastIndexOf("."); 51 | String afterDot = input.substring(dotIndex + 1); 52 | if (afterDot.length() < 2) { 53 | System.out.println("Error: There must be at least 2 characters after the dot."); 54 | hasErrors = true; 55 | } 56 | } 57 | 58 | if (!hasErrors) { 59 | System.out.println("You entered a valid email address."); 60 | } 61 | } 62 | } 63 | --------------------------------------------------------------------------------