Blog Detail

September 23, 2023

Java Conditionals Exercises

  • Write a condition that checks if the item is affordable.

                    class Main {
      public static void main(String[] args) {
        double itemPrice = 1900.25;
        double wallet = 2100.50;
    
        if ""{
          System.out.println("The item is affordable!");
        }
      }
    }
  • Hold the result of the comparison in a variable and then use only the variable as the condition.

                    class Main {
      public static void main(String[] args) {
        double itemPrice = 1900.25;
        double wallet = 2100.50;
    
        boolean isAffordable = itemPrice <= wallet;
    
        if ""{
          System.out.println("The item is affordable!");
        }
      }
    }
  • If the user wants to rent a car for more than 30 days, announce to the user that they are eligible for a 15% discount.

                    class Main {
      public static void main(String[] args) {
        int rentDays = 34;
    
        boolean isEligibleForDiscount = ""
    
        if (isEligibleForDiscount){
          System.out.println("You are eligible for a discount of 15% on your order!");
        }
      }
    }
  • Check if the file size exceeds the maximum allowable size for uploading.

                    class Main {
      public static void main(String[] args) {
        int fileSize = 10;   // in MB
        int fileSizeLimitation = 5;  // in MB
    
        "" (fileSize > fileSizeLimitation){
          System.out.println("File size exceeds the maximum allowable limit.");
          System.out.println("Please choose a smaller file.");
        }
      }
    }
  • Check if the file size exceeds the maximum allowable size for uploading.
    Otherwise upload the file.

                    class Main {
      public static void main(String[] args) {
        double fileSize = 2.2;   // in MB
        double fileSizeLimitation = 5.0;  // in MB
    
        if (fileSize > fileSizeLimitation){
          System.out.println("File size exceeds the maximum allowable limit.");
          System.out.println("Please choose a smaller file.");
        }""{
          System.out.println("Uploading the file...");
        }
      }
    }
  • Check if password and confirmedPassword are the same.

                    class Main {
      public static void main(String[] args) {
        String password = "aaA123";
        String confirmedPassword = "aaA123";
    
        if (""){
          System.out.println("Password and confiremed password are the same.");
          System.out.println("Logging in...");
        }else{
          System.out.println("Password and confiremed password are not the same.");
          System.out.println("Please confirm your password.");
        }
      }
    }
  • Check if the email and the confirmed email are the same.

                    class Main {
      public static void main(String[] args) {
        String email = System.console().readLine("Enter your email: ");
        String confirmedEmail = System.console().readLine("Confirm the email: ");
    
        email = email.toLowerCase();
        confirmedEmail = email.""
    
        if (email.contains(confirmedEmail)){
          System.out.println("Email and confiremed email are the same.");
          System.out.println("Logging in...");
        }else{
          System.out.println("Email and confiremed email are not the same.");
          System.out.println("Please confirm your email.");
        }
      }
    }
  • What is the output of this code?

                    class Main {
      public static void main(String[] args) {
        String country = "UK";
        double shippingFee = 0.0;
        
        if (country.equals("USA")){
          shippingFee = 10.0;      
        } else if (country.equals("Canada")){
          shippingFee = 15.0;
        } else if (country.equals("UK")){
          shippingFee = 18.0;
        } else {
          shippingFee = 20.0;
        }
    
        System.out.println(shippingFee);
            
      }
    }
  • What is the output of this code?

                    class Main {
      public static void main(String[] args) {
        String country = "usa";
        double shippingFee = 0.0;
        
        if (country.equals("USA")){
          shippingFee = 10.0;      
        } else if (country.equals("Canada")){
          shippingFee = 15.0;
        } else if (country.equals("UK")){
          shippingFee = 18.0;
        } else {
          shippingFee = 20.0;
        }
    
        System.out.println(shippingFee);
            
      }
    }
  • Java Exercises for Conditionals

    • If you've faced difficulties with these exercises, take a look at Conditionals Module on my Java course.


    You can also share 'Java Conditionals' Exercises on your:
    X (Twitter) - Telegram - Linkedin - Facebook - or on your favorite platform...




    Happy Coding!
    Behnam Khani