Question: A Looping Multiplier Program In your Java programs, the multiplication operator usually maps onto a multiplication instruction on the computer architecture when it runs. However,
A Looping Multiplier Program
In your Java programs, the multiplication operator usually maps onto a multiplication instruction on the computer architecture when it runs. However, many computer architectures (such as the MOS 6502, which runs on older Atari, Apple, and Nintendo computers and game systems and is still sold today for embedded system applications) do not have a multiplication instruction. For architectures like these, multiplications must be implemented in a program as a loop that performs repeated additions. In this assignment, you will write a looping multiplier program that does this.
In this assignment, you will make some modifications to the Multiplier program from Assignment 3 to make it a multiplier program that uses a while loop or a for loop to perform the multiplication instead of the multiplication operator. Your multiplier program will take two integers from the user, but rather than use the normal multiplication operator (the asterisk, *) to multiply them, it should do the multiplication by using the addition operator (+) repeatedly in a loop. Your code only needs to work with non-negative integers (0 or positive).
For example, if the integers 10 and 3 are entered, the output from your Multiplier program should look like this:
Enter two integers. Press return after each integer.
10 3 10 times 3 is 30
Notice that there is a carriage return after is. Your multiplier program should have its result on the last line by itself, so use System.out.println() instead of System.out.print() when printing is.
You starter code for this assignment will be the starter code from Assignment 3. No separate zip file is provided for this assignment. To read input from the console, you will use the A3Helper.java file just as you did for Assignment 3. To get the Assignment 3 code and get it running on Eclipse or on the command line, follow the same instructions as for Assignment 3.
This is the code that needs to be fixed:
class LoopingMultiplier {
public static void main(String[] args) {
int firstInteger;
int secondInteger;
System.out.println("Enter two integers. Press return after each integer.");
firstInteger = A3Helper.nextInteger();
secondInteger = A3Helper.nextInteger();
int product=0;
while (product>=secondInteger) {
product=product+firstInteger;
do {
product++;
System.out.print(firstInteger);
System.out.print(" times " );
System.out.print(secondInteger);
System.out.print(" is ");
System.out.print(product);
}
}
}
This is the A3Helper code provided:
import java.util.Scanner;
class A3Helper {
static Scanner keyboard = new Scanner(System.in);
static int nextInteger() {
return keyboard.nextInt();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
