Question

In: Computer Science

Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week...

  • 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.

Solutions

Expert Solution

The Java programs for your requirement given below,

Project structure:

Add.java

package add;

public class Add {
   private int n1;
   private int n2;

   public Add(int n1, int n2) {

       this.n1 = n1;
       this.n2 = n2;
   }

   public int getResult() {
       return n1 + n2;
   }

   public void changeVals(int n1, int n2) {
       this.n1 = n1;
       this.n2 = n2;
   }

   public String toString() {
       return n1 + "+" + n2 + "=" + (n1 + n2);
   }

}
Subtract.java

package subtract;

public class Subtract {
   private int n1;
   private int n2;

   public Subtract(int n1, int n2) {

       this.n1 = n1;
       this.n2 = n2;
   }

   public int getResult() {
       return n1 - n2;
   }

   public void changeVals(int n1, int n2) {
       this.n1 = n1;
       this.n2 = n2;
   }

   public String toString() {
       return n1 + "-" + n2 + "=" + (n1 - n2);
   }

}
Multiply.java

package multiply;

public class Multiply {
   private int n1;
   private int n2;

   public Multiply(int n1, int n2) {

       this.n1 = n1;
       this.n2 = n2;
   }

   public int getResult() {
       return n1 * n2;
   }

   public void changeVals(int n1, int n2) {
       this.n1 = n1;
       this.n2 = n2;
   }

   public String toString() {
       return n1 + "*" + n2 + "=" + (n1 * n2);
   }

}
Divide.java

package divide;

public class Divide {
   private int n1;
   private int n2;

   public Divide(int n1, int n2) {

       this.n1 = n1;
       this.n2 = n2;
   }

   public int getResult() {
       return n1 / n2;
   }

   public void changeVals(int n1, int n2) {
       this.n1 = n1;
       this.n2 = n2;
   }

   public String toString() {
       return n1 + "/" + n2 + "=" + (n1 / n2);
   }

}
Modulo.java

package modulo;

public class Modulo {
   private int n1;
   private int n2;

   public Modulo(int n1, int n2) {

       this.n1 = n1;
       this.n2 = n2;
   }

   public int getResult() {
       return n1 % n2;
   }

   public void changeVals(int n1, int n2) {
       this.n1 = n1;
       this.n2 = n2;
   }

   public String toString() {
       return n1 + "%" + n2 + "=" + (n1 % n2);
   }

}
Lab04.java

package Main;

import add.Add;
import divide.Divide;
import modulo.Modulo;
import multiply.Multiply;
import subtract.Subtract;

public class Lab04 {
   public static void main(String args[]) {
       // addition
       Add a = new Add(5, 3);
       System.out.println(a.toString());
       a.changeVals(4, 4);
       System.out.println(a.toString());
       // subtraction
       Subtract s = new Subtract(5, 3);
       System.out.println(s.toString());
       s.changeVals(4, 4);
       System.out.println(s.toString());
       // multiplication
       Multiply m = new Multiply(5, 3);
       System.out.println(m.toString());
       m.changeVals(4, 4);
       System.out.println(m.toString());
       // division
       Divide d = new Divide(5, 3);
       System.out.println(d.toString());
       d.changeVals(4, 4);
       System.out.println(d.toString());
       // modulo
       Modulo mo = new Modulo(5, 3);
       System.out.println(mo.toString());
       mo.changeVals(4, 4);
       System.out.println(mo.toString());

   }
}
Sample output:


Related Solutions

For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
Using eclipse IDE, create a java project and call it applets. Create a package and call...
Using eclipse IDE, create a java project and call it applets. Create a package and call it multimedia. Download an image and save it in package folder. In the package, create a java applet where you load the image. Use the drawImage() method to display the image, scaled to different sizes. Create animation in Java using the image. In the same package folder, download a sound. Include the sound in your applets and make it repeated while the applet executes....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT