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 =...
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...
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...
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
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
write code in java and comment. thanks. the program is about interface . Implement the basics...
write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are abstract...
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT