Question

5. Write a static method f(n) in the space provide, that returns O if n is even, and if n is odd, returns 1 or -1 according
7. Write a static method max in the space provide, that returns the maximum value from 3 input integers. import java.util.S
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Open JDK Runtime Environment (build 10.0.2+13-Ubuntu-lubuntu 0.18 .04.4) ? javac -classpath .:/run dir/junit-4.12.jar -d . Ma

code:

public static int f(int n){

//check for even

if(n%2 == 0){

return 0;

}else if(n > 0){//it is not even means it is odd so need to check for odd again, just check greater than 0 or not

return 1;

}else{

return -1;

}

}

public static int max(int a, int b, int c){

//check which is maximum

if(a > b && a > c){

return a;

}else if(b > a && b > c){

return b;

}else{

return c;

}

}

Add a comment
Know the answer?
Add Answer to:
5. Write a static method "f(n)" in the space provide, that returns O if n is...
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
  • what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int...

    what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int w 0; int x; int y 5; float z = 6.1 System.out.print("Enter y: "); x keyboard.nextint); System.out.println('y'); System.out.println(x); System.out.println(w*3); x- x+(int)z; System.out.println(x); 0 System.out.println(u); System.out.,println(u); System.out.println"x In" + y); System.out.print(y + z); ) liclosing main method 1 liclosing class header

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

  • Here is the finsihed java program, it just needs to be converted to MIPs assembly language:...

    Here is the finsihed java program, it just needs to be converted to MIPs assembly language: import java.util.*; public class LoveCS { public static void main(String[] args) { int timesMessagePrinted; int count = 0; int sum = 0; Scanner input = new Scanner(System.in); System.out.print("How many times should the message be printed?: "); timesMessagePrinted = input.nextInt(); for(int x = 1; x <= timesMessagePrinted; x++) { System.out.println(x +" I love Computer Science!!"); count++; sum += x; } System.out.print("Printed this message " +count...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • Complete the following Java program by writing the missing methods. public class 02 { public static...

    Complete the following Java program by writing the missing methods. public class 02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number: int n = scan.nextInt(); if (isOdd (n)) { //Print n to 1 System.out.println("Print all numbers from "+n+" to 1"); printNumToOne (n); } else { System.out.println(n + " is //End of main()

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • 8. What is the output of this program if the input: 1 2 3 4 -5...

    8. What is the output of this program if the input: 1 2 3 4 -5 0 26? importjava.util.Scanner; public class Q_09 Your answer: public static void main(String[] args) int n, neg_count = 0, sum = 0; Scanner input = new Scanner(System.in); System.out.println("Enter n:"); n = input.nextInt(); while(n !=0) neg_count++; sum +=n; System.out.println("sum =" + sum); System.out.println("#of negatives:" + neg_count + "\t" +"Total = "+sum);

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • in java, my IF statement is being ignored & my print statement won't work..why? Start Page xIẾ》 Smallestintege...

    in java, my IF statement is being ignored & my print statement won't work..why? Start Page xIẾ》 Smallestinteger.java x 1|曰import java.util. Scanner; 3public class SmallestInteger i public static void main (String[] args) i Scanner input - new Scanner (System.in); int lowValue0 int nextöne; 10 int counter: 12 System.out.print ("How many integers?" 13 int index = input .next Int ( ) ; 14 15 for (counter=1; counter index -counter){ 16 System.out.print ("Enter integer, please: "); 17 nextOne - input.nextInt ) 18...

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