Question

In: Computer Science

Java Special Methods: A) Write a method that finds the maximum of two numbers. You should...

Java Special Methods:

A) Write a method that finds the maximum of two numbers. You should not use if-else or any other comparison operator.

B) Write methods to implement the multiply, subtract and divide operations for integers. The results of all of these are integers. Use only the add operator.

Solutions

Expert Solution

Answer A:

Answer B:


static int flipSign(int a)
{
int neg = 0;
  
// If sign is + ve turn it -ve
// and vice-versa
int tmp = a < 0 ? 1 : -1;
while (a != 0)
{
neg += tmp;
a += tmp;
}
return neg;
}
  
// Check if a and b are of different signs
static boolean areDifferentSign(int a, int b)
{
return ((a < 0 && b > 0) || (a > 0 && b < 0));
}
  
// Function to subtract two numbers
// by negating b and adding them
static int sub(int a, int b)
{
// Negating b
return a + flipSign(b);
}
  
// Function to multiply a by b by
// adding a to itself b times
static int mul(int a, int b)
{
// because algo is faster if b<a
if (a < b)
return mul(b, a);
  
// Adding a to itself b times
int sum = 0;
for (int i = Math.abs(b); i > 0; i--)
sum += a;
  
// Check if final sign must be -ve or + ve
if (b < 0)
sum = flipSign(sum);
  
return sum;
}
  
// Function to divide a by b by counting   
// how many times 'b' can be subtracted   
// from 'a' before getting 0
static int division(int a, int b)
{
// Raise exception if b is 0
if (b == 0)
throw new ArithmeticException();
  
int quotient = 0, dividend;
  
// Negating b to subtract from a
int divisor = flipSign(Math.abs(b));
  
// Subtracting divisor from dividend
for (dividend = Math.abs(a); dividend >= Math.abs(divisor);
dividend += divisor)
quotient++;
  
// Check if a and b are of similar symbols or not
if (areDifferentSign(a, b))
quotient = flipSign(quotient);
return quotient;
}
  
// Driver code
public static void main(String[] args)
{
System.out.println("Subtraction is " + sub(4, -2));
System.out.println("Product is " + mul(-9, 6));
  
try
{
System.out.println("Division is " + division(8, 2));
}
  
catch (ArithmeticException e)
{
System.out.println("Exception :- Divide by 0");
}
}
}


Related Solutions

Please write in Java and have two methods: the main method and the reverse word Write...
Please write in Java and have two methods: the main method and the reverse word Write a method that reads a line and reverses the words in the line (not the characters) using a stack. For example, given the following input: The quick brown fox jumps over the lazy dog you should get the following output: dog lazy the over jumps fox brown quick The Then create a main method to prompt the user to enter a line of words...
write a C program that asks the user to enter numbers and finds maximum among them
write a C program that asks the user to enter numbers and finds maximum among them
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list....
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list. If the list is empty, then it should return 0. use the provided code below public class Question03 { public class ListNode//public for testing purposes { public int data;//public for testing purposes public ListNode link;//public for testing purposes public ListNode(int aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//public for testing purposes public int getMaxValue() { //----------------------------------------------------------------------------------- //Write...
Write the following methods in a Java project: a) A Java method to determine and return...
Write the following methods in a Java project: a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters. b) A Java method to determine and return the highest of N integers. The number of integers is received as a parameter. The method should prompt the user to enter the N numbers, then it return the highest. c) A Java method to determine and return an appropriate value indicating if...
Write a Java method called printAvg that takes in two floating point numbers and prints out...
Write a Java method called printAvg that takes in two floating point numbers and prints out the average of them.
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Write a C function that finds and displays the maximum value ina two-dimensional array of...
Write a C function that finds and displays the maximum value in a two-dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main (), and the starting the address of the array should be passed to the function. Modify the function so that it also displays the rows and columns number of the element with the maximum value
(Intro/Basic) JAVA Write a small program that gets some numbers as command-line arguments and finds the...
(Intro/Basic) JAVA Write a small program that gets some numbers as command-line arguments and finds the maximum of those numbers. You can assume that the user will provide some command-line arguments when running the program (so you don’t have to worry about what to do if the user doesn’t provide any arguments -- that won’t happen). Also, you can assume that all the command line arguments will be floating-point numbers, i.e., numbers with a decimal point, like 2.95.
write a program to find the maximum possible sum such that no two chosen numbers are...
write a program to find the maximum possible sum such that no two chosen numbers are adjacent either vertically, horizontally, or diagonally. code in java
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT