Topic 4 Exercises
Exercise 1. Write a day of the week on the keyboard and, depending on which one it is, we will show the activity that we have 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 enter 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 word is acceptable
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. In this case we put four variables in case someone forgets the accent.
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 respond if we have entered erroneous 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 will see below:
if ( day.equals("Monday") || day.equals("monday")) {
System.out.print("Yoga");
System.exit(0);
} else
//In this way the program will close if it matches the day of the week we have 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 enter the final line that will only be shown if none of the previous days have been mentioned.
}
}
Exercise 3. Enter a number from 1 to 5 on the keyboard and, depending on the number, a text in a color will appear on the program screen: 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 indicated data is not correct");
}
}
💡 It’s important that your conditionals are easy to read and understand. Avoid nesting too many if, else if and else, as this can make the code confusing. Instead, consider dividing 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 on the keyboard that will refer to the temperature of a pool. If it is less than 20, the program will say it is cold; for any other temperature, it will say it is hot. It can have decimals.
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 the grade of an exam on the keyboard. The program will tell us if the student has failed, passed, received a good grade or an excellent grade. If it is less than 5, it is a fail. Between 5 and 6.99 is sufficient. Between 7 and 8.99 is good, and greater than 9 is excellent.
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 will take into account 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("Sufficient");
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("Good");
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("Excellent");
System.exit(0);
} else System.out.println("The entered data is not correct");
}
}
💡 Review your code to eliminate redundant conditions. For example, if a condition has already been checked and handled, it is 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);
//Create the salary variable. In this case it is necessary to initialize it.
//Otherwise we could not use it in the last line of this program.
//You cannot ask the program to display a variable if it has no value.
//Therefore we give it a value of zero, but it will change depending on the entered data.
double salary = 0;
System.out.println("Enter the number of hours worked");
int hours = exercise.nextInt();
//First we make a calculation assuming there were no overtime hours.
if (hours <= 160) {
salary = (hours*7.8);
}
// In this case there were overtime hours
// Therefore we know that there are always more than 160. So we will always pay 160 hours at 7.8 euros.
// To those 160 hours we now add the overtime. To find out how many overtime hours we worked, we subtract 160 from the total number of hours.
// Example. If the worker did 180 hours. 180-160 = 20. Twenty are overtime.
//Finally we simply add the normal hours plus the overtime.
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 most likely to be met at the beginning of your if blocks. This can improve program performance by reducing the number of checks needed in common cases.
Exercise 7. Enter a number on the keyboard and the program must 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 on 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 in which you enter a three-digit number on the keyboard and the program will tell you if it is a palindrome 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 entered number must have three digits");
}
if (num>999) {
System.out.println("The entered number must have three digits");
}
//Calculating hundreds is easy. We divide the number by 100.
// For example 356/100 = 3.56. Being an int variable it does not show decimals, only the 3.
int hundreds = num/100;
// For tens we do the same only that before we must eliminate the hundreds.
//Example 678. If we could remove the 6, we would have 78.
//If we could divide 78 by 10 we would get 7. The ten 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 as 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 says how many digits a number entered on the 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 large");
}
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");
}
}
}