Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

SumOfSquares BlueJ Project

Click here to download a BlueJ project for SumOfSquares.


SumOfSquares Source Code

The Java source code for SumOfSquares is below. Click on a file name to download it.


SumOfSquares.java

/*
 * Example SumOfSquares
 *
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 *
 * $Id: SumOfSquares.java 1995 2012-11-27 01:39:43Z terescoj $
 */

import javax.swing.JOptionPane;

public class SumOfSquares {

    // a method that takes 2 parameters, both numbers, and then computes
    // the sum of their squares and prints a message.
    // num1 and num2 act like variables that are initialized to the first
    // and second parameters in each call to sumSquares from somewhere else
    public static void sumSquares(int num1, int num2) {
        
        int sumSq = num1 * num1 + num2 * num2;
        JOptionPane.showMessageDialog(null, "The sum of the squares of " + num1 
            + " and " + num2 + " is " + sumSq);
    }
    
    public static void main(String[] args) {
    
        // we haven't used JOptionPanes much lately, so let's.
        
        // we need two integers to operate on
        String input = JOptionPane.showInputDialog("Please enter the first number");
        int firstNum = Integer.parseInt(input);
        
        input = JOptionPane.showInputDialog("Please enter the second number");
        int secondNum = Integer.parseInt(input);
        
        // call our method.  The value of firstNum will be used to initialize
        // num1 in the method, secondNum will be used to initialize num2 in 
        // the method
        sumSquares(firstNum, secondNum);
        
    }
}