Question

Least Square Lines Equation- Text File /O Suppose we have a text file (which I supplied named data.txt) that has the followin

please answer it in a java console application form (java GUI).

0 0
Add a comment Improve this question Transcribed image text
Answer #1
// Please find the required solution

import java.io.*;
import java.io.File;
import java.util.Scanner;

public class LeastSquaresLineEquation {
    public static void main(String[] args) throws IOException {

        // 1.   prompt the user name of text file
        Scanner keyBoard = new Scanner(System.in);
        System.out.print("Enter file name:");
        String fileName = keyBoard.nextLine();
        File file = new File(fileName);
        if (!file.exists()) {
            System.out.println(fileName + " not found.");
            return;
        }
        keyBoard.close();



        // 2.   open text file and read ordered pairs
        Scanner fileScanner = new Scanner(file);

        // 3.   while looping through the ordered pairs, have variables used
        int orderedPairs = 0;
        float sumX = 0, sumY = 0, sumX2 = 0, sumXY = 0;

        while (fileScanner.hasNextLine()) {
            String[] array = fileScanner.nextLine().split("\\s+");
            float x = Float.valueOf(array[0]);
            float y = Float.valueOf(array[1]);
            sumX += x;
            sumY += y;
            sumX2 += x * x;
            sumXY += x * y;
            orderedPairs++;
        }

        //  4.  After reading ordered pairs close the line
        fileScanner.close();

        //  5.  computes regression coefficients m and b
        float avgX = sumX/orderedPairs;
        float avgY = sumY/orderedPairs;
        float slope = (sumXY - (sumX*avgY))/(sumX2 - (sumX *avgX));
        float yIntercept = avgY - slope * avgX;

        //  6,7. output the equation to terminal
        System.out.printf("Equation of least square line:y= %.5f x + %.5f", slope, yIntercept);
    }
}

data.txt riit.ixi 20.0 761 31.5 817 2 50.0 874 3 71.8 917 91.3 1018

sample output:

Enter file name:data.txt Equation of least square line:y- 3.33658 x + 700.82837

Add a comment
Know the answer?
Add Answer to:
please answer it in a java console application form (java GUI). Least Square Lines Equation- Text File /O Suppose we ha...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • We use the form y a + bx for the least-squares line. In some computer printouts, the least-square...

    we use the form y a + bx for the least-squares line. In some computer printouts, the least-squares equation is not given directly. Instead, the value of the constant a is given, and the coefficient b of the explanatory or predictor variable is displayed. Sometimes a is referred to as the constant, and sometimes as the intercept. Data from a report showed the following relationship between elevation (in thousands of feet) and average number of frost-free days per year in...

  • In c++ visual studio Write a program that does the following: Reads the input data set...

    In c++ visual studio Write a program that does the following: Reads the input data set from file named "data.txt". Assume that the input file contains x and y values as shown in the sample to the right (the first number in each line is the x value). The number of data points in the input file is not known but assume that they will not exceed 100. Once it gets the data in two one-dimensional arrays (x and y),...

  • We use the form ŷ = a + bx for the least-squares line. In some computer...

    We use the form ŷ = a + bx for the least-squares line. In some computer printouts, the least-squares equation is not given directly. Instead, the value of the constant a is given, and the coefficient b of the explanatory or predictor variable is displayed. Sometimes a is referred to as the constant, and sometimes as the intercept. Data from a report showed the following relationship between elevation (in thousands of feet) and average number of frost-free days per year...

  • Hello. I am using a Java program, which is console-baed. Here is my question. Thank you....

    Hello. I am using a Java program, which is console-baed. Here is my question. Thank you. 1-1 Write a Java program, using appropriate methods and parameter passing, that obtains from the user the following items: a person’s age, the person’s gender (male or female), the person’s email address, and the person’s annual salary. The program should continue obtaining these details for as many people as the user wishes. As the data is obtained from the user validate the age to...

  • Please answer all fill in the blanks, thanks! In the United States, tire tread depth is...

    Please answer all fill in the blanks, thanks! In the United States, tire tread depth is measured in 32nds of an inch. Car tires typically start out with 10/32 to 11/32 of an inch of tread depth. In most states, a tire is legally worn out when its tread depth reaches 2/32 of an inch. A random sample of four tires provides the following data on mileage and tread depth: Mileage (10,000 miles) Tread Depth (32nds of an inch) Tire...

  • use scattergraph method, high low method, and the least square regression 247 Cost-Volume-Profit Relationships EXHIBIT SA-5...

    use scattergraph method, high low method, and the least square regression 247 Cost-Volume-Profit Relationships EXHIBIT SA-5 A Scattergraph Plot for Brentine Hospital Using Microsoft Excel 5:2.000 $10,000 58,000 Maintenance cost 56.000 54.000 52.000 2,000 2000 6.000 4000 Patient Day To prepare a scattergraph plot in Excel, begin by highlighting the data in cells B4 through CIO (as shown in Exhibit 5A-4). From the Charts group within the Insert tab, select the "Scatter" subgroup and then click on the choice that...

  • JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year...

    JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm/ //made up data 2004/Ali/1212/1219/1 2003/Bob/1123/1222/3 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2001/Jim/1101/1201/3 Text file Class storm{ private String nameStorm; private int yearStorm; private int startStorm; private int endStorm; private int magStorm; public storm(String name, int year, int start, int end, int mag){ nameStorm = name; yearStorm = year; startStorm...

  • File_Size_(MB) Transfer_Time_(sec) 48 17.3 59 30.3 81 27.8 74 24.3 22 8.3 21 12.9 31 16.4...

    File_Size_(MB) Transfer_Time_(sec) 48 17.3 59 30.3 81 27.8 74 24.3 22 8.3 21 12.9 31 16.4 49 18.6 99 32.5 22 19.6 51 29.2 99 31.6 68 26.7 69 23.6 98 44.6 32 23.8 87 35.1 81 29.6 29 14.6 97 32.9 Before taking the plunge into videoconferencing, a company ran tests of its current internal computer network. The goal of the tests was to measure how rapidly data moved through the network given the current demand on the network....

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • (Just need help with part F) File_size_(MB) Time_(sec) 77 31.1 93 35.1 85 35.3 94 35.7...

    (Just need help with part F) File_size_(MB) Time_(sec) 77 31.1 93 35.1 85 35.3 94 35.7 20 14.4 74 28.9 68 29.6 88 33.5 42 21.7 20 15.3 72 28.4 24 11.5 95 35.7 59 25.6 93 36.6 71 29.1 87 34.3 92 37.2 90 35.6 67 26.8 87 32.6 83 31.2 80 34.1 57 22.8 52 25.4 76 28.9 96 38.7 70 31.8 59 24.2 57 28.1 Before taking the plunge into videoconferencing, a company ran tests of its...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT