Question:
Need help with this small java program?
?
2012-09-06 12:03:17 UTC
I have updated the code, thing is im trying to write a program (assignment) that does the following but i have hit abit of a dead end, below are the instructions and after them is the code i have completed so far, Im confused on how to use the 'Lists',

//Instructions..
What are the requirements on the classes?
1. Each desktop will have its own message store.
2. The dekstopMessageHolder will consist of recent and history messages and the
desktopId.
3. As soon as the recent message list has more than 10 entries the oldest message is
removed and transferred to the history message list.
4. The history messages list can only hold 1000 messages at which point the oldest
message is discarded.

How its supposed to work:
1. The DekstopMessageHandler class is instantiated.
2. A message is send to the DesktopMessageHandler to store using its
storeMessage(String message,int desktopId) method.
3. The MessageHandler should check its desktopMessageData list to see if it has a
object with the correct desktop ID. If so store the message in recent and handle
the overflows. Else create a new store object in the list and store the message in
recent.
4. Messages are retrieved as a complete list of messages via the List
getRecent/HistoryMessage(int dekstopId) method.

//How far I got..
//========================MessageHandl… class
import java.util.list;

class MessageHandler{

//Variables
String message;
int desktopId;

//Constructor
public storeMessage(String message, int desktopId){

this.message = message;
this.desktopId = desktopId;
}

public String getRecentMessages(int desktopId){
return recentMessages[desktopId]; //??? Not sure if this is right
}
}

//========================DesktopMessa…
import java.uitl.List;
import java.util.Scanner;
import java.uitl.Random;

class DesktopMessageHolder{

public static void main(String[]args){

List recentMessages = new LinkedList();
recentMessages = new ArrayList();

List historyMessages = new LinkedList();
historyMessages = new ArrayList();

int desktopId = 1;

MessageHandler myMessageHandler = new MessageHandler();

for(int x = 0; x++; x < 1000){
desktopId = random(10);
recentMessages[desktopId]; //??? Not sure if this is right either
myMessageHandler.storeMessage("Msg No: " + x.toString(), desktopId);
}

System.out.print(myMessageHandler.getRecentMessages(desktopId).toString());


myMessageHandler = new MessageHandler();
desktopId = 1;
for(int i = 0; i++; i < 1001){
myMessageHandler.storeMessage("Message number:" + i.toString(), desktopId);
}
}
}

Please help me.. :I
Three answers:
Jordan
2012-09-06 12:39:14 UTC
You have the classes kinda flipped. You instantiating DesktopMessageHolder from MessageHandler, you currently do it the other way around. All DesktopMessageHolder is doing is storing messages in two different lists, along with the desktopId.

The constructor in MessageHandler has to be named MessageHandler() or else it isn't a constructor. And storeMessage() as well as getRecentMessages() and getHistoryMessages() for that matter should be in DesktopMessageHolder instead of MessageHandler.

MessageHandler should have an ArrayList that consists of the desktopIds which represent the instances of DesktopMessageHolder that it has created. It should check this list for a match for the desktopId that was given to it by either the command line or arguments.

Also the main method should probably be in a class totally separate from these classes, but if not it should be in MessageHandler, not DesktopMessageHandler.



When you declare your lists you want to say List recentMessages = new ArrayList();

You don't need anything about a linked list.

I know this is a lot to take in, try to make some changes and if you still have questions feel free to PM me and I can give you my email.
?
2016-10-23 03:00:51 UTC
What on God's eco-friendly earth is this? Your code has such various syntax mistakes, that is like various of them merged jointly into one large syntax mistakes that is threatening to spill out of my pc demonstrate screen and onto my keyboard. pass lower back and look over the syntax of an if statement again. stick with it very heavily. What you've immediately must be no longer following it.
Langelihle
2015-08-13 23:18:54 UTC
Did you mange to solve the problem. I'm working on a similar problem...I'm stuck, needed guidance


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...