Derek Harper

Greenhorn
+ Follow
since Aug 02, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Derek Harper

Hi all,

I recently wrote a program to calculate the time it takes to download a file with a 56k modem. The program looks ok but I would like to refine it so that it gives me the exact time as opposed to a 'rounded off' time.

The program is below:





For example, the time it takes to download a 50MB file is 2hrs 44 mins & 6 seconds. Unfortunately the program I wrote prints out the following:




What is the best way to refine this program so that it gives me the EXACT time instead of 'rounded' numbers?


BTW....I used the "setMaximumFractionDigits()" method in the NumberFormat class but it actually increased the hour by 1. What am I doing wrong? Please help.


Thanks in advance.........

[ September 17, 2008: Message edited by: Derek Harper ]
[ September 17, 2008: Message edited by: Derek Harper ]
15 years ago
Greetings friends,

I am working on an exercise in "Murach's Servlets & JSP" where i am trying to add code to a method in order to return an arrayList of objects. Here is the code below:




In the latter part of the code, i've added code to the "selectUsers" method to return an arrayList of User objects that corresponds with the rows in the User table. When I run the program, it compiles and brings up the page, and when I click on the link to display users, it only shows the table heading.

Please advise as to what I am doing wrong. Is something wrong with the arrayList?

If you need any additional information from me, please let me know. Thanks in advance.
15 years ago
Thanks for your reply Steve. I'll make sure to take into consideration what you recommended. I'm sorta "new" at this so I want to make sure I fully understand what's happening first.

Thanks again!
15 years ago
JSP
Hi guys,

I'm doing some experimenting with cookies. Below is code from a "Murach's Java Servlets & JSP" exercise to create a cookie that stores the first name of a user and adds it to the browser:





Yes, I know it's pretty "ugly" and i've spent hours trying to figure this out :-). Where am I going wrong? Please help. Thanks in advance.

[ September 17, 2008: Message edited by: Derek Harper ]
[ September 17, 2008: Message edited by: Derek Harper ]
15 years ago
JSP
Hi guys,

I have a question that i would like some feedback on. Take a look at the following code fragments:


Example A:

public boolean equals(Object obj) {
if(this == obj)
return true;

// 1st conditional check

if(obj == null) || (obj.getClass() != this.getClass()))
return false;

Example B:

public boolean equals(Object obj) {
if(this == obj)
return true;

// 2nd conditional check

if(!(obj instanceof Test)) return false;


My question is wouldn't the 1st conditional check be best to use in case the argument is a subclass of the superclass? It is my understanding that the 'instanceof' operator wouldn't return false.



Thanks in advance......



Also, to the moderators.....i wasn't sure which forum this question would be best served in, so forgive me if I broke any rules.
15 years ago
Ahhh, so that's what i forgot to do.

You're exactly right because once i added the e.printStackTrace() to the code, i got the following:

java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at NoFileDirDemo.main(NoFileDirDemo.java:11)

Thanks Rob!
15 years ago
Hi guys,

I'm currently experimenting with creating files and directories. I've learned that it's good practice to "play" with code in order to gain a better understanding of how something works or doesn't work.

Take a look at the following code:



Now when i compile the program, i expect an exception to occur because the call to "mkdir' has been purposely omitted. However, the code compiles fine. However, when i look in the 'bin' directory, the "dexDir" is not there.

My question is why didn't the program throw an exception? Is there something i did wrong or omitted from the code?

Thanks in advance.....
[ September 17, 2008: Message edited by: Derek Harper ]
15 years ago
Thanks Marc.

Peter,

I'm curious about your statement. Why is it not good programming style? Please enlighten me.


Thanks in advance.
15 years ago
Greetings,

I have another question regarding a "for" loop. Take a look at the following program:

class ForLoop{
public static void main(String[]args){

int i;
OuterLoop: for(i = 3; i > 0; i--){
InnerLoop: for(int j = 0; j < 4; j++){
System.out.println("i = " + i + " and j = " + j);
if(i == j)
break InnerLoop;
}
}
}
}


The output is:

i = 3 and j = 0
i = 3 and j = 1
i = 3 and j = 2
i = 3 and j = 3
i = 2 and j = 0
i = 2 and j = 1
i = 2 and j = 2
i = 1 and j = 0
i = 1 and j = 1

I'm trying to make sure that i'm understanding why i get the output i did. My understanding is when starting out, i = 3, it goes through the OuterLoop, the condition is checked (true), and it is then passed into the value of 'i':

i = 3

Next, in the InnerLoop, j starts out as '0' and goes through the loop, the condition is checked(true) and is passed into the value of 'j':

j = 0

Looking at the if statement, the lnnerLoop continues through the loop, increments according to the rule until 'j' becomes '3':

i = 3 and j = 1
i = 3 and j = 2
i = 3 and j = 3

(i == j) <-------- code breaks when i = 3 and j = 3

The InnerLoop stops ("break InnerLoop") and the OuterLoop starts over as i decrements (i--) and becomes 2, goes through the OuterLoop again, the condition is checked (true) and passed into 'i' again:

i = 2

Then the InnerLoop continues it's loop as j starts out again as 0, increments according to the rule until 'j' becomes '2':


i = 2 and j = 0
i = 2 and j = 1
i = 2 and j = 2

(i == j) <-------- code breaks when i = 2 and j = 2

.....and so on and so forth.


Am i correct in my assessment? Thanks in advance.
15 years ago
Aha!!!

Yep, that did it. Thanks Campbell.
15 years ago
Thanks for the assistance guys.....

I think i follow you now. When starting out, i = 0, it goes through the loop, the condition is checked (true), and it is then passed into the value of 'i':

i = ++0(1) + ++1(2) <------- becomes 2 after 1st increment
i = 3

The value becomes 3 and then it has to be incremented (i++, i = 4). Going thru the loop again, i = 4 and the condition is checked (true), and it is then passed into the value of 'i':

i = ++4(5) + ++5(6) <------- becomes 6 after 1st increment
i = 11

The value becomes 11 and then it has to be incremented once more (i++, i = 12).


Is this accurate now?
15 years ago
I have a question concerning the following program:


class ForDemo {
public static void main(String[]args) {

int i;
for(i = 0; i < 5; i++)
i = ++i + ++i;
System.out.println(i);
}
}

After compiling and running the program, the output is '12'. Is this due to the fact that after the loop is finished, i has the value of 5 and the pre increment operators increase the values of both "i"s to 6? ( i = ++i + ++i)

i = 6 + 6
i = 12

Am i accurate on this explanation? Thanks in advance.
16 years ago
I figured it out. I went in under 'System' and edited the System variable under "Path". Everything now works like a champ.


Thanks guys!
17 years ago
Cameron,

I understand what you are suggesting but I don't think that's the problem. I didn't have a problem compiling and executing programs before until yesterday. It's like all of a sudden my machine refuses to execute the code.

BTW, i did a search for the class file (as you suggested) and only found one on my HD.

If i had to guess, i'm thinking maybe something is not right with my CLASSPATH.
17 years ago