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
Java 9 with JShell
Java 9 with JShell

Java 9 with JShell: Introducing the full range of Java 9's new features via JShell

Arrow left icon
Profile Icon Gaston C. Hillar
Arrow right icon
€18.99 per month
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2 (2 Ratings)
Paperback Mar 2017 408 pages 1st Edition
eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Gaston C. Hillar
Arrow right icon
€18.99 per month
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2 (2 Ratings)
Paperback Mar 2017 408 pages 1st Edition
eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Java 9 with JShell

Chapter 2. Real-World Objects to UML Diagrams and Java 9 via JShell

In this chapter, we will learn how to recognize objects from real-life situations. We will understand that working with objects makes it simpler to write code that is easier to understand and reuse. We will learn how to recognize real-world elements and translate them into the different components of the object-oriented paradigm supported in Java 9. We will:

  • Identify objects from applications requirements
  • Capture objects from the real world
  • Generate classes to create objects
  • Recognize variables and constants to create fields
  • Identify actions to create methods
  • Organize classes with UML diagrams
  • Use feedback from domain experts to improve our classes
  • Work with Java objects in JShell

Identifying objects from applications requirements

Whenever you have to solve a problem in the real world, you use elements and interact with them. For example, when you are thirsty, you take a glass, fill it up with water, soda, or your favorite juice, and then you drink. Similarly, you can easily recognize elements, known as objects, from real-world scenarios and then translate them into object-oriented code. We will start learning the principles of object-oriented programming to use them in the Java 9 programming language to develop any kind of applications.

Now, we will imagine we have to develop a RESTful Web Service that will be consumed by mobile apps and a web application. These apps and applications will have different user interfaces and diverse user experiences. However, we don't have to worry about these differences because we will be focused on the Web Service, that is, we will be backend developers.

Artists use different combinations of geometric shapes and organic shapes...

Capturing real-world objects

We could easily recognize objects from Pitstop's artwork. We understood that each pattern is composed of many 2D geometric shapes and we recognized the different shapes that she used in all the examples we analyzed. Now, let's focus on one of the core requirements for the Web Service, which is calculating the required amounts of acrylic paint to produce the artwork. We must take into account the following data for each 2D shape included in the pattern in order to calculate the required materials and the amount of acrylic paint to produce each shape:

  • The line color
  • The perimeter
  • The fill color
  • The area

It is possible to use a specific color for the line that draws the borders of each shape, and therefore, we have to calculate the perimeter to use it as one of the values that will allow us to estimate the amount of acrylic paint that the user must buy to paint the border of each 2D shape. Then, we have to calculate the area to use it as one of the values...

Generating classes to create objects

Imagine that we have to draw and calculate the perimeters and areas of three different rectangles. You will end up with three rectangles drawn with their widths and height values and their calculated perimeters and areas. It would be great to have a blueprint to simplify the process of drawing each rectangle with their different width and height values.

In object-oriented programming, a class is a template definition or blueprint from which objects are created. Classes are models that define the state and behavior of an object. After declaring a class that defines the state and behavior of a rectangle, we can use it to generate objects that represent the state and behavior of each real-world rectangle.

Note

Objects are also known as instances. For example, we can say each rectangle object is an instance of the Rectangle class.

The following picture shows two rectangle instances named rectangle1 and rectangle2. These instances are drawn with their width and...

Recognizing variables and constants

We know the information required for each of the shapes to achieve our goals. Now, we have to design the classes to include the necessary fields that provide the required data to each instance. We have to make sure that each class has the necessary fields that encapsulate all the data required by the objects to perform all the tasks based on our application domain.

Let's start with the Circle class. We need to know the radius for each instance of this class, that is, for each circle object. Thus, we need an encapsulated variable that allows each instance of the Circle class to specify the value for the radius.

Note

The variables defined in a class to encapsulate the data for each instance of the class in Java 9 are known as fields. Each instance has its own independent value for the fields defined in the class. The fields allow us to define the characteristics for an instance of the class. In other programming languages that support object-oriented...

Identifying actions to create methods

So far, we designed nine classes and identified the necessary fields for each of them. Now, it is time to add the necessary pieces of code that work with the previously defined fields to perform all the necessary tasks, that is, to calculate perimeters and areas. We have to make sure that each class has the necessary encapsulated functions that process the property values specified in the objects to perform all the tasks.

Let's forget a bit about similarities between the different classes. We will work with them individually as if we didn't have the necessary knowledge of geometric formulas. We will start with the Circle class. We need pieces of code that allow each instance of this class to use the value of the radius property to calculate the area and perimeter.

Tip

The functions defined in a class to encapsulate the behavior of each instance of the class are known as methods. Each instance can access the set of methods exposed by the class...

Organizing classes with UML diagrams

So far, our object-oriented solution includes nine classes with their fields and methods. However, if we take another look at these nine classes, we will notice that all of them have the same two methods: calculateArea and calculatePerimeter. The code for the methods in each class is different because each shape uses a special formula to calculate either the area or perimeter. However, the declarations, contracts, interfaces, or protocols for the methods are the same. Both methods have the same name, are always parameterless, and return a floating point value. Thus, all of them return the same type.

When we talked about the nine classes, we said we were talking about nine different geometrical 2D shapes or simply shapes. Thus, we can generalize the required behavior, protocol, or interface for these nine shapes. The nine shapes must define the calculateArea and calculatePerimeter methods with the previously explained declarations. We can create an interface...

Identifying objects from applications requirements


Whenever you have to solve a problem in the real world, you use elements and interact with them. For example, when you are thirsty, you take a glass, fill it up with water, soda, or your favorite juice, and then you drink. Similarly, you can easily recognize elements, known as objects, from real-world scenarios and then translate them into object-oriented code. We will start learning the principles of object-oriented programming to use them in the Java 9 programming language to develop any kind of applications.

Now, we will imagine we have to develop a RESTful Web Service that will be consumed by mobile apps and a web application. These apps and applications will have different user interfaces and diverse user experiences. However, we don't have to worry about these differences because we will be focused on the Web Service, that is, we will be backend developers.

Artists use different combinations of geometric shapes and organic shapes to...

Capturing real-world objects


We could easily recognize objects from Pitstop's artwork. We understood that each pattern is composed of many 2D geometric shapes and we recognized the different shapes that she used in all the examples we analyzed. Now, let's focus on one of the core requirements for the Web Service, which is calculating the required amounts of acrylic paint to produce the artwork. We must take into account the following data for each 2D shape included in the pattern in order to calculate the required materials and the amount of acrylic paint to produce each shape:

  • The line color

  • The perimeter

  • The fill color

  • The area

It is possible to use a specific color for the line that draws the borders of each shape, and therefore, we have to calculate the perimeter to use it as one of the values that will allow us to estimate the amount of acrylic paint that the user must buy to paint the border of each 2D shape. Then, we have to calculate the area to use it as one of the values that will...

Generating classes to create objects


Imagine that we have to draw and calculate the perimeters and areas of three different rectangles. You will end up with three rectangles drawn with their widths and height values and their calculated perimeters and areas. It would be great to have a blueprint to simplify the process of drawing each rectangle with their different width and height values.

In object-oriented programming, a class is a template definition or blueprint from which objects are created. Classes are models that define the state and behavior of an object. After declaring a class that defines the state and behavior of a rectangle, we can use it to generate objects that represent the state and behavior of each real-world rectangle.

Note

Objects are also known as instances. For example, we can say each rectangle object is an instance of the Rectangle class.

The following picture shows two rectangle instances named rectangle1 and rectangle2. These instances are drawn with their width and...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • • A full account of Java 9’s new features
  • • This tutorial emphasises fluency using JShell exercises
  • • Get a thorough introduction to contract programming code reuse via Java generics
  • • Learn how to use the new module system
  • • How to use proper functional programming style inside Java 9

Description

The release of Java 9 has brought many subtle and not-so-subtle changes to the way in which Java programmers approach their code. The most important ones are definitely the availability of a REPL, known as JShell, which will make experiments and prototyping much more straightforward than the old IDE-based project-led approach. Another, more subtle change can be seen in the module system, which will lead to more modularized, maintainable code. The techniques to take full advantage of object-oriented code, functional programming and the new modularity features in Java 9 form the main subjects of this book. Each chapter will add to the full picture of Java 9 programming starting out with classes and instances and ending with generics and modularity in Java.

Who is this book for?

This book can be understood by anyone who is a graduate of computer science or someone who has just begun working as a software engineer. Basically, an understanding of an object-oriented programming language like Python, C++ or indeed, an earlier Java version is sufficient. It would be helpful to have participated in the full product cycle of a software engineering project.

What you will learn

  • • Engage with object-oriented programming in Java 9, starting with code snippets in JShell
  • • Optimize your code, applying functional programming features
  • • Discover the advantages of modularity
  • • Become very proficient at using JShell itself
  • • Learn the new approach to Java programming, which uses the REPL as a prototyping tool

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 29, 2017
Length: 408 pages
Edition : 1st
Language : English
ISBN-13 : 9781787282841
Vendor :
Oracle
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Mar 29, 2017
Length: 408 pages
Edition : 1st
Language : English
ISBN-13 : 9781787282841
Vendor :
Oracle
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 111.97
Java 9 Data Structures and Algorithms
€32.99
Java 9 Programming By Example
€36.99
Java 9 with JShell
€41.99
Total 111.97 Stars icon
Banner background image

Table of Contents

15 Chapters
1. JShell – A Read-Evaluate-Print-Loop for Java 9 Chevron down icon Chevron up icon
2. Real-World Objects to UML Diagrams and Java 9 via JShell Chevron down icon Chevron up icon
3. Classes and Instances Chevron down icon Chevron up icon
4. Encapsulation of Data Chevron down icon Chevron up icon
5. Mutable and Immutable Classes Chevron down icon Chevron up icon
6. Inheritance, Abstraction, Extension, and Specialization Chevron down icon Chevron up icon
7. Members Inheritance and Polymorphism Chevron down icon Chevron up icon
8. Contract Programming with Interfaces Chevron down icon Chevron up icon
9. Advanced Contract Programming with Interfaces Chevron down icon Chevron up icon
10. Maximization of Code Reuse with Generics Chevron down icon Chevron up icon
11. Advanced Generics Chevron down icon Chevron up icon
12. Object-Oriented, Functional Programming, and Lambda Expressions Chevron down icon Chevron up icon
13. Modularity in Java 9 Chevron down icon Chevron up icon
A. Exercise Answers Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
(2 Ratings)
5 star 0%
4 star 0%
3 star 50%
2 star 0%
1 star 50%
ares09x Apr 30, 2017
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Three things:1) The author seems to assume that the reader knows something about O-O languages; it's definitely not for those new to O-O.2) There are a few errors in the end-of-chapter "test your knowledge" questions.3) There's a major problem in Chapter 9 - the section called "Downcasting with interfaces and classes" is not about downcasting, it's about upcasting. When you go up a class or interface hierarchy, as the examples in this section do, from a more specific class or interface to a more general one, you are upcasting. Downcasting is treating an object that is declared as a more general type as if it were a more specific type. So, in the author's example, treating a SpiderDog object as though it were a DrawableInComic object is upcasting; treating an object declared to be of type DrawableInComic as though it were a SpiderDog object is downcasting.Note: this problem may be corrected in the paperback version, I obtained a pdf version of this book.
Amazon Verified review Amazon
Markus W. Apr 27, 2017
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
"Introducing the full range of Java 9's new features via JShell" is the subtitle of this book.More than half of the book is about basic concepts of object oriented programming. For a professional knowing his/her Java these chapters are completely useless.Neither default methods in interfaces nor generics or functional programming with lambda are new to Java 9. The new Stream methods iterate, takeWhile, dropWhile, and ofNullable to be introduced with Java 9 are not even mentioned.The pages are filled with repeated examples and how they are evaluated/displayed in JShell (pleeease, show me one or two examples, not the same five times with different values).But nonetheless learners would have a hard time with this book, too, because the author repeatedly uses concepts without introducing them properly. Ten pages with skecthes of geometric shapes shall lead you to abstraction and inheritance, without defining these concepts at this point. Base classes and interfaces are mixed chapters before they are discussed in detail. The author talks about the benefits of constructors, switches to garbage collection, and only afterwards really tells you what a constructor is.And more basic concepts (data types, variables, methods, conditions, loops) are missing completely, so no chance to learn the language with this book. Even collections appear at one point in the examples out of nowhere.Furthermore some of the presented ideas are misleading. Using immutable objects to prevent problems with concurrent programs is neither the solution to thread safe programs nor the sense of immutable objects.The description of the Jigsaw modularity system new to Java 9 lacks important features, e.g. how to integrate new code with existing one (designed for Java 8 or lower) or how to open modules for reflection, and only provides a basic idea.Even the introduction to JShell is quite basic, not a word about how to set the classpath or modulepath in JShell--which you will need to work with this tool productively.Other changes in the tooling around Java 9 or new API components are completely ignored.Of course, this is an early shot at Java 9 (and an expensive one), but neither will it present you the full range of new features nor is it an introduction for starters. Maybe programmers proficient in other languages but new to Java could benefit from the book. But even for them there will be better introductions to Java 9 soon.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.