Question

Problem 5) Simplify the code for the following method as much as you can.(Java) public static...

Problem 5) Simplify the code for the following method as much as you can.(Java)

   public static int inTheInterval(int n)
   {
      if (n < 0)
         return -1;
      else if (0 <= n && n <= 1)
         return 0;
      else if (n > 1)
         return 1;
      return 42;  // we never get here
   }
0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static int inTheInterval(int n)
{
    if (n < 0)
        return -1;
    else if (n <= 1)
        return 0;
    else 
        return 1;
}
Add a comment
Know the answer?
Add Answer to:
Problem 5) Simplify the code for the following method as much as you can.(Java) public static...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • b) Consider the following code. public static int f(int n) if (n == 1) return 0;...

    b) Consider the following code. public static int f(int n) if (n == 1) return 0; else if (n % 2 == 0). return g(n/2); else return g(n+1); public static int g(int n) int r = n % 3; if (r == 0) return f(n/3); else if (r == 1) return f(n+2); else return f(2 * n); // (HERE) public static void main(String[] args) { int x = 3; System.out.println(f(x)); (1) (5 points) Draw the call stack as it would...

  • (20 pts) Fill in the missing code: This recursive method returns “even” if the length of...

    (20 pts) Fill in the missing code: This recursive method returns “even” if the length of a give String is even, and “odd” if the length of the String is odd. public static String foo(String s) { if (s.length() ==0)    return “even”; else if (s.length() = = 1)    return “odd”; else     //your code goes here } (40 pts) You coded the following in the file Test.java : System.out.println( foo(5)); //more code here public static int foo(int n)...

  • (b) Trace the following code. // Precondition: n > 0 // <method description here> public static...

    (b) Trace the following code. // Precondition: n > 0 // <method description here> public static int dothing (int n) { int ans = ; for (int x 1; x <= n; x++) ( for (int y = x; y <= n; y++) ( ans+ } return ans; } (ii) If you were asked to write a one-line description above this method, what would it be? (iii) Could you rewrite the method so that it runs more efficiently?

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • Java, how would i do this public static void main(String[] args) { int n = 3;...

    Java, how would i do this public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...

  • Question 5 please java JAVA 3. CODE class MyCustomException extends Exception protected static int numberOfExceptions 0;...

    Question 5 please java JAVA 3. CODE class MyCustomException extends Exception protected static int numberOfExceptions 0; public MyCustomException) super public int getNumberOfExceptions 0 { return numberOfExceptions 4. CODE class MyCustomTooHighException extends MyCustomException { public MyCustomTooHighException() { super.numberOfExceptions ++ class MyCustomTooLowException extends MyCustomException{ public MyCustomTooLowException(){ super.numberOfExceptions ++ We were unable to transcribe this image 3. CODE class MyCustomException extends Exception protected static int numberOfExceptions 0; public MyCustomException) super public int getNumberOfExceptions 0 { return numberOfExceptions 4. CODE class MyCustomTooHighException extends MyCustomException...

  • In the class GraphAlgorithm, insert java code for the method prim public class MyGraph { public...

    In the class GraphAlgorithm, insert java code for the method prim public class MyGraph { public static final int MAXSIZE = 100; public static final double BIGM = 10000000; public MyGraph(int size) { n = size; nodeStart = new Edge[MAXSIZE]; for (int i=0; i<n; i++) { nodeStart[i] = null; } } public int getSize() { return n; } public double getCost(int i, int j) { double value = BIGM; Edge e = nodeStart[i]; while (e !=null) { if (e.dest ==...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

  • JAVA public static String generatePassword(String gp)        {            String password="";       ...

    JAVA public static String generatePassword(String gp)        {            String password="";            boolean b= false;            for (int i =0;i            {                char ch = gp.charAt(i);                if (i == 0)                {                    password += Character.toUpperCase(ch);                }                if (ch == 'S' || ch == 's')           ...

  • Which of the following is NOT part of the heading of the following method? public static...

    Which of the following is NOT part of the heading of the following method? public static int minimum(int x, int y) int smaller; if (x < y) smaller = x; else smaller = y; return smaller; C static C minimum(int x, int y) cint smaller; C public Consider the following method. public static int minimum(int x, int y) int smaller; if (x = y) smaller = x; else smaller = y; return smaller; Which of the following is a valid...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT