Apologies for vague title as i couldn’t think what the name is.
Basically creating a small program that calculates a students financial payment.
When i run it, it calculates the object allowance no problem.
However whatever i try the object ‘Bursary’ doesn’t seem to come up with anything aside 0.
The code is as follows :
import java.util.Scanner;
public class studentFinance implements Payment {
private String stuname, department, course;
private float budget, allowance, bursary;
private int id, currentbudget, attendance;
private double numbofcourses;
// student name, id, department, course enrolledon, attendance, bursary,allowance
//course and numbofcourses already read in
Scanner in = new Scanner(System.in);
public studentFinance(float currentBudget) {
budget = currentbudget;
}
public float amendBudget(float newbudget) {
budget = newbudget;//Transfer data from parameter to instance variable
return budget;//Return statement
}
public float calcPayment() {
//stuname,department,numbofcourses,attendance,id
System.out.println("Please enter the student name");
stuname = in.next();
System.out.println("Please enter the department name");
department = in.next();
System.out.println("Please enter the number of numbofcourses");
numbofcourses = in.nextDouble();
System.out.println("Please enter the attendance");
attendance = in.nextInt();
System.out.println("Please enter their ID number");
id = in.nextInt();
System.out.println("Enter HND,HNC or NC");
course = in.next();
if (attendance > 95 & numbofcourses >= 6) {
allowance = 1000;
} else if (attendance > 85 & numbofcourses >5) {
allowance = (1000 * .75F);
} else if (attendance > 75 & numbofcourses >= 4) {
allowance = (1000 * .5F);
} else if (attendance > 75 & numbofcourses >= 3) {
allowance = 100;
} else {
allowance = 0;
}
if(course=="HND") {
bursary = 250;
} else if(course=="HNC") {
bursary = 200;
} else if(course=="NC") {
bursary = 100;
} else {
bursary = 0;
}
return bursary + allowance;
}
double payment;
public void makePayment() {
System.out.println("The allowance total is : " + payment);
payment = bursuary + allowance;
}
public void print() {
}
}
If it isany use this is the code used for the interface , theirs other elements to it but i don’t think they are relevant here.
interface Payment {
public float amendBudget(float budget);
public float calcPayment();
public void makePayment();
public void print();
}
Regards for any help.
==doesn’t do what you think it does here.==(when applied to objects) just checks to see if two objects are the same object, not if their contents are equivalent, so it doesn’t work for strings here. What you want is:Generally you only want to use
==when dealing with primitives (int,float, etc), or you explicitly just want to see if two variables point to exactly the same object.You could also write them as:
but I prefer to use
.equalson the constants as you know they will never benull. It’s probably not a concern for you here, but it’s just the pattern I’ve gotten used to.