Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Java Core Concepts Wrap-up

This section covered the fundamental building blocks of the Java programming language. You learned about:

  • Variables: How to declare and initialize variables of different data types.
  • Control Flow: How to use if, else, switch, for, and while to control the flow of your program.
  • Methods: How to define and call methods to organize your code.
  • Classes and Objects: The basics of object-oriented programming in Java.
  • Enums: How to use enums to define a set of constants.

Exercise

Write a simple Java program that simulates a basic calculator. The program should be able to perform addition, subtraction, multiplication, and division.

Requirements:

  1. Create a Calculator class.
  2. The Calculator class should have methods for add, subtract, multiply, and divide.
  3. Create a Main class with a main method to test your Calculator.
  4. In the main method, create an instance of the Calculator class and call its methods with some sample numbers.
  5. Print the results of the calculations to the console.
  6. Use a switch statement to select the operation to perform based on a variable.