Computer Science 210
Data Structures

Fall 2017, Siena College

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
 * Updated for Siena College, CSIS 210, Fall 2016
 *
 * $Id$
 */

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;
	// instead of printing to the console with System.out.println,
	// we will print our result here with a popup window.  Note the
	// additional import statement at the top to bring in the
	// JOptionPane class
        JOptionPane.showMessageDialog(null, "The sum of the squares of " + num1 
            + " and " + num2 + " is " + sumSq);
    }
    
    public static void main(String[] args) {
    
	// We also use a JOptionPane input dialog box to read in our input.
	// Note that showInputDialog always gives us back a String, so we
	// need to convert that to an int using Integer.parseInt.
        
        // 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);
        
    }
}