So I just finished up a way to solve Standard Form Quadratic Equations, Now I want to code a way to automatically Convert a given Standard Form equation to Vertex Form, or Vertex to Standard. Is it possible due to the fact that you have to get a perfect square binomial? Could you just use the Square Root of h in y = a(x-h)^2+k?

I've searched google and elsewhere with no luck finding code for a converter. So I came here for a start.

``````package com.github.geodox.quadraticconverter;

import java.util.Scanner;

{

public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
String newEquation = null;

System.out.println("Standard to Vertex? Type stv");
System.out.println("Vertex to Standard? Type vts");
String option = s.nextLine();

//Standard to Vertex
if(option == "stv")
{
double a, b, c;

//Take Inputs
System.out.println("Equation: y = ax^2 + bx + c");
System.out.println("Input a: ");
a = s.nextDouble();
System.out.println("Input b: ");
b = s.nextDouble();
System.out.println("Input c: ");
c = s.nextDouble();
System.out.println("Standard Form: y = " + a + "x^2 + " + b + "x + " + c);

//Convert Equation

//Print Resulting Equation
System.out.println("Vertex Form: " + newEquation);

}
//Vertex to Standard
else if(option == "vts")
{
double x, h;

//Take Inputs
System.out.println("Equation: y = a(x-h)^2 + k");
System.out.println("Input x: ");
x = s.nextDouble();
System.out.println("Input k: ");
h = s.nextDouble();
System.out.println("Vertex Form: y = a(" + x + " - " + h + ") + k");

//Convert Equation

//Print Resulting Equation
System.out.println("Standard Form: " + newEquation);

}
//Invalid Response
else
System.out.println("Invalid Response");

s.close();
}

}
``````

Here is what I've done so far.
I can't find what is done in each step of conversion. That should at least get me started.

## All 9 Replies

This type of math is definitely not my strongest point so I'm hoping that this can help with your equations.

In saying that, if it were me writing the code, I would create two methods that used the variables a, b, & c, and x & h to perform and return the calculations instead of doing it all in the main.

Sorry I can't be of much help but don't worry, someone will be able to help you soon I'm sure.

For something as basic as this, where it will only be called once every run, you don't need to have methods for it. Only if it could be called multiple times.
As for the website, I have looked at all of those regarding this subject. I know for converting standard to vertex you need to calculate the perfect square binomial but how would I do that given the standard form equation? I'm not very good at math either which is why I'm programming the tools to help me through it. After I get the program working I will be adding code so it prints each step.

This is the updated code.

``````package com.github.geodox.quadraticconverter;

import java.util.Scanner;

{

public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
String newEquation = null;

System.out.println("Standard to Vertex? Type stv");
System.out.println("Vertex to Standard? Type vts");
String option = s.nextLine();

//Standard to Vertex
if(option == "stv")
{
double a, b, c;
double x, h;

//Take Inputs
System.out.println("Equation: y = ax^2 + bx + c");
System.out.println("Input a: ");
a = s.nextDouble();
System.out.println("Input b: ");
b = s.nextDouble();
System.out.println("Input c: ");
c = s.nextDouble();
System.out.println("Standard Form: y = " + a + "x^2 + " + b + "x + " + c);

//Convert Equation

x = 0.0D;
h = 0.0D;

//Print Resulting Equation
newEquation = generateVertex(x, h);
System.out.println("Vertex Form: " + newEquation);

}
//Vertex to Standard
else if(option == "vts")
{
double x, h;
double a, b, c;

//Take Inputs
System.out.println("Equation: y = a(x-h)^2 + k");
System.out.println("Input x: ");
x = s.nextDouble();
System.out.println("Input k: ");
h = s.nextDouble();
System.out.println("Vertex Form: y = a(" + x + " - " + h + ") + k");

//Convert Equation

a = 0.0D;
b = 0.0D;
c = 0.0D;

//Print Resulting Equation
newEquation = generateStandard(a, b, c);
System.out.println("Standard Form: " + newEquation);

}
//Invalid Response
else
System.out.println("Invalid Response");

s.close();
}

private static String generateVertex(double x, double h)
{
String equation = "y = a(" + x + " - " + h + ") + k";

return equation;
}

private static String generateStandard(double a, double b, double c)
{
String equation = "y = " + a + "x^2 + " + b + "x + " + c;

return equation;
}

}
``````

For something as basic as this, where it will only be called once every run, you don't need to have methods for it. Only if it could be called multiple times.

I disagree absolutely. Splitting long methods into sub-methods can be a massive boost to readability and clarity. By delegating well-defined pieces of functionality to sub-methods a large multi-level problem gets turned into a number of relatively independent small single-level problems.
There'a a popular guideline that goes like "if your method doesn't all fit on one screen, break it down". I think that's too simplictic, but the instinct is right.
Looking at your code above, there's an `if` on line 18 with an `elseif` after 26 lines, and an `else` after a massive 52 lines. I'm 100% with stuggie here, and I'd start with a main that looked like:

``````public static void main(String[] args) {
String option = promtUserForOption();
if ("stv".equals(option)) {   // NB DON'T TEST STRINGS WITH ==
convertStandardToVertex();
} else if ("vts".equals(option)) {
convertVertexToStandard();
} else {
System.out.println("Invalid Response");
}
}

private convertStandardToVertex() {
// just do the conversion here
...
``````

Just compare that to the original in the situation where:
a) you want to repeat the prompt after an invalid input
b) you decide a switch would be better than elseifs
Which version would you rather be changing?

I disagree absolutely. Splitting long methods into sub-methods can be a massive boost to readability and clarity. By delegating well-defined pieces of functionality to sub-methods a large multi-level problem gets turned into a number of relatively independent small single-level problems.
There'a a popular guideline that goes like "if your method doesn't all fit on one screen, break it down". I think that's too simplictic, but the instinct is right.
Looking at your code above, there's an if on line 18 with an elseif after 26 lines, and an else after a massive 52 lines. I'm 100% with stuggie here, and I'd start with a main that looked like:

In that aspect, I agree. I just have never liked there being Methods for every step, it creates pointless sub methods.

Just compare that to the original in the situation where:
a) you want to repeat the prompt after an invalid input
b) you decide a switch would be better than elseifs
Which version would you rather be changing?

I've wanted to get around to implmenting repeat after Invalid response but never got to it, Not sure how I would.

Anyway, back to the situation still at hand. Where would I start? The perfect square binomial? How would I do this? If someone gives me a hand with doing standard to vertex, I should be able to do vertex to standard.

Sorry I can't help with that kind of advanced algebra, I'm just a menial coder. ;)

If someone gives me a hand with doing standard to vertex, I should be able to do vertex to standard.

This engaged my curiosity, and I found that vertx to standard is easy, so here's an attempt at that.
More important, this is an opportunity to show how "pointless" methods and classes(!) can make spaghetti code so very understandable.
First, let's have a couple of trivial classes for the two kinds of equation. Here the standard form:

``````class StandardQ {

final int a, b, c;

public StandardQ(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}

@Override
public String toString() {
return "Standard form: " + a + "x^2 + " + b + "x + " + c;
}

}
``````

The vertex form is the same, but I've implemented a little conversion method in there:

``````class VertexQ {

final int a, h, k;

public VertexQ(int a, int h, int k) {
this.a = a;
this.h = h;
this.k = k;
}

public StandardQ toStandardQ() {
// expand to  ax^2 + 2ahx + ah^2 + k
return new StandardQ(a, a*h*2 ,a*h*h+k);
}

@Override
public String toString() {
return "Vertex form: " + a + "(x +  " + h + ")^2 + " + k;
}
}
``````

Still easy, yes?

Now a little test case:

``````    VertexQ vq = new VertexQ(2,-1,3);
System.out.println(vq);
System.out.println(vq.toStandardQ());
``````

Which is also trivial to read and understand, and gives us:

Vertex form: 2(x + -1)^2 + 3
Standard form: 2x^2 + -4x + 5

OK, maybe the coefficients should be doubles, and the printout is a little rough for negative coefficients, but that's just a bit more code in the toString() methods. You get my point...

commented: Much appreciated for the help. +2 Yes converting standard form to vertex form would be trivial as you would just use 'completing the square' which most of us have used in high school math.

To do the opposite would be slightly more involved, as you would have to expand and simplify the coeffs.

First thoughts are this isn't easy. You would have to utilise a polynomial class with a Reverse Polish Notation parser - LOL

good luck with that!

The following site implements mathematica's software, http://www.quickmath.com/

but don't be fooling yourself in thinking this is easy. If you can't solve this on paper (which you admit to sucking in math) this is NOT for you. **Edit

___
Ignore above James answer probably does enough. I thought the user input was a string '2(x + -1)^2 + 3'

Going to try to solve it tonight/tomorrow. Will post back soon with results.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.