|
toxcct wrote: Hi David, coding java now ?! ^^
Yes, and have been for nearly two years now. It's been an enjoyable change.
What I have since found out is that I can use instanceof on the objects in the ObjectName[] array. It correctly reports them as being Apple, Orange, Truck, etc. My print routine now looks like:
public void printContents( ObjectName objectNames[] )
{
if (objectNames[0] instanceof Apple)
...
else if (objectNames[0] instanceof Orange)
...
else if (objectNames[0] instanceof Truck)
...
}
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
hum, yes, I thought of that way, but since reflexion is performance consuming (IIRC), I always prefer using "regular" OOP, not tricky solution, but it's true, you can to this (and explicit cast back to the actual type before calling methods)...
|
|
|
|
|
toxcct wrote: ...I always prefer using "regular" OOP, not tricky solution...
What did you have in mind?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
class Fruit{}
class Orange extends Fruit {}
class Banana extends Fruit {}
|
|
|
|
|
In reality, that is what I have in place:
class InfoBase
{
}
class MessageBase extends InfoBase
{
String strDate;
String strTime;
}
class RecipientBase extends InfoBase
{
String strName;
}
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Somewhere at home I have a wicked cool printing function that I tweaked from the original code, taken from an online article. I even got the author to suggest some improvements. Unfortunately, I'm at work and the whole thing is based on reflection.
What you seem to want is to override the toString() method.
By default, your objects will use Object.toString(), which merely prints out a hash code or some such. Not very useful for complex requirements.
Override it in your Fruit InfoBase class to print out whatever it is you want. Then, override it again in the subclasses only if you have special changes that the super class cannot see.
[Edited: just realized Fruit was not your class.]
If you want a one-size fits all solution, however, you'll definitely need reflection. If I remember to look tonight, I'll see if I can find that class.
Don't let my name fool you. That's my job.
modified on Monday, February 9, 2009 9:30 PM
|
|
|
|
|
Hi all.. our project is an IEEE paper.Title of the project is "Application of Software Agent to e-Commerce consumer buying support".Where can i get the source code for this project?at least i need some code snippets for this project.Kindly guide us.
|
|
|
|
|
Hello,
I have downloaded Sun Java SE version and NetBeans but dont know where to run the Hello World program.It seems that I should not use NetBeans before I know the basics of Java programming.
I am a VC++ programmer and aware of object oriented language.
Need Help
Prithaa
|
|
|
|
|
hi,
simply click on the 'file' menu in netbeans and make a new project. Select 'Java' from 'catagory' pane and 'Java Application' from 'project' pane. click next and then finish. File 'main.java' will open itself with the following code in it.
package javaapplication2;
public class Main {
public static void main(String[] args) {
}
}
add the following statement in static main after "// TODO code application logic here".
System.out.println("Hello World!");
NetBeans is a very good IDE. infact not that much like Visual studio but still good enough. I can provide you with some tutorials if you want.
|
|
|
|
|
Hello,
Thanks for your help,
Yes I do want the tutorials.
Pritha
|
|
|
|
|
is Sun's very own.
If you are using NetBeans (or Eclipse or another IDE), then you won't need to worry too much about the installation stuff.
If you want to do things the hard way (as Sun's tutorials assume), then you need to make sure you follow the installation guide carefully. Mainly so the Java binaries are in the system path.
When you do it this way, you will have to understand how the package hierarchy works and where you need to be relative to that when you launch your programs. It's a real PITA, but the experience is very helpful. Good luck.
|
|
|
|
|
C#
private void button_Click(object sender, EventArgs e)
{
SerialPort sp = new SerialPort(SerialPort.GetPortNames()[0], 9600, Parity.None, 8, StopBits.One);
String[] str = SerialPort.GetPortNames();
sp.Open();
sp.DtrEnable = true;
sp.RtsEnable = true;
sp.WriteLine("Hello World!");
sp.Close();
}
Java
Enumeration pList = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier cpi = (CommPortIdentifier) pList.nextElement();
if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL && cpi.getName().compareTo("COM1") == 0) {
try {
CommPort port = cpi.open("DarwinSys Printing", 30 * 1000);
try {
OutputStream os = port.getOutputStream();
String str = "Hello World!";
byte[] b = str.getBytes();
os.write(b);
os.flush();
os.close();
} catch (IOException ex) {
}
} catch (PortInUseException ex) {
}
I have a device on the serial port which has a led on it. The LED blinks on the C#'s code. I think the code i have written in java is approximately doing the samething but the LED does not blink.
thanks in advance.
|
|
|
|
|
Hi team,
plz explain this program how to run, i want full source code..
thanks
MANI.G
|
|
|
|
|
I got a kind of weird problem. I have created a Java Desktop Application project in NetBeans. I have created a new java class say A. Class A does not has MAIN in it. It contains just METHODS like insert, delete, update, openConnection, closeConection and others like that. To test the class A, I created the MAIN in it and tried to connect to the database, the database connects successfully and all the methods work aswell.
I created a button on the java application and the method which is called when the button is called is named as button1Pressed. Here is the simple code in the button event.
@Action
public void button1Pressed() {
A a = new A();
a.openConnection();
}
Here is the method in class A
public void openConnection() {
String connectionString = "jdbc:mysql://172.16.11.12:3306/Projector_Manager";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(connectionString,"root","1");
insertStatement = conn.prepareStatement("insert into Current_Status values (?,?,?,?,?)");
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
The error on the statement is
Communications link failure<br />
<br />
Last packet sent to the server was 1 ms ago. the error comes after 4-5 minutes and until then the application hangs.
but when I declare MAIN in the class A like
public static void main(String[] args)
{
A a = new A();
a.openConnection();
} and execute the code it connects to the database fine.
I dont know why it connects to the database the single file is executed and not when i run the GUI and press the button and it goes to the same code and does not connect.
I have also pasted the whole code in the button1Pressed but still it does not connects to the database
|
|
|
|
|
Can any some say whether its possible to develop a discussion forum using core java? if so can somebody share info on how to start on with that...
|
|
|
|
|
hi
i need to know how to impliment port adress translation for a simulator program, can anyone help
many thanks
|
|
|
|
|
Hi
i know this is possile but i dont know how. i need to set up multiple IP adresses on a single machine using java and forward each IP to a specified port. can some one please point me in the right direction to creating multiple IPs on a single machine.
note this is for a simulator program and requires the possibility to create a few hundred IP's.
many thanks
rich
|
|
|
|
|
You can assign multiple IP addresses on a single machine by entering into the network adapter's properties and then open "Internet Protocol Version 4 (TCP/ IPv4)" properties. Press the advance button and add as many IP addresses as you want. And then use those IP addresses in java.
I hope I answered it right. (for vista)
|
|
|
|
|
thanks for your responce, yeah thats correct however i need to impliment these IP's at run time and dynamically. The program im creating is part of a server application and will need to create these IP's based on data from clients at runtime rather than creating them through TCP/IP settings before hand.
is there a way to impliment multiple IP's throught java code?
|
|
|
|
|
i tried alot. even searched through net. but could no find anything. one way is to add maximum number of ipaddresses in the list and keep a list in ur program. use the free one when a request comes.
|
|
|
|
|
Hi Everybody,
I have a file which I use for writing some output during the run of the program. I also want this file to hold the error stream, in case of a problem.
I created the following code, to check what happen:
PrintStream psMyFile = new PrintStream("myFile.txt");
FileOutputStream fos = new FileOutputStream("myFile.txt", true);
PrintStream psErrorStream = new PrintStream(fos);
System.setErr(psErrorStream);
for (int i=0; i<5; i++)
psMyFile.println("line " + i);
System.err.println("This is an error 1");
psMyFile.println("after the error");
System.err.println("This is an error 2");
psMyFile.println("finished");
The content of "myFile.txt" afterward are:
line 0
line 1
line 2
line 3
line 4
after the error
finished
an error 2
As you can see, some of the errorStream text was lost. Also, I would expect that the last line will appear before the line before it.
Why does that happen? Is there a way to synchronize the writing, so that nothing will get lost?
TIA!
Dikla
|
|
|
|
|
Is there any java program that can present hierarchical data in a true tree format, as an alternative to using the standard TreeView control.
This is already avaible in C# and the link below would show you where the example is.
I wonder if the java version is avaible anywhere?
www.asp101.com/articles/jayram/treecontrol/default.asp
|
|
|
|
|
hi guys, my programming knowledge is very little. recently, i'm trying to implement a program that will do the following things :
i want to develop a program that will check how much data i'm receiving (just like a network monitoring software) while i'm connected to the internet. if my receiving amount of data is less than 2 KBps then it will play an audio file located on my hard disk. otherwise, it will do nothing. oh one more thing, the program will check the amount of receiving data every hour.
i know my idea will seem weird to all of you but i'm in serious need of this program. so please if anybody can do it for me i'll be grateful to him forever. u can make it using any language. VB,C,C++,Java doesn't matter to me. i want this program as soon as possible.this is my earnest request to all of you.
my email address : r2000.gp@gmail.com
romance
|
|
|
|
|
Hi,
I have an issue that I have to parse a query and separate all the words as well as I have to separate all the phrases from those
can any one suggest for this
|
|
|
|
|
Hello,
Assume I have this class
public class Car {
public Car() {};
}
and this class:
public class Mercedes extends Car {
public Mercedes() { super(); }
}
and this method:
public Car getNewMercedes() {
return new Mercedes();
}
I get this error:
This method must return a result of type Car
How do I fix this
Thanks in advance
|
|
|
|