简体   繁体   中英

Java Programming Help for 2 files with a controlling class

First java class is the primary one which invokes other classes. I will copy the code for the two files. I cannot compile this code and I do not know why.

When I had the program in one file it ran fine, but the instructor wants it in two files. Can someone explain to me what I am doing, or did incorrectly?

File 1:

public class Wk2ToddFoughty {        
    public static void main(String[] args) {
        SalaryCalc FinalSalaryCalc = new SalaryCalc();            
        FinalSalaryCalc.SalaryCalc();               
    }
}

File 2:

import java.util.Scanner;

class SalaryCalc {                                  
      Scanner in = new Scanner(System.in);
      System.out.println("Enter your annual sales: $");          
      double sales = in.nextDouble();
      double salary = 35000.00;
      double commission = (sales * .015);
      double totalSalary = salary + sales;
      System.out.println("Your Salary + Commission is: $" + totalSalary );   
}

It might be easier to understand if you kept to Java naming conventions - also see inline comments

public class Wk2ToddFoughty {        
    public static void main(String[] args) {
        SalaryCalc myCalc = new SalaryCalc();  //  SalaryCalc - name of class
                                               // myCalc - name of object            
        myCalc.doCalc();                       // doCalc - name of method (missing from your class)           
    }
}

// edit this class to add missing method

public class SalaryCalc {                                  

      public void doCalc () {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter your annual sales: $");          
        double sales = in.nextDouble();
        double salary = 35000.00;
        double commission = (sales * .015);
        double totalSalary = salary + sales;
        System.out.println("Your Salary + Commission is: $" + totalSalary );   

    }
}

Your second file doesn't define a method called SalaryCalc . You should also honor standard Java naming practices, and keep input/output logic outside of your business classes.

import java.util.Scanner;

public class Wk2ToddFoughty {        
    public static void main(String[] args) {  
        Scanner in = new Scanner(System.in);
        System.out.println("Enter your annual sales: $");   
        SalaryCalc calc = new SalaryCalc(35000);
        double totalSalary = calc.calcSalary(in.nextDouble());    
        System.out.println("Your Salary + Commission is: $" + totalSalary);
    }
}

public class SalaryCalc {
    private double salary;

    public SalaryCalc(double salary) {
        this.salary = salary;
    }

    public void calcSalary(double sales) {
        double commission = sales * 0.015;
        return salary + sales + commission;
    } 
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM