简体   繁体   中英

How can I use variables from a class in another class in Java

I'm very new to java. I am writing this program that calculates Compound and Simple Interest and some other things which I'll add in the future.

Syntax-

Class Data

import java.util.Scanner;
public class DATA {
    public static void main(String[] args) {
        Scanner m=new Scanner(System.in);
        System.out.println("What do you want to calculate SI/CI");
        String choice=m.nextLine();
        {
            if (choice.equalsIgnoreCase("si") || choice.equalsIgnoreCase("ci"))
                Interest.var();
            if (choice.equalsIgnoreCase("si")){
                Interest.ci();
            }
        }


    }
}

Class Interest

import java.util.Scanner;

public class Interest {
    public static void var(){
        Scanner m=new Scanner(System.in);
        System.out.println("Enter the principle");
        double p=m.nextDouble();
        System.out.println("Enter the rate");
        double r=m.nextDouble();
        System.out.println("Enter the time");
        double t=m.nextDouble();
    }
     public static void si(double p, double r, double t){
        double si=(p*r*t)/100;
        System.out.println("The Simple Interest is "+si);
    }
    public static void ci(double p, double r, double t){
        double ci=p*Math.pow((1+(r/100)),(t))-p;
        System.out.println("The Compound Interest is "+ci);
    }
}

I've created a method called var in the Interest class which asks for the principle and other data for calculation. So If the user asks to calculate CI/SI, it imports the var method and asks the questions. now I can't figure out a way to use those variable for calculation. I hope you guys can help me, criticisms and suggestions are welcomed but if you can help me with the current problem, that would be more then helpful and again I'm really new to java

Create private variables p,r and t inside the class Interest. Create a constructor and pass the 3 values. So if you call the method si() or ci(), you can simply use this.p, this.r and this.t to calculate. You can also remove the params in si() and ci() methods.

When you define a class in JAVA, usually if you have variables that you use in more than one function, you define them like global variables for that class and use getters and setters methods to get or set them, like:

import java.util.Scanner;

public class Interest {

    private double p;
    private double r;
    private double t;
    ......
    public void setP(double p){this.p=p;}
    public void setR(double r){this.r=r;}
    public void setT(double t){this.t=t;}
    public double getP(){return p;}
    public double getR(){return r;}
    public double getT(){return t;}
}

However, your code could looks like:

import java.util.Scanner;

public class Interest {
    private double p;
    private double r;
    private double t;
    public static void var(){
        Scanner m=new Scanner(System.in);
        System.out.println("Enter the principle");
        p=m.nextDouble();
        System.out.println("Enter the rate");
        r=m.nextDouble();
        System.out.println("Enter the time");
        t=m.nextDouble();
    }
     public static void si(){
        double si=(p*r*t)/100;
        System.out.println("The Simple Interest is "+si);
    }
    public static void ci(){
        double ci=p*Math.pow((1+(r/100)),(t))-p;
        System.out.println("The Compound Interest is "+ci);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM