Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
  
Lab 6b: Returning Arrays with Functions 
 
Introduction: 
 
As we have already seen, functions can be used in Java to package code so it can be easily reused 
in our program or in different programs. In most cases a function will calculate and return one 
value, but there are times when we want to return multiple values.  In this lab, we will 
demonstrate how a function can return an array of results. 
 
Instructions: 
 
Consider the following Java program. We have one function “solve_quadratic” that solves for 
the roots of a quadratic equation of the form y = ax2 + bx + c. The main program loops over a 
range of a,b,c coefficients to test this function. 
 
import java.util.Scanner; 
public class Main 
{ 
    static void solve_quadratic(double a, double b, double c) 
    { 
        // Check for NO roots  
        double discriminant = b * b - 4 * a * c; 
        if ((a == 0 && b == 0) || (discriminant < 0)) 
        { 
            System.out.printf("No roots:"); 
        }    
        // Solve linear equation 
        else if (a == 0) 
        { 
            double root = -c / b; 
            System.out.printf("Root: %4.3f", root); 
        } 
        // Solve for ONE root 
        else if (discriminant == 0) 
        { 
            double root = -b / (2 * a); 
            System.out.printf("Root: %4.3f", root); 
        } 
        // Solve for TWO roots 
        else 
        { 
            double root1 = (- b - Math.sqrt(discriminant)) / (2 * a); 
            double root2 = (- b + Math.sqrt(discriminant)) / (2 * a); 
            System.out.printf("Roots: %4.3f %4.3f", root1, root2); 
        } 
    } 
     
    public static void main(String[] args) 
    { 
        // Solve Quadratic 
        int range = 1; 
        for (int a = -range; a <= range; a++) 
        for (int b = -range; b <= range; b++) 
        for (int c = -range; c <= range; c++) 
        { 
            System.out.print("\nQuadratic equation: "); 
            System.out.println("y="+a+"x^2"+"+"+b+"x"+"+"+c); 
            solve_quadratic(a, b, c); 
        } 
 } 
}     
     
Step 1: Copy this program into your Java program editor, and compile and run it. You will see 
that the program calculates and prints the roots for 27 quadratic equations with coefficients 
ranging from [-1,0,1]. 
 
Step 2: Instead of printing the roots, now lets modify the “solve_quadratic” function to return an 
array of doubles that contain the roots.  The first step is to modify the return type for 
“solve_quadratic”.  Edit your program and replace “static void” with “static double[]”.  If you 
compile the program, you should get an error message saying, “missing return statement”.  This 
is because we are not returning an array anywhere. 
 
Step 3: Edit solve_quadratic, and go to the section that calculates two roots.  Add the following 
code after the print statement to declare and initialize an array, and then return this array. 
 
double result[] = {root1, root2}; 
return result; 
 
If you compile the program, you will still get an error message saying, “missing return 
statement”.  This is because the program is not returning a value in all of the if-statements. 
 
Step 4: Edit solve_quadratic, and go to the section that returns one root.  We only want to return 
one value here.  If we try to simply “return root” we will get a syntax error.  Instead we need to 
create an array that is initialized with one value in it and return this array.  Edit your program and 
make this change.  Copy and paste this code into the “linear equation” section. 
 
Step 5: Finally, edit solve_quadratic, and go to the section that checks for no roots.  This time we 
need to declare an empty array using “double result[] = {}” and return this array.  When we 
compile the syntax errors should be gone. 
 
Step 6: When you run the program, it should print the same output as before.  Now we need to 
modify the main program to do something with the array that was returned.  First we must store 
the return value in an array using the following.   
 
double roots[] = solve_quadratic(a, b, c); 
Step 7: The next step is to print the roots.  One solution would be to write a for-loop that goes 
from 0 to roots.length to print 0, 1, or 2 roots.  Another option is to use one of the functions in 
the Arrays library.  Go to the top of your program and add the following. 
 
import java.util.Arrays 
 
Now we can use the “Arrays.toString” function to create a string representation of the array 
contents that we can print using println.  Do a google search for “java Arrays toString” to see the 
Java documentation for the Arrays class and some examples of how to call this function. 
 
Step 8: Edit your main program call the Arrays.toString function inside a println statement to 
output the values returned by the “solve_quadratic” call.  Now when you run the program, you 
should see the roots printed inside square brackets. 
 
Step 9: Finally, edit the “solve_quadratic” function to comment out all of the println statements 
and run your program.  Now you should see the roots of the 27 quadratic equations printed once. 
 
Step 10: When your program is working correctly, upload your final program and a copy of your 
program output into Blackboard for grading.