Lab exercise #3: Getting Input from keyboard
A. Create the LastThreeWords.java program using the BufferedReader Class.
import java.io.*;
public class LastThreeWords{
public static void main (String[] args){
//declares the variable reader as the
BufferedReader reader = new BufferedReader( new InputStreamReader( System.in));
//declares the String variables for the three words
String firstWord = " ";
String secondWord = " ";
String thirdWord = " ";
try{
System.out.print("Enter word1: ");
firstWord = reader.readLine(); //gets the first word
System.out.print("Enter word2: ");
secondWord = reader.readLine(); //gets the second word
System.out.print("Enter word3: ");
thirdWord = reader.readLine(); //gets the third word
}catch( IOException e){
System.out.println("Error in getting input");
//prints the phrase
System.out.println(firstWord+" "+secondWord+" "+thirdWord);
}
B. SimpleCalculator.java using BufferedReader class.
import java.io.*;
public class SimpleCalcIOException {
public static void main(String[] args)throws IOException{
// write your code here
BufferedReader dataln = new BufferedReader(new InputStreamReader(System.in));
int num1 = 0;
int num2 = 0;
int sum = 0;
int diff = 0;
float prod = 0;
float quo = 0;
System.out.println("Please enter first number: ");
num1 = Integer.parseInt(dataln.readLine());
System.out.println("Please enter second number: ");
num2 = Integer.parseInt(dataln.readLine());
sum = num1 + num2;
diff = num1 - num2;
prod = num1 * num2;
quo = num1 / num2;
System.out.println("The sum of two number is: " +sum);
System.out.println("The difference of two number is: " +diff);
System.out.println("The product of two number is: " +prod);
System.out.println("The quotient of two number is: " +quo);
}
c.SimpleCalcSwing.java using Scanner class
import java.util.Scanner;
import java.io.*;
public class SimpleCalcScanner {
public static void main(String[] args)throws IOException {
Scanner scan = new Scanner(System.in);
int sum = 0;
int diff = 0;
float prod = 0;
float quo = 0;
System.out.print("Please enter first number: ");
int num1 = scan.nextInt();
System.out.print("Please enter second number: ");
int num2 = scan.nextInt();
sum = num1 + num2;
diff = num1 - num2;
prod = num1 * num2;
quo = num1 / num2;
System.out.println("The Sum of two numbers is: " +sum);
System.out.println("The Difference of two numbers is: " +diff);
System.out.println("The Product of two numbers is: " +prod);
System.out.println("The Quotient of two numbers is: " +quo);
}
D.LastThreeWords.java using JOptionsPane class
import javax.swing.JOptionPane;
public class LastThreeWords{
public static void main(String[] args){
//gets the first word from the user
String firstWord = JOptionPane.showInputDialog("Enter word1");
//gets the second word from the user
String secondWord = JOptionPane.showInputDialog("Enter word2");
//gets the third word from the user
String thirdWord = JOptionPane.showInputDialog("Enter word3");
//displays the message
JOptionPane.showMessageDialog(null,firstWord+" "+secondWord+" "+thirdWord);
}
E.SimpleCalcSwing.java using JOptionsPane class
import javax.swing.JOptionPane;
import java.io.IOException;
public class Main {
public static void main(String[] args)throws IOException {
int sum = 0;
int diff = 0;
float prod = 0;
float quo = 0;
int num1, num2;
String firstNum = JOptionPane.showInputDialog("Enter first number: ");
num1 = Integer.parseInt(firstNum);
String secondNum = JOptionPane.showInputDialog("Enter second number: ");
num2 = Integer.parseInt(secondNum);
sum = num1 + num2;
diff = num1 - num2;
prod = num1 * num2;
quo = num1 / num2;
JOptionPane.showMessageDialog(null,"Good bye!!!");
JOptionPane.showMessageDialog(null,"The sum of two number is: "+ sum +"\n"+
"The difference of two number is: " + diff +"\n" +
"The product of two number is: " +prod +"\n" +
"The quotient of two number is: " + quo );
JOptionPane.showMessageDialog(null,"Good bye!!!");