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

eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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 : 9781787285194
Vendor :
Oracle
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Mar 29, 2017
Length: 408 pages
Edition : 1st
Language : English
ISBN-13 : 9781787285194
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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.