Blake's 21 Days Experience - Original Day 1 and Appendix A

Day 1, Getting Started with Java

Appendix A - First Try

Appendix A - Second Try's a Charm


Reserve for the continuation of Chapter One

120

Paths to different files


Creating the Second Java program from this book

    Only do this if you didn't create the last program.
    page 019 It will be useful to have a project to hold the classes you create in this book
  1. Choose the menu command File, New Project. The New Project dialog appears.
  2. In the Categories pane, choose Java.
  3. In the Projects pane, choose Java Application and click Next. The New Java Application dialog opens.
  4. In the Project Name text field, enter the name of the project (I used Java21). The Project Folder field is updated as you type the name. Make a note of this folder, it's where your Java programs can be found on your computer.
  5. Deselect the check box Create Main Class.
  6. Click Finish.
    page 020 To begin your first class, run NetBeans and start a new prgram.
  1. Choose the menu command File, New File. The New File dialog appears.
  2. In the Categories pane, choose Java.
  3. In the File Types pane, choose Empty Java File and click Next. The Empty Java File dialog opens.
  4. In the Class Name text field, enter MarsRobot. The file you're creating is shown in the Created File field, which can't be edited. This file has the name MarsRobot.java.
  5. Click Finish.
  1. class MarsRobot {
  2.     String status;
  3.     int speed;
  4.     float temperature;
  5.     
  6.     void checkTemperature() {
  7.         if (Temperature < -80) {
  8.             status = "return home";
  9.             speed = 5;
  10.         }
  11.     }
  12.     
  13.     void showAttributes() {
  14.         System.out.println("Status: " + status);
  15.         System.out.println("Speed: " + speed);
  16.         System.out.println("Temperature: " + temperature);
  17.     }
  18. }
121

Screenshots for the instructions above

122

"File" from the menu bar, "New File" from the submenu.

123

The "New File" dialog box

124

The "New File" dialog box

125

The "New Empty Java File" dialog box

126

The "New Empty Java File" dialog box

127

The MarsRobot.java tab is created & displayed

128

after typing in the code, note line 7 "Temparature" should not be capitalized

129

Getting ready to save

130

The "Save All" button is now greyed out

131

Fixing my Typo

133

About the MarsRobot Class

About the Three Instance Variables

Page 21 - The First Instance Method ...

Page 22 - The Second Instance Method ...

Time to save the file if you haven't done so.


Why You Cannot Run This Program

Two ways to put MarsRobot to use

The first option is chosen for this exercise


To begin the second class follow these steps in NetBeans.

  1. Choose File, New File from the menu. The New File dialog opens.
  2. In the Category pane choose Java
  3. In the File Types pane, choose Empty Java File and click Next. The Empty Java File dialog opens
  4. In the Class Name text field, enter MarsApplication. The file you're creating is shown in the Created File field and has the name MarsApplication.java.
  5. Click Finish.

Listing 1.2

class MarsApplication {
    public static void main(String[] arguments) {
        MarsRobot spirit = new MarsRobot();
        spirit.status = "exploring";
        spirit.speed = 2;
        spirit.temperature = -60;

        spirit.showAttributes();
        System.out.println("Increasing speed to 3.");
        spirit.speed = 3;
        spirit.showAttributes();
        System.out.println("Changing temperature to -90.");
        spirit.temperature = -90;
        spirit.showAttributes();
        System.out.println("Changing the temperature.");
        spirit.checkTemperature();
        spirit.showAttributes();
    }
}

Run the Program

135

When I run the program I get the wrong output.

140

Now I have the correct output.

141

Displaying the Java code for the MarsApplication: Program or Class

137

About the Java code for the MarsApplication: Program or Class


Organizing Classes and Class Behavior

Inheritance - is a mechanism that enables one class to inhert the behavior and attributes of another class

Interfaces - is a collection of methods that indicate a class has some behavior in addition to what it inherits from its superclasses

Packages - enable groups of classes to be referrenced more easily in other classes



Summary

Q&A

Quiz

Questions

  1. What is another word for class?
    1. Object
    2. Template
    3. Instance
  2. When you create a subclass, what must you define about that class?
    1. Nothing. Everything is defined already.
    2. Things that are different from its superclass
    3. Everything about the class
  3. What does an instance method of a class represent?
    1. The attributes of that class
    2. The behavior of that class
    3. The behavior of an object created from that class
  4. reserve

Answers

  1. B. A class is an abstract template used to create objects that are similar to each other.
  2. B. You define how the subclass is different from its superclass.
  3. C. Instance methods refer to a specific object's behavior. Class methods refer to the behavior of all objects belonging to that class.

Certification Practice

  1. All objects created from the same class must be identical.
  2. All objects created from the same class can have different attributes than each other.
  3. An object inherits attributes and behavior from the class used to create it.
  4. A class inherits attributes and behavior from its subclass.

Exercises

  1. In the main() method of the MarsRobot class, create a second MarsRobot robot named opportunity, set up its instance variables, and display them.
  2. Create an inheritance hierarchy for the pieces of a chess set.


End of Day One



Download JDK 8 - Not Successful

Safari

09

Firefox

10
10a

Questions that I answered for "Java SE Development Kit 8 Downloads" using the Firefox browser on my Apple


Download NetBeans - Not Successful

11
12
13
14
15

Another Failure

16

Java SE Development Kit 8 Downloads (8u112 for Mac OS X) - Success

10
020
021
022
023
024
025
026

Downloaded NetBeans IDE 8.0.2 using Java SE - Success

030
031
032
033
034
035
036
037
038
039
040
041

Using NetBeans

042

Splash Screen

043

first view of NetBeans

044

New Project window

045

Name and Location

046

After changing "Project Name"

047

After changing "Create Main Class"

048

view code for first time

049

after inserting new line of code

050

Compile Code

051

Run File

052

Output Pane first appears

053

Fixing Errors

the Before shot

057

the After shot

058

The yellow Tool Tip

059

replacing the semi-colon gets rid of the error warnings

060

after "Saving All" button pressed and "Run File" selected

061

Opening Sparticus.java to see the code

066

Sparticus.java displayed in the Projects pane, Start Page tab has focus

067

Sparticus.java displayed with it's submenu

068

Sparticus.java code is revealed

069

Creating a NetBeans alias and placing it into the Dock


Expanding and Shrinking a pane

071

Expanding and Shrinking a pane

072

Expanding and Shrinking a pane

073

Install the Java SE Development Kit 8u112 for Windows x64 on my Laptop

080
081
082
083
084
085
086
087
088

Installing NetBeans on the Laptop

090
091
092
093
094
095
096

Installation Folders

097
098
099

Setup Complete

100

First time viewing Netbeans on my Laptop

NetBeans seen from the Start Button

102

NetBeans seen from the Desktop

103

The Splash screen

104
105

The New Project button

107

After clicking the Next button

108

Changing Project Name

109

Changing Name to Create Main Class

110

First time seeing code

111

Adding the line of code

114

Compiling the Java Code

115
116

Run File

117
118

After updating OS X, to run Dreamwaeaver CS6, I had to do the following

630
632
634
636
638
640

From Version 23


Back to the Top ?