(JAVA) (OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide Exam 1Z0-808)
(JAVA) (OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide Exam 1Z0-808)
About Intertech
Thank you for choosing Intertech for your training from Udemy. The next page of this document
will get you started with your lab if you’d like to skip ahead. Below is a brief overview of
Intertech as well as a promo code for you for future live Intertech trainings.
Our training organization offers live in-classroom and online deliveries, private on-site deliveries,
and on-demand options such as the course in which you’ve enrolled from Udemy. We cover a
broad spectrum of .NET, Java, Agile/Scrum, Web Development, and Mobile technologies. See
more information on our training and search for courses by clicking here.
As a Udemy customer, you can save on any live in-classroom, live online, or onsite training with
Intertech as well. Just use promo code “Udemy_Labs” when enrolling and save 35% on the
course price.
Page 1
Creating Classes
Lab Exercise
Creating Classes
Java classes are the fundamental building blocks in Java applications. In this lab, you
create a Java class that is used to create many Java objects. The objects are used to
hold data and to invoke actions in the form of methods.
Test your definition of the MyDate class with a previously coded test class
Scenario
Congratulations! You have just been hired by the Acme Company, a worldwide
conglomerate with a diverse product portfolio, from adding machines to X-ray
machines (see http://en.wikipedia.org/wiki/Acme_Corporation). One of its best-
known products is the ever-popular Acme anvil.
Over the remaining labs, as a member of Acme, you are going to help Acme build a
product order system.
Particularly, in this lab you make your first Java class for this project. A class defines
a type that is a template for objects. It is your job to create a utility class called
MyDate. An application that will use and test your version of the MyDate type has
already been created. The test code appears below.
In this step, you create a MyDate type. The Date type already exists in Java (see
java.util.Date and java.util.Calendar). However, you create another much simpler
date type called MyDate to demonstrate object-orientated programming (OOP)
concepts. You will modify MyDate later to demonstrate more advanced OOP
concepts.
1.1 Make a “Java Project” to store your new classes and code.
1.1.1 Select File > New > Java Project ...
1.1.2 In the window that appears, enter AcmeOrderSystem as the project name
and click the Finish button.
1.2.2 In the dialog window that displays, enter MyDate as the name of the class,
and press the Finish button.
Note: Don’t add a main method to MyDate, as it is not the application’s starting point.
This should create a MyDate.java file in the src folder of the project (under the
default package) and open a MyDate.java editor for you to add code.
1.3 Add day, month, and year attributes to MyDate. These three attributes
(member variables) should be of type int.
int day;
int year;
int month;
public MyDate(){}
2.2 Add a constructor that has three integer arguments (parameters). This will
enable the user to create a MyDate using a constructor like this: new
MyDate(2, 6, 2004)
2.3 Save your file, and fix any compiler errors before moving to the next step.
The MyDate class you have created needs two methods. Recall that a method
represents an action or something an object can do. Users of MyDate should be able
to see the dates represented by the object. Therefore, you must provide a method,
called toString( ), that turns the MyDate object into a String. Users of MyDate should
also be able to reset the date represented by MyDate by feeding in three parameters,
so you must create a setDate( ) method.
3.1 In the MyDate.java editor, add a toString( ) method. The code below shows
a partially completed method. The method should return a String that
contains the values of day, month, and year.
Note: Java Strings can be concatenated using the + symbol like “Cat” + “Dog”, so the
month and day can be concatenated as month + “/” + day.
3.2 Create a setDate(m, d, y) method. The setDate( ) method enables the user
to call one method to set day, month, and year of a MyDate object. An
empty version of this method appears below.
A program is provided to test your new class. It appeared at the beginning of this
lab. You don’t need to write this program, as it is available in the lab folder.
4.1.3 Back in the Eclipse IDE, right-click on the src folder in the AcmeOrderSystem
project, and request to paste the copied file into the project.
4.2 Fix any errors in MyDate or the TestMyDate file. The most likely place for
an error is in the MyDate type. If you didn’t follow the directions exactly,
the test program can fail to compile.
4.3.2 When you run the test, the output in the Console view should look like the
following. If it looks different, go back and try to fix it. Ask for help if you get stuck.
11/11/1918
11/11/1918
4/21/1968
Note: Throughout class, the lab book will show you the output of running your Java
application as shown in the block above. In most cases, your output should be
identical to that shown in the lab book. However, there may be cases, especially later
in class, where your output will vary slightly. This may be due to a number or
circumstances such as parameters/data used or how many of the bonus labs you
choose to complete.
Lab Solution
MyDate.java
// Constructors:
// 1. Same name as the class
// 2. No return type
//Methods
public String toString(){
return month + "/" + day + "/" + year;
}
public void setDate(int m, int d, int y){
day = d;
year = y;
month = m;
}
}
TestMyDate.java
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
Bonus Lab
Add an initialization block to MyDate that defaults the day, month, and year to
January 1, 2000. After completing the initialization block, add code similar to that
shown below to test your initialization block in the main method of
TestMyDate.java.