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 »
Java Programming Lab with Scanner Class Instructions/Guide



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 this lab - • Declare and define variables to store input given by the user • 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 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 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 correct 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 your lab in. 1 1.3 comment header 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 header 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 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 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 consider 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 correct 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 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="" your="" lab="" in.="" 1="" 1.3="" comment="" header="" 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="" header="" 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="" 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="" 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="" consider="" 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="" correct="" 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="">
Answered Same DayFeb 28, 2021

Answer To: Lab 4 - Iteration Statements CSE 110 Principles of Programming with Java Spring 2021 Due February...

Neha answered on Feb 28 2021
136 Votes
// ================================================
// Lab4 . j a v a
// Name :
// ASU ID :
// T
ime 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