Learn Java Easy. Chapter 3 Exercises

🎯 Exercises: Keyboard Input in Java | Topic 3

Topic 3 Exercises

Exercise 1. Create a program where we can input an integer and then display it on the screen. Remember to import the Scanner class: import java.util.Scanner;

Solution:

Scanner exercise = new Scanner(System.in); 

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

System.out.println("The entered number is: "); 

System.out.println(a);

Exercise 2. Create a currency converter program where we input an amount in euros and it returns the result in dollars. Assume the equivalence of 1 euro is 1.10 dollars.

Solution:

Scanner exercise = new Scanner(System.in); 

System.out.println("Enter the amount in euros"); 
double euros = exercise.nextDouble(); 

System.out.println("The amount in euros is: " + euros);
System.out.println("The amount in dollars is: " + (euros * 1.10));

Exercise 3. Create four variables of type double and name them a, b, c, and d. The program should ask us to input the data for each variable. Then, the program will multiply a by b, divide the result by c, and add d to the whole result. The result must be displayed on the console.

Solution:

Scanner exercise = new Scanner(System.in); 

System.out.println("Enter data a"); 
double a = exercise.nextDouble(); 

System.out.println("Enter data b"); 
double b = exercise.nextDouble(); 

System.out.println("Enter data c"); 
double c = exercise.nextDouble(); 

System.out.println("Enter data d"); 
double d = exercise.nextDouble();

System.out.println("((a * b)/c) + d = " + (((a * b) / c) + d));

💡 Scanner provides specific methods to read different types of data, such as nextInt() for integers, nextDouble() for floating-point numbers, and nextLine() for strings. Use the appropriate method according to the type of data you expect to receive.

Exercise 4. Create a program that calculates the area of a rectangle. For this, it should ask us for the height and width of the rectangle. Remember that the area of a rectangle is equal to the multiplication of its sides.

Solution:

Scanner exercise = new Scanner(System.in); 

System.out.println("Enter the height of the rectangle"); 
int a = exercise.nextInt();

System.out.println("Enter the width of the rectangle"); 
int b = exercise.nextInt();

System.out.println("The area of the rectangle is = " + (a * b));

Exercise 5. Create the same program as in the previous exercise, but in this case, calculate the area of a triangle. Remember that the area of a triangle is base times height, all divided by two.

Solution:

Scanner exercise = new Scanner(System.in); 

System.out.println("Enter the height of the triangle"); 
int a = exercise.nextInt(); 

System.out.println("Enter the base of the triangle"); 
int b = exercise.nextInt();

System.out.println("The area of the triangle is = " + ((a * b) / 2.0));

Exercise 6. Create a program where the total amount of an invoice is entered via the keyboard, and it indicates how much VAT has been paid. The tax rate is 16%.

Solution:

Scanner exercise = new Scanner(System.in); 

System.out.println("Enter the total invoice amount"); 
double total = exercise.nextDouble();

System.out.println("The net value is " + (total * 0.84) + " euros"); 
System.out.println("The VAT value is " + (total * 0.16) + " euros"); 
System.out.println("The total value is " + total + " euros");

Exercise 7. Create a program that calculates the weekly salary of a worker depending on the hours worked. Up to 40 hours, the rate is 7 euros/hour. The program should display the total number of hours as well as the total salary.

Solution:

Scanner exercise = new Scanner(System.in); 

System.out.print("Enter the total number of hours "); 
int hours = exercise.nextInt();

System.out.println("Total hours worked: " + hours); 
System.out.println("Salary in euros: " + (hours * 7));

Exercise 8. Celsius to Fahrenheit temperature converter. The formula is: (Celsius * 9 / 5) + 32.

Solution:

Scanner exercise = new Scanner(System.in); 

System.out.println("Welcome to the temperature converter!"); 

System.out.println("Please enter the temperature in degrees Celsius:"); 
double celsius = exercise.nextDouble();

System.out.println("The temperature in Fahrenheit is: " + ((celsius * 9 / 5) + 32));

Exercise 9. Create a program where we are asked to enter a symbol. We will store it in a variable of type char. Then, the program should display the symbol on the console.

Solution:

Scanner exercise = new Scanner(System.in); 

System.out.print("Enter a symbol "); 
char a = exercise.next().charAt(0);

System.out.println("The entered symbol is: " + a);

💡 It is important to close the Scanner object with the close() method at the end of its use to properly release system resources. This prevents potential memory leaks and ensures efficient resource management.

Exercise 10. Create a program that simulates the usage control of a photocopier in an office. The program will allow employees to enter the number of copies they want to make and calculate the total cost. It will also keep track of the total number of copies made during the day.

Solution:


Scanner exercise = new Scanner(System.in);

// Variables

int costPerCopy = 5; 

// Cost per copy

// Keyboard input 

System.out.println("Welcome to the office photocopier.");
System.out.print("Enter the number of copies you want to make: "); 
int numberOfCopies = exercise.nextInt();

// Calculating total cost

int totalCost = numberOfCopies * costPerCopy;

// Displaying results

System.out.println("The total cost for " + numberOfCopies + " copies is $" + totalCost);
System.out.println("A total of " + numberOfCopies + " copies have been made in this operation.");

// Closing scanner 

exercise.close();

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