计算结果为 0.0

huangapple go评论122阅读模式
英文:

Calculation returning 0.0

问题

我在我的Java课程中遇到了一些问题。我的numCredits * creditFee + addFee的计算没有进行处理;它继续返回0.0

作业截图:

![作业截图][1]

(注意,我还没有添加作业的其他部分,即向RegistrationTester添加和减少3学分的部分。一旦我解决了这个问题,我会添加这些部分。)

这是我的代码:

public class Registration {
    //属性
    private String firstName = "";
    private String lastName = "";
    private double numCredits = 0;
    private double addFee = 0;
    private double creditFee = 70;
    public double balance = 0;

    //构造函数
    public Registration(String firstName, String lastName, double numCredits, double addFee){
        this.firstName = firstName;
        this.lastName = lastName;
        this.numCredits = numCredits;
        this.addFee = addFee;
    }

    public Registration(){} //
    public void balance(double balance){
        System.out.println(numCredits * creditFee + addFee);
    }
    public void addCredit(double addCredit){
        if (this.balance > 0) {
            this.balance = balance + addCredit * creditFee;
        }
    }

    public void subCredit(double subCredit){
        if (this.balance > 0){
            this.balance = balance - (subCredit * creditFee);
        }
    }

    //设置器
    public void setFirstName(String fname){
        this.firstName = fname;
    }
    public void setLastName(String lname){
        this.lastName = lname;
    }
    public void setNumCredits(double credits){
        this.numCredits = credits;
    }
    public void setAddFee(double fee){
        this.addFee = fee;
    }
    public void setcreditFee(double creditFee){
        this.creditFee = 70;
    }
    public void setBalance(double balance){
        this.balance = balance;
    }

    //获取器
    public String getFirstName(){
        return this.firstName;
    }
    public String getLastName(){
        return this.lastName;
    }
    public double getNumCredits(){
        return this.numCredits;
    }
    public double getAddFee(){
        return this.addFee;
    }
    public double getCreditFee(){
        return this.creditFee;
    }
    public double getBalance(){
        return this.balance;
    }
}

public class RegistrationTester {
    public static void main(String[] args){
        Registration student1 = new Registration ("m", "w", 15, 60);
        Registration student2 = new Registration ("d", "y", 12, 45);

        //显示学生及其当前状态

        System.out.printf("%s %s:%n Current Credit Hours: %f%n Additional Fees: $%.2f%n"
                + "Current balance due: $ %.2f%n", 
                student1.getFirstName(),student1.getLastName(), student1.getNumCredits(), student1.getAddFee(),
                student1.getBalance());
        System.out.printf("%s %s:%n Current Credit Hours: %f%n Additional Fees: $%.2f%n"
                + "Current balance due: $ %.2f%n", 
                student2.getFirstName(), student2.getLastName(), student2.getNumCredits(), student2.getAddFee(),
                student2.getBalance());        
    }
}
英文:

I'm having some issues with an assignment for my Java class. My calculation of numCredits * creditFee + addFee is not processing; it continues to return 0.0

The assignment:

计算结果为 0.0

(Note, I have not yet added the additional parts of the assignment, the adding and subtracting of 3 credit hours to RegistrationTester. I will add those once I figure out this problem.)

Here is a look at my code:

public class Registration {
//attributes
private String firstName = ""; 
private String lastName = "";
private double numCredits = 0;
private double addFee = 0;
private double creditFee = 70;
public double balance = 0;
//constructor
public Registration(String firstName, String lastName, double numCredits, double addFee){
this.firstName = firstName;
this.lastName = lastName;
this.numCredits = numCredits;
this.addFee = addFee;
}
public Registration(){} //
public void balance(double balance){
System.out.println(numCredits * creditFee + addFee);
}
public void addCredit(double addCredit){
if (this.balance > 0) {
this.balance = balance + addCredit * creditFee;
}
}
public void subCredit(double subCredit){
if (this.balance > 0){
this.balance = balance - (subCredit * creditFee);
}
}
//setters
public void setFirstName(String fname){
this.firstName = fname;
}
public void setLastName(String lname){
this.lastName = lname;
}
public void setNumCredits(double credits){
this.numCredits = credits;
}
public void setAddFee(double fee){
this.addFee = fee;
}
public void setcreditFee(double creditFee){
this.creditFee = 70;
}
public void setBalance(double balance){
this.balance = balance;
}
//getters
public String getFirstName(){
return this.firstName;
}
public String getLastName(){
return this.lastName;
}
public double getNumCredits(){
return this.numCredits;
}
public double getAddFee(){
return this.addFee;
}
public double getCreditFee(){
return this.creditFee;
}
public double getBalance(){
return this.balance;
}
}

and

public class RegistrationTester {
public static void main(String[] args){
Registration student1 = new Registration ("m", "w", 15, 60);
Registration student2 = new Registration ("d", "y", 12, 45);
//display students and their current stats
System.out.printf("%s %s:%n Current Credit Hours: %f%n Additional Fees: $%.2f%n"
+ "Current balance due: $ %.2f%n", 
student1.getFirstName(),student1.getLastName(), student1.getNumCredits(), student1.getAddFee(),
student1.getBalance());
System.out.printf("%s %s:%n Current Credit Hours: %f%n Additional Fees: $%.2f%n"
+ "Current balance due: $ %.2f%n", 
student2.getFirstName(), student2.getLastName(), student2.getNumCredits(), student2.getAddFee(),
student2.getBalance());        
}
}

答案1

得分: 0

Method 1: Properly setting balance

余额始终返回0.0,因为您从未在方法中设置余额。

我认为您的余额方法应该是:

public void balance(double balance){
  this.balance=(numCredits * creditFee + addFee);
}

Method 2: Returning balance

您可以将RegistrationTest.java中的student1.getBalance()替换为返回doublestudent1.balance()

RegistrationTest应该是:

System.out.printf("%s %s:%n Current Credit Hours: %f%n Additional Fees: $%.2f%n"
        + "Current balance due: $ %.2f%n", 
        student1.getFirstName(),student1.getLastName(), student1.getNumCredits(), student1.getAddFee(),
        student1.balance());

balance()方法应该是:

public double balance(){
  return (numCredits * creditFee + addFee);
}

通过这些更改,它的输出将是:

Mihail Dichev:
Current Credit Hours: 15.000000
Additional Fees: $60.00
Current balance due: $ 1110.00
Dene Logan:
Current Credit Hours: 12.000000
Additional Fees: $45.00
Current balance due: $ 885.00
英文:

Method 1: Properly setting balance

The balance always returns 0.0 because you never set the balance in your method.

I believe your balance method should be:

 public void balance(double balance){
this.balance=(numCredits * creditFee + addFee);
}

Method 2: Returning balance

You can replace student1.getBalance() in RegistrationTest.java with student1.balance() that returns a double.

RegistrationTest should be:

System.out.printf("%s %s:%n Current Credit Hours: %f%n Additional Fees: $%.2f%n"
+ "Current balance due: $ %.2f%n", 
student1.getFirstName(),student1.getLastName(), student1.getNumCredits(), student1.getAddFee(),
student1.balance());

And the method balance() should be:

 public double balance(){
return (numCredits * creditFee + addFee);
}

With these changes it outputs:

Mihail Dichev:
Current Credit Hours: 15.000000
Additional Fees: $60.00
Current balance due: $ 1110.00
Dene Logan:
Current Credit Hours: 12.000000
Additional Fees: $45.00
Current balance due: $ 885.00

huangapple
  • 本文由 发表于 2020年9月19日 09:00:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63964325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定