Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is the solution to the Pythagorean Theorem problem from Assignment 2 of the Stanford CS106A Introduction to Programming Methodology Class
/*
* File: PythagoreanTheorem.java
* Name:
* Section Leader:
* -----------------------------
* This file is the starter file for the PythagoreanTheorem problem.
*/
import acm.program.*;
public class PythagoreanTheorem extends ConsoleProgram {
public void run() {
sayWelcomeMessage();
askUserInput();
}
private void sayWelcomeMessage() {
println( "Enter values to compute the Pythagorian theorem" );
}
private void askUserInput() {
int a = readInt ("a:"); //asks user to enter an integer for a
int b = readInt ("b:"); //asks user to enter an integer for b
double x = (double)a; // converts variable "a" from an integer to a double
double c = Math.sqrt((x*x) + (b*b)); //calculates square root
println("c:"+ c); //displays value as a double
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.