Abstract test method error when printing arraylist - java

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();
}
}

Related

How to add objects that use attributes from multiple info classes to an array list in main program?

I have a info class Vehicle that has multiple attributes and I have a Moped info class that extends the vehicle class. I need to create a main program that I can add multiple mopeds to an array list and then print them. Here is my attempt:
info class1:
package data;
public class Vehicle {
private float price;
private String color;
private int maximumSpeed;
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color= color;
}
public int getMaximumSpeed() {
return maximumSpeed;
}
public void setMaximumSpeed(int maximumSpeed) {
this.maximumSpeed = maximumSpeed;
}
public String toString() {
return "Price: "+this.price+" Color: "+this.color+" Maximum speed: "+this.maximumSpeed;
}
}
Info class2:
package data;
public class Moped extends Vehicle{
private String Brand;
private String type;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand= brand;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String toString() {
return "Brand: "+this.brand+" Type: "+this.type;
}
}
Main program:
package app;
import java.util.ArrayList;
import java.util.Scanner;
import data.*;
public class ExtendsTest {
public static void main(String[] args) {
ArrayList<Moped> list=new ArrayList<>();
Moped mp=new Moped();
askForMopedInfo(mp);
lista.add(mp);
printMoped(list);
}
private static void printMoped(ArrayList<Moped> list) {
for (int i=0;i<list.size(); i++) {
System.out.println(list.get(i));
}
}
private static void askForMopedInfo(Moped mp) {
Scanner scanner = new Scanner(System.in);
System.out.print("Brand: ");
String k = scanner.nextLine();
mp.setBrad(k);
System.out.print("Type: ");
k = scanner.nextLine();
mp.setType(k);
System.out.print("Price: ");
int k1 = Integer.parseInt(scanner.nextLine());
mp.setPrice(k1);
System.out.print("Color: ");
k = scanner.nextLine();
mp.setColor(k);
System.out.print("Maximum speed: ");
k1 = Integer.parseInt(scanner.nextLine());
mp.setMaximumSpeed(k1);
}
}
When I run the program I get asked for:
Brand:
Type:
Price:
Color:
Maximum speed:
But the program only prints:
Brand: x Type: x
And I am still not sure how to add multiple mopeds to the array list. Any help would be much appreciated!
Update toString method of your Moped class, like this:
package data;
public class Moped extends Vehicle {
/*
other code
*/
public String toString() {
return super.toString() +" Brand: "+this.brand+" Type: "+this.type;
}
}
to Add more mopeds to the array list, you can do this:
public static void main(String[] args) {
ArrayList<Moped> list=new ArrayList<>();
While(true){
Moped mp=new Moped();
askForMopedInfo(mp);
list.add(mp);
printMoped(list);
}
}

Compilation error..Class cannot be resolved to a type

I have created abstract class Employee and I define a method called calculateSalary() as an abstract method. Employee abstract class is inherited by Contractor & FullTimeEmployee classes. When I try implementation of Abstract in main class I got a compiler error that shows class cannot be resolved to a type.
Employee.Class:
package Abstract;
public abstract class Employee
{
String name;
int salaryPerHour;
public Employee(String name, int salaryPerHour) {
this.name = name;
this.salaryPerHour = salaryPerHour;
}
public abstract int calculateSalary();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalaryPerHour() {
return salaryPerHour;
}
public void setSalaryPerHour(int salaryPerHour) {
this.salaryPerHour = salaryPerHour;
}
public class Contractor extends Employee
{
int workingHours;
public Contractor(String name, int salaryPerHour, int workingHours) {
super(name, salaryPerHour);
this.workingHours = workingHours;
}
#Override
public int calculateSalary()
{
return getSalaryPerHour() * workingHours;
}
}
public class FullTimeEmployee extends Employee
{
public FullTimeEmployee(String name, int salaryPerHour) {
super(name, salaryPerHour);
}
#Override
public int calculateSalary()
{
return getSalaryPerHour() * 8;
}
}
}
In Main class:
package Abstract;
public class AbstractClassExample {
public static void main(String[] args) {
Employee contractor = new Contractor("contractor", 10, 10);
Employee fullTimeEmployee = new FullTimeEmployee("full time employee", 8);
System.out.println(contractor.calculateSalary());
System.out.println(fullTimeEmployee.calculateSalary());
}
}

How can i get information from object- Java? Can I simplify my code more?

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;
}
}

Adding elements to an ArrayList from another class

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);
}

How to print setSalary()?

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;
}
}

Categories