Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Linear Equation

  Asked By: Kiswar    Date: Apr 24    Category: Java    Views: 2615
  

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" );
}
}
}



Share: 

 

5 Answers Found

 
Answer #1    Answered By: Timothy Patterson     Answered On: Apr 24

OK. Without seeing the actual problem  wording, I am going to assume
assume from what you told us that all math  is to be done within the
LinearEquation class. This complicates things a bit, as your two
parameters are not the same units.

Now, onto LinearEquation. This class  solves equations of the form
y=mx+b. You must supply 'm' and 'b', or it does nothing.

I suggest determining which parts of that equation  (y=mx+b) the user
is giving you, then figuring out how to get the parts you need. It can
be done using no math in PhoneBill except one input  transformation to
correct units.

If this weren't an assignment, I would be more forthcoming. However, I
strongly believe in giving someone a map, compass, and directions in
using them rather than airdropping them directly where they want to be.

 
Answer #2    Answered By: Jezza Brown     Answered On: Apr 24

Thanks for the reply but to clarify I will have to see a tutor at school on
Monday or Wednesday as this assignment is due Thursday and I am really stuck. If
you care to explain my error here I will be up and running once again and it
will save me three days time for my studies. To refresh your memory the class
linear equation  does compute the double as y=m*x=b and where I am now I can only
input x without knowing  m and b like so:

public class  PhoneBill
{



public static  void main  ( String[] args  )
{

LinearEquation max  = new LinearEquation(1.0, 0.0 );

Terminal pb = new Terminal();

int b = pb.readInt("Basic rate (in dollars): " + max.compute(b));
double m = pb.readDouble("Cost per message  unit (in cents): ");
double xb = pb.readDouble("Number of messages units: ");
pb.println("Phone bill is: " + max.compute(m));




}
}

So the problem  still remains how to input  m and b from the prompt and also when
create  LinearEquation it takes to parameters  of type  a[][], and b[]. Now I
thought of something like double myDouble = m*xb to input the double but that is
doing math  in the PhoneBill.java file  and not allowed. For your information my
original post  is #13786. Any assistance  you can give  in this matter is greatly
appreciated.

 
Answer #3    Answered By: Norman Ray     Answered On: Apr 24

> So the problem  still remains how to input  m and b from the prompt
and also when I create  LinearEquation it takes to parameters  of type
a[][], and b[]. Now I thought of something like double myDouble = m*xb
to input the double but that is doing math  in the PhoneBill.java file
and not allowed. For your information my original post  is #13786. Any
assistance you can give  in this matter is greatly appreciated.

Firstly, there is no method LinearEquation(a[][], b[]). There are 2
constructors that take 2 and 4 doubles respectively.

You already have m and b stored in the vars of the same name. The user
will also input y (Hint: Look at the example  inputs and output  and
figure out what inputs are what). Use the same syntax as the previous
inputs.

The last output you do is the value of x for the given value of y.
Look at the code for LinearEquation. There is a way to do this.

I still have a issue with not being allowed to do any math in
PhoneBill, as you must convert at least one  of m or b to the other's
units. Without being allowed to do that, the problem is unsolvable.

 
Answer #4    Answered By: Leon Evans     Answered On: Apr 24

This is the final version:

// PhoneBill.java
//
//
// Copyright 2004 Reynold DeMarco Jr

/**
class  for computing Phone Bills
* using LinearEquation objects.
*
* @version 1
*/

public class PhoneBill
{

public static  void main  ( String[] args  )
{

//declare doubles
double a_new, b_new, bill, end ;

//create a Terminal object
Terminal pb = new Terminal();

//get user  input parameters
b_new = pb.readDouble("Basic rate (in dollars): ");
a_new = pb.readDouble("Cost per message  unit (in cents): ");

//create a LinearEquation object
LinearEquation showBill = new LinearEquation(a_new*0.01,
b_new);

//last user input parameter  for computation
bill = pb.readDouble("Number of message units: ");

//have the LinearEquation object compute the bill
pb.println("Phone bill is: $" + showBill.compute(bill) );

//use LinearEquation objects to get units
LinearEquation changeToUnits = new LinearEquation(-1.0, 0.0);
LinearEquation showEnd = new LinearEquation(100/a_new, 0.0);

//get user input  for second computation
end = pb.readInt("Amount willing to spend (in dollars): ") +
changeToUnits.compute(b_new);

//have the LinearEquation object compute
pb.println("Maximum number  of message units: " +
showEnd.compute(end));

}
}

 
Answer #5    Answered By: Garai Chalthoum     Answered On: Apr 24

I found the answer as follows:

// PhoneBill.java
//
//
// Copyright 2004 Reynold DeMarco Jr

/**
class  for computing Phone Bills
* using LinearEquation objects.
*
* @version 1
*/

public class PhoneBill
{

public static  void main  ( String[] args  )
{

//declare doubles
double a_new, b_new, bill, end ;

//create a Terminal object
Terminal pb = new Terminal();

//get user  input parameters
b_new = pb.readDouble("Basic rate (in dollars): ");
a_new = pb.readDouble("Cost per message  unit (in cents): ");

//create a LinearEquation object
LinearEquation showBill = new LinearEquation(a_new*0.01,
b_new);

//last user input parameter  for computation
bill = pb.readDouble("Number of message units: ");

//have the LinearEquation object compute
pb.println("Phone bill is: $" + showBill.compute(bill) );

//create a second LinearEquation object
LinearEquation showEnd = new LinearEquation(100/a_new, 0.0);

//get user input  for second computation
end = pb.readInt("Amount willing to spend (in dollars): ") + -
b_new;

//have the LinearEquation object compute
pb.println("Maximum number  of message units: " +
showEnd.compute(end));

}
}

 
Didn't find what you were looking for? Find more on Linear Equation Or get search suggestion and latest updates.




Tagged: