0% found this document useful (0 votes)
44 views3 pages

Taller 1

This document contains 3 Java programs that demonstrate basic operations and comparisons of integers. The first program sums two integers input by the user. The second program calculates the product of three integers input by the user. The third program compares two integers input by the user using equality and relational operators and displays the results. All three programs use JOptionPane to input values from the user and display results. They convert the string inputs to integers, perform the relevant mathematical or comparison operations, and display the output in a message dialog box before terminating the application.

Uploaded by

Matte Ramirez
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views3 pages

Taller 1

This document contains 3 Java programs that demonstrate basic operations and comparisons of integers. The first program sums two integers input by the user. The second program calculates the product of three integers input by the user. The third program compares two integers input by the user using equality and relational operators and displays the results. All three programs use JOptionPane to input values from the user and display results. They convert the string inputs to integers, perform the relevant mathematical or comparison operations, and display the output in a message dialog box before terminating the application.

Uploaded by

Matte Ramirez
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ESCUELA DE ADMINISTRACIN DE NEGOCIOS ESTRUCTURA DE DATOS

Taller 1. Estructuras de Seleccin


// Program 1. Sum of two numbers. // Java packages import javax.swing.JOptionPane; public class Addition { // main method begins execution of Java application public static void main( String args[] ) { String firstNumber; // first string entered by user String secondNumber; // second string entered by user int number1; int number2; int sum; // first number to add // second number to add // sum of number1 and number2 // program uses JOptionPane

// read in first number from user as a string firstNumber = JOptionPane.showInputDialog( "Enter first integer" ); // read in second number from user as a string secondNumber = JOptionPane.showInputDialog( "Enter second integer" ); // convert numbers from type String to type int number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); // add numbers sum = number1 + number2; // display result JOptionPane.showMessageDialog( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit( 0 ); } // end method main } // end class Addition // terminate application with window

Segundo Programa
// Program 2. Product.java // Calculate the product of three integers. // Java packages import javax.swing.JOptionPane; public class Product { public { int int int int static void main( String args[] ) x; y; z; result; // // // // first number second number third number product of numbers

String xVal; String yVal; String zVal;

// first string input by user // second string input by user // third string input by user

xVal = JOptionPane.showInputDialog( "Enter first integer:" ); yVal = JOptionPane.showInputDialog( "Enter second integer:" ); zVal = JOptionPane.showInputDialog( "Enter third integer:" ); x = Integer.parseInt( xVal ); y = Integer.parseInt( yVal ); z = Integer.parseInt( zVal ); result = x * y * z; JOptionPane.showMessageDialog( null, "The product is " + result ); System.exit( 0 ); } // end method main } // end class Product

Tercer Programa
// Program 3. // Java packages import javax.swing.JOptionPane; public class Comparison { // main method begins execution of Java application public static void main( String args[] ) { String firstNumber; // first string entered by user String secondNumber; // second string entered by user String result; // a string containing the output int number1; int number2; // first number to compare // second number to compare

// read first number from user as a string firstNumber = JOptionPane.showInputDialog( "Enter first integer:" ); // read second number from user as a string secondNumber = JOptionPane.showInputDialog( "Enter second integer:" ); // convert numbers from type String to type int number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); // initialize result to empty String result = ""; if ( number1 == number2 ) result = result + number1 + " == " + number2; if ( number1 != number2 ) result = result + number1 + " != " + number2; if ( number1 < number2 ) result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) result = result + "\n" + number1 + " <= " + number2; if ( number1 >= number2 ) result = result + "\n" + number1 + " >= " + number2; // Display results JOptionPane.showMessageDialog( null, result, "Comparison Results", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end method main } // end class Comparison // terminate application

You might also like