First Java class. Cannot figure out programming assignment. Please help.
have a program assigned to me for school and have no idea where to start. I don't know what it is about classes and objects but it loses me. This is what I have to make the program do:
Attributes:
String assetID
double cost
integer lifeExpectancy
String employee ID
String purchaseDate
String description
Methods:
Special construcor that sets assetID and employeeID, defualt values for the rest
Special constructor that takes and sets all data
Typical getters for all data
Special setters for assetID and employeeID that gurantees the two data are always stored in uppercase. These setters should be called from the special constructor.
Typcial setter for the rest
Create a method called calcAge that accepts the current date in a String, formatted as mm/dd/yyyy, and returns the age (in years only).
Predicate method to decide if the component needs to be replaced; name it outdated. Return true if the age exceeds the life expectancy, return false if the age is less than or equal to the life expectancy
Method called printData that prints all data including the age of the component to the console with labels.
This is what I have so far:
public class Asset
{
private String assetID;
private double cost;
private int lifeExpectancy;
private String employeeID;
private String purchaseDate;
private String description;
public Asset(String aid, String eid)
{
assetID = aid;
employeeID = eid;
}
public void setAssetID(String aid)
{
assetID=aid;
}
public void setCost(double cost)
{
cost=cost;
}
public void setLifeExpectancy(int lifeExpectancy)
{
lifeExpectancy=lifeExpectancy;
}
public void setEmployeeID(String eid)
{
employeeID=eid;
}
public void setPurachaseDate(String purchaseDate)
{
purchaseDate=purchaseDate;
}
public void setDescription(String description)
{
description=description;
}
public String assetID()
{
return assetID;
}
public double cost()
{
return cost;
}
public int lifeExpectancy()
{
return lifeExpectancy;
}
public String employeeID()
{
return employeeID;
}
public String purchaseDate()
{
return purchaseDate;
}
public String description()
{
return description;
}
}
Any help would be appreciated. I have no idea where to go with this since it is my first Java class taken and her assignments are a bit hard for what we have learned thus far.
Re: First Java class. Cannot figure out programming assignment. Please help.
Please explain what step you are having problems with. Ask some specific questions about your problem.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: First Java class. Cannot figure out programming assignment. Please help.
Code java:
public class Asset
{
private String assetID;
private double cost;
private int lifeExpectancy;
private String employeeID;
private String purchaseDate;
private String description;
public Asset(String aid, String eid)
{
assetID = aid;
employeeID = eid;
}
public void setAssetID(String aid)
{
assetID=aid;
}
public void setCost(double cost)
{
cost=cost;
}
public void setLifeExpectancy(int lifeExpectancy)
{
lifeExpectancy=lifeExpectancy;
}
public void setEmployeeID(String eid)
{
employeeID=eid;
}
public void setPurachaseDate(String purchaseDate)
{
purchaseDate=purchaseDate;
}
public void setDescription(String description)
{
description=description;
}
public String assetID()
{
return assetID;
}
public double cost()
{
return cost;
}
public int lifeExpectancy()
{
return lifeExpectancy;
}
public String employeeID()
{
return employeeID;
}
public String purchaseDate()
{
return purchaseDate;
}
public String description()
{
return description;
}
}
I am having trouble with the:
Special constructor that takes and sets all data
Special setters for assetID and employeeID that gurantees the two data are always stored in uppercase.
I haven't even started the other steps yet; partially because I cannot even figure these ones out.
Re: First Java class. Cannot figure out programming assignment. Please help.
Quote:
Special constructor that takes and sets all data
Take a look at the tutorial: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Quote:
Special setters for assetID and employeeID that gurantees the two data are always stored in uppercase.
Look at the String class's API doc for a method to change a String to uppercase.
http://docs.oracle.com/javase/7/docs/api/
Re: First Java class. Cannot figure out programming assignment. Please help.
Re: First Java class. Cannot figure out programming assignment. Please help.
Quote:
Originally Posted by
freestylpolaris
I am having trouble with the:
Special constructor that takes and sets all data
One way to figure out how this constructor should be written is to think how you would call a method from the Asset class, such as one of your getters. You could use the, "public Asset(String aid, String eid) {...}" constructor but you would be forced to set values for those two field variables. If you just want to create an object without having to change the value of any of your field variables, think how the method signature (or parameter list) should be. Once you have that figured out, setting all of the data should be straight forward.
Quote:
Originally Posted by
freestylpolaris
Special setters for assetID and employeeID that gurantees the two data are always stored in uppercase.
These two variables are String objects, so you want to look at the documentation for primitive String manipulation methods. norm provided a link for that, here is another one: JavaScript - Strings
Re: First Java class. Cannot figure out programming assignment. Please help.
Quote:
Originally Posted by
freestylpolaris
Special constructor that takes and sets all data
Special setters for assetID and employeeID that gurantees the two data are always stored in uppercase.
You already have one constructor that takes and sets the values for assetId and employeeId. If you do this for all your field variables, then this constructor will be created. You can also consider using the keyword, "this" in both of your constructors when setting the field variables.
assetID and employeeID are String objects, meaning you can use primitive String methods to manipulate them. norm provided an excellent link explaining how to do this.
Re: First Java class. Cannot figure out programming assignment. Please help.
Resolved it; had to add another buffer in the driver class for calculating the int. All that trouble for just one buffer statement to fix it all. Thank you everybody for your help.