Learn Java Easy. Chapter 4 Exercises

Chapter 4 Exercises

Exercise 1. Type a day of the week and, depending on which one it is, we will show the activity that we need to do. The activities are: Yoga, Biking, Gym, Painting, and Reading.

 

Solution:

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

//Create a scanner

        Scanner exercise = new Scanner(System.in);

//Now we need to ask the user to input a day of the week
       
        System.out.println("Enter a day of the week");
        String day = exercise.nextLine();

//Now we will write our conditional

//For Monday. We write with uppercase and lowercase. For this we use ||
//With this we indicate that either of the two words is valid

        if (day.equals("Monday") || day.equals("monday")) {
            System.out.print("Yoga");
        }

//For Tuesday.

        if (day.equals("Tuesday") || day.equals("tuesday")) {
            System.out.print("Biking");
        }


//For Wednesday.

        if (day.equals("Wednesday") || day.equals("wednesday")) {
            System.out.print("Gym");
        }

//For Thursday.

        if (day.equals("Thursday") || day.equals("thursday")) {
            System.out.print("Painting");
        }

//For Friday.

        if (day.equals("Friday") || day.equals("friday")) {
            System.out.print("Reading");
        } 

    }
}

Exercise 2. Exactly the same as the previous one, but in case we enter an incorrect day, the program will tell us if we have entered incorrect data.

 

Solution:

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

        Scanner exercise = new Scanner(System.in);
        System.out.println("Enter a day of the week");

        String day = exercise.nextLine();

//We are going to use else and system.exit in the way you'll see next:

        if (day.equals("Monday") || day.equals("monday")) {
            System.out.print("Yoga");
            System.exit(0);
        } else
        
//This way the program will close if it matches the day of the week we entered. Otherwise, it will use the else to go to the next conditional.

//For Tuesday.

        if (day.equals("Tuesday") || day.equals("tuesday")) {
            System.out.print("Biking");
            System.exit(0);
        } else

//For Wednesday.

        if (day.equals("Wednesday") || day.equals("wednesday")) {
            System.out.print("Gym");
            System.exit(0);
        } else

//For Thursday.

        if (day.equals("Thursday") || day.equals("thursday")) {
            System.out.print("Painting");
            System.exit(0);
        } else

//For Friday.

        if (day.equals("Friday") || day.equals("friday")) {
            System.out.print("Reading");
            System.exit(0);
        } else System.out.println("Incorrect data entered");

//On Friday at the end, after the else we introduce the final line that will only be shown if none of the previous days were mentioned.

    }
}

Exercise 3. Enter a number from 1 to 5 using the keyboard and, depending on the number, a text will appear on the program screen in a color: yellow, blue, green, black or white.

 

Solution:

 



import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
 
        Scanner exercise = new Scanner(System.in);
      
        System.out.println("Enter a number from 1 to 5");
        int number = exercise.nextInt();

        if (number == 1) {
            System.out.println("Yellow");
            System.exit(0);
        }

        if (number == 2) {
            System.out.println("Blue");
            System.exit(0);
        }

        if (number == 3) {
            System.out.println("Green");
            System.exit(0);
        }

        if (number == 4) {
            System.out.println("Black");
            System.exit(0);
        }

        if (number == 5) {
            System.out.println("White");
            System.exit(0);
        } else System.out.println("The entered data is incorrect");
        
    }
}

πŸ’‘ It’s important that your conditionals are easy to read and understand. Avoid nesting too many if, else if, and else statements, as this can make the code become confusing. Instead, consider splitting the logic into separate functions if it becomes too complex.

πŸ’‘ Take advantage of logical operators (&&, ||, !) and comparison operators (==, !=, >, <, >=, <=) to combine conditions and make your if statements more efficient. Make sure you understand how they work to avoid logical errors in your code.

Exercise 4. Enter a number using the keyboard that will refer to the temperature of a swimming pool. If it’s less than 20, the program will say it’s cold; for any other temperature, it will say it’s hot. It can have decimal places.

 

Solution:

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        
        Scanner exercise = new Scanner(System.in);

        System.out.println("Enter the pool temperature");

        double temperature = exercise.nextDouble();

        if (temperature < 20) {

            System.out.println("Cold");

        } else System.out.println("Hot");
        
    }

Exercise 5. We are going to enter an exam grade using the keyboard. The program will tell us if the student has failed, passed, received a B, or received an A. If it’s less than 5, it’s a fail. Between 5 and 6.99 is a pass. Between 7 and 8.99 is a B, and greater than 9 is an A.

 

Solution:

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

        Scanner exercise = new Scanner(System.in);
        System.out.println("Enter the exam grade");
        double grade = exercise.nextDouble();

//First we'll consider grades that are less than 5

        if (grade < 5) {
            System.out.println("Fail");
            System.exit(0);
        }

//Then those that are greater than or equal to 5
//And at the same time less than 7

        if (grade >= 5 && grade < 7) {
            System.out.println("Pass");
            System.exit(0);
        }

/Now those that are greater than or equal to 7
//And at the same time less than 9

        if (grade >= 7 && grade < 9) {
            System.out.println("B");
            System.exit(0);
        }

//Finally those that are greater than or equal to 9
//And at the same time less than or equal to 10
//We also include a line to indicate that any value greater than 10 is an error

        if (grade >= 9 && grade <= 10) {
            System.out.println("A");
            System.exit(0);
        } else System.out.println("The entered data is incorrect");
    }
}

πŸ’‘ Review your code to eliminate redundant conditions. For example, if a condition has already been checked and handled, it’s not necessary to check it again. This simplifies the code and improves performance.

Exercise 6. We are going to create a program that calculates a worker’s salary. The first 160 hours are paid at 7.8 euros. Hours exceeding 160 are considered overtime and are paid at 12.2 euros.

 

Solution:

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

        Scanner exercise = new Scanner(System.in);

//We create the salary variable. In this case it's necessary to initialize it.
//Otherwise we couldn't use it in the last line of this program.
//You can't ask the program to show a variable if it doesn't have a value.
//Therefore we set it to zero, but it will change later depending on the data entered.
        
        double salary = 0;
        
        System.out.println("Enter the number of hours worked");

        int hours = exercise.nextInt();


//First we make a calculation assuming there was no overtime.


        if (hours <= 160) {
            salary = (hours * 7.8);
        }

// In this case there was overtime
// Therefore we know there are always more than 160. So we will always pay 160 hours at 7.8 euros.
// To these 160 hours we now add the extra ones. To know how many extra hours we worked, we subtract 160 from the total hours.
// Example. If the worker did 180 hours. 180-160 = 20. Twenty are extra.
// Finally we simply add the normal hours plus the extra hours.

        
        if (hours > 160) {
            salary = (160 * 7.8) + ((hours-160) * 12.2);
        }

        System.out.println("The total salary is " + salary + " euros");

    }
}

πŸ’‘ Place the conditions that are more likely to be met at the beginning of your if blocks. This can improve the program’s performance by reducing the number of checks needed in common cases.

Exercise 7. Enter a number using the keyboard and the program should tell us if it is a multiple of 5.

 

Solution:

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

        Scanner exercise = new Scanner(System.in);
        System.out.println("Enter the number");

        int number = exercise.nextInt();

        if (number%5==0) {
            System.out.println("The number is a multiple of 5");
        } else System.out.println("The number is not a multiple of 5");

    }
}

Exercise 8. We enter a number using the keyboard and ask it to tell us if it is even or odd.

 

Solution:

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

    Scanner exercise = new Scanner(System.in);
        System.out.println("Enter the number");

        int number = exercise.nextInt();

        if (number%2==0) {
            System.out.println("The number is even");
        } else System.out.println("The number is odd");

    }
}

Exercise 9. Create a program where you enter a three-digit number using the keyboard and the program will tell you if it’s a palindrome number or not.

 

Solution:

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

//Create a scanner

        Scanner exercise = new Scanner(System.in);

//Now we need to ask the user to enter the number

        System.out.println("Enter a number");
        int num = exercise.nextInt();

if (num<=99) {
            System.out.println("The number must have three digits");
        }
        if (num>999) {
            System.out.println("The number must have three digits");
        }

//Calculating the hundreds is easy. We divide the number by 100.
//For example 356/100 = 3.56. Being an int variable it only shows the 3, no decimals.

        int hundreds = num/100;

//For the tens we'll do the same but first we must eliminate the hundreds.
//Example 678. If we could remove the 6, we'd have 78.
//If we could divide 78 by 10 we'd get 7. The hundred we need.
//So we multiply the hundreds by 100. In this case 6*100 = 600.
//We subtract 600 from our number; 678-600 = 78. 78/10 = 7.8 but being an int = 7

        int tens = ((num - (hundreds * 100))/10);

//We do the same to get the units.
//(Hundreds * 100) + (tens * 10) and we subtract it from our number.
//600 * 100 + 7*10 = 670. 678-670 = 8. The unit we need.

        int units = (num - (hundreds * 100) - tens * 10);

        if (units==hundreds) {
            System.out.println("The number is a palindrome");
        } else System.out.println("The number is not a palindrome");

    }
}

Exercise 10. Create a program that tells how many digits a number entered via keyboard has.

 

Solution:

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {


        Scanner exercise = new Scanner(System.in);


        System.out.println("Enter a number");
        int num = exercise.nextInt();

        if (num>9999) {
            System.out.println("The number is too big");
        }
        if (num<10) {
            System.out.println("The number has one digit");
        }
        if (num>=10&&num<100) {
            System.out.println("The number has two digits");
        }
        if (num>=100&&num<1000) {
            System.out.println("The number has three digits");
        }
        if (num>=1000&&num<10000) {
            System.out.println("The number has four digits");
        }
    }
}

Did you like it? Don’t keep it to yourself β€” share it like juicy gossip! 😏