Lab 2 - User Input and Selection Statements CSE 110 Principles of Programming with Java Spring 2021 Due February 7th 2021, 11:59PM Arizona Time 1 Lab Objectives The following objectives will be met at...

1 answer below »
Java Programming Lab Solution (Budget $50)



Lab 2 - User Input and Selection Statements CSE 110 Principles of Programming with Java Spring 2021 Due February 7th 2021, 11:59PM Arizona Time 1 Lab Objectives The following objectives will be met at the end of this lab - • Declare and define variables to store input given by the user • Accept user input using the Scanner class • Use if statements to decide the flow of execution during runtime 1.1 User Input For this lab we will be focussing on practicing how to take user input, flow of execution management during runtime using the selection statement if and using both of the aforementioned concepts in our program successfully. Before we go on to the objectives, a quick reminder on user inputs and variables. Remember that user input or input values given by the user are data of different datatypes. In order to handle and store this incoming data we need some sort of storage containers. This is where variables come in (variables are storage containers of a particular datatype). Why do we need to store user inputs? This is simply so that we can manipulate the value or use them in some computation sometime later in our code. If we don’t store the user input in a variable then we won’t be to able recover and use that value later on. Thus it is important to always declare and define variables of the same datatype as the expected user input. For example, if we wanted to take two user inputs related to the divisor and dividend of a division operation, we would need two floating point variable or two integer variables - one to hold/store the divisor and the other to hold/store the dividend. It is good practice to understand the program requirements, figure out the number of inputs and outputs and then declare (and define) the same number of variables. In case you need more variables to hold intermediate values during computation, you can always add them to your code. To be able to accept user input in JAVA we need to use the Scanner class located in the java.util package. So we need to import this package into our source code file using the import keyword as shown below - import java.util.Scanner; Remember that all import statements need to be at the top of your source code file. Once you have imported the Scanner class into your source code file, you can create objects of it to take user input. Please refer to the Scanner class PDF on Canvas for details and examples. 1.2 Lab Objectives The source code file Lab2.java that you will create in this section, is what you will upload as your submission file to Canvas by the due date for this lab. Please ensure that the source code runs on your machine and produces the correct output as required. Overall Objective: For this lab, we will write a JAVA program to accept two real numbers from the user. If the first number is greater than or equal to the second, then we will display their sum. If the second number is greater than the first then we will display their difference. For this section, you will create a new project in your IDE called Lab2 and create a source file called Lab2.java inside that project. The following requirements must be met to successfully complete this section - Obj.1 [4 points] Declare and define two variables of datatype double. You can define them with names of your choice (make sure they are relevant). Obj.2 [4 points] Declare and define an object of the Scanner class to enable your source code to take user input. Obj.3 [4 points] Accept two real numbers from the user using the Scanner class object and store them in the variables you created earlier. Obj.4 [6 points] Use the selection statement if to decide whether the first user input number is greater than or equal to the second and display their sum. Otherwise display the difference of the two user inputs as your output. You can format the output as you see fit but ensure that it is understandable by the user. Obj.5 [2 points] Close the Scanner class object as discussed in the lectures. Once you are done editing your source code, make sure to save it (save often to prevent loss of data and work) and then compile your source code. The next step is to follow the submission guidelines in Section 2 of this document and turn your lab in. 1 1.3 Comment Header Please include the following comment lines at the top of your Lab2.java file. Make sure you fill in the required fields as well. Listing 1: Comment Header 1 // ================================================ 2 // Lab2 . java 3 // Name : 4 // ASU ID : 5 // Time taken to complete t h i s lab : 6 // ================================================ 2 Submission Guidelines Please follow the guidelines listed below prior to submitting your source code file Lab2.java on Canvas - 1. Make sure that your source code file is named Lab2.java prior to submitting. 2. Make sure that you have completed all five objectives listed in section 1.2. 3. Include the completed comment header shown in section 1.3 at the top of your source code file 4. Submit your Lab2.java file only to the Canvas link for Lab 1 by February 7th 2021, 11:59PM Arizona Time. 3 Grading Rubric As noted in Section 1.2, each of the five objectives have their own points. They are independent of each other and you will be scored for each objective that you complete successfully. Partial points will be awarded for partially completing objectives. 2 Lab Objectives User Input Lab Objectives Comment Header Submission Guidelines Grading Rubric
Answered Same DayFeb 07, 2021

Answer To: Lab 2 - User Input and Selection Statements CSE 110 Principles of Programming with Java Spring 2021...

Neha answered on Feb 07 2021
137 Votes
import java.util.Scanner;
public class Lab2
{
    public static void main(String[] args) {
     Sca
nner input = new Scanner(System.in);
     double number1;
     double number2;
System.out.println("Enter number one");
number1 = input.nextDouble();
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here