The Artima Developer Community
Sponsored Link

Java Answers Forum
Question about variable, methods and constants

1 reply on 1 page. Most recent reply: May 29, 2002 1:22 PM by Guido

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
jessica

Posts: 1
Nickname: purplelige
Registered: May, 2002

Question about variable, methods and constants Posted: May 28, 2002 12:00 PM
Reply to this message Reply
Advertisement
I am working on a program I am new to Java and I am having a problem: I need to use the class below and create a variable to represent whether the employee donates money or not. I also need a constant to represent the set donation value of $10.00. Finally, a method to set the status of the employee if they donate or not. I am a little confused where do I put this in my code???
Thanks


public class Employee{

//Class instance variables

int empId;
String firstName;
String lastName;
String streetAddress;
String city;
String state;
String zip;
int empId;

double hourlyRate = 0.00; //$Per Hour default to 0
double hoursWorked = 0.0; //Total hours worked default to 0



//Class Constructor

public Employee(){
//Default constructor, sent out a basic notification message
System.out.println("New Employee Created");
}

public Employee(int id){
//Overloaded constructor, provides a shortcut for the adding the id
this.empId = id;
System.out.println("New Employee#"+empId+"Created");

}

public String empDepartment;

public Employee(int id, String dept){
//Overloaded constructor, provides a shortcut for the adding the id
this.empId = id;
this.empDepartment = dept;
System.out.println("New Employee#"+empId+"Created");
}

//Assessor Methods

public void setId(int id)
{
//Assigns a value to the emp id
this.empId = id;

}

public int getId(){
//Returns the employee's id
return(empId);
}

public void setName(String fName, String lName){
//Assigns values for the employee's name
this.firstName = fName;
this.lastName = lName;
}



public void setAddress(String street, String city, String state, String zip){
//Assigns variables for the employee's address
this.streetAddress = street;
this.city = city;
this.state = state;
this.zip = zip;
}
public void setHourlyRate(double rate)
{
//Assigns hourly rate variable
this.hourlyRate = rate;
}
public void setHoursWorked(double hours)
{
//Assigns hours worked variable
this.hoursWorked = hours;
}
public double getSalary()
{
//calculate correct salary
double salary;
salary =hoursWorked*hourlyRate;
return(salary);
}
public String toString()
{

//Declares a tempory variable and assigns basic employee
//information to it, with some basic formatting
//Note the operator '+=', what does it do?

String formattedString;
formattedString = firstName + "" + lastName +"\n";
formattedString += "Employee ID: "+ empId + "\n";
formattedString += streetAddress + "\n";
formattedString += city + "," + state + ""+ zip + "\n";

return(formattedString);
}

}


Guido

Posts: 38
Nickname: kiethb
Registered: May, 2002

Re: Question about variable, methods and constants Posted: May 29, 2002 1:22 PM
Reply to this message Reply
I am quite new as well, hopefully this will help a little bit.

First you can create a float variable called didDonate like so: private boolean didDonate;

then you also wanted a constant of $10.00 so create that with your instance variables as well like this: final float fixedDonation = 10.00;

finally, create a set method like all the rest in this class. e.g. setName etc... only this time you are setting the donation by receiving a boolean value of yes or no as to whether they donated or not. then in the new method, you can use fixedDonation to subtract from the employees paycheck amount if you like...

good luck

Flat View: This topic has 1 reply on 1 page
Topic: My tabbed panes won't appear. :^( Previous Topic   Next Topic Topic: please help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use