I am trying to create a program where there is a method with a formula.This formula is exported to other classes, but each class uses different variables.
when I use my program the mothods and variables are exported and I only want to export the method (formula) from Bronce class to Silver class.
If I cannot hide variables, how canI overcome this problem??
I am new at java
Bronze Class
public class Bronze {
// ----------------- Atributes -----------------------
private static final double costDay = 0.12;
public int dayMinutes; // daytime telphone minutes used
public double dayTimeCost; //Total daytime calls cost
// ------------- CONSTRUCTORS (inputs) ---------------
public Bronze(int theDayMinutes ) {
dayMinutes = theDayMinutes;
}
// ------------------ METHODS ------------------------
// Calculate Total daytime calls cost
public double calcDayTimeCost() {
dayTimeCost = dayMinutes * costDay;
return dayTimeCost;
}
//toString method to override that in Object
public String toString(){
return("\nCost of daytime calls = " + costDay + "/min"+
"\n\nTotal daytime calls cost = " + dayTimeCost +
"\n"
);
}
//Returns the type of account
public String type(){
return "Bronze";
}
}
Silver Class
public class Silver extends Bronze {
private static final double costDay = 0.22;
public Silver(int theDayMinutes ) {
super(theDayMinutes );
}
//Returns the type of account
public String type(){
return "Silver";
}
}
Main Class
import java.util.Scanner;
public class AccountUser {
// ------------------- FIELDS ------------------------
// Create instance of Scanner class
public static Scanner input = new Scanner(System.in);
// variables
public static Bronze bron;
public static Silver silv;
public static int dayMinutes;
// ------------------ METHODS ------------------------
public static void main(String [] args) {
// Input dayMinutes (with error message)
do{
System.out.print("Please daytime telphone minutes used --> ");
dayMinutes = input.nextInt();
if ( dayMinutes <= 0){System.out.print("\n" + "Input value outside the range!!!" + "\n");}
}while( dayMinutes <= 0);
// Create new Bronze instance
bron = new Bronze(dayMinutes);
silv = new Silver(dayMinutes);
// Calculate scheme1, scheme2
bron.calcDayTimeCost();
silv.calcDayTimeCost();
System.out.println(bron);
System.out.println(silv);
}
}
Make costDay a parameter to the method. public double calcDayTimeCost(double costDay) then call it with silv.calcDayTimeCost(0.22)
While the keyword overriding
is wrong here because it does not apply for class fields, the concept of shadowing
still applies.
Right now when you invoke calcDayTimeCost
on your Silver
object it uses the super
field of costDay
.
You have to change it to
protected double costDay = 0.12; // Bronze class
and
public Silver(int theDayMinutes ) {
super(theDayMinutes );
super.costDay = 0.12;
}
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.