Lab04 Packaging
Lab04 Packaging
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
Packages
Lab Exercise
Packages
Begin to explore the effect of packaging on access to data and methods in classes
Scenario
The manager of the Acme order system had been pleased with the results of your
work so far, but then he took a look at the source code organization and wasn’t so
happy! “Get that stuff organized!” he demands.
You decide to organize the code into several packages. Business classes like Order
will go into a com.acme.domain package. Utility code, like the MyDate class, will go
into com.acme.utils package. Finally, testing code will go into a com.acme.testing
package.
1.1 In the Package Explorer view, right-click on the src folder in the
AcmeOrderSystem project and select New > Package.
1.2 In the resulting New Java Package window, enter com.acme.domain as the
package name for the new package and click the Finish button.
This should result in a new package displayed in the Package Explorer view.
Notice that in the Package Explorer view, your existing classes are all in a single
“(default package)”. In this step, you move the classes to their appropriate package.
2.1 Close all open editors. From the Eclipse menu bar, select File > Close All.
2.2.2 This will cause the file to be listed as part of the com.acme.domain package.
Note: Moving Order to the com.acme.domain package also creates compiler errors in
other areas of your code! Do you know why? Don’t worry, as you fix these problems
later.
2.2.3 Open the Order.java file in an editor view by double-clicking on the file name
in the Package Explorer view. What changes did this move create in your code?
Notice the package and import lines of code at the top of the file.
package com.acme.domain;
import MyDate;
2.3.2 This time you should also be confronted with a warning window as shown
below.
Note: With the last class moved, you may notice that the default package is removed
from the Package Explorer view!
package com.acme.domain;
import com.acme.utils.MyDate;
package com.acme.testing;
2.6.2 Notice that since TestOrders has to work with both MyDate objects and
Order objects, it has two import statements.
import com.acme.domain.Order;
import com.acme.utils.MyDate;
Some of the errors created in all the moving of classes to the different packages
went away, but compiler errors in the test classes (TestOrders and TestMyDate) still
remain. You must now fix these issues.
3.1.1 Notice that there are three compile errors (maybe more depending on what
bonus labs you completed). What is the cause of these errors?
3.1.2 The errors have to do with the fact that the MyDate object fields (month, day,
and year) are not public and therefore not accessible to TestMyDate once it moved
to a different package.
3.1.3 You will cover access to data in objects a little later in this class. How do you
fix these errors now? Go to the next step to see.
3.2.2 This change should make the compiler errors in TestMyDate.java go away.
3.3 Open TestOrders.java in an editor view. Notice that the same type of errors
exist in this test class for the static variable taxRate.
3.4 Open Order.java in an editor view. Make taxRate public too. This should
make the other compiler errors go away.
Run both TestMyDate and TestOrders to ensure all of your code is still working
correctly after the package reorganization.
Lab Solution
Order.java
package com.acme.domain;
import com.acme.utils.MyDate;
MyDate.java
package com.acme.utils;
// 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;
}
}
TestOrders.java
package com.acme.testing;
import com.acme.domain.Order;
import com.acme.utils.MyDate;
System.out.println(anvil);
System.out.println(balloons);
Order.setTaxRate(0.06);
System.out.println("The tax rate is currently: " +
Order.taxRate);
Order.computeTaxOn(3000.00);
anvil.computeTax();
balloons.computeTax();
}
TestMyDate.java
package com.acme.testing;
import com.acme.utils.MyDate;
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
Bonus Lab
Another team at Acme has found out about the wonderful job you are doing with the
new order system. They would like to get your code to start experimenting with it,
so you need to create a JAR file with your current code.
5.2 In the Export window, expand the Java type, select JAR file (as shown in the
image below), and click the Next> button.
5.3 In the JAR Export window, uncheck the .classpath file and .project file.
Check the Export Java source files and resources option and provide a
location for the JAR file and click the Finish button.
5.4 In the destination location, you should find a JAR file. You can use a zip
utility to open the file and ensure its contents include your project’s Java
source and class files.