//  Scott DeRuiter      7/2/2002
//  Parabola.java
//  A Parabola class that can be used by the user to find
//  some information about a parabola.

public class Parabola   
{
	private double a, b, c;

	public Parabola ( double atemp, double btemp, double ctemp )   
	{
		a = atemp;
		b = btemp;
		c = ctemp;
	}

	public double Discriminant ( )  
	{
		return ( b * b - 4.0 * a * c );
	}

	public double VertexX ( )  
	{
		return ( -b / ( 2 * a ) );
	}

	public double VertexY ( )  
	{
		double vertx = VertexX ( );
		return ( a * vertx * vertx + b * vertx + c );
	}

	public double FunctionValue ( double x )  
	{
		return ( a * x * x + b * x + c );
	}
} 

//  Scott DeRuiter      7/2/2002
//  ParabolaTest.java
//  A client class for the Parabola class, to find some
//  info about a parabola
//  7/15/2011 - Greenstein: Changed TextReader to Scanner

import java.util.Scanner;

public class ParabolaTest   
{
	public static void main ( String [] args )   
	{
		Parabola first = new Parabola ( 1.0, -1.0, -6.0 );
		Parabola second = new Parabola ( 5.0, -3.0, 2.1 );
		double xval;
		Scanner input = new Scanner ( System.in );

		System.out.print ( "\n\nEnter an x value to plug in  ->  " );
		xval = input.nextDouble ( );

		System.out.print ( "\nDiscriminant for first:       " );
		System.out.println ( Format.right ( first.Discriminant ( ), 18, 3 ) );
		System.out.print ( "\nVertex for the first  :       " );
		System.out.print ( "( " + Format.right ( first.VertexX ( ), 6, 3 ) );
		System.out.println ( ", " + Format.right ( first.VertexY ( ), 6, 3 ) + " )" );
		System.out.print ( "\nThe first function value at " + Format.right ( xval, 6, 3 ) );
		System.out.println ( " is " + Format.right ( first.FunctionValue ( xval ), 6, 3 ) );

		System.out.print ( "\nDiscriminant for second:       " );
		System.out.println ( Format.right ( second.Discriminant ( ), 18, 3 ) );
		System.out.print ( "\nVertex for the second  :       " );
		System.out.print ( "( " + Format.right ( second.VertexX ( ), 6, 3 ) );
		System.out.println ( ", " + Format.right ( second.VertexY ( ), 6, 3 ) + " )" );
		System.out.print ( "\nThe second function value at " + Format.right ( xval, 6, 3 ) );
		System.out.println ( " is " + Format.right ( second.FunctionValue ( xval ), 6, 3 ) );
	}
}

Back to Lesson 5 Examples

Back to Java Main Page