• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Desperately need help writing Tester

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, i have written out an entire program and have compiled it. so i know that the entire program runs without any mistakes. however i am having some difficulty making the tester. any help at all will be appreciated.

the program that i have written is below. under that i will put what is my stab at a tester. however, i think that my tester is completely wrong. if any one can show me how to do the tester for this program that would be great. i have done testers before but never for programs with if else statements.


the program i have written and know works:

// Creates a car that has variables that can be changed such as gastank capacity, distance traveled, and fuel effficiency
public class Car
{
public static final double GALLON_VALUE = 1;
public static final double MILE_VALUE = 1;



private String name;
private double milesPerGallon;// fuel efficiency of the car
private double capacity;
private double distance;// distance already travelled in miles
private double gasAvailable;
private double emptySpace;
/**
Constructs a car with default charachteristics.
*/
public Car()
{
name = "defaultCar";
capacity = 20.0;
distance = 0.0;
milesPerGallon = 40.0;
}

/**
Constructs a car with the following: name, Fuel Efficiency, tank Capacity, Distance Traveled and available gas.
*/
void Car(String customName,double customfueleff, double tankCapacity ) {
name =customName;
milesPerGallon = customfueleff;
capacity = tankCapacity;
distance = 0.0;
emptySpace = capacity;
gasAvailable = 0.0; //should depend on car
}

/**
Adds a specified amount of gas n gallons relative to the tank Capacity
*/

public boolean addGas(double gallons){

emptySpace = capacity - gasAvailable;

if (emptySpace <=0 )
return false;
else if(gallons > emptySpace ) {
gasAvailable =gasAvailable +emptySpace;
return true;
}
else{
gasAvailable= gasAvailable +gallons;
return true;
}
}

/**
drives a specified distance proportinate to amount of gas available
*/

public boolean drive(double requestedDistance) {

double possibleDistance = (gasAvailable*milesPerGallon);
if (requestedDistance < 0) {
return false; }
if (requestedDistance <= possibleDistance) {
distance += requestedDistance;
gasAvailable-=(requestedDistance/milesPerGallon);
return true;
}
else { return false; }
}



/**
Returns the name of the current car as a String
*/
public String getName()
{
return ("The name of the car is "+ name);
}
/**
Returns the fuel efficiency of the current car in miles per gallon
*/
public double getEfficiency()
{
return milesPerGallon;
}

/**
returns the gas tank capacity of the current car in gallons
*/
public double getCapacity()
{
return capacity;
}

/**
gets the distance traveled in miles
*/
public double getdistance()
{
return distance;
}
/**
gets the amount of gas that currently available in the tank in gallons
*/

public double getGasAvailable()
{
return gasAvailable;
}
/**
returns the amount of gas that can currently be added to the taknk in gallons
*/
public double getSpace()
{
emptySpace = capacity - gasAvailable;
return emptySpace;
}


}
/**
takes out gas from the gas tank
*/
/*
public boolean withdraw(double amount)
{

emptySpace = capacity - gasAvailable;

if (emptySpace <=0 )
return false;
else if(gallons > emptySpace ) {
gasAvailable =gasAvailable +emptySpace;
return true;
}
else{
gasAvailable= gasAvailable +gallons;
true;


}
}
}

/**
Transfers the specified gas from the strangersCar to MasoodsCar
*/
/*
public double Transfer(double amount);
{
double transfer Amount = gasTransferAmount;
strangersCar.withdraw(gasTransferAmount);
masoodsCar.deposit(gasTransferAmount);
}
*/






below is the tester that i have tried to do but know is completely incorrect


public class CarTester
{
public static void main(String[]args)
{
// this will test the Car object

Car gallons = new gallons(10);

gallons = 0;
if gallons (<= 10.0);
return
System.out.println("add more gas");
gallons = 10;
else if gallons >=(0.0);
return
System.out.println("Do not add more gas");
}

public static void main(String[]args);
{
miles = 0;
if (miles <= 10.0);
return
System.out.println("The car has travelled");
miles = 10;
else if (miles >= 0.0);
return
System.out.println("the car has not traveled at all");
}

public static void main(String[]args);
{
// int...

system.out.println();

}
}
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Use Code Tags, and also Ease Up.
 
Amaad Isam
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about that. here is the code using the code tags.

the program i have written and know works:







below is the tester that i have tried to do but know is completely incorrect


 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

Have you had a look at JUnit, you'll find it gives you a test framework as well as tips and tricks on how to write effective tests.

Good luck!
 
reply
    Bookmark Topic Watch Topic
  • New Topic