Python Programming: An Introduction to Computer Science, 3rd Ed.
Python Programming: An Introduction to Computer Science, 3rd Ed.
3rd Edition
ISBN: 9781590282755
Author: John Zelle
Publisher: Franklin, Beedle & Associates
bartleby

Videos

Question
Book Icon
Chapter 8, Problem 13PE
Program Plan Intro

Regression Line

Program plan:

  • Import the header file.
  • Define the “graphWin” method
    • Create a window
    • Set the coords
    • Display the message on the window
    • Call the “draw” method for set the point
    • Call the “Line” method for draw the line
    • Call the “draw” method for set the point
    • Call the “Line” method for draw the line
    • Call the “draw” method
    • Call the “Rectangle” method
    • Get the center position
    • Set the “Done” text
    • Call the “draw” method stop the program after clicking the “Done” text
    • Accept the input from the user
    • Set the list
    • Make the condition true
      • Get the mouse action
      • Check the condition
        • Break the loop
      • Otherwise, store user points in an appended list
      • Call the “draw” method
    • Return the result
  • Define the “average” method
    • Set the values for the variables
    • Iterate “i” until it reaches “allPoints” value
      • Get the “x” and “y” value
      • Calculate “sumX”, and “sumY”
      • Increment the “count” value
      • Calculate the “sumXiYi, “sqX”, “sumSqXi”, “SqY”, and sumSqYi” values
    • Calculate the “a” and “b” values
    • Return the values
  • Define “main” method
    • Display the text on the window
    • Call the “average” method
    • Set the values
    • Call the “Line” method
    • Call the “draw” method
    • Get the mouse action
  • Call the function “main()”.

Expert Solution & Answer
Check Mark

Explanation of Solution

Program:

#import the required header files

from graphics import *

import math as m

#definition of "graphWin" method

def graphWin(Title):

    #Create a graphics window

    win = GraphWin(Title, 400, 400)

    #set the coords

    win.setCoords(-10, -10, 10, 10)

    #tell user to select multiple locations on the screen to designate points

    message = Text(Point(-3, 8), "Click to delineate points on the graph.")

    #draw the point

    message.draw(win)

    #call the "Line" method

    axisX = Line(Point(-10,0), Point(10,0))

    #call the "draw" method

    axisX.draw(win)

    #call the "Line" method

    axisY = Line(Point(0,10), Point(0,-10))

    #call the "draw" method

    axisY.draw(win)

    #call the "Rectangle" method

    r = Rectangle(Point(-9, -9), Point(-7,-8))

    #call the "draw" method

    r.draw(win)

    #get the centre position

    rCenter = r.getCenter()

    #set the text

    stopMouse = Text(rCenter, "Done")

    #call the "draw" method

    stopMouse.draw(win)

    #accept input from user until <done> button is pressed

    click = Point(0,0)

    #set the list

    allPoints = []

    #check the condition

    while True:

        #get the mouse action

        click = win.getMouse()

        #check the condition

        if ((-9 <= click.getX() <= -7) and (-9 <= click.getY() <= -8)):

            #break the loop

            break

        #otherwise

        else:

            #store user points in an appended list

            allPoints.append(click)

            #call the "draw" method

            click.draw(win)

    #return the values

    return allPoints, win

#definition of "average" method

def average(allPoints):

    #set the values for the variables

    sumX = 0

    sumY = 0

    count = 0

    sumXiYi = 0

    sumSqXi = 0

    sumSqYi = 0

    #iterate "i" until it reaches "allPoints" value

    for i in allPoints:

        #get the "x" and "y" value

        x = i.getX()

        y = i.getY()

        #calculate the "sumX" value

        sumX = sumX + x

        #calculate the "sumY" value

        sumY = sumY + y

        #increment the value

        count = count + 1

        #calculate the "xy" value

        xy = x * y

        #calculate the "sumXiYi" value

        sumXiYi = sumXiYi + xy

        #calculate the "SqX" value

        SqX = x * x

        #calculate the "sumSqXi" value

        sumSqXi = sumSqXi + SqX

        #calculate the "SqY" value

        SqY = y * y

        #calculate the "sumSqYi" value

        sumSqYi = sumSqYi + SqY

    #calculate the "a" and "b" value

    a = ((sumY * sumSqXi) - (sumX * sumXiYi)) / (count * (sumSqXi) - sumX ** 2)

    b = ((count * sumXiYi) - (sumX * sumY)) / (count * (sumSqXi) - sumX ** 2)

    #return the values

    return a, b

#definition of "main" method

def main():

    #get the text for the window

    allPoints, win = graphWin("Regression Line")

    #call the method

    a, b = average(allPoints)

    #set the values

    x1 = -10

    x2 = 10

    #call the "Line" method

    regressLine = Line(Point(x1, (a + b * x1)), Point(x2, (a + b * x2)))

    #call the "draw" method

    regressLine.draw(win)

    #get the mouse action

    win.getMouse()

#call the "main" method

main()

Sample Output

Output:

Screenshot of “Regression Line” window

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 8, Problem 13PE , additional homework tip  1

After clicking “Done” button, the below window will appear

Screenshot of “Regression Line” window

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 8, Problem 13PE , additional homework tip  2

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write a program that displays twocircles with radius 10 at location (40, 40) and (120, 150) with a line connectingthe two circles, as shown in Figure . The distance between the circlesis displayed along the line. The user can drag a circle. When that happens, thecircle and its line are moved, and the distance between the circles is updated.
You are going to write a simulation for a mini survival game. In this game, one flyand three frogs are placed randomly on a board with the size 7x7.In each iteration, they move randomlywithin the board, and the simulation stops when one of the frogs eats the fly.Fly and frogs are essentially creatures that can move, make noise, and eat. Frogs can move up to 2squares in any direction and flies can move up to 1. Frogs make the "Croak! Croak!" sound, and fliesmake "ZzzZZz!". Since frogs should not eat their kind, a function of "isEatable" should also beimplemented in decision making. A variable or function to check if the fly is alive is also required as aterminate condition for simulation.In each iteration, an 'f' character represents frogs' position on board, and an '*' character is used torepresent the position of fly. Use the topics (Abstract classes, Interface, etc.) to implementthe simulation. (in java) An example output is given below.
Write a program that simulates a snail trying to crawl up a building of height 6steps. The snail starts on the ground, at height 0. In each iteration, the snaileither crawls up one step, or slips off one step and falls all the way back to theground. In each iteration you would take a number as input from user and if the number is less 50 than the snail will slip otherwise it will crawl up. The program should keep going until the snail gets to the top of the building. It should then print out the number of falls that the snail took before it finally reached the top.Here is a sample execution:Iteration 1: Please enter a number between [1,100]: 60 Snail Moved: UpIteration 2: Please enter a number between [1,100]: 60 Snail Moved: UpIteration 2: Please enter a number between [1,100]: 40 Snail Moved: DownNumber of falls: 8
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Java random numbers; Author: Bro code;https://www.youtube.com/watch?v=VMZLPl16P5c;License: Standard YouTube License, CC-BY