Introduction: Java Programming for Beginners: Quadratic Formula

These instructions will teach you how to code and solve for the quadratic formula using Java coding language on a browser platform. You should be able to take these basic coding principles and knowledge from this equation and use it for other basic coding projects.

*This Instructable is designed for those who have no prior coding knowledge and consider themselves beginners.


Project Goals:

To inform beginners on how to code a basic program on the computer, and more specifically, how to code the quadratic formula and receive an answer through a programming website.

Apply those skills & show how coding can be applied to other uses.

Notes:

Notes to keep in mind before coding:

ALWAYS use a semicolon “;” when ending a line. Capitalization MATTERS - otherwise the program will not run.

Spacing ("white space") between lines of codes does not matter, however the order that the commands are listed does matter.

We recommend typing out the code exactly as written in instructions, NOT copy/pasting. This will ensure that you can more easily identify and correct any mistakes you may have personally made.

Step 1: Access

1. Go to https://repl.it

2. In search bar, type in "java": This indicates the programming language for the site.

Step 2: Setup

1. Click on Blue "examples" : A line will appear on the first line on the input box saying: "Not sure what to do? Run some examples" The word 'examples' in the phrase will link you to the correct page.

2. From Example Page, click on "Hello World" example code box.

3. Click the "Run" button at the top of the page.: This button is next to the "Share" and "Save" buttons.

The phrase "hello world" should appear in the black 'output' box on the right side of the screen.

This step ensures the site is functioning properly.

Step 3: Code Block 1: Variables

In Block 1, you will be assigning variables as an integer value. (i.e. a=3, b=4, c=-4)

1. Highlight and delete the entire line ONLY ON LINE 3 that says “System.out.println(“Hello world”);”

Note: DO NOT DELETE BRACES ("}") ON LINES 4 AND 5.

All code will come before these braces. These are needed to complete and run the code.

2. On the same line (line 3), type out

double a = 3;

3. Press "enter" on your keyboard and then type out

double b = 4;

4. Press "enter" and then type out

double c = -4;


A double is a Java expression that means a number that can contain a decimal value. ex. 3.13 is a double.


Step 4: Code Block 2:

In Block 2, you will be coding the addition portion (-b + etc) of the quadratic formula.

1. Press "enter" twice and then type out:

double answer1; 

This creates a Java Double without setting the value.

2. Press "enter" and type out:

answer1 = Math.sqrt((b*b)-4*a*c);

This line will start doing math using the a, b, and c variables that were defined at the beginning of the program. Math.sqrt() is a Java command that takes the square root of everything within the parenthesis.

The operations performed do the first part of the Quadratic formula: b squared minus 4 times a times c.

NOTE: This line MUST be typed exactly as shown!


3. Press "enter" and type out:

answer1 = -b + answer1;

This line continues to calculate the answer. It takes the variable answer1 and takes negative b plus the previous output of answer1 from the previous step.

4.Press "enter" and type out:

answer1 = answer1/(2*a);

This finishes the quadratic formula by taking answer1 and dividing it by 2 times a.

Step 5: Block 2 Test and Error Check

This step will solve the Block 2 equation and provides a check point to identify any errors that may have occurred so far.

1. Press "enter" twice and type out:

System.out.println("X = " + answer1);

This line prints out the variable value to the console, which is the right-hand bar on your screen.

2. Click the "Run" button at the top of the screen. An answer should appear in the black output box. (In this example the answer would be X=0.6666666)

3. If the output box shows a red error code (like the one shown in the sample picture above), go back and check that the coding is exactly as shown in the instructions/examples. Check for spacing, semicolons, misspelling, etc.


Note: System.out.println() will print EXACTLY what is shown between the double quotes. The plus "+" symbol allows another variable to be added to this expression.

Step 6: Block 3: Subtraction

In Block 3, you will be coding the subtraction portion (the quadratic formula produces TWO outputs: one where addition is used, and one where subtraction is used. It is changed where the "plus or minus" symbol is used.)

1. Press "enter" twice and type out:

double answer2;

This is the same as before, however because we are outputting a second answer, we will use answer2 as the variable.

2. Press "enter" and type out:

answer2 = Math.sqrt((b*b)-4*a*c);

This is the same expression as before; it will square root everything in the parenthesis.

3. Press "enter" and type out:

answer2 = -b - answer2;

This is different than the addition section; make sure to use a subtraction sign instead of addition.

4. Press "enter and type out:

answer2 = answer2/(2*a);

This is the same as the addition section.


Step 7: Solve Equation

In this step you solve the Block 3 equation and therefore the whole formula.

1. Press "enter" twice and type out:

System.out.println("X = " + answer2);

This will print out the subtraction answer right underneath the addition answer from before.

2. Click the "Run" button at the top of the screen. An answer should appear in the black output box. (In this example, the answer should be "X = -2.0".

3. If the output box shows a red error code go back and check that the coding is exactly as shown in the instructions/examples. Check for spacing, semicolons, misspelling, etc.

Step 8: Error Checking

If you clicked RUN at the end of this guide and successfully got this output, you have successfully coded the formula! Otherwise, see below.

Correct output:

X = 0.6666666666666666
X = -2.0

Errors:

If you get an output like in the picture, you did not space something correctly, forgot a semicolon ";", or misspelled something. Here is a link to a fully working program. Compare your code to this and it will help you find any errors.