Question

In: Computer Science

Begin by creating a Java project with one class – Addition. Start with the class Addition...

Begin by creating a Java project with one class – Addition. Start with the class Addition as shown in Figure 12.2. This program uses dialog boxes for I/O to get two integers and display the result of adding them together. The program should run “as is”. Change the program so that it gets and adds two doubles instead of integers. When that is working get a third double using a dialog box. Add the three doubles together, and display the result in a dialog box.

// Fig. 12.2: Addition.java
// Addition program that uses JOptionPane for input and output.
import javax.swing.JOptionPane;

public class Addition
{
public static void main(String[] args)
{
// obtain user input from JOptionPane input dialogs
String firstNumber =
JOptionPane.showInputDialog("Enter first integer");
String secondNumber =
JOptionPane.showInputDialog("Enter second integer");

// convert String inputs to int values for use in a calculation
int number1 = Integer.parseInt(firstNumber);
int number2 = Integer.parseInt(secondNumber);

int sum = number1 + number2; // add numbers

// display result in a JOptionPane message dialog
JOptionPane.showMessageDialog(null, "The sum is " + sum,
"Sum of Two Integers", JOptionPane.PLAIN_MESSAGE);
}
} // end class Addition

Solutions

Expert Solution

// Fig. 12.2: Addition.java
// Addition program that uses JOptionPane for input and output.

import javax.swing.JOptionPane;

public class Addition {
    public static void main(String[] args) {
// obtain user input from JOptionPane input dialogs
        String firstNumber = JOptionPane.showInputDialog("Enter first integer");
        String secondNumber = JOptionPane.showInputDialog("Enter second integer");
        String thirdNumber = JOptionPane.showInputDialog("Enter third integer");

// convert String inputs to int values for use in a calculation
        double number1 = Double.parseDouble(firstNumber);
        double number2 = Double.parseDouble(secondNumber);
        double number3 = Double.parseDouble(thirdNumber);

        double sum = number1 + number2 + number3; // add numbers

// display result in a JOptionPane message dialog
        JOptionPane.showMessageDialog(null, "The sum is " + sum, "Sum of Three Doubles", JOptionPane.PLAIN_MESSAGE);
    }
} // end class Addition

Related Solutions

in Java please For this assignment you are to write a class that supports the addition...
in Java please For this assignment you are to write a class that supports the addition of extra long integers, by using linked-lists. Longer than what is supported by Java's built-in data type, called long. Your program will take in two strings, consisting of only digits, covert each of them to a linked-list that represents an integer version on that string. Then it will create a third linked-list that represents the sum of both of the linked lists. Lastly, it...
A project consists of two tasks. Task A is scheduled to begin at the start of...
A project consists of two tasks. Task A is scheduled to begin at the start of Week 1 and finish at the end of Week 4. Task B is scheduled to begin at the start of Week 2 and finish at the end of Week 3. The budgeted cost for Task A is $80,000, and for Task B is $40,000. 1. Creat a cost loaded for this project: Round the number to the closest integer number and input it without...
We started creating a Java class for Car. In this lab we are going to complete...
We started creating a Java class for Car. In this lab we are going to complete it in the following steps: 1- First create the Car class with some required instance variables, such make, model, year, price, speed, maxSpeed, isOn, isMoving, and any other properties you like to add. 2- Provide couple of constructors for initializing different set of important properties, such as make, model, year, and price. Make sure that you do not repeat initialization of instance variables in...
android studio Begin a new app and create a Java class for products with data: productName,...
android studio Begin a new app and create a Java class for products with data: productName, productCode and price and use a file of objects to store product data and read back and display on screen. Do this by creating a simple form using EditTexts, buttons or ActionBar items and display output using an Alert.
Start by creating a new Visual Studio solution, but chose a “class library .Net Core” as...
Start by creating a new Visual Studio solution, but chose a “class library .Net Core” as the project type. It will create a file and a class definition in that file.  Rename the file to be ToDo.cs  and accept the suggestion which will also rename that class to be Class Todo { } In that class, create             - a string property called Title             - an int property called  Priority             - a bool property called Complete Also, define one Constructor that takes in one...
Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
Create a project plan on the game or application you are creating. Using java programming. The...
Create a project plan on the game or application you are creating. Using java programming. The project plan should include the following: A description of the game or application The IDE or game engine your plan to use to create the game or app and information on how you are going to develop the game or app If you choose to create a game, how are you going to approach the game design and game development process or if you...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT