Java programming using RMI please help!!
Hi guys, ive been given a task to:
design a distributed multiple-choice exam application with client-server
communication. only considering one round of communication: A client
requests the text of a question from the server, shows the text to a user, and asks the user to
choose a number between 1 and 5 (the user answer); then the client sends the user answer
to the server. The client can also request the correct answer.
I have so far designed a
- remote object interface called remoteMCQ.java
- Class remoteMCQImp.java (This class has to implement the remoteMCQ interface.)
- Server side (The server has to initiate and register the remote object.)
- Client side (The client should show the question text, ask a user for an answer, submit the answer to the server, and, finally, display the correct answer.)
so far i have the followng code for each part but i cant get it to work any help would be much appreciated!!!
//remoteMCQ.java//
import java.rmi.*;
public interface remoteMCQ extends java.rmi.Remote
{
public String getQuestion() throws java.rmi.RemoteException;
public void selectAnswer(int answer) throws java.rmi.RemoteException;
public int getCorrectAnswer() throws java.rmi.RemoteException;
}
//remoteMCQImp.java//
import java.util.Date;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class remoteMCQImp extends UnicastRemoteObject implements remoteMCQ
{
public remoteMCQImp() throws RemoteException { };
public getQuestion() throws RemoteException {
//The method getCorrectAnswer returns the answer to the question//
System.out.println("How are you?");
int CorrectAnswer = new selectAnswer().getCorrectAnswer();
System.out.println("Please input an answer");
return(CorrectAnswer);
};
}
//QuestionServer.java
import java.rmi.Naming;
public class QuestionServer {
public static void main(String[] args) {
try {
remoteMCQImp remote = new remoteMCQImp();
Naming.rebind ("Dater", remote);
System.out.println("Object bound to name");
}
catch(Exception e) {
System.out.println("Error occurred at server "+e.getMessage());
}
}
}
//QuestionClient.java//
import java.rmi.*;
public class QuestionClient {
public static void main(String[] args) {
try {
remoteMCQ sgen = (remoteMCQ) Naming.lookup("rmi://localhost/Dater");
System.out.println("Question is "+sgen.getQuestion());
}
catch(Exception e) {
System.out.println("Problem encountered accessing remote object "+e);
}
}
}
any help thanks guys
So what's the error message? And why don't you print exceptions' stack traces? And why didn't you use [ code ] tags?
give us the error!!most people know what is wrong with code by reading the error.
H:\COMP212>javac QuestionServer.java
.\remoteMCQImp.java:10: invalid method declaration; return type required
public getQuestion() throws RemoteException {
^
.\remoteMCQImp.java:7: remoteMCQImp is not abstract and does not override abstra
ct method getCorrectAnswer() in remoteMCQ
public class remoteMCQImp extends UnicastRemoteObject implements remoteMCQ
^
.\remoteMCQImp.java:13: cannot find symbol
symbol : class selectAnswer
location: class remoteMCQImp
int CorrectAnswer = new selectAnswer().getCorrectAnswer();
^
.\remoteMCQImp.java:15: cannot return a value from method whose result type is v
oid
return(CorrectAnswer);
^
4 errors
H:\COMP212>javac QuestionServer.java
.\remoteMCQImp.java:10: invalid method declaration; return type required
public getQuestion() throws RemoteException {
^
.\remoteMCQImp.java:7: remoteMCQImp is not abstract and does not override abstra
ct method getCorrectAnswer() in remoteMCQ
public class remoteMCQImp extends UnicastRemoteObject implements remoteMCQ
^
.\remoteMCQImp.java:13: cannot find symbol
symbol : class selectAnswer
location: class remoteMCQImp
int CorrectAnswer = new selectAnswer().getCorrectAnswer();
^
.\remoteMCQImp.java:15: cannot return a value from method whose result type is v
oid
return(CorrectAnswer);
^
4 errors
i just need it to display the question and answer. Im really new to this!!
I think that an RMI project is a little bit ambitous for your current level of understanding.May I suggest you complete the tutorial available on this site before attempting this.
See the comments:
public class remoteMCQImp extends UnicastRemoteObject implements remoteMCQ
{
public remoteMCQImp() throws RemoteException {// YOU HAVE TO DECLARE A RETURN TYPE FOR THE METHOD, USE VOID FOR NO RETURN TYPE
};// DON'T PUT ; AFTER }
public getQuestion() throws RemoteException {// NEED RETURN TYPE HERE TOO
//The method getCorrectAnswer returns the answer to the question//
System.out.println("How are you?");
int CorrectAnswer = new selectAnswer().getCorrectAnswer();
System.out.println("Please input an answer");
return(CorrectAnswer);// DON'T PUT () AROUND THE VARIABLE
};// AGAIN, NO SEMICOLON AFTER A BLOCK
}
What's wrong with putting ( ) around a return variable?
> What's wrong with putting ( ) around a return variable?
There is no such thing as a "return variable". "return" is a statement with
well defined semantics. Return can either be followed by nothing or by
an expression of type T where T is the type of the method itself.
An expression doesn't need parentheses per se. If that were the case,
and applying that 'rule' consistently one has to enter an infinite amount
of parentheses because an expression embraced by parentheses is
an expression by itself. This would go turtles all the way down.
For the "return" statement variant that *doesnt* take an expression
because the method happens to be a void method, the following is
simply wrong:return ();
Using parentheses always breaks down for the example above.
There's nothing wrong with, say:return true;
instead of:return (true);
kind regards,
Jos
> What's wrong with putting ( ) around a return> variable?Nothing. JosAH already explained it, but I meant that it wasn't necessary, and likely to cause confusion for a new programmer, not that it was wrong.
All right then - I just wanted to make sure I hadn't been committing a flagrant code layout crime for the last four years!
public class remoteMCQImp extends UnicastRemoteObject implements remoteMCQ
{
public remoteMCQImp() throws RemoteException {
// YOU HAVE TO DECLARE A RETURN TYPE FOR THE METHOD
// but constructors do not have return types
> > public class remoteMCQImp extends UnicastRemoteObject
> implements remoteMCQ
> {
> public remoteMCQImp() throws RemoteException {
>
> HAVE TO DECLARE A RETURN TYPE FOR THE METHOD
> / but constructors do not have return types
>
>
DOH again! That's it, I'm going to lunch!
Thanks very much for all the help i'm very grateful.
Im stil not getting the question to appear tho, maybe im doing it the hard way i just want it to go:
How are you?
1.) Devastated
1.) Not good
2.) so so
3.) Good
4.) Over the moon!
PLease input an answer 1 - 5
that is the correct/incorrect answer
I jUst dont think im cut out for this Java Stuff!!
Thanks guys
> I jUst dont think im cut out for this Java Stuff!!> Thanks guysRepost the code you have now, and explain what the problem is. That's how programming works, you get small pieces working step by step, then build on them for the next step.