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

JAVA LANGUAGE Create a new project named BankAccount in Eclipse. Then, implement the following requirements. You...
JAVA LANGUAGE Create a new project named BankAccount in Eclipse. Then, implement the following requirements. You need to create two classes, one for BankAccount (BankAccount.java) and the other for the tester (BankAccountTest.java). Make sure your program compile without errors before submitting. Submit .java files to eCampus by the end of next Thursday, 10/15/2020. Part 1: Create the BankAccount class (template is attached after the project description) in the project. You must add the following to the BankAccount class: An instance...
open up a new Java project on Eclipse named Review and create a new class called...
open up a new Java project on Eclipse named Review and create a new class called Review.java. Copy and paste the below starter code into your file: /** * @author * @author * CIS 36B */ //write your two import statements here public class Review {        public static void main(String[] args) { //don't forget IOException         File infile = new File("scores.txt");         //declare scores array         //Use a for or while loop to read in...
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...
In Java: 1) Create a new eclipse project. 2) Create a basic SWING window 3) Create...
In Java: 1) Create a new eclipse project. 2) Create a basic SWING window 3) Create a Label Called Name 4) Create a Text Field for entering the name 5) Create a Text Field for entering and email 5) Create a text area to push the results to 6) Create two buttons, one that says submit and the other that says clear. 7) When the user enters their name and email, they should press submit and see the Text area...
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,...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT