This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2021/8/11

can yuh tell me what is wrong and what I am missing.

1
2
divinity divinity

2021/8/11

#
hi this is another one of my practicing program. One called personnel, there are three classes. one is hourlywage employee, salaredemployee and employess(which is abstract) along with my main class called personnel: i am going to put all three classes along with my main for you all to take a look at them and i am also going to post the end result. (how it supposed to run and the result that I am looking for. the first class will be the employee class. this is what the employees class should do. I cant figure out to get a method to increase the hourly wage by percentage , an abstract method called computePay that calculate the weekly pay for the employee. here is the employee class
public  abstract class Employee {
    
    String firstname;
    String lastname;
    double hourlywage;
    float rate;
    double pay;
    int extrahours;
    double wagepercentage;
    boolean wage;
    

public Employee(String firstname, String lastname, double hourlywage){
    
    this.firstname = firstname;
    this.lastname = lastname;
    setWage(hourlywage);
        
    }
   
    public abstract double comPay(double hours);{
    
    pay=(40 * rate)+ (extrahours * rate + 1.5);
   }
    
    public String getfirstname(){
        return firstname;
    }
    public String getlastname(){
        return lastname;
    }
    public double gethourlywage(){
        return hourlywage;
    }
    public void setfirstname(String firstname){
        this.firstname = firstname;
    }
    public void setlastname(String lastname){
        this.lastname = lastname;
    }
   
     public void setWage(double hourlywage) {
         wage = (hourlywage < 0.0) && (hourlywage <= 1.5);
        
    }
    public void wagepercentage(double percentage){
        wagepercentage = wagepercentage * ((100 + percentage)/100);
        
    }
    @Override
    public String toString(){
        return "Employee details "+"\n"+ "Employee firstname is"+ firstname+"\n"+"Employee last name is:  "+"" + "Hourly wage is "+hourlywage;
    }

   
that is the employee class above. the next class is the hourlywage employees class which extends the employee class. I think there is some pretty much calculation still to be done but dont know if what I have is correct. check it for me plz. here. here is what the hourlywage class supposed to do in respect in calculation; Methods will include computePay and toString. To determine the employee’s pay, computePay mulitplies the first 40 hours (or ferwer) by the employee’s hourly wage. Hours worked beyond 40 are paid at time-and-a-half (1.5 times the hourly wage). toString returns a string containing the employee’s name and hourly wage, formatted as shown in the example of the r command. here is the hourlywage class
public  class HourlyEmployee extends Employee {
    
    String firstname;
    String lastname;    
    float rate;
    double comPay;
    double hourlyWage;    
    double totalpay=0;
    double wage;
    double hours;
    double raiseWage;
    double percent;

    public HourlyEmployee(String firstname, String lastname, double hourlywage) {
        super(firstname, lastname, hourlywage);
        setWage(hourlyWage);
    }
    
    public  double comPay(){
        
        return 0;
    }
    public String getfirstname(){
        return firstname;
    }
    public String getlastname(){
        return lastname;
    }
    public double raiseWage(){
        return raiseWage;
    }
    public double gethourlywage(){
        return hourlywage;
    }
    public void setfirstname(String firstname){
        this.firstname = firstname;
    }
    public void setlastname(String lastname){
        this.lastname = lastname;
    }
    public void sethourlywage(double hourlywage){

        this.hourlywage = hourlywage;
     }
        
    public double getWage(){
        return wage;
    }

     public void setWage(double hourlyWage) {
              
    }                                                                                    

     public double getHours() {
        return hours;
    }
  
    public void setHours(double hoursWorked){
      
      hours = hoursWorked;
        
    }
    public void setraiseWage(double percent){
        wage = wage *((100 + percent)/ 100);
    }
    public double earnings(){
      if(getHours()<=40)       
       
      return getWage()* getHours();
      
     return 40 * getWage() + (getHours() - 40 * getWage() * 1.5);
       
    }
    
    @Override
    public String toString(){
        return "Employees details"+ " "+ "First name is "+ firstname+""+"Hourlywage is "+hourlywage;
    }

    @Override
    public double comPay(double hours) {
       return 0;
    }

   
}
this is the salariedemployee class which is also an extension to employee class: To do this conversion, assume that a salaried employee works a 40 hours a week for 52 weeks.) comptePay always returns 1/52 of the annual salary, regardless of the number of hours worked. toString returns a string containing the employee’s name and annual salary, formatted as shown in the example of the r command. here is the salaried employee class
public class SalariedEmployee extends Employee {
    
    String firstname;
    String lastname;
    double annualsalary;
    double comPay;
    int hours;
    
    
    public SalariedEmployee(String firstname,String lastname, double annualsalary){
        super(firstname, lastname, annualsalary);
        annualsalary = this.annualsalary;
    }
    public String getfirstname(){
        return firstname;
    }
    public String getlastname(){
        return lastname;
    }
    public double getannualsalary(){
        return annualsalary;
    }
    public void setfirstname(String firstname){
        this.firstname = firstname;
    }
    public void setlastname(String lastname){
        this.lastname = lastname;
    }
    public void setannualsalary(double annualsalary){
        this.annualsalary = annualsalary;
    }
    @Override
    public double comPay(double hours) {
        if(hours > 40){
            double extrahours = hours - 40;
            pay = (40 * rate) + (extrahours * rate);
        }
        else{
            pay =  hours * rate;
        }
       return annualsalary;
    }
    
    public String toString(){
        return "Salaried Employee "+"\n"+"Employee name is"+firstname+"\n"+"Annual Salaried is"+ annualsalary;
    }
and last but not least here is the main, it is called personnel: in the main, i am trying to bring up a method called equals. using the while loop. trying to quite the program using the while loop but not getting it to come up. the while loop supposed to be like this: while(!choice.equals("q") for some reason cant get it. here is the main class
ublic class Personnel {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scanin = new Scanner(System.in);
        
        
        String firstname, lastname;
        double hourlywage;
        char command = 0;
        String n,p,r, s,h;
        double annualsalary, new_wage;
        double hoursworked;
        double hourly =0, salaried=0;
        float percentinc;
        boolean choice = true;
        Employee[] newemp = new Employee[50];
        
        while(command != 'q'){
            System.out.print("Please select one of the following option");
            System.out.print("n - Select new employee");
            System.out.print("p - To compute paychecks");
            System.out.print("r - For raise wages");
            System.out.print("q - To quit");
            
        if(command == 'n'){
            System.out.print("Enter new employee name");
            firstname = scanin.next();
            System.out.print("Enter new employee last name");
            lastname = scanin.next();
        }else if(hourly == 'h' || salaried == 's'){
            System.out.print("Enter number of hourly wage");
        }else if(command == 'n'){
            System.out.print("Enter new employee name");
            firstname = scanin.next();
            
            System.out.print("Enter last name");
            lastname = scanin.next();
        }else if(hourly == 'h' || salaried == 's'){
            System.out.print("Enter annual salary");
            annualsalary = scanin.nextDouble();
        }
        break;
     }
        if(command == 'p'){
            System.out.print("Enter number of hours worked");
            hoursworked = scanin.nextDouble();
        }else if(command == 'r'){
            System.out.print("Enter percentage increase");
            percentinc = scanin.nextFloat();
        } else{
            System.out.print("Enter new wages");
            new_wage = scanin.nextDouble();
        }
        while(!choice.){
            
        }
    
    }
    
}
this is the while loop am trying to use
while(!choice.equals("q")){
    System.out.println("Enter a number or q to quit");
    choice = input.next();

    if(!choice.equals("q")){
        int number = Integer.parseInt(choice);
    }
} 
will post the result that I am looking for when i hear from u
danpost danpost

2021/8/11

#
You still do not seem to have a grasp on basic Object oriented programming fundamentals. INHERITANCE: a child class will inherit non-private fields and methods of its parents (and its parent's parents, etc). Declaring fields again in a child class will duplicate the field and actions done on one will not affect the other. This means that you can just about empty out all codes from HourlyEmployee and SalariedEmployee classes (all codes duplicated in the Employee class). COMPARTMENTALIZATION: A class describes an object by giving it its possible states (in field values) and behaviors (in method implementation). Your Personnel class should have an Employee field -- not firstname, lastname, etc, as these are contained within the specific Employee object. SCOPE: variables should be limited in scope to their minimum requirements. Most other fields in your Personnel class should be declared within the methods they are used in -- not as Personnel fields, but as local variables. Unless they need retained from one run to the next, they need no be declared in the header. First fix: Employee class, line 21: remove "abstract" and ";". However, that method is for hourly employees only and therefore the method should be in the HourlyEmployee class.
danpost danpost

2021/8/11

#
These should be the basics of these classes:
public  abstract class Employee
{     
    String firstname;
    String lastname;
    
    public Employee(String firstname, String lastname){
        this.firstname = firstname;
        this.lastname = lastname;
    }

    public String getFirstName(){
        return firstname;
    }
    public String getLastName(){
        return lastname;
    }
    
    public void setFirstName(String name){
        firstname = name;
    }
    
    public void setLastName(String name){
        lastname = name;
    }
    
    @Override
    public String toString(){
        String str = "Employee details\n";
        str += "First name is "+firstname+"\n";
        str += "Last name is "+lastname+"\n";
        return str;
    }
}
public  class HourlyEmployee extends Employee {
    double wage;    
 
    public HourlyEmployee(String firstname, String lastname, double hourlywage) {
        super(firstname, lastname);
        setWage(hourlyWage);
    }
    
    public double getHourlyWage(){
        return wage;
    }
    
    public void setWage(double hourlywage){
        wage = hourlywage;
    }
    
    public void raiseWageByPercent(double percent){
        wage = wage *((100+percent)/ 100);
    }
    
    public double getEarnings(double hoursworked){
        if (hoursworked<=40) return wage*hoursworked;
        return (40+(hoursworked-40)*1.5)*wage;
    }
    
    @Override
    public String toString(){
        return super.toString()+"Hourly wage is "+wage+"\n";
    }
}
public class SalariedEmployee extends Employee {
    double salary;
     
    public SalariedEmployee(String firstname,String lastname, double annualsalary){
        super(firstname, lastname);
        salary = annualsalary;
    }
    
    public double getAnnualSalary(){
        return salary;
    }
    
    public void setAnnualSalary(double annualsalary){
        salary = annualsalary;
    }

    public void increaseSalaryByPercent(double percent){
        setAnnualSalary(salary*((100+percent)/100));
    }
    
    public double getWeeklyPay(){
        return salary/52;
    }
    
    public String toString(){
        return super.toString()+"Annual salary is"+salary+"\n";
    }
}
divinity divinity

2021/8/12

#
I would like to use the while loop to end the program, how do i get to use it without it giving me any errors. am trying to bring the choice.equals("q") method but I am not getting it to come up. can u show me how to do it without the errors this is the while loop
while(!choice.equals("q")){
    System.out.println("Enter a number or q to quit");
    choice = input.next();
 
    if(!choice.equals("q")){
        int number = Integer.parseInt(choice);
    }
}
and in my main, personnel, can u point out any errors or what I may have to change to make it work
danpost danpost

2021/8/12

#
You do not have to do anything:
// ...
    while (!choice.equals("q"){ // your line 1 above
        // ...
    }
    System.out.println("Goodbye");
}
My line 5 shows the exit to end program.
danpost danpost

2021/8/12

#
If you are getting errors, you need to copy/paste the error message and show the codes around which the error occurs. I do not see where any errors may occur with the given code (unless you are entering, or scanning in, a non-numeric or non-integer string that is NOT "q").
divinity divinity

2021/8/12

#
while(!choice.equals("q")){ error is; boolean cannot be dereferenced
             System.out.println("Enter a number or q to quit"); boolean cannot be dereferenced
            choice = scanin.next(); error: incompatible types: String cannot be converted to boolean
----

        if(!choice.equals("q")){
            int number = Integer.parseInt(choice); error; incompatible types: String cannot be converted to boolean
----
danpost danpost

2021/8/12

#
Wait ... shouldn't choice be declared as a String object? In your full Personnel class posting above, you declared it as a true boolean. There is that, and I believe you now have two fields, command and choice, which may refer to the same data (if so, one should be removed).
divinity divinity

2021/8/13

#
hi danpost, I removed one, which was choice and use command but am still getting the same error. it is now telling char cannot be deference.
}
        while(!command.equals("q")){
            System.out.println("Enter a number or q to quit"); the error now is: char cannot be dereferenced
            

        if(!command.equals("q")){
            int number = Integer.parseInt(command);
            }
    
danpost danpost

2021/8/13

#
At line 12 in Personnel class, you have command declared as a char type variable. You want it to be of type String .
String command = "";
divinity divinity

2021/8/13

#
hey danpost: something is wrong, it is not running the way it is supposed to run. it supposed to start run by asking the user to enter the command n for the new employees or select from the option given but instead it starting from enter new wages. which is not supposed to do, can u tell what is wrong here. and here is the result when i run the program but it is nothing like it supposed tol here is the whole code
package personnel;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author User
 */
public class Personnel {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scanin = new Scanner(System.in);
        
        
        String firstname, lastname;
        double hourlywage;
        String command = "";
        String n,p,r, s,h;
        double annualsalary, new_wage;
        double hoursworked;
        double hourly =0, salaried=0;
        float percentinc;       
        Employee[] newemp = new Employee[50];
        
        while(command.equalsIgnoreCase("q")){
            System.out.print("Please select one of the following option");
            System.out.print("n - Select new employee");
            System.out.print("p - To compute paychecks");
            System.out.print("r - For raise wages");
            System.out.print("q - To quit");
            
        if(command.equals("n")){
            System.out.print("Enternew employee name");
            firstname = scanin.next();
            System.out.print("Enter new employee last name");
            lastname = scanin.next();
        }else if(hourly == 'h' || salaried == 's'){
            System.out.print("Enter number of hourly wage");
        }else if(command.equals("n")){
            System.out.print("Enter new employee name");
            firstname = scanin.next();
            
            System.out.print("Enter last name");
            lastname = scanin.next();
        }else if(hourly == 'h' || salaried == 's'){
            System.out.print("Enter annual salary");
            annualsalary = scanin.nextDouble();
        }
        break;
     }
        if(command.equals("p")){
            System.out.print("Enter number of hours worked");
            hoursworked = scanin.nextDouble();
        }else if(command.equals("r")){
            System.out.print("Enter percentage increase");
            percentinc = scanin.nextFloat();
        } else{
            System.out.print("Enter new wages");
            new_wage = scanin.nextDouble();
        }
        while(!command.equals("q")){
            System.out.println("Enter a number or q to quit");
            command = scanin.next();

        if(!command.equals("q")){
            int num = Integer.parseInt(command);
    }
} 
    
    }
    
}
and here is the result when run
run:
Enter new wages
q
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:864)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextDouble(Scanner.java:2413)
	at personnel.Personnel.main(Personnel.java:68)
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 16 seconds)
danpost danpost

2021/8/13

#
No code should follow after the end of the while loop currently ending at line 54 (except maybe a "Goodbye." message being given -- as I had shown above). Also, there is no input line following the main menu (should be a scanin line at 35).
divinity divinity

2021/8/13

#
what u mean by except a goodbye at line 54, and what u mean by no input following scanin line at 35
danpost danpost

2021/8/14

#
divinity wrote...
what u mean by except a goodbye at line 54, and what u mean by no input following scanin line at 35
Just what I wrote:
danpost wrote...
No code should follow after the end of the while loop currently ending at line 54 (except maybe a "Goodbye." message being given -- as I had shown above). Also, there is no input line following the main menu (should be a scanin line at 35).
Also, remove line 53
divinity divinity

2021/8/14

#
pardon me if i am sounding like a broken record but i still dont understand what u mean and i have remove line 53 and i am still getting the same result. nothing has change
There are more replies on the next page.
1
2