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()”.
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()
Output:
Screenshot of “Regression Line” window
After clicking “Done” button, the below window will appear
Screenshot of “Regression Line” window
Want to see more full solutions like this?
Chapter 8 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- I need help creating the network diagram and then revising it for the modified activity times.arrow_forwardActivity No. Activity Time (weeks) Immediate Predecessors 1 Requirements collection 3 2 Requirements structuring 4 1 3 Process analysis 3 2 4 Data analysis 3 2 5 Logical design 50 3,4 6 Physical design 5 5 7 Implementation 6 6 c. Using the information from part b, prepare a network diagram. Identify the critical path.arrow_forwardGiven the following Extended-BNF grammar of the basic mathematical expressions: Show the derivation steps for the expression: ( 2 + 3 ) * 6 – 20 / ( 3 + 1 ) Draw the parsing tree of this expression. SEE IMAGEarrow_forward
- Whentheuserenters!!,themostrecentcommandinthehistoryisexecuted.In the example above, if the user entered the command: Osh> !! The ‘ls -l’ command should be executed and echoed on user’s screen. The command should also be placed in the history buffer as the next command. Whentheuserentersasingle!followedbyanintegerN,theNthcommandin the history is executed. In the example above, if the user entered the command: Osh> ! 3 The ‘ps’ command should be executed and echoed on the user’s screen. The command should also be placed in the history buffer as the next command. Error handling: The program should also manage basic error handling. For example, if there are no commands in the history, entering !! should result in a message “No commands in history.” Also, if there is no command corresponding to the number entered with the single !, the program should output "No such command in history."arrow_forwardActivity No. Activity Time (weeks) Immediate Predecessors 1 Requirements collection 3 2 Requirements structuring 4 1 3 Process analysis 3 2 4 Data analysis 3 2 5 Logical design 50 3,4 6 Physical design 5 5 7 Implementation 6 6 c. Using the information from part b, prepare a network diagram. Identify the critical path.arrow_forward2. UNIX Shell and History Feature [20 points] This question consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. A shell interface gives the user a prompt, after which the next command is entered. The example below illustrates the prompt osh> and the user's next command: cat prog.c. The UNIX/Linux cat command displays the contents of the file prog.c on the terminal using the UNIX/Linux cat command and your program needs to do the same. osh> cat prog.c The above can be achieved by running your shell interface as a parent process. Every time a command is entered, you create a child process by using fork(), which then executes the user's command using one of the system calls in the exec() family (as described in Chapter 3). A C program that provides the general operations of a command-line shell can be seen below. #include #include #define MAX LINE 80 /* The maximum length command */ { int…arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage