buglouie
asked on
test programme not working - compilation error?
Cannot understanding why my test file does not compile?
/*This programme tests the DataLog class*/
public class DataLogTestProg
{
public static void main(String [] args)
{
DataLog d1, d2;//declare two DataLog objects
d1 = new DataLog();//create the first DataLog instance
d2 = new DataLog();//create the second DataLog instance
System.out.println("d1:" + d1.toString());
System.out.println("d2:" + d2.toString());
d1.setData(4);//send value 4 to ojbect 1
d2.setData(6);//send value 6 to ojbect 2
}
}
public class DataLog
{
private int x;//the current data value
private int lastx;//the previous data value
//Constructors
/*Default constructor, sets data to zero*/
public DataLog()
{
x = 0;
lastx = 0;
}
/*Sets data to initialValue. This is also the previous value*/
public DataLog(int initialValue)
{
x = initialValue;
lastx = initialValue;
}
//Methods
/*Sets data to dataValue*/
public void setData(int dataValue)
{
lastx = x;
x = dataValue;
}
/*Returns the current data value*/
public int getData()
{
return x;
}
/*Restores the previous value of the data value*/
public void undo()
{
x = lastx;
}
/*Returns a text representation of the current and last value of the data value*/
public String toString()
{
return "new value" + x + "last value" + lastx;
}
}
/*This programme tests the DataLog class*/
public class DataLogTestProg
{
public static void main(String [] args)
{
DataLog d1, d2;//declare two DataLog objects
d1 = new DataLog();//create the first DataLog instance
d2 = new DataLog();//create the second DataLog instance
System.out.println("d1:" + d1.toString());
System.out.println("d2:" + d2.toString());
d1.setData(4);//send value 4 to ojbect 1
d2.setData(6);//send value 6 to ojbect 2
}
}
public class DataLog
{
private int x;//the current data value
private int lastx;//the previous data value
//Constructors
/*Default constructor, sets data to zero*/
public DataLog()
{
x = 0;
lastx = 0;
}
/*Sets data to initialValue. This is also the previous value*/
public DataLog(int initialValue)
{
x = initialValue;
lastx = initialValue;
}
//Methods
/*Sets data to dataValue*/
public void setData(int dataValue)
{
lastx = x;
x = dataValue;
}
/*Returns the current data value*/
public int getData()
{
return x;
}
/*Restores the previous value of the data value*/
public void undo()
{
x = lastx;
}
/*Returns a text representation of the current and last value of the data value*/
public String toString()
{
return "new value" + x + "last value" + lastx;
}
}
Nothing wrong with code posted - what problem are you getting?
What error message are you getting?
P.S. I assume those two classes are in separate files with the correct names?
P.S. I assume those two classes are in separate files with the correct names?
ASKER
Error message reads:-
DataLogTestProg.java:8:can not resolve symbol
symbol:class DataLog
location:class DataLogTestProg
DataLog d1, d2;//
^
etc
DataLogTestProg.java:8:can
symbol:class DataLog
location:class DataLogTestProg
DataLog d1, d2;//
^
etc
Make sure DataLog class is in the other's classpath
If they're in one file, remove second 'public' and compile the one with main method. The file should be DataLogTestProg.java
Or, simply put the two files in the same directory (if possible). It should run fine then. You may also have to compile DataLog first as some compilers will not look for and compile necessary class files for you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
8-)