Function with branching structures - Java Language Basics

Java examples for Language Basics:switch

Description

Function with branching structures

Demo Code

public class Main {
  public static void main(String[] arguments) {
    double x = Math.E;

    System.out.println("Choose function to evalute for x=" + x);
    System.out.print("(1) Identity, (2) Logarithm, (3) Sinus.Your choice ?");

    System.out.println("F(x)=" + F(2, x));
  }/*from   w  ww .j a  v  a 2 s.c  o  m*/

  public static double F(int generator, double x) {
    double v = 0.0;

    switch (generator) {
    case 1:
      v = x;
      break;
    case 2:
      v = Math.log(x);
      break;
    case 3:
      v = Math.sin(x);
      break;
    }

    return v;
  }
}

Result


Related Tutorials