Amaad Isam

Greenhorn
+ Follow
since Oct 30, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Amaad Isam

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


15 years ago
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();

}
}
15 years ago
i was wondering if any one can help me in writing this program. your help would be appreciated. The instructions for the program are below


Overview
Create a simple Car class that represents a car with certain characteristics such as name, gas tank
capacity, distance traveled, and fuel efficiency (miles per gallon). Your class will include methods to
add gas to the car and to drive it a certain distance (mutators) and a few simple accessor methods to
return field values.
What You Need to Know
This assignment uses information in Chapters 1-3 from the Horstmann textbook as well as a little
information from Chapter 6 (section 6.1) that I will preview in lecture. To complete this assignment
successfully, you will need to understand how to design, implement, document, and test a simple
Java class. You will also need to understand how to use fields, local variables and parameters of
type double and now to do simple numeric operations like addition, subtraction, multiplication and
division on floating-point (double) values. You will need to know how to write and call methods that
accept parameters, alter field values, and return results.
You will also be asked to use simple if and if-else statements in Java to include some simple error
checking logic in your methods. Finally, you will need to design a tester class to verify that you have
implemented each method properly.
Your resulting Car and CarTester classes will be similar to the CashRegister and CashRegisterTester
classes described on pages 85-87.
Implementing Your Car Class
Your Car class will include methods to do the following (use the method names below):
� addGas: add a specified amount of gas (given in gallons)
� drive: drive a specified distance (given in miles)
� getName: return the name of the current car (as a String)
� getEfficiency: return the fuel efficiency of the current car (in miles per gallon)
� getCapacity: return the gas tank capacity of the current car (in gallons)
� getDistanceTraveled: return the distance traveled (in miles)
� getGas: return the amount of gas currently available in the tank (in gallons)
� getSpace: return the amount of gas that can currently be added to the tank (in gallons)
You will also need a constructor that accepts as parameters a name, the fuel efficiency, and the
tank capacity.
You will need at least five fields in your Car class, all of type double, except for the car name which
is a String:
2 / 3
� car name
� fuel efficiency
� tank capacity
� distance traveled
� available gas
You should select appropriate names and declare these fields properly at the bottom of the class.
Make sure they are all properly initialized by the constructor. Note that the first three fields should
never change after they are initialized while the other two fields may be altered by the addGas() and
drive() methods.
Error Checking
There are several error conditions that you need to detect and handle. In later programs you will
generate error messages (exceptions). For this assignment, you need to simply detect invalid
parameters and do something �reasonable� despite the erroneous method parameters.
In your addGas() method, if 0 or a negative amount is specified, simply return 0 and do nothing else.
If the amount specified is greater than the space currently available in the tank, add as much as you
can and return that amount. In all other cases, add (and return) the requested amount.
In the drive() method, if the specified distance is 0 or negative, simply return 0 and do nothing else.
Otherwise, determine how much gas is required to drive the requested distance. If the amount
required is less than or equal to the current amount of gas in the tank, then �drive� that distance (up
date the distance traveled and available gas fields) and return the distance driven. If the requested
distance requires more gas than is available, drive as far as you can (returning that distance).
Here is an example of what is called �pseudo-code� (half English, half Java) for the drive() method
with error checking:
if (distance <= 0)
return 0.0;
compute gas needed
if (gas needed <= gas available)
drive requested distance
return requested distance
else
drive as far as possible
return actual distance (available gas * efficiency)
Style and Documentation
You must include a javadoc header comment for the class as a whole and for each method. Your
method headers should document each parameter and return values. Follow the stylistic guidelines
used in the book and outlined in Appendix A. That means use appropriate variables names, indent
properly, space around operators, make curlies line up, and use blank lines as appropriate. We will
only take off a few points for incorrect style on this assignment but subsequent assignments will
require proper style.
3 / 3
Testing Your Car Class
Use incremental development. Add the basic field definitions and the outlines of the methods (without
bodies) to your class first. Make sure it compiles before proceeding. Then implement the constructor.
You can now write a very simple main() in CarTester.java that just instantiates a few Car objects.
Then implement the simple accessor methods. Make sure everything compiles. Add some more tests
to main(). Next implement simple versions of the addGas() and drive() methods that don�t worry about
error checking. Try a variety of tests before proceedings. Once you have a class that works for the
�normal cases�, then add some additional error checking logic to addGas(). Add more tests to main()
to check for all the different possible error conditions you are trying to handle. Once you have that
much working add the error checking logic to drive().
15 years ago