Question

This is part of my employee program. I've only been practicing Java for a few months and I'm having some trouble. I need to use the new MonthDataPair provided in the findHightest method which provides an object with the paramaters highestMonth and maxSalary. I need to use that object's getter method to return the month and salary. The code below simply finds the month in which the data I wish to access is stored. Now I need to display the data and I'm not sure how to do that correctly. All of the data comes from a document that was read and stored in an array.

    private MonthDataPair findHighest() {
        int highestMonth = 0;
        double maxSal = -1;
        for (int index = 0; index < MonthCount; index++) {
            double total = theMonth[index].totalSalary();
            if (total > maxSalary) {
                maxSalary = total;
                highestMonth = [theMonthindex].Month();
            } 
        } 
        return new MonthDataPair(highestMonth, maxSalary);
    } 

OK guys here's the MonthDataPair this is practice for an exam and I'm not allowed to modify the MonthDataPair class that is below. It was provided.

public class MonthDataPair {

private final int    month;
private final double data;

 public MonthDataPair(int month, double data) {
  this.data = data;
  this.month = month;
 } 

 public int month() {
    return month;
} 

   public double data() {
   return data;
  }  

} 
Was it helpful?

Solution 2

Add

 int getMonth(){
    return month;
   }

   double getSalar(){
    return salary;
   } 

methods in MonthDataPair class

You class should be like this

public class MonthDataPair {
int month;
double salary;

public MonthDataPair(int month, double salary) {
    this.month = month;
    this.salary = salary;
}

public int getMonth() {
    return month;
}

public void setMonth(int month) {
    this.month = month;
}

public double getSalary() {
    return salary;
}

  public void setSalary(double salary) {
    this.salary = salary;
  }

 }

OTHER TIPS

What you are doing here is you return an object of MonthDataPair class, from where the function findHighest is called. So, if you have the getter method in class MonthDataPair for Month and Salary instance, then you can use it there with the returned object, as like following:

MonthDataPair mdp = findHighest();
Month month = mdp.getMonth();
Salary salary = mdp.getSalary();

The getter method in MonthDataPair must be liked following:

public Month getMonth(){
    return this.month;
}

Hopefully it help you :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top