Book Image

Java Fundamentals

By : Gazihan Alankus, Rogério Theodoro de Brito, Basheer Ahamed Fazal, Vinicius Isola, Miles Obare
Book Image

Java Fundamentals

By: Gazihan Alankus, Rogério Theodoro de Brito, Basheer Ahamed Fazal, Vinicius Isola, Miles Obare

Overview of this book

Since its inception, Java has stormed the programming world. Its features and functionalities provide developers with the tools needed to write robust cross-platform applications. Java Fundamentals introduces you to these tools and functionalities that will enable you to create Java programs. The book begins with an introduction to the language, its philosophy, and evolution over time, until the latest release. You'll learn how the javac/java tools work and what Java packages are - the way a Java program is usually organized. Once you are comfortable with this, you'll be introduced to advanced concepts of the language, such as control flow keywords. You'll explore object-oriented programming and the part it plays in making Java what it is. In the concluding chapters, you'll get to grips with classes, typecasting, and interfaces, and understand the use of data structures, arrays, strings, handling exceptions, and creating generics. By the end of this book, you will have learned to write programs, automate tasks, and follow advanced courses on algorithms and data structures or explore more advanced Java courses.
Table of Contents (12 chapters)
Java Fundamentals
Preface

Lesson 1: Introduction to Java


Activity 1: Printing the Results of Simple Arithmetic Operations

Solution:

  1. Create a class named Operations as follows:

    public class Operations
    {
  2. Within main(), print a sentence describing the operation on the values you will be performing along with the result:

        public static void main(String[] args) {
            System.out.println("The sum of 3 + 4 is " + (3 + 4));
            System.out.println("The product of 3 + 4 is " + (3 * 4));
        }
    }

    The output will be as follows:

    The sum of 3 + 4 is 7
    The product of 3 + 4 is 12

Activity 2: Reading Values from the User and Performing Operations Using the Scanner Class.

Solution:

  1. Right-click the src folder and select New | Class.

  2. Enter ReadScanner as the class name, and then click OK.

  3. Import the java.util.Scanner package:

    import java.util.Scanner;
  4. In the main() enter the following:

    public class ReadScanner
    {
        static Scanner sc = new Scanner(System.in);
      public static void main(String[] args) {
        System.out.print("Enter a number: ");
        int a = sc.nextInt();
        System.out.print("Enter 2nd number: ");
        int b = sc.nextInt();
        System.out.println("The sum is " + (a + b) + ".");
    
        }
    }
  5. Run the main program.

    The output will be as follows:

    Enter a number: 12                                                                                                             
    Enter 2nd number: 23
    The sum is 35.

Activity 3: Calculating the Percent Increase or Decrease of Financial Instruments

Solution:

  1. Right-click the src folder and select New | Class.

  2. Enter StockChangeCalculator as the class name, and then click OK.

  3. Import the java.util.Scanner package:

    import java.util.Scanner;
  4. In the main() enter the following:

    public class StockChangeCalculator{
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.print("Enter the stock symbol: ");
        String symbol = sc.nextLine();
        System.out.printf("Enter %s's day 1 value: ", symbol);
        double day1 = sc.nextDouble();
        System.out.printf("Enter %s's day 2 value: ", symbol);
        double day2 = sc.nextDouble();
        double percentChange = 100 * (day2 - day1) / day1;
        System.out.printf("%s has changed %.2f%% in one day.", symbol, percentChange);
    }
    }
  5. Run the main program.

    The output should be similar to:

    Enter the stock symbol: AAPL                                                                                                             
    Enter AAPL's day 1 value: 100                                                                                                           
    Enter AAPL's day 2 value: 91.5                                                                                                           
    AAPL has changed -8.50% in one day.