Book Image

Learn Java with Projects

By : Dr. Seán Kennedy, Maaike van Putten
5 (3)
Book Image

Learn Java with Projects

5 (3)
By: Dr. Seán Kennedy, Maaike van Putten

Overview of this book

Learn Java with Projects stands out in the world of Java guides; while some books skim the surface and others get lost in too much detail, this one finds a nice middle ground. You’ll begin by exploring the fundamentals of Java, from its primitive data types through to loops and arrays. Next, you’ll move on to object-oriented programming (OOP), where you’ll get to grips with key topics such as classes, objects, encapsulation, inheritance, polymorphism, interfaces, and more. The chapters are designed in a way that focuses on topics that really matter in real-life work situations. No extra fluff here, so that you get more time to spend on the basics and form a solid foundation. As you make progress, you’ll learn advanced topics including generics, collections, lambda expressions, streams and concurrency. This book doesn't just talk about theory—it shows you how things work with little projects, which eventually add up to one big project that brings it all together. By the end of this Java book, you’ll have sound practical knowledge of Java and a helpful guide to walk you through the important parts of Java.
Table of Contents (22 chapters)
1
Part 1: Java Fundamentals
9
Part 2: Object-Oriented Programming
15
Part 3: Advanced Topics

Exercises

And that’s all theory for this chapter! So, roll up those sleeves, and let’s dive into your first day at Mesozoic Eden. Welcome aboard! Mesozoic Eden is a famous zoo where dinosaurs live that have been brought to live with high end genetic manipulation techniques. Here are some exercises for you to test your knowledge so far:

  1. Your first task involves welcoming our guests. Modify the following code snippet so that it outputs "Welcome to Mesozoic Eden":
    public class Main {
        public static void main(String[] args) {
            System.out.println("Hello world");
        }
    }
  2. Complete the following programs by filling out the blanks so that it prints out your name and the position you want to have in Mesozoic Eden 5 years from now:
    public class Main {
        public static void main(String[] args) {
            __________________________;
            String position = "Park Manager";
            System.out.println("My name is " + name + "
              and I want to be a " ______ " in Mesozoic
                Eden.");
        }
    }
  3. We’ve received some questions about opening hours. Complete the following program so that it prints the park’s opening and closing hours:
    public class Main {
        public static void main(String[] args) {
            String openingHours = "08:00";
            String closingHours = "20:00";
        }
    }
  4. Create a Java project with a package named dinosaur. You can create a package by right-clicking on the src/main/java folder, selecting “new” and choosing “package”.
  5. Modify the code from exercise 1 so that it prints out "Welcome, [YourName] to Mesozoic Eden!", where [YourName] is replaced by, surprise surprise, your name. Bonus: try to create a separate String variable as shown in the second and third exercises.
  6. Some guests reported feeling unsafe near the T-Rex. Let’s solve this by adding another System.out.println to the program of exercise 5. It should print the phrase "Mesozoic Eden is safe and secure." after the welcome message.