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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.