NEWS: Total Picture Views: 147066 | Total Article Views: 155635 | Top 5 Most Popular Article: 1. Am I Stuck Algorithm with Becker Robot | 2. How to read a JSON and work with it | 3. PickUpThings with Becker Robot | 4. Encrypted Data GUI (Download .Zip) | 5. Create Wall and Things with Becker

Using Method via Multiple Class Java

Description:

In this example I will be using Switch Case instead of If statement. I will also be using 3 classes (Launch,Math and Question).

Launch call the Question class so we can get the question. After that the Question class call Math class with the method you want.

Code:

/************************** Launch.java **********************************/
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package runtime;

/**
 *
 * @author Jean-Mathieu
 */
public class Launch {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Question q = new Question();
        q.Question();
    }
    
}

/************************** Question.java **********************************/
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package runtime;

import java.util.Scanner;

/**
 *
 * @author Jean-Mathieu
 */
public class Question {
    public static void Question(){
        Math math = new Math();
        double x,y;
        String answer;
        Scanner input = new Scanner(System.in);
        
        System.out.println("Choose between add,sub,mul and div: ");
        answer = input.nextLine();
        
        System.out.println("Enter the first number: ");
        x = input.nextDouble();
        
        System.out.println("Enter the second number: ");
        y = input.nextDouble();
        
        switch(answer){
            case "add":
                System.out.println("The answer is : " + math.add(x, y));
                break;
            case "sub":
                System.out.println("The answer is : " + math.sub(x, y));
                break;
            case "mul":
                System.out.println("The answer is : " + math.mul(x, y));
                break;
            case "div":
                System.out.println("The answer is : " + math.div(x, y));
                break;
            default:
                System.out.println("Invalid input...");
                break;
        }
        
        
    }
}

/************************** Math.java **********************************/
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package runtime;

/**
 *
 * @author Jean-Mathieu
 */
public class Math {
    public double add(double x,double y){
        return (x+y);
    }
    
    public double sub(double x,double y){     
        return (x-y);
    }
    
    public double mul(double x,double y){        
        return (x*y);
    }
    
    public double div (double x,double y){
        if(y == 0){
            return 0;
        }else{
           return (x/y); 
        }       
    }
}


Attachments:

Tags: Method Class

Total Views: 812

My name is Jean-Mathieu

I created this website so other people could enjoy finding useful stuff easier. If you have any question do not hesitate to contact me.

jean8mathieuCreated on 11/23/14


affiliate_link

Disclosure: We are a website that needs compensation to operate like any other website on the internet.
We may receive consideration for our reviews but we are totally unbiased and do not accept paid reviews or fake reviews claiming to be something they are not.