I keep on getting an error saying Cannot find symbol when trying to compile. The files are both in the same folder, i'm not really sure where i went wrong here.
In this assignment im supposed to write a program that reads a list of employees from a file. The name of the file will be ‘Employee.txt’. The program should output the sorted array to a file called “SortedEmployee.txt”. I already have the Heap class done. Need assistance please.
public class Employee
{
String id;
String name;
String department;
String position;
double salary;
int yos; //Year of Service
//constructor w/ no args
public Employee()
{ salary = 0.0;
id = name = department = position = "";
yos = 0;
}
//constructor w/ args
public Employee(String i, String n, String d, String p, double s, int y)
{
id = i;
name = n;
department = d;
position = p;
salary = s;
yos = y;
}
public void setID(String i)
{ id = i;}
public void setName(String n)
{ name = n;}
public void setDepartment(String d)
{department = d;}
public void setPosition(String p)
{position = p;}
public void setSalary(double s)
{salary =s;}
public void setYOS(int y)
{yos = y;}
public String getID()
{ return id;}
public String getName()
{ return name;}
public String getDepartment()
{return department;}
public String getPosition()
{return position;}
public double getSalary()
{return salary;}
public int getYOS()
{return yos;}
public String toString()
{
String str = "Emplyee Id: " + id + "\nName: " + name +
"\nDepartment: " + department + "\nPosition: " + position
+ "\nSalary: " + salary;
return str;
}
public int compareTo(Employee emp)
{
int idONE = id.compareToIgnoreCase(emp.id);
if (idONE != 0)
return idONE;
return 0;
}
}
public class EmployeeCOMP implements Comparable<Employee>{
#Override
public int compareTo(Employee emp){
return this.id.compareToIgnoreCase(emp.id);
}
}
This is the error I keep on getting.
EmployeeCOMP.java:4: error: cannot find symbol
return this.id.compareToIgnoreCase(emp.id);
^
symbol: variable id
1 error
this refers to the instance of EmployeeCOMP which does not have an id. In this context the compareTo method should be part of the Employee class (not a separate class):
public class Employee {
...
public int compareTo(Employee emp) {
return this.id.compareToIgnoreCase(emp.id); // **this** refers to an Employee instance
}
}
Attempting to use through a separate class suggests you might be needing to implement a Comparator.
Related
I have created a class employee in Java, where each object of the class stands for a staff. The objects take 3 parameters - Name, Dept. and Salary. The program looks like this:
public class employee
{
String name;
int salary;
String dept;
employee staff1 = new employee("x","IT",100000);
employee staff2 = new employee("y", "HR", 200000);
public employee(String n, String d, int s)
{
this.name= n;
this.salary= s;
this.dept = d;
}
public static void main (String args [])
{
}
public void Display()
{
}
}
I want to make a method (the Display method in the code) which takes the object name as a parameter (the code does not have a parameter) and returns (or prints) its data values. Please also tell me what should come in the main method. Thanks in advance.
You can use this -
public class employee {
String name;
int salary;
String dept;
public employee(String n, String d, int s) {
this.name = n;
this.salary = s;
this.dept = d;
}
public static void main(String args[]) {
employee staff1 = new employee("x", "IT", 100000);
employee staff2 = new employee("y", "HR", 200000);
Display(staff1);
Display(staff2);
}
public static void Display(employee object) {
System.out.println("name='" + object.name + '\'' +
", salary=" + object.salary +
", dept='" + object.dept + '\'');
}
}
My programming assignment tasked me with writing an increase/decreasePay abstract method that must be put in my abstract employee class. I can't seem to get the the method correct in HourlyWorker so that it will take increase or decrease the pay by a "percentage". My math is sound (monthly pay - or + (monthly pay * the percentage), but my output in my test class is coming out the same after increasing/decreasing pay. Any help?
Employee class:
abstract public class Employee
{
private String lastName;
private String firstName;
private String ID;
public abstract void increasePay(double percentage);
public abstract void decreasePay(double percentage);
public abstract double getMonthlyPay();
public Employee(String last, String first, String ID)
{
lastName = last;
firstName = first;
this.ID = ID;
}
public void setLast(String last)
{
lastName = last;
}
public void setFirst(String first)
{
firstName = first;
}
public void setIdNumber(String ID)
{
this.ID = ID;
}
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
public String getName()
{
return firstName + lastName;
}
public String getIdNumber()
{
return ID;
}
}
HourlyWorkerClass
public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
public HourlyWorker(String last, String first, String ID, double rate)
{
super(last, first, ID);
hourlyRate = rate;
}
public void setHours(int hours)
{
this.hours = hours;
}
public int getHours()
{
return hours;
}
public void setHourlyRate(double rate)
{
if ( hours > 160 )
this.hourlyRate = hourlyRate * 1.5;
else
this.hourlyRate = rate;
}
public double getHourlyRate()
{
return hourlyRate;
}
public void setMonthlyPay(double monthlyPay)
{
monthlyPay = hourlyRate * hours;
}
public double getMonthlyPay()
{
return hourlyRate * hours;
}
public void increasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public void decreasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public String toString()
{
String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
+ getIdNumber() + " \nHourly Rate: " + hourlyRate;
return result;
}
}
Testing class (currently testing increase
public class TestEmployee2
{
public static void main(String[] args)
{
Employee [] staff = new Employee[3];
Supervisor sup = new Supervisor("Boss", "Jim", "JB7865", 54000);
HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 11.95);
hw1.setHours(200);
staff[0] = sup;
staff[1] = hw1;
System.out.println(staff[0].getMonthlyPay());
staff[0].increasePay(5);
System.out.println(staff[0].getMonthlyPay());
System.out.println(staff[1].getMonthlyPay());
staff[1].increasePay(10);
System.out.println(staff[1].getMonthlyPay());
}
}
Supervisor class:
public class Supervisor extends Employee
{
private double annualSalary;
private double monthlyPay;
public Supervisor(String last, String first, String ID, double salary)
{
super(last, first, ID);
annualSalary = salary;
}
public void setAnnualSalary(double salary)
{
annualSalary = salary;
}
public double getAnnualSalary()
{
return annualSalary;
}
public double getMonthlyPay()
{
return ((annualSalary + (annualSalary * .02)) / 12);
}
public void increasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public void decreasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public String toString()
{
String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
+ getIdNumber() + "\nAnnual Salary: " + annualSalary;
return result;
}
}
Output is:
4590.0 4590.0 2390.0 2390.0
Doesn't appear to be modifying getMonthlyPay()
Should be:
4590.00 4819.50 2390.00 2629.00
Generally, when implementing equals(), you compare “key” fields whose values don’t change for the entity, and don’t compare “state” fields whose values change from time to time.
You are comparing sharePrice, when I believe you should be comparing symbol.
When you do list.indexOf(temp), what that does, right now, is look for a Stock that is equals to the argument passed to it -- so it looks for a Stock with price zero, not caring about the symbol at all. That's what the code does right now.
Honestly, using indexOf and equals is not really appropriate for this problem. indexOf is really only useful when you have something that's totally equal to the target you're looking for.
The best way to do something like this is
Optional<Stock> foundStock = list.stream().filter(stock -> stock.getName().equals(symbol)).findAny();
if (foundStock.isPresent()) {
// do something with foundStock.get()
} else {
// no found stock
}
indexOf() is a method return the index of the first occurrence of the specified element in the returned list. If the list does not contain this element, value -1 is returned.
More formally, return the lowest index i that meets the following conditions:
if(o==null? get(i)==null :o.equals(get(i))){
return i;
}
return -1;
If there is no such index, return -1.
And you have override the equals method, I guess you just want to focus on the same price Stock?:
#Override
public boolean equals(Object obj){
if (obj instanceof Stock){
Stock other = (Stock) obj;
return getPrice() == other.getPrice();
}
return false;
}
As my opinion, you have use List<Stock> list so the Object in the list is all Stock. Maybe it could be simplifed:
#Override
public boolean equals(Object obj){
Stock other = (Stock) obj;
return getPrice() == other.getPrice();
}
Not quite sure why, but eclipse is putting an error on my setter methods that reads "Syntax error, insert "...VariableDeclaratoid" to complete FormalParamaterList."
Here's my code:
public class Student {
public int id;
public String name;
Student() {
}
public int getID() {
return id;
}
public void setID(i) {
this.id = i;
}
public String getName() {
return name;
}
public void setName(n) {
this.name = n;
}
public String toString() {
System.out.println("The student's name is: " + this.name);
System.out.println("The student's ID is: " + this.id);
}
}
Because you did not give your parameter i and n their types. See below
public void setID(int i) {
this.id = i;
}
public void setName(String n) {
this.name = n;
}
Got an error at : Movie m = new Movie(id, name, cost);
"cannot find symbol - var cost"
cost is set only when user insert input and cannot put actual value e.g.200.00
What should I put as argument?
Also, Session can only be created if user enters correct movie ID.
How do I match compare input(int) to an array?
Any help will be appreciated. Explanation is also important for me
Movie Class:
private void addMovie()
{
System.out.println("Setup a Movie");
int id = movies.setId();
String name = In.readString("Enter Movie Name: ");
double cost = In.readDouble("Enter Movie Cost:" );
Movie movie = new Movie(id, name, cost);
movies.add(movie);
menu();
}
Session Class:
private void addSession()
{
System.out.println("Add a Session");
int id = sessions.setId();
String name = In.readString("Enter Session Name: ");
int movieId = In.readInt("Enter Movie id:" );
**//match input with id array**
int theatreId = In.readInt("Enter Theatre id:" );
**//match input with theatre id array**
String sessionTime = In.readString("Enter Session Time - 0 for 9am, 1 for 12noon, 2 for 3pm or 3 for 6pm: ");
double GoldSeatsPrices = In.readDouble("Enter Prices fro Gold Class Seats:");
double ReguSeatsPrices = In.readDouble("Enter Prices for Regular Seats:");
Movie m = new Movie(id, name, cost);
Session session = new Session(id, name, m);
sessions.add(session);
menu();
}
Movie Class:
public class Movie extends Record
{
private double cost;
public Movie(int id, String name, double cost)
{
super(id, name);
this.cost = cost;
}
public double getCost()
{
return cost;
}
public String toString()
{
return "Movie: "+ super.toString() + " cost: $"+ cost;
}
}
Records Class: (super.):
import java.util.*;
/**
* class Records - complete
*/
public class Records
{
protected LinkedList<Record> records = new LinkedList<Record>();
protected int id = 0;
protected Record find(int id)
{
for(Record record: records)
{
if (record.matches(id))
return record;
}
return null;
}
protected void add(Record record)
{
records.add(record);
}
public int size()
{
return records.size();
}
public void show()
{
System.out.println(toString());
}
public String toString()
{
String str = "";
for(Record record : records)
str += record.toString() + "\n";
return str;
}
}
Record:
/**
* class Record - complete
*/
public class Record
{
protected int id;
protected String name;
public Record(int id, String name)
{
this.id = id;
this.name = name;
}
public String getName()
{
return name;
}
public int getId()
{
return id;
}
public boolean matches(int id)
{
return this.id == id;
}
public String toString()
{
return id + " " + name;
}
}
To the first question: There is no variable cost within addSession(). If you have not defined an attribute cost within Session then this is the problem.
To the second question: I am not quite sure that I understand your problem correctly. You have an int[] values and want to know, whether a given int x is within that array? If so, you can achieve this with this code snippet:
for (int value : values) {
if (value == x) {
// Put code, that should be executed when the value is found, here
}
}
Can you place the Super class Record also in your question?
Probably, you need to check the super constructor, which is differing from your sub class constructor.
this is my current code to store rooms(it compiles fine) but in the UML there is a variable called addEquipment and there is also another class called Equipment to be defined. I'm having trouble wrapping my head around what I'm supposed to do with this. Am I supposed to create and call an object called Equipment? what goes in addEquipment?
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private String equipmentList;
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public String getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(String anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
}
//Create room object
public Room(int capacity, String equipmentList) {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
return "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
}
}
You can create a new class Equipment and modify your attribute equipmentList to be a List:
public class Equipment {
private String name;
public Equipment(String name) {
this.name = name;
}
}
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private List<Equipment> equipmentList = new ArrayList<Equipment>();
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public List<Equipment> getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(List<Equipment> anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
Equipment oneEquipment = new Equipment(newEquipment);
equipmentList.add(oneEquipment);
}
//Create room object
public Room() {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
String capacity=String.valueOf(getCapacity());
String room = "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
return room;
}
}
In the method addEquipment, you can create a new Equipment and add it to equipmentList, like code above.
An Equipment class could be anything. Lets assume the "Equipment"-class has a String called "name" as it's attribute
public class Equipment {
String name;
public Equipment( String name) {
this.name = name;
}
public String getName() {
return this.name
}
}
When you extend your Room class by the requested "addEquipment" method, you can do something like this.
public class Room {
... // Your code
private int equipmentIndex = 0;
private Equipment[] equipment = new Equipment[10]; // hold 10 Equipment objects
public void addEquipment( Equipment eq ) {
if ( equipmentIndex < 10 ) {
equipment[ equipmentIndex ] = eq;
equipmentIndex++;
System.out.println("Added new equipment: " + eq.getName());
} else {
System.out.println("The equipment " + eq.getName() + " was not added (array is full)");
}
}
}
Now when you call
room.addEquipment( new Equipment("Chair") );
on your previously initialized object of the Room-class, you will get
"Added new equipment: Chair"
Hope this helps a bit.
PS: The code is untestet (maybe there hides a syntax error somewhere)