Write a Java program that outputs the sum, product, difference and quotientof two numbers using the class and object concept. That means you need to define a class,say MiniCalculator, which can be instantiated with the two given numbers. The sum,product, difference and quotient values should be computed using the methods of theMiniCalculator object. Use overloaded methods in the class you define to deal separatelywith integer and double type inputs. (e.g., sum(34, 678), sum(34.567, 234. 54) ). Write atest class to instantiate the MiniCalculator and demonstrate its functionality.
Question
Write a Java program that outputs the sum, product, difference and quotientof two numbers using the class and object concept. That means you need to define a class,say MiniCalculator, which can be instantiated with the two given numbers. The sum,product, difference and quotient values should be computed using the methods of theMiniCalculator object. Use overloaded methods in the class you define to deal separatelywith integer and double type inputs. (e.g., sum(34, 678), sum(34.567, 234. 54) ). Write atest class to instantiate the MiniCalculator and demonstrate its functionality.
Solution
Here is a simple Java program that meets your requirements:
public class MiniCalculator {
private double num1;
private double num2;
public MiniCalculator(double num1, double num2) {
this.num1 = num1;
this.num2 = num2;
}
public double sum() {
return this.num1 + this.num2;
}
public double product() {
return this.num1 * this.num2;
}
public double difference() {
return this.num1 - this.num2;
}
public double quotient() {
if (this.num2 != 0) {
return this.num1 / this.num2;
} else {
throw new IllegalArgumentException("Division by zero is not allowed.");
}
}
}
public class Test {
public static void main(String[] args) {
MiniCalculator calculator = new MiniCalculator(34.567, 234.54);
System.out.println("Sum: " + calculator.sum());
System.out.println("Product: " + calculator.product());
System.out.println("Difference: " + calculator.difference());
System.out.println("Quotient: " + calculator.quotient());
}
}
This program defines a MiniCalculator class with methods for sum, product, difference, and quotient. It then creates a Test class with a main method that creates an instance of MiniCalculator and uses it to perform calculations. The results are then printed to the console.
Similar Questions
Add two numbersYou have been provided with some code. The code should take in two numbers from the user, add them together, and then display the result to the user. However, the code has bugs!Debug this code!Here are some examples of how the program should work:Example 1Enter a number: 32Enter another number: 64The sum of your two numbers is: 96.00Example 2Enter a number: 1234.5678Enter another number: 9999.99The sum of your two numbers is: 11234.56Example 3Enter a number: 23Enter another number: -532.824The sum of your two numbers is: -509.82
TaskThe provided code stub reads two integers from STDIN, and . Add code to print three lines where:The first line contains the sum of the two numbers.The second line contains the difference of the two numbers (first - second).The third line contains the product of the two numbers.ExamplePrint the following:8-215Input FormatThe first line contains the first integer, .The second line contains the second integer, .
Write a program that computes the sum from the integers a to b.The method you will write ispublic int sumFrom(int a, int b)For example, sumFrom(1, 10) returns 55 since the sum from 1 to 10 inclusive is 55.
Create a Java program that takes two integer inputs 'a' and 'b' from the user and calculates 'a/b'. Implement exception handling to catch potential arithmetic and input mismatch errors, providing appropriate error messages for each case.Your task is to write a Java Program to Handle Arithmetic Exceptions and InputMisMatchExceptions.Note: Refer to the visible test cases for more clarity.Constraint:0≤𝑎≤1020≤𝑏≤102Input Format: The input consists of two numbers each in a new line (integer).Output Format: The output represents the result of the division of the two numbers or any error that occurred while dividing.Sample Test CasesTest Case 1:Expected Output:Enter·the·value·of·'a':·1Enter·the·value·of·'b':·0Arithmetic·Exception:·/·by·zeroTest Case 2:Expected Output:Enter·the·value·of·'a':·sInput·Mismatch·Exception:·Please·enter·valid·integer·values.Test Case 3:Expected Output:Enter·the·value·of·'a':·50Enter·the·value·of·'b':·4Result·of·a/b:·12
Create a mini calculator which has the ability to add, subtract, multiply and divide. Follow the steps below to complete this assignment.Step 1: Your code should be able to add, subtract, divide and multiplyStep 2: Your program should ask the user for two numbers.Step 3: Your code should calculate and print the output for all the calculations. Use appropriate variables and output names.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.