Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 03-15-2009, 03:09 PM   #1
In Runtime
 
akira282's Avatar
 
Join Date: Mar 2009
Posts: 172
Default Java project help

Hello, just doing a project for school that requires I use two jsps, one of which is a html page for the user, and the other is the server page, and two java classes.

http://kyler282.googlepages.com/4630..._Exam_SP09.doc


The above link is a verbatim list of what I have to do. I have already created the jsp form for the user.

After you take a look, can you tell me how to parse each from the form?
Meaning

asset id is a string so is it String parse.String etc in the jsp?
__________________

akira282 is offline   Reply With Quote
Old 03-16-2009, 08:16 AM   #2
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: Java project help

Well first of all, this is a pretty sweet class (this isn't a Java Web 101 class is it?).

Anyway, after reading the assignment, query Strings are not allowed, therefore I would opt for sessions because they are more "secure" than querystrings or cookies, and they're relatively easy to work with. I'll assume you've worked with sessions before in JSP but here is the basic run-down of the psuedo-code, which you seem to already understand, but this helps me understand the flow:
Code:
1.) Input data into form.
3.) *optional* use client-side (javascript) validation for the form's inputs.
2.) Save form fields in session object (easier for cross-page handling).
3.) Validate inputs at the server side.
4.) Parse input's accordingly
Here is a sample of what I mean:
First jsp page:
Code:
<?xml version="1.0" charset="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-us" xml:lang="en-us">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1;" />
  <title>JSP Form</title>
 </head>
 <body>
  <form name="form1" id="form1" method="post" action="jspPage2.jsp">
    <b><label for="assetID">Asset ID:</label></b><input type="text" name="assetID" id="assetID" maxlength="50" style="width: 120px;" />
    <input type="submit" name="submit" id="submit" value="Submit" onclick="return myValidationFunction();" />
  </form>
 </body>
</html>
jspPage2.jsp:
Code:
<?xml version="1.0" charset="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-us" xml:lang="en-us">
 <head>
  <%
      // repeat this for the rest of the itmes you need to bring in
      String aID = request.getParameter("assetID");
      if(aID.length > 0) {
        // store AssetID
      } else {
        // print error to client
      } // end if/else
  %>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1;" />
  <title>Second JSP Page</title>
 </head>
 <body>
 </body>
</html>
Since the Asset ID is a string, you really don't need to do a whole lot of parsing, just check to make sure it's not null (if you were inserting into a database, you'd also want to check against SQL Injection).

For the others, you'd follow the same type of idea except for the integer fields, you would say:
Code:
int assetPurchPrice = -1;
try {
   assetPurchPrice = Integer.parse(request.getParameter("assetPurchasePriceInputName"));
} catch (NumberFormatException nFeX) {
   // error handling if it's not a valid integer
} catch (Exception ex) {
   // error handling if an unknown error occurs
} // end try/catch
Then later on, for output for the user you could say:
Code:
if (assetPurchPrice < 0) {
  // our variable still has the initial value of -1 or, they entered a negative number
} else {
  // looks good, continue what we were doing
}
Hope all of this helps.
__________________

__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Old 03-16-2009, 10:29 AM   #3
In Runtime
 
akira282's Avatar
 
Join Date: Mar 2009
Posts: 172
Default Re: Java project help

What about the java classes: asset and depreciator. What would the "get" methods be for the depreciator class?
akira282 is offline   Reply With Quote
Old 03-16-2009, 11:23 AM   #4
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: Java project help

Hmm, I guess I'm not fully understanding your question. The instructions outline the get Methods you should use for the depreciator class and the asset class.

When outputting the asset for example, you would call the getDepreciationTable(String method) function.

This function would call the many get methods associated with the asset in question. What I tend to do is if it is a small class, store the class itself in a session variable, so for example:
Code:
<%
  Depreciator myDepreciator = new Depreciator();
  session.setAttribute("dep",myDepriciator);
// then to set the asset here
  session.getAttribute("dep").setAsset(new Asset(basisValue,lifeValue,yearPurchased,assetID));
//now we have an asset created in our session in the myDepreciator object with the values passed in from the form.
%>
html code here
<%=session.getAttribute("dep").getDepreciationTable("someMethod")%>
The set methods in the Asset class are responsible for validating the input. So,
Code:
In your asset class:
//private instance variable
private int _life = 0;
private setLife(int lifeVal) {
  if (lifeVal <= 0 || lifeVal >= 100) {
    // handle invalid range for asset life
  } else {
   _life = lifeVal;
  } // end if/else
} // end setLife
public int getLife() {
  return _life;
} // end function getLife
then in your depreciator class' getDepreciationTable function:
Code:
public String getDepreciationTable(String method) {
  String htmlTable = "";
  //htmlTable building code here
  htmlTable += getAsset().getLife() + " years.\n";
  return htmlTable;
} // end function getDepreciationTable
Am I understanding correctly? Do you want to know what all the methods for the depreciator class would look like?
__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Old 03-16-2009, 01:16 PM   #5
In Runtime
 
akira282's Avatar
 
Join Date: Mar 2009
Posts: 172
Default Re: Java project help

Sorry for making it so ambiguous, I have completed the form for the user and the jsp echo back inputs, so what I type in it spits out. Anyway, I have completed the get/set methods for asset. However, for the depreciator class

I am stuck on how to do the get methods.

For example GetSYD would be: Public double getSYD{
return getSYD

but where do I put the formulas for the methods? I suppose I put them in each get method which makes sense.

For one "get" method" it has a parameter (int years) which is confusing me because I havent' encountered a "get" method with a parameter.
I hope this help. Thanks for all the input. I'm so close to completing this assignment I just need a little more help as to the following:

Where do I put the formulas?
Do I need "conditionals" in the jsp to determine which method I am using?

I think that should do it for now. I'll keep you posted tonight.

Also, I'm not familiar with sessions. This is my second java class so far.
akira282 is offline   Reply With Quote
Old 03-16-2009, 01:55 PM   #6
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: Java project help

Well, I'm making a couple assumptions here but here goes.
1.) I'll assume that these classes are setup to handle only 1 asset per depreciator class
2.) With that in mind, the class structure for Depreciator your instructor has outlined seems slightly flawed (too much recursion).
3.) No error handling has been built in to check to see if the depreciator has an asset object before you call the getDepreciationTable function, if an asset has not been set at this time, the program will throw a run-time exception

With all that in mind it should probably look something like this:
Code:
public String getDepreciationTable(String method) {
  String returnString = "";
  String returnValue = null;
  NumberFormat nf = NumberFormat.getCurrencyInstance();
  switch(method) {
    case "SLD":
      returnValue = nf.format(getSLD());
      break;
    case "SYD":
      // this is where the run-time exception will occur if an asset hasn't been created
      returnValue = nf.format(getSYD(getAsset().getYearPurchased()));
      break;
    case "DDB":
      //assuming double basis variable is not private
      returnValue = nf.format(getDDB(bookValueVariable);
      break;
    } // end switch
    return ;// do your return statement here, outputting your html and returnValue
} // end function getDepreciationTable
Depending on how you actually compute those values (sorry I didn't take the time to read up on deprecation) you may need to encapsulate the switch statement in a for loop

Yes the formulas should be in the getSYD and getSLD, etc...

These functions (if you implement the code I wrote) should return raw double's which will then be formatted with commas and dollar signs using the nf (NumberFormat) object. To use this, you'll need to import java.text.NumberFormat if you haven't already.

Not sure where the book value variable should come from, the form probably. In which case, you'll need to do one of two things, add a new variable and a get/set method for it, that stores the bookValue, or pass a bookValue variable along with the method name in the getDepreciationTable function.
If it still doesn't make sense, I should be able to sit down and work the whole class out.

Don't worry about the ambiguity. Happens all the time, it's only bad if you don't know how to express your problem fully even after someone asks you to that it becomes a problem.
__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Old 03-16-2009, 02:21 PM   #7
In Runtime
 
akira282's Avatar
 
Join Date: Mar 2009
Posts: 172
Default Re: Java project help

Thanks for the help. I'll post something later tonight when I've had time to sit down again and start at it.

Also, I'll post what I have so you know what I have later.

Also, lol, I will be using a "for" loop inside the depreciationtable method
akira282 is offline   Reply With Quote
Old 03-16-2009, 02:49 PM   #8
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: Java project help

Look forward to it! I love projects like this.
__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Old 03-16-2009, 05:39 PM   #9
In Runtime
 
akira282's Avatar
 
Join Date: Mar 2009
Posts: 172
Default Re: Java project help

Ok first question:

Back story: I added to the getSLD method
in this manner double SLD = asset.getBasis()/asset.getLlife;
return SLD;

ok now, I am on the getSYD method. The first thing that confuses me is this is:
why is there a parameter for this method (int year) for a get method?
Anyway, I'm going to use the formula but I need understanding on that

Next important question:

Where do I put the conditionals, I think I'm going to put it in the JSP. In other words, depending on the radio button chosen, I need it to pass on that information and which formula to use. Any help in this area would be good too thanks.

This is how I solved the second one:

public double getSYD(){
double SYD = asset.getBasis()*(asset.getLife()-asset.getYearPurchased()+1)/(asset.getLife()*(asset.getLife()+1)/2);
return SYD;
akira282 is offline   Reply With Quote
Old 03-16-2009, 05:55 PM   #10
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: Java project help

One of the things they attempted to drill into my head when I was in Java class was get methods don't take arguments, just return values, and set methods always take arguments and set values. This doesn't have to be the case.

Take for example, lets say you had a class with a private instance variable that was an array. You don't want them to be able to modify the array directly, but how do you give them access to what is stored in the array without making it public?

Enter the get method that takes an argument:
Code:
private arr[];

public Object getArrayItem(int index) {
  return arr[index];
} // end getArrayItem
In short, the only reason a get is different from a set, is because you put the word get or set in front of the function name. How many arguments it takes is totally up to you. It can take as many arguments as you need it to take to return the value.

Another example:
Code:
public int getSum(int num1, int num2) {
  return num1 + num2;
} // end getSum
Hope that clarifies a little bit.

I think putting the conditional inside of the getDepreciationTable function is the best route to take.

If you put conditionals in the JSP, you'll still have to pass which formula to use into getDepreciationTable, at which time you'll need to call the function anyway no?

Could you post some example pseudo-code of what you would make the conditional in your JSP look like? There are a couple ways to do it.
__________________

__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 03:04 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0