Question

In: Computer Science

Create a Java method that does the following: 1) Ask the user for a dividend and...

Create a Java method that does the following:

1) Ask the user for a dividend and a divisor both of "int" type.

2) Computes the remainder of the division. The quotient (answer) must be of the "int" type.

Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Please see the videos for the explanation.

Solutions

Expert Solution

import java.util.*;
public class Main
{
   //Method to compute the remainder of the division
   static int getRemainder()
{
int remainder = 0;
Scanner input = new Scanner(System.in);
  
//Input the divident from user
System.out.print("Please enter the divident: ");
int divident = input.nextInt();
  
//Input the divisor from user
System.out.print("Please enter the divisor: ");
int divisor = input.nextInt();
  
// Case I: Divisor equals to 0
if (divisor == 0)
{
System.out.println("ERROR !! Divisor cannot be zero !");
return -1;
}
  
// Case II: Divident equals to 0
if (divident == 0)
{
remainder = 0;
return remainder;
}
  
// Case III: Divisor and/or Divident is negative
if (divisor < 0)
divisor = -divisor;
  
if (divident < 0)
divident = -divident;
  
// Loop to find the largest multiple of divisor that is less than or equal to divident
int i = 1, multiple = 0;
while ((multiple + divisor) <= divident)
{
  
multiple = divisor * i;
i++;
}
  
remainder = divident - multiple;
return remainder;
}
  
// Driver method
public static void main(String[] args)
{
int remainder = getRemainder();
// Prints the remainder
if (remainder != -1)
System.out.print("The remainder of the division is " + remainder);
}
}


Related Solutions

Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Here is the code that I have so far I can't seem to get it...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the...
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
Create method addUserInput Write a method called addUserInput(). The method should ask the user to input...
Create method addUserInput Write a method called addUserInput(). The method should ask the user to input two integers (one by one), add the two integers, and return the sum. By using java.util.Scanner to get user input; The method may not compile due to Scanner Class which need to add a "throws" statement onto the end of the method header because some lines may throw exceptions Refer to the Java API documentation on Scanner to figure out which specific Exception should...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Using java: Write a class called StringAlternateSorting what it does: 1. ask user for one String...
Using java: Write a class called StringAlternateSorting what it does: 1. ask user for one String (String1) (must be sorted) 2. ask user for second String (String2) (must be sorted) 3. Then prints all the characters of two previous Strings, in such a way that they are sorted (see examples below). 4. consider the possibility that strings are of different sizes, one or both can be empty, etc. 5. strings can contain any kind of characters as long as they...
10) Create a java program that will ask for name and age with method age where...
10) Create a java program that will ask for name and age with method age where it will check the condition that id you are greater than 18 you can vote.
Write a method in JAVA that does this: Presents the user with a header stating this...
Write a method in JAVA that does this: Presents the user with a header stating this is for assignment: Lab, and the names Bob and Bill Present the user with a menu to run a random method(just make this a dummy method) , another random method method,(just make this a dummy method) or another dummy method (just make this a dummy method) Repeat the menu until the user enters -1.
Write a java program that perform the following: 1. Ask a user ti enter 10 student...
Write a java program that perform the following: 1. Ask a user ti enter 10 student test score on a test (100 point test and save the score in an array 2. Iterate through the array to find the average and highest of these score, print them out 3 Count how many student score below average and print them out 4 . Instructor decide to curve the student score using square root curving method( take the square root of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT