I am new to Java programming, and am taking a course through EdX that uses the BlueJ IDE. I'm trying to duplicate everything I do in Eclipse, since I understand it is a commonly-used IDE for professional programmers. I have a question about some functionality I use in BlueJ that I haven't figured out yet in Eclipse.
Is there a way to create test instances of objects in Eclipse like you can in BlueJ?
Screenshot 1 shows the two constructors available for this class. I chose the highlighted one.
Screenshot 2 is the dialogue for naming the instance.
Screenshot 3 is the dialogue menu with the various methods available for the object.
So is there a way to do this in Eclipse? I have read a bit about JUnit, and intend to learn to use it, but was wondering if there was a similar function to the simple BlueJ testing in Eclipse.
Here is the code for the Car class:
import comp102x.IO; //External library available in many of the later demo programs from https://courses.edx.org/courses/course-v1:HKUSTx+COMP102.1x+2T2015/f792f3a2057040aa959e606d687e9bc4/
/**
* A class of Car objects that can move forward, backward and turn
*/
public class Car
{
private int odometer = 0; // An odometer reading initialized to 0
private String owner = "NoName"; // Name of owner
/**
* Default constructor for a Car object
*/
public Car () {}
/**
* Constructor for a Car object with a new owner’s name
* @param name name of owner
*/
public Car(String name) { // Constructor takes a name as argument
owner = name;
}
/**
* moveCar moves a car forward or backward by dist units
* @param dist Moving distance
*/
public void moveCar(int dist) {
odometer = odometer + Math.abs(dist);
IO.outputln(owner + "'s car has moved " + dist + " units.");
}
/**
* turnCar turns a car by a given degree
* @param angle Turn angle in degrees
*/
public void turnCar(double angle) {
IO.outputln(owner + "'s car has turned " + angle + " degrees.");
}
/**
* getOdometer gets the odometer reading of a car
* @return The value of odometer
*/
public int getOdometer() {
return odometer;
}
}
From my experience in working with BlueJ, I can tell you that it's very simplified for beginners. There's a lot of conveniences allowed to the end user to do stuff like quick instantiation and quick testing.
However, you're not afforded such conveniences (graphically) in an IDE like Eclipse, NetBeans, or IntelliJ. These IDEs make it easier to develop enterprise level code, and as such, the graphical interface would serve to get in the way more than help.
Depending on your context of "test", you have several options.
If you just want to instantiate an instance and play around with it, create a new class with a main
method. From there, instantiate an instance of Car
by hand:
public static void main(String[] args) {
Car car = new Car();
Car altCar = new Car("The Ride");
// code that calls methods on each instance to follow
}
If you want to actually do unit tests, then JUnit is a good choice. You'll have to put JUnit somewhere on your classpath (documentation for Eclipse exists).
public class CarTest {
@Test
public void testCar() {
Car car = new Car();
// put state into the car
// assert expectations
}
}