Question

In: Computer Science

public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...

public class OperationsBetween {
// You must define the following:
// 1.) Two private instance variables, min and max  
// 2.) A constructor which takes initial values for
// min and max
// 3.) An instance method named sum, which sums the
// values between min and max and returns the
// result. For example, if min = 3 and max = 5,
// then this should return 12 (3 + 4 + 5). If
// min is greater than max, then this should
// return 0.
//
// 4.) An instance method named product, which
// multiplies the values between min and max and
// returns the result. For example, if min = 6
// and max = 9, then this should return 3024
// (6 * 7 * 8 * 9). If min is greater than
// max, then this should return 1. A stub has
// been provided that simply returns 1; this is
// just to allow things to compile while you're
// working on the sum method.

public int product() {
// You'll need to replace this code with something
// else to make product() work; this is just to make
// things compile while you work on sum()
return 1;
}

// DO NOT MODIFY main!
public static void main(String[] args) {
OperationsBetween between =
new OperationsBetween(Integer.parseInt(args[0]),
Integer.parseInt(args[1]));
System.out.println("Sum: " + between.sum());
System.out.println("Product: " + between.product());
}
}

Solutions

Expert Solution

In the main function the program is taking command line argument. So wee need to run the program and pass in command line itself.

Have a look at the below code.

public class OperationsBetween{
  private int min,max;
  public OperationsBetween(int a, int b){

    this.min = a;

    this.max = b;

  }

  public int sum(){
    if (min>max){
      return 0;
    }
    int res = 0;

    for (int i=min;i<=max;i++){
      res+=i;
    }
    return res;
  }

  public int product(){

     if (min>max){
      return 1;
    }
    int res = 1;

    for (int i=min;i<=max;i++){
      res*=i;
    }
    return res;
  }

}
class Main {
  public static void main(String[] args) {
    OperationsBetween between =new OperationsBetween(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
    System.out.println("Sum: " + between.sum());
    System.out.println("Product: " + between.product());
  }
}

Just complete the code. Then go to command line and compile you java code by running ...

javac filename.java 

Then run you java code by passing command line arguments...

java filename arg1 arg2 

Use your filename instead of filename and your arguments value of min max instead of arg1 and arg2.

Happy Learning!


Related Solutions

public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max // // 2.) A constructor which takes initial values for // min and max // // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
Write a class called Pen that contains the following information: Private instance variables for the price...
Write a class called Pen that contains the following information: Private instance variables for the price of the pen (float) and color of the pen (String). A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables....
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables. The first instance variable will store the days the bear has been awake. The second instance variable will store the number of teeth for the bear. The AngryBear class will have 1 constructor that takes in values for days awake and number of teeth. The AngryBear class will have one method isAngry(); An AngryBear is angry if it has been awake for more than...
1. Define a class counterType to implement a counter. Your class must have a private data...
1. Define a class counterType to implement a counter. Your class must have a private data member counter of type int and functions to set counter to the value specified by the user, initialize counter to 0, retrieve the value of counter, and increment and decrement counter by one. The value of counter must be nonnegative. 2. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design a class bookType that...
public class AddValueToArray { // You must define the addValueTo method, which will add // a...
public class AddValueToArray { // You must define the addValueTo method, which will add // a given value to each element of the given array. // // TODO - define your code below this comment // // DO NOT MODIFY main! public static void main(String[] args) { int[] array = new int[]{3, 8, 6, 4}; int valueToAdd = Integer.parseInt(args[0]); addValueTo(valueToAdd, array); for (int index = 0; index < array.length; index++) { System.out.println(array[index]); } } }
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: **...
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: ** The head pointer ** The tail pointer */ public class Node { // declare member variables (data and next) // finish these constructors public Node(int data, Node next) {} public Node(int data) {} // HINT: use this() with next = null } // Initialize the linked list (set head and tail pointers) public MyLinkedList() {} @Override public boolean add(Integer item) { return false; }...
public class AllEqual { // You must define the allEqual method, which will return // true...
public class AllEqual { // You must define the allEqual method, which will return // true if either: // 1.) The given array contains fewer than two elements, or... // 2.) All elements of the array are equal to each other. // As a hint, you only need to compare the first element // to all subsequent elements for this check. // // TODO - define your code below this comment // // DO NOT MODIFY parseStrings! public static int[]...
public class AddValueNewArray { // You must define the addValueNew method, which behaves // similarly to...
public class AddValueNewArray { // You must define the addValueNew method, which behaves // similarly to the addValueTo method in AddValueToArray.java. // However, instead of adding the given value to the given // array, it will return a _new_ array, where the new array // is the result of adding the value to each element of the // given array. To be clear, the given array NEVER CHANGES. // // TODO - define your code below this comment // //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT