|  | |  |
11-22-2004, 12:11 AM
|
#1 (permalink)
| | Registered User
Join Date: Nov 2004
Posts: 14
| Datagrams, multi threading, popping and pushing Hi guys. Though I'm not all that good at Java programming, don't let my thread here fool you. It's just that it's 3 AM, my programs are due tomorrow night, and I don't have time to explain everything I know and what it is I need explained for my programs.
So, I have a few programs I need help with. I'll just retype some of the directions for making them, and display what I have so far.
For this program, it's one file of a two parter. The first must ask the user for 10 messages, and send each message as a datagram to the server. Code: import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame
{
private JTextArea displayArea;
private DatagramSocket socket;
//set up GUI and DatagramSocket
public Server()
{
super("server");
displayArea=new JTextArea();
getContentPane().add(new JScrollPane(displayArea),
BorderLayout.CENTER);
setSize(400,300);
setVisible(true);
//create DatagramSocket for sending and receiving packets
try
{
socket=new DatagramSocket(5000);
}
//process problems creating DatagramSocket
catch(SocketException socketException)
{socketException.printStackTrace();
System.exit(1);
}
} //end Server constructor
//wait for packets to arrive, display data and echo packet to client
private void waitForPackets()
{
while (true)
//loop forever
{
//receive packet, display contents, return copy to client
try
{
//set up packet
byte data[]=new byte[100];
DatagramPacket receivePacket=
new DatagramPacket(data, data.length);
socket.receive(receivePacket); //wait for packet
//display information received from packet
displayMessage("/nPacket received:" -----------------------
It's been a while since I've even dealt with this one, so I'm not sure how complete it is. The second part must accept datagrams from the client, handle each message as a thread, append the message in the datagram to a file, and use my name as part of the file name. Here's what little I have of that, though I have some good info to help finish it. Code: import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame
{
private JTextField enterField;
private DatagramSocket socket;
//set up GUI and DatagramSocket
public Client()
{
super("Client");
Container container=getContentPane();
enterField=new JTextField("Type message here");
enterField.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event)
{
//create and send packet
try {
displayArea.append("/nSending packet containing: " +
event.getActionCommand() + "/n"); ----------------------
I'll put my other programs up in additional posts. And please, try and guide me in the right direction, help me with syntax, errors, etc. I'm not trying to cheat, and will need to be able to understand this stuff. Chunks of code, definitions, etc. are all strongly appreciated as well, though keep in mind I may have to adapt them.
Last edited by Belisarius; 11-22-2004 at 05:20 AM.
|
| |
11-22-2004, 12:17 AM
|
#2 (permalink)
| | Registered User
Join Date: Nov 2004
Posts: 14
| I haven't gotten started on this program. I will work on it tomorrow and have help, but I'd appreciate any I can get here as I'm very pressed on time. I need to make a series of predefined queries, each with their own name, displayed in a JComboBox. Users must be able to supply their own queries and add them to the JCOMBO Box.
Now, all I have to do is make the following predefined queries: Select all authors from the authors table, select all publishers from the publishers table, select a specific author and list all books for that author. Include the title, year, and ISBN. Order information alphabetically by the author's last name and first name. Select a publisher and list all books published by tha tpublisher. Include the title, year, and ISBN. Order the information alphabetically by title.
This last one is optional for the assignment, but I'd like to be able to do it. Provide any other queries which may be appropriate.
On this, I'll need all the help I can get. |
| |
11-22-2004, 12:21 AM
|
#3 (permalink)
| | Registered User
Join Date: Nov 2004
Posts: 14
| Last program, which I have virtually nothing for. It's not that hard, but I just need to sort out what to do for it. It gets a word from a user and checks if it is a palindrone (a word which reads the same backwards and forwards, such as Anna). I know I have to set up some kind of while loop and push the letters into a stack and pop them to check if they're equal, but I need some help with my definitions, objects, etc. Not that complicated. Also, the program should ignore case, spaces, and punctuation. For example, if the term "An Na" is put in, it should compare correctly as a palindrone and the program will say "This term is a palindrone". Edit: Here's the code I now have so far on this. I am unsure of how to push and pop the characters as opposed to the whole string into the stack, and then initialize a new string to compare it to the first one. Code: public class Lab6 extends List
{
private String string;
//create stack
Stack stack=new Stack();
//ask user for a string
string=JOptionPane.ShowInputDialog("Enter a string: ");
//add object to stack
public synchronized void push(string)
{
insertAtFront(string);
}
//remove object from stack
public synchronized String pop() throws EmptyListException
{
return removeFromFront();
}
//compare characters in string
do
{
}
while
{
}
public static void main(String args[])
Last edited by Crow T. Robot; 11-22-2004 at 01:40 PM.
|
| |
11-22-2004, 05:30 AM
|
#4 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,174
| Good grief, that's at least two, and maybe three different classes worth of stuff. Networking, database, algorithms . . .
Datagrams are not a reliable network protocol; what do you have planned for in case you drop a packet?
Your queries question, apart from the fact you'll be using JDBC, is really an SQL question. Normally I'd suggest you put it in the SQL forum, but it's really a trivial query. I think all you really need to do is look up how to use SELECT and ORDER for your database; the query itself should be quite trivial. Give it a shot on your own, and if you still have trouble post a question in the SQL forums.
As for the palindrome, work some of it out and let us know if you get stuck. It sounds like you have a decent idea to start out with, although I'm not certain you even need a stack.
BTW, I'm not very good with GUI stuff, so I won't be able to help you out with that stuff. However, if you need help with pretty much anything else I should be able to give you some help. |
| |
11-22-2004, 05:33 AM
|
#5 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,174
| Oh yeah, before I forget, you should use the code tags to post any more code. It keeps the spacing intact, and makes it much easier to read. I added them for you around your previous code snippets. |
| |
11-22-2004, 09:42 AM
|
#6 (permalink)
| | Registered User
Join Date: Nov 2004
Posts: 14
| Quote: |
Originally Posted by Belisarius Oh yeah, before I forget, you should use the code tags to post any more code. It keeps the spacing intact, and makes it much easier to read. I added them for you around your previous code snippets. | Thanks for the other info. And I've never posted here before. I take it it's html, so what are the tags I need to put around my code? |
| |
11-22-2004, 10:20 AM
|
#7 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,530
| the CODE tag will allow for indentations. if you use PHP or HTML, then there will be syntax highlighting for those languages. Here is an example of how to use the tags. ( just take out the space in the tag for it to work )
[CODE ]
place code here
[/CODE ]
[PHP ]
place code here
[/PHP ]
[HTML ]
place code here
[/HTML ]
__________________ Mike |
| |
11-22-2004, 01:45 PM
|
#8 (permalink)
| | Registered User
Join Date: Nov 2004
Posts: 14
| For the palindrone program, I may not necessarily need a stack, but for my assignment must use one. Here's now what I have (though I edited the earlier post I made with the code I now have). Check above for the main things I need help on with this one. Code: public class Lab6 extends List
{
private String string;
//create stack
Stack stack=new Stack();
//ask user for a string
string=JOptionPane.ShowInputDialog("Enter a string: ");
//add object to stack
public synchronized void push(string)
{
insertAtFront(string);
}
//remove object from stack
public synchronized String pop() throws EmptyListException
{
return removeFromFront();
string=new string1
}
//compare characters in string for a palindrone
public static void main(String args[])
if (
{
string=string1
}
then
{
JOptionPane.ShowDialog("This is a palindrone.");
}
else
{
JOptionPane.ShowDialog("This is not a palindrone.");
}
}
Last edited by Crow T. Robot; 11-22-2004 at 02:46 PM.
|
| |
11-22-2004, 03:06 PM
|
#9 (permalink)
| | Registered User
Join Date: Nov 2004
Posts: 14
| Quote: |
Originally Posted by Belisarius Good grief, that's at least two, and maybe three different classes worth of stuff. Networking, database, algorithms . . .
Datagrams are not a reliable network protocol; what do you have planned for in case you drop a packet?
Your queries question, apart from the fact you'll be using JDBC, is really an SQL question. Normally I'd suggest you put it in the SQL forum, but it's really a trivial query. I think all you really need to do is look up how to use SELECT and ORDER for your database; the query itself should be quite trivial. Give it a shot on your own, and if you still have trouble post a question in the SQL forums.
As for the palindrome, work some of it out and let us know if you get stuck. It sounds like you have a decent idea to start out with, although I'm not certain you even need a stack.
BTW, I'm not very good with GUI stuff, so I won't be able to help you out with that stuff. However, if you need help with pretty much anything else I should be able to give you some help. | The GUI shouldn't be too hard. But I have little clue how to start accessing the books database and get started. The SELECT and ORDER statements are explained pretty well with the textbook I have. |
| |
11-22-2004, 03:30 PM
|
#10 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,174
| JDBC is pretty easy, but I haven't done a plain old connection in a while (been using database connection pools for some time).
You need to download the JDBC driver for whatever database it is you're using, and include it in you classpath.
Sun has a JDBC tutorial online here. Skip the bits about setting up a database and go right to chapter 3, "Establishing a Connection". Let me know if you need help with anything. |
| |
11-22-2004, 03:33 PM
|
#11 (permalink)
| | Registered User
Join Date: Nov 2004
Posts: 14
| Quote: |
Originally Posted by Belisarius JDBC is pretty easy, but I haven't done a plain old connection in a while (been using database connection pools for some time).
You need to download the JDBC driver for whatever database it is you're using, and include it in you classpath.
Sun has a JDBC tutorial online here. Skip the bits about setting up a database and go right to chapter 3, "Establishing a Connection". Let me know if you need help with anything. | Ugh. Turns out I can just do some of the SELECT and whatever other operations I have to do in a word document, and not as an actual program.
Here's the recently revised palindrone program I have. Code: import java.util.Stack;
import java.awt.*;
import javax.swing.*;
public class Lab6
{
//ask user for a string
public Lab6()
//declare a string for user input
private String string;
//create stack
Stack stack=new Stack();
{
string=JOptionPane.ShowInputDialog("Enter a string: ");
}
//add object to stack
stack.push(string);
//remove object from stack
stack.pop(string);
string=new String string1
//compare characters in string for a palindrone
public static void main(String args[])
if (string=string1)
{
JOptionPane.ShowMessageDialog("This is a palindrone.");
}
else
{
JOptionPane.ShowMessageDialog("This is not a palindrone.");
}
} |
| |
11-22-2004, 03:38 PM
|
#12 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,174
| As for your palindrome stuff, there are a couple points. Code: public class Lab6 extends List Is there a particular reason you're extending list? Code: public synchronized void push(string)
{
insertAtFront(string);
}
//remove object from stack
public synchronized String pop() throws EmptyListException
{
return removeFromFront();
string=new string1
} There's no apparent reason for sychronization here. I assume you're not going to be finding palindromes in parallel.
Further, insertAtFront and removeFromFront aren't defined. You seem to want to treat Lab6 as a stack itself, but at the same time you've declared a stack. Code: if (
{
string=string1
}
then
{
JOptionPane.ShowDialog("This is a palindrone.");
}
else
{
JOptionPane.ShowDialog("This is not a palindrone.");
} You're more comfortable in a different language, aren't you? :p
That's not how you setup a if-then-else in Java.
The format is Code: if(<boolean expression>){
}else if(<boolean expression>){
}else{
} Does this have to be graphical, or can it be command line? Command line is easier to program for. |
| |
11-22-2004, 05:18 PM
|
#13 (permalink)
| | Registered User
Join Date: Nov 2004
Posts: 14
| See the post you quoted again. I changed a lot of stuff. And it is only command line, though I think the JOptionPane will make some windows for text. Maybe not.
As for my SQL coding, I just need to know one thing. How do I choose a specific author with SELECT?
Edit: Here's the code I have for that so far. Code: //select all authors from authors table
{
SELECT*FROM authors
}
//select all publishers from publishers table
{
SELECT*FROM publishers
}
select author, list all books for author including title, year, and ISBN
{
SELECT copyright, title, ISBN, firstName, lastName
FROM titles
INNER JOIN authors
ON title.lastName=authors.lastName
WHERE lastName LIKE ’Nieto’
ORDER BY lastName
}
//select a specific publisher, list all books published by them, include title, year, and ISBN
{
SELECT publisher, title, ISBN
FROM titles
INNER JOIN publishers
ON publishers.publisher=titles.publisher
WHERE publisher LIKE ‘Random House’
ORDER BY title
}
Last edited by Crow T. Robot; 11-22-2004 at 05:45 PM.
|
| |
11-22-2004, 07:37 PM
|
#14 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,174
| Taking the SQL first, you seem to have a decent handle on it. I haven't had the need to explicitly use JOINs, but that looks about right from my understanding (this is why you post SQL stuff in the SQL forum; get more eyes looking at it that know what they're doing). Not sure what the brackets are for though. As for selecting a specific author, the LIKE keyword should do, or you can explicitly say:
lastName=’Nieto’
As for the palindrome stuff, let's see. You need to make it at least: to get past parsing. Code: {
string=JOptionPane.ShowInputDialog("Enter a string: ");
} You must have come from C programming. In Java, you don't randomly insert brackets, like you do in C. This would, I believe, fail parsing. Further, the "s" in "ShowInputDialog" needs to be lower case. In Java naming conventions, the first letter of a method is almost always lower-case. Code:
{
string=JOptionPane.ShowInputDialog("Enter a string: ");
}
//add object to stack
stack.push(string);
//remove object from stack
stack.pop(string);
string=new String string1 None of this is contained in a method or constructor. You'll have all sorts of syntax errors if you try to compile this. Code: string=new String string1 This doesn't make sense. If you want to set "string" equal to a new String, then it should be Code: string = new String(); // or
string = ""; If you want to declare "string1", you should say Code: String string1 = new String(); Code: public static void main(String args[]) This method needs brackets. All methods, unless they're being defined in an abstract class or in an interface, probably should have brackets enclosing code. Code: if (string=string1) This is a no-no in Java. First off, comparison is done with ==, not =. What you have actually done here is set string equal to string1, the result of which will always be true.
Further, you don't want to use "==" most of the time, because what you're doing is comparing the memory locations of the objects, not their values. I can create two different instances of "foo", and a "==" will return false. What you want is to use the ".equals()" method. That will return whether or not the two strings are equal in the traditional sense (whether or not they have the same characters).
Also, I assume you're going to refine your methodology because what you're doing won't actually tell you if you have a palindrome or not. In fact, apart from the reasons I mentioned above, what you're trying to do will always return true; because you are attempting to compare a String to a copy of itself.
Finally, and while I had never tried it before, your method of getting input works just fine. It's almost as simple as getting it from the command line. |
| |
11-22-2004, 08:42 PM
|
#15 (permalink)
| | Registered User
Join Date: Nov 2004
Posts: 14
| Thanks a lot. My compiler on my home PC is screwed up for some reason, but this helps. At least I had a pretty good idea of the mistakes you mentioned when I made the program.
And actually, I'm not familiar with C. Never studied it. I just messily wrote down code 30 minutes before class to get it done already. But thanks for all the help. |
| | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -8. The time now is 02:04 PM. |
Copyright © 2000-2008, Milano Interactive Web Hosting provided by Portal 360 Web Hosting |  | |