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;
}
}
CodePudding user response:
Assuming you are wanting to use computeDiscountInfo
. This belong to the fvx
object so it should be called as fvx.computeDiscountInfo(...)
Now consider
- it returns a double, so you can do
double rv = fvx.computeDiscountInfo(...)
- you notice that I have wrote
(...)
, because this method takes a couple of paramatersdouble price, double discount
. so you should call using the parameters instead of...
maybedouble rv = fvx.computeDiscountInfo(price, discount);
CodePudding user response:
What you want to do is to call the computeDiscountInfo function in the FvX class from your main method.
First create an FvX instance inside your main method and then call the computeDiscountInfo function using that fvx object. You can replace your main class like show below.
public class GnX {
public static void main(String[] args) {
FvX fvx = new FvX( );
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( );
savings = fvx.computeDiscountInfo(price, discount);
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");
}
}