Question

In: Computer Science

Write the following program in java please. a. Include a good comment when you write the...

Write the following program in java please.

a. Include a good comment when you write the method described below:

Method method1 receives an integer, n, and a real number, d, and returns a real number. If the integer is positive it returns the square root of the real number added to the integer. If the integer is negative or zero it returns the absolute value of the integer multiplied by the real number.

b. Write the statements from the main program to do each of the following:

1. Declare variables int1, an integer, and real1, a real number, and a variable result which willl store the value returned from the method.

2. Call the method method1 passing int1 and real1 as parameters, assigning the value returned to result.

3. Print with 4 decimal places the value returned from the method.

Solutions

Expert Solution

Java program:

import java.lang.Math; 
import java.util.*; 

class Test{
        
        //  Method method1 which receives an integer, and a real number, and returns a real number 

        public static double method1(int n,double d)
        {       
                //if n is positive return square root of real number added with interger
                if(n>0)
                {       
                        return Math.sqrt(d)+n;
                }
                
                // else if n is less then or equal to n return n*d
                else {
                        return n*d;
                     }
        }


        public static void main(String args[])
        {
                  int int1;
                  double real1;
                  double result;
                
                  Scanner sc=new Scanner(System.in); 
                  
                  // taking input for integer
                  System.out.print("Enter int1 : ");  
                       int1= sc.nextInt();  
            
                 //taking input for real number      
                 System.out.print("Enter real1 : ");  
                       real1= sc.nextDouble();      
              
                // calling method1 with int1 and real1
                 result=method1(int1,real1);
                   

                 //printing the output 
                 //String.format("%.4f",result) set the precision of result upto 4 decimal places
                 //if NaN is printed then it shows that result is not a real number
                 System.out.println("result : "+String.format("%.4f", result));

        }

}

Note: if output is not a real number then NaN will be printed in place of output.

Test input and output:


Related Solutions

Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at the top and at least one more good comment later in the program), to process data for the registrar as follows: NOTE: Include prompts. Send all output to the screen. 1. Read in the id number of a student, the number of credits the student has, and the student’s grade point average (this is a number like 3.25). Print the original data right after...
PLEASE COMMENT THE FOLLOWING LEXICAL ANALYSER PROGRAM. Thank you. #include #include #include #include #include using namespace...
PLEASE COMMENT THE FOLLOWING LEXICAL ANALYSER PROGRAM. Thank you. #include #include #include #include #include using namespace std; int isKeyword(char buffer[]){ char keywords[32][10] = {"auto","break","case","char","const","continue","default", "do","double","else","enum","extern","float","for","goto", "if","int","long","register","return","short","signed", "sizeof","static","struct","switch","typedef","union", "unsigned","void","volatile","while"}; int i, flag = 0; for(i = 0; i < 32; ++i){ if(strcmp(keywords[i], buffer) == 0){ flag = 1; break; } } return flag; } int main(){ char ch, buffer[15], operators[] = "+-*/%="; ifstream fin("Text.txt"); int i,j=0; if(!fin.is_open()){ cout<<"error while opening the file\n"; exit(0); } while(!fin.eof()){ ch = fin.get();    for(i =...
Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
Hello, Please write this program in java and include a lot of comments and please try...
Hello, Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was: Create your own class Your own class can be anything you want Must have 3 instance variables 1 constructor variable → set the instance variables 1 method that does something useful in relation to the class Create a driver class that creates an...
JAVA - PLEASE COMMENT CODE - THANK YOU: Implement a program that can input an expression...
JAVA - PLEASE COMMENT CODE - THANK YOU: Implement a program that can input an expression in postfix notation and output its value.
This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
Write a JAVA program in eclipse (write comment in every line) that asks the user to...
Write a JAVA program in eclipse (write comment in every line) that asks the user to enter two numbers and an operator (+, -, *, / or %), obtain them from the user and prints their sum, product, difference, quotient or remainder depending on the operator. You need to use OOP techniques (objects, classes, methods….). This program must be place in a loop that runs 3 times.
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
Create JAVA PROGRAM, and write comment for codes also. 3) You want to keep track of...
Create JAVA PROGRAM, and write comment for codes also. 3) You want to keep track of your progress towards running a 10K. There are two kinds of races - 5K and 10K. The program needs to ask what race was run and what the time was in seconds until the user quits. When they quit, display the average and best time for each type of race in minutes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT