Learn Java Easy. Chapter 3 Exercises

Chapter 3 Exercises

Exercise 1. Create a program that allows us to input an integer number and then display it on 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. We’ll assume that 1 euro equals 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: " + (a * 1.10));

Exercise 3. Create four double variables and name them a, b, c, and d. The program should ask to input the data for each variable from the keyboard. Then, the program will multiply a by b, divide the result by c, and add d to all of it. The result should be displayed in the console.

 

Solution:

 

Scanner exercise = new Scanner(System.in);
System.out.println("Enter value a");
double a = exercise.nextDouble();
System.out.println("Enter value b");
double b = exercise.nextDouble();
System.out.println("Enter value c");
double c = exercise.nextDouble();
System.out.println("Enter value d");
double d = exercise.nextDouble();
System.out.println("((a * b)/c) + d = " + (((a * b) / c) + d));

πŸ’‘ Scanner provides specific methods for reading different data types, 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 for the height and width measurements. Remember that the area of a rectangle equals 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 the base times the 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 you input the total amount of an invoice and it tells you how much VAT has been paid. The tax rate is 16%.

 

Solution:

 

Scanner exercise = new Scanner(System.in);
System.out.println("Enter the invoice total");
double total = exercise.nextDouble();
System.out.println("The net value is " + (total * 0.84) + " euros");
System.out.println("The VAT amount is " + (total * 0.16) + " euros");
System.out.println("The total amount is " + total + " euros");

Exercise 7. Create a program that calculates the weekly salary of a worker based on the hours worked. Up to 40 hours, they earn 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 Celsius degrees:");
double celsius = exercise.nextDouble();
System.out.println("The temperature in Fahrenheit is: " + ((celsius * 9 / 5) + 32));

Exercise 9: Create a program that asks us to enter a symbol. We will store it in a char variable. Then, the program should display the symbol in 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’s important to close the Scanner object with the close() method when finishing its use to properly release system resources. This prevents possible 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 will calculate the total cost. It will also keep track of the total copies made during the day.

 

Solution:

 


Scanner exercise = new Scanner(System.in);

// Variables
int costPerCopy = 5; // Cost per copy

// Keyboard data 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! 😏