I have a problem with a Linear Equation java file that I can't seem
to get together. I am supposed to write a java program called
PhoneBill.java for my CS110 class. In this program I am supposed to
take three prompts and perform some math to give another value. The
problem is the Linear Equation is in form y=m*x+b and I don't know
how to give the unknown parameters to the Linear Equation class. The
Linear Equation class is a another class and I can only create an
object of type Linear Equation from this class without doing any math
in PhoneBill.java. An example is given in another file called
Temperatures.java but this class only takes one parameter and the
others are static.
When creating PhoneBill.java I have something of a start like so
public class PhoneBill
{
LinearEquation max = new LinearEquation();
public static void main ( String[] args )
{
Terminal pb = new Terminal();
int b = pb.readInt("Basic rate (in
dollars): ");
int m = pb.readInt("Cost per message unit (in
cents): ");
int xb = pb.readInt("Number of messages
units: ");
pb.readDouble("Amount willing to spend in
dollars: ");
pb.println("Maximum number of message
units: " + max.compute);
}
}
This is not finished. So the instructions are to model the class
after Temperatures.java and this class has a LinearEquation object
with static parameters like this:
LinearEquation c2f = new LinearEquation( 9.0/5.0, 32.0 );
Now what I can't seem to get right is the unknown parameters that are
to be given by the user such as Basic rate etc., and how I am to
place these parameters in the Linear Equation format y=m*x+b without
doing any math in PhoneBill.java and not knowing the parameters until
after the user input. Specifically,
LinearEquation phoneBillTotal = new LinearEquation(What here??, What
here??);
The output is supposed to look like this:
Basic rate (in dollars): 30
Cost per message unit (in cents): 5
Number of message units: 101
Phone bill is: $35.05
Amount willing to spend (in dollars): 40
Maximum number of message units: ???
Terminal is a jar file installed in the JDK. For more information
please go to http://www.cs.umb.edu/cs110/hw2/index.html. I am
terribly sorry about the long post but I am at a complete standstill
on this one. Any assistance in this matter will be greatly
appreciated.
Have a good one,
Reynold DeMarco Jr.
// joi/2/linear/LinearEquation.java
//
//
// Copyright 2003 Bill Campbell and Ethan Bolker
/**
* A LinearEquation models equations of the form y = mx + b.
*
* @version 2
*/
public class LinearEquation
{
private double m; // The equations's slope
private double b; // The equations's y-intercept
/**
* Construct a LinearEquation from a slope and y-intercept.
*
* @param m the slope.
* @param b the y-intercept.
*/
public LinearEquation( double m, double b )
{
this.m = m;
this.b = b;
}
/**
* Construct a LinearEquation from two points.
*
* @param x1 the x coordinate of the first point
* @param y1 the y coordinate of the first point
* @param x2 the x coordinate of the second point
* @param y2 the y coordinate of the second point
*/
public LinearEquation( double x1, double y1,
double x2, double y2 )
{
m = (y2 - y1) / (x2 - x1);
b = y1 - x1 * m;
}
/**
* Compute y, given x.
*
* @param x the input value.
* @return the corresponding value of y: mx+b.
*/
public double compute( double x )
{
return m*x + b;
}
/**
* Compute the inverse of this linear equation.
*
* @return the LinearEquation object you get by "solving for x".
*/
public LinearEquation getInverse()
{
return new LinearEquation( 1.0/m, -b/m );
}
}
// joi/2/linear/Temperatures.java
//
//
// Copyright 2003 Bill Campbell and Ethan Bolker
/**
* Temperature conversion program,
* for exercising LinearEquation objects.
*
* @version 2
*/
public class Temperatures
{
/**
* First a hardcoded test of Celsius-Fahrenheit conversion,
* then a loop allowing the user to test interactively.
*/
public static void main( String[] args )
{
Terminal terminal = new Terminal();
// create a Celsius to Fahrenheit converter
LinearEquation c2f = new LinearEquation( 9.0/5.0, 32.0 );
// ask it to tell us its inverse, for F to C
LinearEquation f2c = c2f.getInverse();
///////////////////////////////////////////////////
// Testing style 1: Hard coded, self-documenting //
///////////////////////////////////////////////////
terminal.println( "Hard coded self documenting tests:" );
terminal.print( "c2f.compute( 0.0 ), should see 32.0: ");
terminal.println( c2f.compute( 0.0 ) );
terminal.print( "f2c.compute( 212.0 ), should see 100.0: ");
terminal.println( f2c.compute( 212.0 ) );
//////////////////////////////////
// Testing style 2: Interactive //
//////////////////////////////////
terminal.println();
terminal.println( "Interactive tests:" );
while ( terminal.readYesOrNo("more?") ) {
double degreesCelsius =
terminal.readDouble( "Celsius: " );
terminal.println(" = "
+ c2f.compute( degreesCelsius )
+ " degrees Fahrenheit" );
double degreesFahrenheit =
terminal.readDouble( "degrees Fahrenheit: " );
terminal.println(" = "
+ f2c.compute( degreesFahrenheit )
+ " degrees Celsius" );
}
}
}