I want to print how much my salary increase.
Employee class:
public class Employee {
public void setSalary(double i) {
this.salario = i;
}
public double getSalary() {
return this.salario;
}
private double salary;
}
TestEmployee class:
public class TestEmployee {
public static void main(String[] args){
Employee e1 = new Employee();
e1.setEmployee(100);
System.out.println("My salary increase by " + e1.setSalary());
}
}
You should never print a setMethod();
set are void (doesn't return nothing), and they are there to change the values. Whenever printing you must call the getMethod();
That being said you are also confused with terminology and with the language as you mixed Salary and salario a few times.
Employee:
public class Employee {
private double salary;
public void setSalary(double i) {
this.salary = i;
}
public double getSalary() {
return this.salary;
}
}
TestEmployee:
public class TestEmployee {
public static void main(String[] args){
Employee e1 = new Employee();
e1.setSalary(100.00);
System.out.println("My salary was set to " + e1.getSalary());
}
}
Note that you have to SET the salary that is an attribute of the employee, and not the employee.
If you want to increase the value you can do:
setSalary(getSalary()+ 50.00);
Mixing English with your language is not a good idea when coding/posting. Always, when you are trying to post here, make sure you translate your entire code.
You could do something like this.
public class Employee {
private BigDecimal salary;
private BigDecimal latestChangeInSalary;
public Employee(BigDecimal salary) {
this.salary = salary;
latestChangeInSalary = BigDecimal.ZERO;
}
public void setSalary(BigDecimal salary) {
if ( ! salary.equals(this.salary)) {
latestChangeInSalary = salary.subtract(this.salary);
}
this.salary = salary;
}
public BigDecimal getSalary() {
return salary;
}
public BigDecimal getLatestChangeInSalary() {
return latestChangeInSalary;
}
}
Related
I have 2 classes, Student and Course. I want to be able to add progress to a course a student follows and see how much progress said student has in specific courses.
Ex:
John.addProgress(Programming, 20);
John.seeProgress(Programming); //(Result = 20)
The problem starts when I create multiple Students. If I add another student named Mike and ask for his progress in programming. I still get as a result 20, because i added 20 progress with John.
John.addProgress(Programming, 20);
John.seeProgress(Programming); //(result = 20)
Mike.seeProgress(Programming); //(result = 20, but result has to be 0)
This feels like such a simple problem with an easy solution, but I just can't figure it out for some reason.
public class Course {
private int percentage;
private String name;
public Course(String name) {
this.name = name;
this.percentage = 0;
}
public void addProgress(int amount) {
this.percentage += amount;
}
public int seeProgress() {
return this.percentage;
}
#Override
public String toString() {
return this.name;
}
}
public class Student {
private String name;
private ArrayList<Course> courses;
private int percentage;
public Student(String name) {
this.name = name;
this.courses = new ArrayList<>();
}
public void addCourse(Course course) {
this.courses.add(course);
}
public void seeCourses() {
for (Course i : courses) {
System.out.println(i);
}
}
public void addProgress(Course course, int amount) {
course.addProgress(20);
}
public int seeProgress(Course course) {
return course.seeProgress();
}
}
public class Main {
public static void main(String[] args) {
Course programming = new Course("Programming");
Course english = new Course("English");
Student john = new Student("John");
Student mike = new Student("Mike");
john.addProgress(english, 20);
System.out.println("John's progress: " + john.seeProgress(english));
System.out.println("Mike's progress: " + mike.seeProgress(english));
}
}
The result i'm getting is:
John's progress: 20
Mike's progress: 20
But the result i want is:
John's progress: 20
Mike's progress: 0
I've tried to look online for some help, but I couldn't really find anything useful, mostly because I also don't really know how to effectively look this problem up.
Thank you Hovercraft Full Of Eels! I've added a hashmap to Course and removed everything that has to do with progress from Student and now it works!
Code for anyone that wants to see:
public class Course {
private int percentage;
private String name;
private Map<Student,Integer> students;
public Course(String name) {
this.name = name;
this.percentage = 0;
this.students = new HashMap();
}
public void addProgress(Student student, int amount) {
students.put(student, students.get(student) + amount);
}
public void addStudents(Student student) {
students.put(student, 0);
}
public int seeProgress(Student student) {
return students.get(student);
}
#Override
public String toString() {
return this.name;
}
}
.
public class Student {
private String name;
private ArrayList<Course> courses;
public Student(String name) {
this.name = name;
this.courses = new ArrayList<>();
}
public void addCourse(Course course) {
this.courses.add(course);
}
public void seeCourses() {
for (Course i : courses) {
System.out.println(i);
}
}
}
.
public class Main {
public static void main(String[] args) {
Course programming = new Course("Programming");
Course english = new Course("English");
Student john = new Student("John");
Student mike = new Student("Mike");
programming.addStudents(mike);
programming.addStudents(john);
programming.addProgress(mike, 20);
System.out.println("John's progress: " +
programming.seeProgress(john));
System.out.println("Mike's progress: " +
programming.seeProgress(mike));
}
}
This question already has answers here:
PriorityQueue not sorting on add
(2 answers)
Closed 2 years ago.
I have already created a comparator for the priority queue.
class CompareBySalary implements Comparator<Employee>{
#Override
public int compare(Employee e1,Employee e2){
return e1.salary<e2.salary ? -1 : e1.salary>e2.salary ? 1 : 0;
}
}
class Employee{
String name;
float salary;
Employee(String name,float salary){
this.name=name;
this.salary=salary;
}
}
public class TryCode{
public static void main(String args[])
{
Employee e1=new Employee("C",10000);
Employee e2=new Employee("A",5000.45f);
Employee e3=new Employee("D",15000);
Employee e4=new Employee("B",5000.67f);
Queue<Employee> q=new PriorityQueue(new CompareBySalary());
q.offer(e1);
q.offer(e2);
q.offer(e3);
q.offer(e4);
for(Employee e:q)
System.out.println(e.name);
}
}
This gives the output:A B D C
Why is it not sorting correctly?
Is it something I am missing?
P.S. I have already tried with Float.compare() it gives the same output.
Change your domain model as follows:
class Employee implements Comparable<Employee> {
private String name;
private float salary;
Employee(String name, float salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public float getSalary() {
return salary;
}
#Override
public int compareTo(Employee o) {
if (o1.getSalary()>o2.getSalary()) {
return 1;
} else if (o1.getSalary()<o2.getSalary()) {
return -1;
} else {
return 0;
}
}
}
Remove CompareBySalary (you won't need it)
Create your PriorityQueue like this:
PriorityQueue<Employee> q = new PriorityQueue<>(Comparator.comparingDouble(Employee::getSalary));
I was assigned a project to create a class, and use methods without editing the "EmployeeTester" class. How can i get the name from the object harry instead of setting name ="harry"? And also can I simplify my code more?
public class EmployeeTester
{
/**
* main() method
*/
public static void main(String[] args)
{
Employee harry = new Employee("Hacker, Harry", 50000.0);**\\
harry.raiseSalary(25.0); // Harry gets a 25 percent raise!
System.out.println(harry.getName() + " now makes " +
harry.getSalary());
}
}
public class Employee
{
// instance variables
private double salary;
private String name;
/**
* Constructor for objects of class Employee
*/
public Employee(String employeeName, double currentSalary)
{
//Initializes instance variables
salary = currentSalary;
name = employeeName;
}
public String getName()
{
//Sets name to harry
name = "Harry";
return name;
}
public void raiseSalary(double byPercent)
{
//multiplies by 1.25 percent to get the 25% raise
salary = salary *(1 + byPercent/100);
return;
}
public double getSalary()
{
//returns the salary
return salary;
}
}
It can be like this
class Employee
{
private double salary;
private String name;
public Employee(String titleName, double salary) {
this.name = titleName.split(",")[1];
this.salary =salary;
}
public void raiseSalary(double byPercent) {
salary = salary *(1 + byPercent/100);
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
}
I just have this basic code where I need help adding employee data to an ArrayList of another class. I am just writing this code in preparation for an assignment, so don't bash my code too much. Essentially though, i'll be needing to add elements of employees and delete them eventually. But for now, I just need help adding the elements to my other Employee class. =]
public class main {
private static Employee employee;
public static void main(String[] args) {
employee = new Employee(10,10);
System.out.println(employee.toString());
}
}
...............
import java.util.ArrayList;
public class Employee {
public int employeeNum;
public double hourRate;
ArrayList<Employee> Employee = new ArrayList<>();
public Employee(int employeeNum, double hourRate){
this.employeeNum = employeeNum;
this.hourRate = hourRate;
}
public String toString(){
return ""+employeeNum+hourRate;
}
}
Simple Example -
package com;
import java.util.ArrayList;
public class TestPage{
public static void main(String[] args){
Employee emp1, emp2;
emp1 = new Employee();
emp2 = new Employee();
emp1.setName("MAK");
emp2.setName("MICHELE");
emp1.setAddress("NY");
emp2.setAddress("WY");
//and keep putting other information like this
ArrayList<Employee> employee = new ArrayList<Employee>();
employee.add(emp1);
employee.add(emp2);
System.out.println("emp1 name is : " + employee.get(0).getName());
System.out.println("emp2 name is : " + employee.get(1).getName());
System.out.println("emp1 address is : " + employee.get(0).getAddress());
System.out.println("emp2 address is : " + employee.get(1).getAddress());
}
}
class Employee{
String name, address;
int age, salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
It seems like what you're asking is based on one employee having sub-employees and that structurally that probably represents a hierarchy (Some commenters seem to be missing that point). But that's an assumption on my part. Based on that assumption.
A little bit of feedback to start on structure of your main class:
public class main {
public static void main(String[] args) {
Employee employee = new Employee(10,10);
System.out.println(employee.toString());
}
}
It seems to me that there's no reason to have a static instance variable for that root employee instance. You should try to limit the scope of variables where possible. It seems like it could very well be in the main() method's scope.
public class Employee {
public int employeeNum;
public double hourRate;
ArrayList<Employee> employees= new ArrayList<>();
public Employee(int employeeNum, double hourRate){
this.employeeNum = employeeNum;
this.hourRate = hourRate;
}
public String toString(){
return ""+employeeNum+hourRate;
}
public ArrayList<Employee> getEmployees() {
return this.employees;
}
}
It may be better to name your arraylist employees or employeeList. I went with employees in this case because that convention is preferable.
And in relation to your question, ArrayList is pass by reference so you could just add a getter method for the sub-employee list (employees).
To add employees from your main method you could do something like
Employee rootEmployee = new Employee(5, 10.0);
rootEmployee.getEmployees().add(new Employee(6, 5.0));
Or you could add an additional method to Employee like this:
public void addEmployee(Employee e) {
employees.add(e);
}
I have created an Employee class used to figure by weekly pay as an abstract. I have then created 2 subclasses for Salary and Hourly employees. The issue I have is in my test class not printing out the array list and then retrieving the correct bi weekly pay. Was hopping another set of eyes could help.
/** Employee class with abstract method*/
public abstract class Employee {
public String fullName;
/** construct a default employee object*/
public Employee() {
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
/** abstract method getBIWeeklyPay */
public abstract double getBiWeeklyPay();
public void printgetBiWeeklPay() {
System.out.println("Employee "+this.fullName+ "'s"+ "Bi-weekly pay is:");
}
}
/**Salary Class*/
public class SalaryEmployee extends Employee {
public SalaryEmployee() {
}
public double salary;
public double getBiWeeklyPay() {
return salary/(52*2) ;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
/** Hourly Class*/
public class HourlyEmployee extends Employee {
public double hourlyRate;
public double hoursPerWeek;
public HourlyEmployee() {
}
public double getBiWeeklyPay() {
return hourlyRate * (hoursPerWeek *2);
}
public double getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public double getHoursPerWeek() {
return hoursPerWeek;
}
public void setHoursPerWeek(double hoursPerWeek) {
this.hoursPerWeek = hoursPerWeek;
}
}
/**Test Class*/
import java.util.*;
public class EmployeeTest {
public static void main(String[] args){
ArrayList<Employee> employees = new ArrayList<Employee>();
SalaryEmployee mike = new SalaryEmployee();
mike.setFullName("Mike Burns");
mike.setSalary(75000.00);
employees.add(mike);
HourlyEmployee tim = new HourlyEmployee();
tim.setFullName("Tim Globe");
tim.setHourlyRate(12.00);
tim.setHoursPerWeek(40.00);
employees.add(tim);
print(employees);
}
public static void print(ArrayList<Employee> employees){
for(Employee e: employees){
e.printgetBiWeeklyPay();
}
}
}
your var employees is of type ArrayList but ArrayList has no printBiWeeklyPay function.
instead you should make your own printBiWeeklyPay function that takes the ArrayList as a parameter and call that instead.
An example would be:
public void printByWeeklyPay(ArrayList<Employee> list) {
for (Employee e : list) {
System.out.println(e.toString());
}
}
Also you'll have to override #toString() in your Employee classes to return some relevant output.
public static void main(String args[]) {
ArrayList<Employee> employees = new ArrayList<Employee>();
employees.add(john); // hourly employee
employees.add(fred); // salary employee
employees.add(bob); //hourly employee
print(employees);
}
public static void print(ArrayList<Employee> employees) {
for each employee in employees {
employee.printBiWeeklyPay();
}
}