Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java Fundamentals

You're reading from   Java Fundamentals A fast-paced and pragmatic introduction to one of the world's most popular programming languages

Arrow left icon
Product type Paperback
Published in Mar 2019
Publisher
ISBN-13 9781789801736
Length 408 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (5):
Arrow left icon
Miles Obare Miles Obare
Author Profile Icon Miles Obare
Miles Obare
Basheer Ahamed Fazal Basheer Ahamed Fazal
Author Profile Icon Basheer Ahamed Fazal
Basheer Ahamed Fazal
Rogério Theodoro de Brito Rogério Theodoro de Brito
Author Profile Icon Rogério Theodoro de Brito
Rogério Theodoro de Brito
Gazihan Alankus Gazihan Alankus
Author Profile Icon Gazihan Alankus
Gazihan Alankus
Vinicius Isola Vinicius Isola
Author Profile Icon Vinicius Isola
Vinicius Isola
+1 more Show less
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Java Fundamentals
Preface
1. Introduction to Java FREE CHAPTER 2. Variables, Data Types, and Operators 3. Control Flow 4. Object-Oriented Programming 5. OOP in Depth 6. Data Structures, Arrays, and Strings 7. The Java Collections Framework and Generics 8. Advanced Data Structures in Java 9. Exception Handling Appendix

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.
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image