Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Lab 4 - Iteration Statements CSE 110 Principles of Programming with Java Spring 2021 Due February 14th 2021, 11:59PM Arizona Time 1 Lab Objectives The following objectives will be met at the end of...

1 answer below »
Lab 4 - Iteration Statements
CSE 110 Principles of Programming with Java
Spring 2021
Due Fe
uary 14th 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 use
• Accept user input using the Scanner class
• Use for, while to iterate over source code
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 Scanne
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 Lab3.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 co
ect output as required.
Overall Objective: For this lab, we will write a JAVA program that displays the following series -
• 1, 3, 5, 7, 9, ..., till < n
• 2, 4, 6, 8, 20, ..., till < n
• 0, 1, 1, 2, 3, 5, 8, ... till < n
For this section, you will create a new project in your IDE called Lab4 and create a source file called Lab4.java inside that
project. The following requirements must be met to successfully complete this section -
Obj.1 [1 point] Declare and define one variable of datatype int to hold the value of n.
Obj.2 [5 point] Using a for loop display the series 1,3,5,7,..., till < n.
Obj.3 [5 point] Using a for loop display the series 2,4,6,8,..., till < n.
Obj.4 [5 point] Using a while loop display the series 0,1,1,2,3,5,8,,..., for n terms.
Obj.5 [4 point] Using a for loop display the series 0,1,1,2,3,5,8,,..., for n terms.
A quick note on the series 0,1,1,2,3,5,8,13, ..., till < n. This series is called the Fibonacci series and it has a special property
- the next number in the series is the sum of the previous two. You will start with two numbers 0 and 1. To compute the
third you must find the sum of the first two i.e. 0 + 1 = 1. To compute the fourth, you need to add the second (1) and third
number (1) i.e. 1 + 1 = 2 and so on till you reach the value < n.
Note that when the series is to be displayed till < n, you need to display all the terms in the series whose value is less than
n. For example, if n=10, then the first series would be 1,3,5,7,9; the second would be 2,4,6,8;
Also note that when you need to display n terms of the series, you will be keeping count of the number of terms dis-
played. For example, if n=7, then the third series (Fibonacci series in this case) will be displayed for 7 terms i.e. 0,1,1,2,3,5,8;
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 you
lab in.
1
1.3 Comment Heade
Please include the following comment lines at the top of your Lab3.java file. Make sure you fill in the required fields as well.
Listing 1: Comment Heade
1
================================================
2
Lab4 . 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 Lab4.java on Canvas -
1. Make sure that your source code file is med Lab4.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 Lab4.java file only to the Canvas link for Lab 4 by March 1st 2021, 11:59PM Arizona Time.
3 Grading Ru
ic
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 Heade
    Submission Guidelines
    Grading Ru
ic

Input Mechanism - Scanner Class
CSE 110 Principles of Programming with Java
Spring 2021
This document is meant to be used as a supplement to a text book. This document takes a look at how to use the
Scanner class in JAVA to acquire user input. The examples used in this document are referenced from various textbooks
and online resources. It is advisable to use a text book for a deeper understanding of each concept and for more examples.
1 User Input
In the past few lectures we have taken a look at how to declare and define variables of various datatypes. However, for each
of those examples, it was we, the programmer, who always set the value of those variables. In real world scenarios, more
often than not, user input is required prior to doing any of the assigned tasks. For example consider a simple calculator.
You need to give the numbers to the calculator and assign the operation of choice and only then will it compute the result
for you. In this case, the numbers and operation are both said to be given by the user (user input). If we were to conside
this simple calculator as a JAVA program, then we would need some mechanism or programming construct that allows us
to take inputs from the user. JAVA has multiple pre-defined classes that programmers can take advantage of and get input
from the user. User input can be acquired in the form of keyboard input, mouse movement and so on. In this course, we will
mainly be focussing on acquiring the data (letters and numbers) that user types on their keyboard.
2 Scanner Class
For the duration of this semester, the JAVA class that we will be leveraging to acquire input from the user is called the
Scanner class. It is located within the util package and contains methods and variables (linked to the input stream) which
facilitate acquiring data or input from the user. Since the Scanner class is located in a package that is separate from the
one in which we write our source code it must be imported inside our source code file prior to using it in our own code. The
purpose of importing a class ( or classes or an entire package) within our source file is to be able to access the variables and
methods inside those classes. Packages and importing them are topics that we will take a closer look at once we cover class
and objects later in the semester. For now please think of them as black box constructs that when given an input provide
you with your desired output. In this case, the Scanner class and associated objects are black boxes that allow us to tap in
to the input stream and acquire user input.
To use the Scanner class to get user input in our program we need to create objects of that class. Since we are just starting
out learning how to program in JAVA, we will not dive into the depths of object creation. Instead we will treat objects as
just another variable whose datatype is the class we are dealing with and whose value will always be predefined by a set of
keywords. Objects of classes are in fact variables as well and their datatype is the associated class (remember that classes
are examples of referenced/derived datatypes). We will be going into the details of classes and objects later in the semester.
In this case, the Scanner class is the one whose objects we are interested in. Creating an object is very similar to declaring a
variable of a particular datatype - you need to use the co
ect datatype (in this case the Scanner class) and then give a name
for the object of your choice. When you want to define what the object’s initial value will be, we use the new operator and
then a few keywords (that will be made known to you).
classname objectname = new classname(optional parameters);
With this general syntax in mind, let us try to create an object of the Scanner class called sc . Note here that similar to
naming of
Answered Same Day Feb 28, 2021

Solution

Neha answered on Feb 28 2021
151 Votes

================================================
Lab4 . j a v a
Name :
ASU ID :
Time taken t o c omple te t h i s l a b :30 mins
================================================
import java.util.Scanner;
public class Lab4
{
    public static void main(String[] args) {
     Scanner myObj = new Scanner(System.in);
     int num1 = 0;
int num2 = 1;
        System.out.println("Please enter value of n");
        int n =...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here