Search icon CANCEL
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 2. Variables, Data Types, and Operators FREE CHAPTER 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

Packages


Packages are namespaces in Java that can be used to avoid name collisions when you have more than one class with the same name.

For example, we might have more than one class named Student being developed by Sam and another class with the same name being developed by David. We need a way to differentiate between the two classes if we need to use them in our code. We use packages to put the two classes in two different namespaces.

For example, we might have the two classes in two packages:

  • sam.Student

  • david.Student

The two packages look as follows in File Explorer:

Figure 1.3: Screenshot of the sam.Student and david.Student packages in File Explorer

All the classes that are fundamental to the Java language belong to the java.lang package. All the classes that contain utility classes in Java, such as collections, classes for localization, and time utilities, belong to the java.util package.

As a programmer, you can create and use your own packages.

Rules to Follow When Using Packages

Here are a few rules to be considered while using packages:

  • Packages are written in lowercase

  • To avoid name conflicts, the package name should be the reverse domain of the company. For example, if the company domain is example.com, then the package name should be com.example. So, if we have a Student class in that package, the class can be accessed with com.example.Student.

  • Package names should correspond to folder names. For the preceding example, the folder structure would be as follows:

    Figure 1.4: Screenshot of the folder structure in File Explorer

To use a class from a package in your code, you need to import the class at the top of your Java file. For example, to use the Student class, you would import it as follows:

import com.example.Student;
public class MyClass {

}

Scanner is a useful class in the java.util package. It is an easy way of inputting types, such as int or strings. As we saw in an earlier exercise, the packages use nextInt() to input an integer with the following syntax:

sc = new Scanner(System.in);
int x =  sc.nextIn()

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

To read two numbers from the user and print their sum, perform the following steps:

  1. Create a new class and enter ReadScanner as the class name

  2. Import the java.util.Scanner package

  3. In the main() use System.out.print to ask the user to enter two numbers of variables a and b.

  4. Use System.out.println to output the sum of the two numbers.

  5. Run the main program.

    The output should be similar to this:

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

Note

The solution for this activity can be found on page 304.

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

Users expect to see the daily percentage of increase or decrease of financial instruments such as stocks and foreign currency. We will ask the user for the stock symbol, the value of the stock on day 1, the value of the same stock on day 2, calculate the percent change and print it in a nicely formatted way. To achieve this, perform the following steps:

  1. Create a new class and enter StockChangeCalculator as the class name

  2. Import the java.util.Scanner package:

  3. In the main() use System.out.print to ask the user for the symbol of the stock, followed by the day1 and day2 values of the stock.

  4. Calculate the percentChange value.

  5. Use System.out.println to output the symbol and the percent change with two decimal digits.

  6. 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.

    Note

    The solution for this activity can be found on page 305.

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 €18.99/month. Cancel anytime