JAVA Ninth Assignment "Chapter 11 - Method Overloading - Assignment - Segmentation Function"

CG system URL: http://211.81.175.89

What is method overloading

[Problem description] The known piecewise function formula is as follows,

Define the Function class, which contains 4 overloaded f() methods to calculate the value of each segmentation situation.

Define the test class, and realize the input of x, y, and z values, the judgment of conditions and the output of results in the main method.

The main method is written as follows. Be careful not to modify the main code, otherwise points will be deducted:

[Input form] Three integers
[Output form] One integer, indicating the calculation result of the piecewise function
[Sample input]

-1 5 7

【Sample output】

0

【Sample input】

2 -2 7

【Sample output】

4

import java.util.Scanner;
class Function{
    public int f(){
        return 0;
    }
    public int f(int x){
        return x*x;
    }
    public int f(int x,int y){
        return x*x+y*y;
    }
    public int f(int x,int y,int z){
        return x*x+y*y+z*z;
    }
}
public class two {
    public static void main(String [] args){
        int x,y,z;
        Scanner in =new Scanner(System.in);
        x=in.nextInt();
        y=in.nextInt();
        z=in.nextInt();
        in.close();
        Function function=new Function();
        int result;
        if(x<0){
            result=function.f();
        }
        else if(x>=0 && y<0){
            result=function.f(x);
        }
        else if(x>=0 && y>=0 && z<0){
            result=function.f(x,y);
        }
        else{
            result = function.f(x,y,z);
        }

        System.out.println(result);

    }
}

Guess you like

Origin blog.csdn.net/qq_25887493/article/details/123982320