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.

Here is the code that I have so far I can't seem to get it correct. Here is the feedback from the instructor: It appears you are computing the quotient here. This program is supposed to compute the remainder. This can be fixed easily with one line of code.

Which line of code needs to be corrected to determine the remainder?

import java.util.Scanner;

public class Main {

   //method to get the floor value

   static int floorDivide(int a, int b)

   {

   if(a % b != 0 && ((a < 0 && b > 0) || (a > 0 && b < 0)))

   {

   return (a / b - 1);

   }

   else

   {

   return (a / b);

   }

   }

public static void main(String args[]) {

int a,b, result;

Scanner sc=new Scanner(System.in);

System.out.println("Please Enter a Dividend: ");

a=sc.nextInt();

System.out.println("Please Enter a Divisor: ");

b=sc.nextInt();

result= floorDivide(a,b);

System.out.println("The Result is: "+result);

}

}

Solutions

Expert Solution

If you need any corrections/clarifications kindly comment.

Please give a Thumps Up if you like the answer.

Program

import java.util.Scanner;
public class Division
{
   //method to get the floor value
   static int floorDivide(int a, int b)
   {
   //Checking the case divisor equals to 0
   if (b == 0)
   {
    System.out.println("Error: divisor by zero \n");
    return -1;
    }
    // Checking for negative values
    if (b < 0)
       b = -b;
    if (a < 0)
       a = -a;

   return(a-b*(a/b));
   }

public static void main(String args[]) {
int a,b, result;
Scanner sc=new Scanner(System.in);
System.out.print("Please Enter a Dividend: ");
a=sc.nextInt();
System.out.print("Please Enter a Divisor: ");
b=sc.nextInt();
result= floorDivide(a,b);
System.out.print("The remainder is: "+result);
}
}

Output

user@user-Lenovo-V330-15IKB:~/JavaPrograms$ java Division
Please Enter a Dividend: 10
Please Enter a Divisor: 3
The remainder is: 1

user@user-Lenovo-V330-15IKB:~/JavaPrograms$ java Division
Please Enter a Dividend: -10
Please Enter a Divisor: 3
The remainder is: 1

user@user-Lenovo-V330-15IKB:~/JavaPrograms$ java Division
Please Enter a Dividend: 10
Please Enter a Divisor: -3
The remainder is: 1


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. Please see the videos for the explanation.
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