Question

In Java! NotMultipleStream: a stream that returns, in numeric order, integers that are not multiples of...

In Java!

NotMultipleStream: a stream that returns, in numeric order, integers that are not multiples of a certain value, starting from an initial value. The NotMultipleStream should behave like an IntegerStream, but it should include a NotMultipleFilter to filter out multiples of a certain value. The class will need the following methods and constructors:

  1. The constructor takes two integer values. The first is the base value and the second is the initial value.

  2. next: takes no input and returns an integer. The first value returned by next should be the the smallest number, no smaller than the initial value such that the number is not divisible by the base value. Each subsequent call should return the next largest integer that is not a multiple of the base value.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE: Lines starting with // are comments, screen shot of code and testing class and outputs is provided below

_________________________________________________________________________

class NonMultipleStream{

   //decalare variables
   int base;
   int initVal;
   static int currentNum;

   NonMultipleStream(int base,int initVal){

       this.base=base;
       this.initVal=initVal;
       this.currentNum=initVal;

   }

   //method to return next Integer of the Stream
   public int next(){
       //loop
       while(true){
           //check if currentNum is divisible by base
           if(currentNum%base!=0){
               //return the number
               this.currentNum++;
               return this.currentNum-1;
           }
           else
               //increment current number
               this.currentNum++;
       }


   }


}

________________________________________________

import java.util.Scanner;
import java.io.*;

class TestNonMultipleStream{


public static void main(String[] args) {
  
   //decalre variables
   int base,initval;
   //create scanner class object
   Scanner s=new Scanner(System.in);
   //prompt user to enter input
   System.out.print("Enter base value:");
   base=s.nextInt();
   System.out.print("Enter Initial Vaule:");
   initval=s.nextInt();

   //create object of NonMultipleStream Class
   //pass inputs as arguments to constructor
   NonMultipleStream nsm=new NonMultipleStream(base,initval);

   System.out.println("Stream Outputs:");
   System.out.println(nsm.next());
   System.out.println(nsm.next());
   System.out.println(nsm.next());
   System.out.println(nsm.next());
   System.out.println(nsm.next());
   System.out.println(nsm.next());
   System.out.println(nsm.next());

}
}

____________________________________________________________

CODE screen shots:

__________________________________

Outputs:

Add a comment
Know the answer?
Add Answer to:
In Java! NotMultipleStream: a stream that returns, in numeric order, integers that are not multiples of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to write a C++ program that reads two integers from a data filed called...

    I need to write a C++ program that reads two integers from a data filed called "integers.dat" The first integer should be low and the other one high. The program should loop through all integers between the low and high values and include the low and high integers and then display Display the integers in the file Displays the number of integers divisible by 5 OR 6, but not both. Displays the sum of the integers from condition 2 as...

  • DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is...

    DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is only from-263 to 263-1. What if we need to manipulate integer values beyond this range? In this assignment you will write a HugeInteger class which is able to represent arbitrar- ily large integer numbers. This class must implement arithmetic operations on integers such as addition, subtraction, multiplication, division and comparison. You have to implement this class without using Java predefined classes, unless specified...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

  • You will write a two-class Java program that implements the Game of 21. This is a...

    You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...

  • java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named...

    java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named "MyRectangle3d" Your class must have the following attributes or variables · x (the x coordinate of the rectangle, an integer) * y (the y coordinate of the rectangle, an integer) * length (the length of the rectangle, an integer) * width (the width of the rectangle, an integer) * height (the height of the rectangle, an integer) * color( the color of the rectangle,...

  • Java question Q1) Use the following code snippet which generates a random sized array with random...

    Java question Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems: public int [] createRandomArray() {         int size = (int) (Math.random() * 10) + 1;         int[] array = new int [size];         for (int i = 0; i < array.length; i++) {             array[i] = (int) (Math.random() * 10 ) + 1;         }         return array;     } Assignment...

  • I only need help with the constructors and add method. Thanks! Background Arbitrary length integers are...

    I only need help with the constructors and add method. Thanks! Background Arbitrary length integers are integers that have no size restriction. Recall that the int type has a range of -2,147,483,648 to 2,147,483,647. The long type runs from -263 to 263 - 1. Sometimes these numbers are not large enough. One example application that may need larger numbers is public-key cryptography where very large integers are used to make decryption hard. A large integer may be represented as a...

  • IN JAVA Write a program that first gets a list of integers from input. The input...

    IN JAVA Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates a threshold. Output all integers less than or equal to that last threshold value. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75...

  • Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes...

    Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT