0

I was wondering if someone could help me with this.

I have defined my interface as:

interface Model 
{
public String  toString();
public Model add (Model m);
}

There are 2 classes implementing the interface (ClassA and ClassB):

class ClassA implements Model
{
private int val;

public ClassA(int x) 
{
    val = x;
}

public String toString() 
{
 return ""+ "value of object of class A is " + val;
}

public Model add (Model m) 
{
 if (m instanceof ClassA)
   return new ClassA(val + ( (ClassA) m).val);
 else
   return null;
}
}

class ClassB implements Model 
{

private String str;

public  ClassB(String s)
{
 str = s;
}

public String toString()
{
 return str;
 }

 public Model add (Model m) 
 {
 if (m instanceof ClassB)                      
 return new ClassB(str + ((ClassB) m).str); 
 else
 return null;
 }
 }

My main defines objects of ClassA and ClassB and calls their tostring() methods.

public class Example {
public static void main (String args[]) {
 ClassA a = new ClassA(5);
 ClassB b= new ClassB("Hi");

 Model m = b;
 System.out.println(m.toString());

 ClassA a1 = new ClassA(7);

 m = a.add(a1);

 System.out.println(m);
}
}

When I try to build this file it compiles fine but, upon trying to run the application I get an error message:

"Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file).....etc...etc"

Can anyone help me with this? It's probably something simple. I'm a beginner Java student.

9
  • 5
    What may be most important is what you're not showing us. How are you trying to run this program? Commented Apr 7, 2012 at 2:56
  • What command are you typing when you try to run your program? What command did you use to compile it? Commented Apr 7, 2012 at 2:56
  • 1
    Can you write the command you launch when you try run the application? Commented Apr 7, 2012 at 2:57
  • I am using JCreator and clicking on the "run file" option after building file. Commented Apr 7, 2012 at 2:58
  • Trying compiling and running it in terminal or command prompt with javac Example.java to compile and java Example to run it. Then tell us what happens. (Make sure you are in the right directory) Commented Apr 7, 2012 at 2:59

1 Answer 1

1

There is no error in your program.Your program is absolutely ok. But , I do not know which command you are writing for execution .Try once again with absolute path setting to JDK and JRE.

Command like:-

for Compile -javac Example.java for Run -java Example

It will successfully run. Hope it will help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.