Edwin Davidson

Greenhorn
+ Follow
since Nov 26, 2003
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 Edwin Davidson

So - what's the code to make this work ? I simply can't be that far off.

Edwin
19 years ago
Dear all:

I need a small hand understanding packages. This is mainproj.java and is
in e:\justjava2\chap9:

----------------------------------------------
package chap9;

import subdir.*;

class mainprog {

public static void main(String args[]) {
mathh math3=new mathh();
int a=3;
int b=4;
int answer=math3.addd(a,b);
System.out.println(answer);
}
}
-----------------------------------------------

below is mathh.java and it is in e:\justjava2\chap9\subdir:

-----------------------------------------------

public class mathh {

int addd(int x, int y) {
return x+y;
}
}

-----------------------------------------------

My classpath variable is set to e:\justjava2;.;e:\justjava2\chap9

The compile error I get is:

E:\justjava2\chap9\mainprog.java:12: cannot access subdir.mathh
bad class file: .\subdir\mathh.java
file does not contain class subdir.mathh
Please remove or make sure it appears in the correct subdirectory of the
classpath.
mathh math3=new mathh();
^
1 error

Tool completed with exit code 1
--------------------------------------------------

Can you tell me please what is wrong with this code ? Can I make addd() static instead in mathh.java ?

Thank you very much. Packages SOUND great in the book but when I try it
things out they just don't work out (sometimes) - I hate that !!

Thanks.
Edwin
19 years ago
On page 225 of Peter van der Linden's Just Java 2 (5th edition) exercise 5 is written as this:

"write some code to clone an object of your class. Change your version of
clone() to do a deep copy for your class. Run the clone program again,
and make it print out enough details that you can tell the difference
between a shallow and a deep clone."

The exercise before it (exercise 4) says to override Object.clone() to do
a shallow copy for your class. My code for exercise 4 is here:
============================================================================
class testclass implements Cloneable {

public Object clone() throws CloneNotSupportedException {
System.out.println("clone "+ ++count);
return super.clone();
}

int geti() {
return i;
}

int i;
double d;
static int count;

testclass() {
i=4;d=5.5;
System.out.println("constructor "+ ++count);
}
}


class exercise4 {
public static void main(String args[]) throws
CloneNotSupportedException {
testclass tc1=new testclass();
testclass tc2;
tc2=(testclass) tc1.clone();
System.out.println("tc1 d "+tc1.d);
System.out.println("tc1 i "+tc1.i);
System.out.println("clone d "+tc2.d);
System.out.println("running clone geti() "+tc2.geti());
}
}
============================================================================
How do I do a deep clone ? The code can't be that much different. I know
if I see the code for it I'll know how it works straight off.

Thanks to all.
Edwin
19 years ago
about how much would you say you guys use nested static, local, member and anonymous classes ? And are there any examples of these around ?
Thank you.
Edwin
This works and makes a ton of sense too - now I'm getting it !! Thank you very much Amit !
Edwin
19 years ago
My CLASSPATH is still set to e:\java;.;e:\justjava2\chap09
OK I renamed e:\justjava2\chap09\mathpackage.java to e:\justjava2\chap09\mathapp.java and the code is below:
class mathapp
{
public static void main(String v[])
{
int a,b;
a=b=10;
mathlibrary ml=new mathlibrary();
System.out.println(ml.multiply(a,b));
}
}
in e:\justjava2\chap09\math\ I have mathlibrary.java - code is here:
class mathlibrary
{
public int multiply(int a, int b)
{
return a*b;
}
}
I compile these 2 files like so:
e:\justjava2\chap09>javac mathapp.java
e:\justjava2\chap09>javac math\mathlibrary.java
And they both compile clean and without error. Now I am getting this error when I type "javac mathapp":
E:\justjava2\chap09>java mathapp
Exception in thread "main" java.lang.IllegalAccessError: tried to access class mathlibrary from class mathapp
at mathapp.main(mathapp.java:10)
Can you please tell me what I am doing wrong ?
Thank you very much.
Edwin
19 years ago
I need a small push in understanding packages.My CLASSPATH variable was set to e:\java;. - I changed it for this to e:\java;.;e:\justjava2\chap09
in e:\justjava2\chap09 I have mathpackage.java - code is here:
*************************************************
package math;
class mathpackage
{
public static void main(String v[])
{
int a,b;
a=b=10;
mathlibrary ml=new mathlibrary();
System.out.println(ml.multiply(a,b));
}
}
**************************************************
in e:\justjava2\chap09\math I have mathlibrary.java - code is here:
**************************************************
class mathlibrary
{
public int multiply(int a, int b)
{
return a*b;
}
}
***************************************************
When I compile mathpackage.java I get this:
E:\justjava2\chap09\mathpackage.java:9: cannot access math.mathlibrary
bad class file: E:\java\math\mathlibrary.class
class file contains wrong class: mathlibrary
Please remove or make sure it appears in the correct subdirectory of the
classpath.
mathlibrary ml=new mathlibrary();
^
1 error
I am compiling with 'javac mathpackage'. You'd really would think that something like this would work straight off. This just can't be that hard. Likely some small silly thing I'm doing wrong. I've tried using a
"import math.*;" statement in mathpackage.java but no use. And is there a site that CLEARLY explains how to setup and properly use packages ? Again - help please.
Thank you.
Edwin
19 years ago
I can tell you that I agonized for a long time over the choice between PL/SQL and Java. I chose Java and do not regret it. Java is a real programmers language and PL/SQL in my eyes isn't - not like Java is.
If your heart tells you to learn Java then do so. Never mind chasing technology - I'm personally sick and frigging tired of doing just that. Jumping from learning this to learning that. You've got to learn some sort of language so why not Java ? I personally would like to see a day when we only have 2-3 programming languages myself. Something that can open/read/write to a database and allow you to build client/server and web apps. Java does all of that and it's abilities keep on growing.
I have a hard time to see how you can go wrong with Java. Of all the languages out there Java is way better than most. Now if only Sun would provide tons of code examples in the language API specification, that would be cool ! I used to program in Foxpro and Microsoft provided all kinds of little example code in the help file for each feature. Wish Sun did.
My 2 cents.
Edwin
19 years ago
by browsing around I came up with a solution to my date arithmetic problem and also learned a bunch of date stuff in the process. NOW I feel better ! My code is below - shows how to do date arithmetic and formatting which is pretty cool. Thanks to all who helped.
And Java Ranch rocks !!!
Edwin
************************************************************
//import these 5 classes first to get their functions
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;
class datetext
{
public static void main(String x[])
{
Date now=new Date();
SimpleDateFormat sdfdate=new SimpleDateFormat("MMMMMMMMM d, yyyy");
SimpleDateFormat sdftime=new SimpleDateFormat("h:m a");
System.out.println(sdfdate.format(now));
System.out.println(sdftime.format(now));
//create a new date - note: months are NOT 1-12 but 0-11 (zero-indexed)
Date otherDate=new GregorianCalendar(1987,02,23).getTime();
System.out.println(sdfdate.format(otherDate));
long diff=now.getTime()-otherDate.getTime();
System.out.println(diff / 86400000L);
//84600000 is the amount of milliseconds in one day 1000L*60L*60L*24L
//how to use DateFormat
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
GregorianCalendar tomorrow=new GregorianCalendar();
//add 1 to the date of tomorrow
tomorrow.add(GregorianCalendar.DAY_OF_YEAR, + 1);
//display it using dateformat
System.out.println(dateFormat.format(tomorrow.getTime()));
//this imposes a Date value on a GregorianCalendar variable - interesting !
tomorrow.setTime(now);
System.out.println(dateFormat.format(tomorrow.getTime()));
}
}
19 years ago
I have 2 Date variables and wish to know the difference (in days) between them. Does anyone have some code for that please ?
Thank you (again).
Edwin
19 years ago
class daysold
{
public static void main(String x[])
{
//import and format the date to start with
// String birthdate=x[0];
String birthdate="1961-01-18";
int birthyear =Integer.valueOf( birthdate.substring(0,4)).intValue();
int birthmonth =Integer.valueOf( birthdate.substring(5,7)).intValue();
int birthday =Integer.valueOf( birthdate.substring(8)).intValue();
}
}
Above is my code. Now I have 3 int variables that I wish to combine into a Date variable such as:
Date mybirthday=new Date(int,int,int);
I know already that the Date line right above is dead wrong. I hear that Java really screwed up on the whole date/time class thing anyway. Now you need GregorianCalendar to create a Date object. So, how do I make a birthday Date variable that I can do date arithmetric with ? And is there a really good tutorial on date and time creatio/manipulation sonewhere ?
Thanks - Javaranch rocks !
Edwin Davidson
freezing my behind on in winter in Halifax, Nova Scotia in Canada !
19 years ago
here is my actual code:
*****************************************
class one
{
public void runthis()
{
System.out.println(getClass());
System.out.println(getClass().getName());
}
}
class namee
{
public static void main(String x[])
{
one edwin=new one();
edwin.runthis();
}
}
*************************************************
what I want is to have edwin.runthis() say "edwin" and not "one". The above 2 runthis() code lines says "class one" and "one", respectfully. But the instance calling runthis() is called "edwin" and I want runthis() to say the name of the instance that is calling it, if you will.
Thanks.
Edwin
19 years ago
here is my actual code:
*****************************************
class one
{
public void runthis()
{
System.out.println(getClass());
System.out.println(getClass().getName());
}
}
class namee
{
public static void main(String x[])
{
one edwin=new one();
edwin.runthis();
}
}
*************************************************
what I want is to have edwin.runthis() say "edwin" and not "one". The above 2 runthis() code lines says "class one" and "one", respectfully. But the instance calling runthis() is called "edwin" and I want runthis() to say the name of the instance that is calling it, if you will.
Thanks.
Edwin
19 years ago
class x
{
public void printline()
{
System.out.println("am in class x"):
System.out.println("instance is newx"):
}
}
***************************************************
In the Main() procedure it says:
x newx = new x();
newx.printline();
newx.printline will spit out 2 lines to the screen. But I want the 2nd line to say the name of the instance i.e. "instance is newx" without my hard coding it like above.
There is a method that will return the name of the instance that calls x, isn't there ? It's something like getNmae() or something of that flavor I think.
Can one of you kind folks clue me in please ?
Thanks.
Edwin
19 years ago
Dear all:
int x=10;
How can my code test later on to see if "x" is indeed an int or not? It may be a byte, long, boolean, etc. And this is just testing to see what primitve data type a varible is, not testing to see which of the wrapper types (Integer, etc.) it might be (can you test for that too, now that I think of it ?).
This answer is likely simple. I went looking through the Java documentation for a function that might tell me that primitive data type of a given varible but was unsuccessful.
Thank you.
Edwin
(digging out from under 90 cm of snow in 2 days in Halifax, Nova Scotia !)
20 years ago