Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I return values from one class to another?

I just need to return a value which is the savings value back to the call method on the main class. I extended the sub class but it doesn’t work. I also tried importing the sub class into the parent class as an object, still no success.

package package1;

import java.util.Scanner;

public class GnX {
   FvX fvx = new FvX( );    
         
   public static void main(String[] args) {
        double price;
        double discount;
        double savings;
        Scanner scan = new Scanner(System.in);  
        
        
        System.out.println("Enter a base price for the discount: ");
        price = scan.nextDouble( );
        System.out.println("Now, Enter a discount rate: ");
        discount = scan.nextDouble( );
    
        displayInfo( );
        computeDiscountInfo( );
    
        System.out.println("Special this week on any service over " + price);
        System.out.println("Discount of " + discount);
        System.out.println("That's a savings of at least $" + savings);
    }

    public static void displayInfo( ) {
        System.out.println("Paradise Guitar Is A Fantastic Place To Reherse"); 
        System.out.println("This Rehersal Room Is Studio Based");
    }
}

Class that’s inherited through parent class in a separate window:


package package1;

public class FvX extends GnX {

    public FvX( ) {
    }
    
    public static double computeDiscountInfo(double price, double discount) {
        double savings;
        savings = price * discount / 100;
        return savings;
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming you are wanting to use computeDiscountInfo. This belong to the fvx object so it should be called as fvx.computeDiscountInfo(...)

Now consider

  1. it returns a double, so you can do double rv = fvx.computeDiscountInfo(...)
  2. you notice that I have wrote (...), because this method takes a couple of paramaters double price, double discount. so you should call using the parameters instead of ... maybe double rv = fvx.computeDiscountInfo(price, discount);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading