Creating Packages and Importing Classes The purpose of this lab is for you to learn how to become proficient in creating packages in Java and being able to import these packages into classes in other packages. Management of different source code files and folders is key to programming organization, scalable project size, and promoting reusability in software development. Steps: Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week 6 lecture, create six packages: Main, add, subtract, multiply, divide, and modulo. ALL of these packages should be within the “src” folder in the Eclipse package Explorer. ALL of the packages should be on the same “level” in the file hierarchy. In the “Main” package, create the “Lab04.java” file and add the needed boilerplate (“Hello, World!” style) code to create the main method for the application. For each of the arithmetic packages (add, subtract, multiply, divide, and modulo), create a single class (you should name these files the same as the package name but capitalized; e.g. “Add.java”, “Subtract.java”, “Multiply.java”, etc.) Follow the specifications below for detailed implementation details: Lab 4 Specifications Inside each of the arithmetic classes (Add, Subtract, Multiply, Divide, Modulo), make sure EACH class contains the following: Two private fields // Example Fields: private int n1; private int n2; A constructor that passes two values that can be saved in the private fields // Example Constructor: public Add(int n1, int n2){ this.n1 = n1; this.n2 = n2; } The following three methods must be supported by ALL arithmetic classes: public int getResult(); // This should return the result of n1 arithmetic operator n2 // n1 + n2 for add public void changeVals(int n1, int n2); // This should change or “reset” the values in the // private class fields public String toString(); // This returns a string that displays the equation with result // e.g. “3 + 5 = 8” Inside the “Lab04.java” file should contain the following: All package and import statements needed to reference the other packages and classes. Inside the main method, create class objects for each of arithmetic classes // Example: Add a = new Add(5, 3); For each class object, make sure to ALSO print out the toString method of each to verify the current equation, call the method to change the values, and print out the toString method again to verify the changes. // Example: System.out.println(a.toString()); a.changeVals(4, 4); System.out.println(a.toString()); Verify, by running the program, that everything works as expected.
Creating Packages and Importing Classes
The purpose of this lab is for you to learn how to become proficient in creating packages in Java and being able to import these packages into classes in other packages. Management of different source code files and folders is key to programming organization, scalable project size, and promoting reusability in software development.
Steps:
- Create a new Java Project named “Packages” from within Eclipse.
- Following the example in the Week 6 lecture, create six packages: Main, add, subtract, multiply, divide, and modulo. ALL of these packages should be within the “src” folder in the Eclipse package Explorer. ALL of the packages should be on the same “level” in the file hierarchy.
- In the “Main” package, create the “Lab04.java” file and add the needed boilerplate (“Hello, World!” style) code to create the main method for the application.
- For each of the arithmetic packages (add, subtract, multiply, divide, and modulo), create a single class (you should name these files the same as the package name but capitalized; e.g. “Add.java”, “Subtract.java”, “Multiply.java”, etc.)
- Follow the specifications below for detailed implementation details:
Lab 4 Specifications
Inside each of the arithmetic classes (Add, Subtract, Multiply, Divide, Modulo), make sure EACH class contains the following:
Two private fields
// Example Fields:
private int n1;
private int n2;
A constructor that passes two values that can be saved in the private fields
// Example Constructor:
public Add(int n1, int n2){
this.n1 = n1;
this.n2 = n2;
}
The following three methods must be supported by ALL arithmetic classes:
public int getResult(); // This should return the result of n1 arithmetic operator n2 // n1 + n2 for add
public void changeVals(int n1, int n2); // This should change or “reset” the values in the
// private class fields
public String toString(); // This returns a string that displays the equation with result
// e.g. “3 + 5 = 8”
Inside the “Lab04.java” file should contain the following:
All package and import statements needed to reference the other packages and classes.
Inside the main method, create class objects for each of arithmetic classes
// Example:
Add a = new Add(5, 3);
For each class object, make sure to ALSO print out the toString method of each to verify the current equation, call the method to change the values, and print out the toString method again to verify the changes.
// Example:
System.out.println(a.toString());
a.changeVals(4, 4);
System.out.println(a.toString());
Verify, by running the program, that everything works as expected.
Step by step
Solved in 4 steps with 2 images