Question

In: Computer Science

b) Name the class ArithmeticMethods, and the java file ArithmeticMethods.java. c) Write separate methods to perform...

b) Name the class ArithmeticMethods, and the java file ArithmeticMethods.java.

c) Write separate methods to perform arithmetic operations of sum, average, product, max and min of three integer numbers. These 5 methods should have three integer parameters to take the three integer values. They should be valuereturn methods, and return the arithmetic result in proper data type when they are invoked (called). The average method should return a double value, and all the other methods should return integer values.

d) In the main method, prompt and accept user inputs for three integer numbers, and invoke (call) the five methods for output display.

e) Program top comments, indentation and programming styles are all required.

Solutions

Expert Solution

ArithmeticMethods.java :

import java.util.Scanner; // Scanner class is used to get user input.

public class ArithmeticMethods
{
        /*
        Method to get sum of three number
        */
        static int sum(int n1, int n2, int n3) {
            
            int sum = n1 + n2 + n3; // calculating sum of three numbers.
            
            return sum; // returning sum of three numbers.
        }
        
        /*
        Method to get average of three numbers.
        */
        static double average(int n1, int n2, int n3) {
            /*
            formula to calculate average = (total sum / total number)
            */
            
            int sum = n1 + n2 + n3; // calculating sum of three numbers.
            double average = (double)sum / 3; // calculating average of three numbers.
            
            return average; // returning average of three numbers.
        }
        
        /*
        Method to get product of three numbers.
        */
        static int product(int n1, int n2, int n3) {
            
            int product = n1 * n2 * n3; // calculating product of three numbers.
            
            return product; // returning product of three numbers.
        }
        
        /*
        Method to get minimum value among three numbers.
        */
        static int max(int n1, int n2, int n3) {
            
            /*
            
            Check if n1 is greater than or equals to  remaining two values  
            if yes then n1 is greater than n1 is max among remaining values (return n1)
            
            */
            
            if(n1 >= n2 && n1 >= n3) { 
                return n1;
            }
            
            /*
            
            Check if n2 is greater than or equals to remaining two values 
            if yes then n2 is max among remaining values (return n2)
            
            */
            
            else if (n2 >= n1 && n2 >= n3)  {
                return n2;
            }
            
            /*
             if above two conditions fails than n3 will be max among remaining values. (return n3)
             
            */
            else {
                return n3;
            }
        }
        
        /*
        
        Method to get min value among three values.
        */
        static int min(int n1, int n2, int n3) {
            
            /*
            Check if n1 is less than or equals to  remaining two values 
            if yes then n1 is min among remaining values (return n3)
            
            */
            
            if(n1 <= n2 && n1 <= n3) { 
                return n1;
            }
            
            /*
            
            Check if n2 is less than or equals to remaining two values 
            if yes then n2 is min among remaining values (return n2)
            
            */
            
            else if (n2 <= n1 && n2 <= n3)  {
                return n2;
            }
            
            /*
             if above two conditions fails than n3 will be min among remaining values (return n3)
             
            */
            else {
                return n3;
            }
        }
        
        public static void main(String[] args) {
                
                int n1,n2,n3; // Integer Variables to hold three Integer user input
                
                Scanner sc = new Scanner(System.in); //Scanner Object to get user input.
                
                // Prompt for enter First Number and store entered Number in n1 Variable
                // sc.nextInt() Method is used to get Integer input.
                System.out.println("Enter First Number: ");
                n1 = sc.nextInt();
                
                // Prompt for enter Second Number and store entered Number in n1 Variable
                // sc.nextInt() Method is used to get Integer input.
                System.out.println("Enter Second Number: ");
                n2 = sc.nextInt();
                
                // Prompt for enter Third Number and store entered Number in n1 Variable
                // sc.nextInt() Method is used to get Integer input.
                System.out.println("Enter Third Number: ");
                n3 = sc.nextInt();
                
                /*
                
                Call five Method that we created one by one by passing three user input as parameter
                all five Method we created will return some values (depending upon input) 
                print returned values on console using println.
                
                */
                System.out.println("Sum of three number is: "+sum(n1,n2,n3));
                System.out.println("Average of three number is: "+average(n1,n2,n3));
                System.out.println("Product of three number is: "+product(n1,n2,n3));
                System.out.println("Max of three number is: "+max(n1,n2,n3));
                System.out.println("Min of three number is: "+min(n1,n2,n3));
        }
}

Sample Output 1 :

Sample Output 2 :

Please refer to the screenshot of the code below to understand indentation of the code :


Related Solutions

How can I write a separate java class to change a given name or a given...
How can I write a separate java class to change a given name or a given email of a user that was already saved (keep in mind that there may be numerous names and emails). Note: It should change the name or email that is saved in the txt file. Here is my code so far: User class public class User { private String fName; private String lName; private String UserEmail; public User(String firstName, String lastName, String email) { this.fName...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods designed to perform simple conversions. Specifically, you will be writing methods to convert temperature between Fahrenheit and Celsius and length between meters and inches and practicing overloading methods. See the API document for the Conversion class for a list of the methods you will write. Also, because all of the methods of the Conversion class will be static, you should ensure that it is...
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
Create separate class with these members a, b, c, x, y, z int a b c...
Create separate class with these members a, b, c, x, y, z int a b c float x y z Demonstrate 3) A two arg float, int constructor, and a three arg int, float, float constructor to instantiate objects, initialize variables read from the keyboard, display the sum Note:- Please type and execute this above java program and also give the output for both problems. (Type a java program)
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
In JAVA Write a brief program that writes your name to a file in text format...
In JAVA Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT