CSE 1310 – Spring 20 Introduction to Programming Lab 3 Assigned: Monday, Feb. 25, 2020 Due: Thursday, March 19, 11:59pm (after Spring Break!) The Lab assignment will be graded out of 100 points. There...


CSE 1310 – Spring 20


Introduction to Programming


Lab 3





Assigned: Monday, Feb. 25, 2020


Due: Thursday, March 19, 11:59pm (after Spring Break!)


The Lab assignment will be graded out of 100 points. There are multiple parts or tasks that make up each Lab.


Instructions about answers document (same as Lab1):


Some tasks ask you to write code, and specify what name to use for the file in NetBeans. You need to use exactly the name that is given (do not change the case, or make any other modification). Remember, the name of the main class must match the filename.


There are further instructions at the bottom (after the questions) about how to save the file from NetBeans in order to be able to turn it in.


For every lab assignment you need to create an answers file. In this answers file you will put in answer any questions that are asked, you will show the output of code that you write and you will reference any code files that you create for a given question. See below for more details about what goes in the answers file.


Your answers document needs to be named with your lastname, your first initial, then LabXanswers. So if your name is Happy Camper, then your Lab 1 file name is CamperHLab1answers.


· The ONLY acceptable file formats are Word document, OpenOffice document, and PDF.


· Put your last name, first name and UTA ID in the file on the first line.


· Label the answers for each question with the number/letter of the question. Separate each answer from the next answer by at least two blank lines.


· Include EVERY question number/letter combination from the assignment in your answers document. If the question is a coding question telling you to save a file, for example some question numbered 17.b), then in your answers document you should have a line like the following for question 17.b):


17.b) Please see file Lab1Part3.java for this question.”


· Put all your question answers the answer document.


· If the lab question asks you to show the output of a doing some particular thing with the code, then you must also put a screenshot of the output in the answer document. For output that takes up more than one screen, make multiple pictures so that every screen is recorded. If you do not include the screenshots in your answer document, then the questions that should have had screenshots will be considered “Not answered” and will be awarded ZERO 0 points.


Each task below will instruct you where to put your answers. If the task says to “Save your program as file
XYZ1234Lab1Task1.java” then this .java file should be turned in as part of the assignment along with the answers file.


Instructions about Java files, i.e. the .java documents (same as Lab1):


Every document that is turned in MUST have your name and your student ID. This means that your .java files must have your name and ID number in the comments at the top of the file. You must put your name and ID just above the @author Javadoc comment that NetBeans puts in your code. This should be just above the class header in the code. Ex:


/**


* Dr. Carter Tiernan


* UTA ID: 1000000000


* @author jcmtiernan


*/


public class JCMT1234Lab1Task1



Every code file should have your initials and the last four digits of your ID number at the beginning of the file name. For example, if my initials are JCMT and the last four digits of my ID are 1234, then the Java code file for my Lab 1 Task 1 would be
JCMT1234Lab1Task1.java. In the questions below, the initials and digits are shown as
XYZ1234
. Every code file name should start with the initials and digits even if not explicitly listed. You will substitute your initials and digits for the
XYZ1234
part of the file name.



Also make sure that you change the comments to describe your own program. You MUST replace the first three lines of comments (the part that starts with “To change”) with something like “CSE1310-005 Lab 3 Part 1” as was specified in the beginning of the Lab1 assignment. After the label @author put your name as specified in the instructions for this lab assignment. We are not using any parameters @param so you can leave that comment as is.



At the comment // under the main function header, REMOVE the statement that starts with “TODO code” and put a one line description of what you are doing. You might type the following for your comment:


// Lab 3 Part X – code to ….




Summary of answer document and .java files prep


1 Create an answers file named with lastname, your first initial, then LabXanswers that is of file type .docx, .pdf, or .odt


2 Put lastname, firstname and UTA ID in the file then put every question number into the file.


3 Make sure to put some answer after every question number. The answer might be a written answer, a pasted pic or screenshot, or a message indicating that the answer is in an external file.


4 Make sure that when you create your Java programs, your program includes your name and UTA ID in the @author block. Also make sure your header comments describe your specific java program and you remove the default messages and put in your own messages.



There is info at the bottom about creating a folder for your assignment files to be turned in and submitting the assignment.


Instructions that apply to all lab questions:

A. For later labs, if the instructions say to use code from previous lab/question then
everything
from previous question should still be in lab except what is explicitly changed in the current question.



B. ** Things not allowed in Dr. Tiernan’s 1310 class in Java programs. {More details about these things are given the posted file “Things Not Allowed”} If you use these elements in your lab assignment Java code, you will get a 0 (zero) for the entire question where you used this element.



1) Cannot use
exit


2) Cannot use
Exception, the general Exception for exception handling


3) Cannot use
throws


4) Cannot use
break
except between case statements in a switch structure


5) Cannot use
continue


6) Cannot use an
intentional infinite loop
such as while (true)






Grading Rubric for Lab 3:






Part 1: 63 points






Part 2: 16 points






Part 3: 21 points





Miscellaneous: If you have questions, e-mail Dr. T and/or the TAs through Canvas.




Lab 3 parts to complete:




Part 1:




1.a)
Create a new Java application called
XYZ1234Lab3Part1a

and copy the code from your Lab2Part1c program into this new application. Your Lab2Part1c program must be doing all of its printing following the instructions as were given.



In this new program we are going to “clean up” the structure of your code from Lab2 Part1c. If your code already meets the requirements below then you won’t have much to do in this question. ;) To ‘clean up’ your code, you will want to make sure that your code meets all of these requirements:


1. You have a working variable that you set initially to the value input for
userDigits
and that you use this working variable to do the calculations for finding the new digit each time. This means that this working variable will get the remainder that is calculated each step. This will allow
userDigits
to keep the same value throughout the program. This variable will be referred to as the
working variable
below. [Your variable can have whatever name you like but use something meaningful so that the TAs and I can understand its use.]


2. You have a single variable that is used to hold the current digit and then each new digit is stored in that same variable when it is calculated, i.e. when the division is performed, the quotient is stored into this variable every time. This means that you will remove any variables like
mill, thou, hund, etc. This new variable will be referred to as the
digit variable
below. [Your variable can have whatever name you like but use something meaningful so that the TAs and I can understand its use.]


3. You have a variable that represents the exponent of 10 for the place value of the current digit that is being worked on (from step 2 above). For example, if you are working on the
userDigits
value of 532 and the current digit is 3, then the current exponent variable value for the 3 should be 1 because 3 is in the tens place which is 101
and the 1 must be printed in the expanded scientific notation. This exponent variable must be used to print the exponent in each print statement. This variable will be referred to as the
exponent variable
below. [Your variable can have whatever name you like but use something meaningful so that the TAs and I can understand its use.]


4. Optionally, you can have a variable that holds the ten to the power value, i.e. the place value. For example, still using 532 and the digit 3, then the exponent is 1 and the place value is 10. You can also calculate the place value from the exponent variable if desired. This value will be referred to as the
place value
below. [Your variable can have whatever name you like but use something meaningful so that the TAs and I can understand its use.]


5. Optionally, you can have a boolean variable to indicate status for printing the plus sign. You must be able to determine whether to print the plus sign only using the
working variable, the
digit, the
exponent, and the
place value. You cannot use the original variables like
mill, thou, hund, etc.


6. Using the three required variables above (and the possible two optionals), you should revise your code such that only these variables are used to print the next expanded term for the output. After printing the first expanded term, your revised code should use the same structures, i.e. the same lines of code, to print each plus sign and expanded term as needed.


7. The output of your program should be the same for
XYZ1234Lab3Part1a
as for your Lab2Part1c program but the code should now meet all the requirements above.



Make sure that all output data is labeled so that a user would know what is represented by each set of data shown in the output. Make sure that your program runs completely without errors and the correct output is produced. Once your program is working correctly, save your working program as
XYZ1234Lab3Part1a.java.

[Don’t forget to put a note in your answer file for this question reminding the grader to look for this .java file to see the code.] {24 points}



Rubric:



Working variable created and used for all the calculated remainder values {4 pts}



Individual variables for mill, thou, hund,
etc. removed {2 pts}



Digit variable created and used for calculations and printing {3 pts}



Exponent variable created {1 pt}



Correct value put into exponent variable at every step {2 pts}



Exponent variable used in printing {2 pts}



Calculation and printing code revised to use only working variable, digit variable, exponent variable, and the optional place value and boolean variables. {5 pts}



No other variables than those mentioned should be used in the code except for the userDigits input variable. {-2 deduction per each other variable in code}



Code for printing pluses and expanded terms is the same structure repeated as needed {5 pts}








1.b)
Create a new Java application called
XYZ1234Lab3Part1b

and copy the code from your Lab3Part1a program into this new application.



In this new program,
XYZ1234Lab3Part1b
, you will be modifying the code in the following ways.


a) Your code from the previous lab was supposed to handle input values up to 7 digits. Your part 1a) code above should still handle 7 digits. For part 1b) your code should now handle positive integer input values that are longer than 7 digits, at least up to 9 digits in length. Remember that you are still using only the variables defined in the part 1a) code. This should probably be a small change in your program.


b) The input for this program is supposed to be a positive integer in the range of 1 to 9 digits. In part 1b) your code should now check to make sure that the input is in the valid range. You will do this checking in two parts.



i. Your code will use a try-catch with the InputMismatchException to handle any input value that is not an integer. The try block should only contain the statements that take in user input but no other code. If the user enters an invalid type of input (double, char, String, etc.) then your program will set
userDigits
= 1000, print a message to the user telling them that there was invalid input data and that the value 1000 is being used, and then continue running. This will handle invalid input data types.



ii. Once the input value has been verified to be an integer by the try-catch, then your code should make sure that the value is between 1 and 999999999. If the value is outside that range, then your program will set
userDigits
= 1000, print a message to the user telling them that there was invalid input data and that the value 1000 is being used, and then continue running. This will handle integer values that are out of range.


Make sure that all output data is labeled so that a user would know what is represented by each set of data shown in the output. Make sure that your program runs completely without errors and the correct output is produced. Once your program is working correctly, save your working program as
XYZ1234Lab3Part1b.java.

[Don’t forget to put a note in your answer file for this question reminding the grader to look for this .java file to see the code.]



{12 points}



Rubric:



Correctly handle integer inputs up to 9 digits in length {2 pts}



Implement try-catch mechanism with
InputMismatchException {2 pts}



Try block contains the input statements only {2 pts}



Catch block handles the incorrect input as defined above {2 pts}



Correctly implement input range checking {2 pts}



Input with incorrect range is handled as described above {2 pts}






1.b2)
Take a set of at least eight screenshots of the complete output of your program
XYZ1234Lab2Part1b.java
with the user input and the program output. Make sure that the output information is understandable to the user. Use these inputs for the required eight runs of your program. You may do additional runs and screenshots if desired. Use each input below for one run of the program:



999999999


14.56


-12


345678


one


0


1


1000000000



Paste the screenshots into your answers document as the answer to this question. {4 pts}




1.c)
Create a new Java application called
XYZ1234Lab3Part1c

and copy the code from your Lab3Part1b program into this new application.



In this new program,
XYZ1234Lab3Part1c
, you will be modifying the code so that the expanded scientific notation is printed in a while loop instead of printed using a bunch of repetitive code. This means that you should remove the repeats of code to just leave one set of the code that was repeated, then put that set of code inside a while loop. You should use one of the variables that you already have in your code to control the while loop. It is OK to have the first expanded term printed before the while loop and then have the plus signs and remaining terms printed by the while loop.




The output of your program should be the same for
XYZ1234Lab3Part1c
as for your Lab3Part1b program but the code should now meet the requirement above.




Make sure that all output data is labeled so that a user would know what is represented by each set of data shown in the output. Make sure that your program runs completely without errors and the correct output is produced. Once your program is working correctly, save your working program as
XYZ1234Lab3Part1c.java.

[Don’t forget to put a note in your answer file for this question reminding the grader to look for this .java file to see the code.] {11 points}



Rubric:



Correctly implement while loop header syntax {3 pts}



Loop header is controlled by one of the existing program variables {3 pts}



Correctly print plus signs and terms inside the while loop {3 pts}



Repeated printing code from earlier Part1b version is removed {2 pts}








1.d)
Give short answers to the questions below: {8 points}



i) How many lines of code were in your Lab3Part1b code? {1 pt}



ii) How many lines of code were in your Lab3Part1c code with the while loop? {1 pt}



iii) Give the percentage change in the size of your code from part 1b to part 1c; i.e. put a sentence basically like: “Part 1c is XX% (larger/smaller) than part 1b.” {3 pts}



iv) Why would it be more beneficial to use a while loop than to use repeated code excluding changes in size of the program? {3 pts}





Part 2:
Answer the questions below. Put the answers in your answers document as specified.




2.a)
Given the code fragment below with nested if statements, rewrite the code to combine all the tests into one if statement with multiple conditionals. Paste (or type) the rewritten code into your answers document. {3 points}




int feb = 28;



if ((year % 4) == 0) // assume that year is an integer with a valid year value



{



if ((year % 100) != 0)



{



System.out.println("This is a leap year");



feb = 29;



}



}




2.a.i)
If the value of year is 2020, what will the value of feb be after this code runs? {1 points}





2.b)
Given the code fragment below with a while loop, rewrite the code to use a for loop. . Paste (or type) the rewritten code into your answers document. {3 points}




int inVal;



// Assume inVal gets a value from the user here – but no code needed



// (you can test your code by just assigning a value to inVal and running it)



int k = inVal;



int result = 0;





while (k > 0)



{



result = result + (2*k-1);



k--;



}





System.out.println("For input of "+inVal+" the result is "+result);




2.b.i)
If the value of inVal is 7, what will be printed after your new code runs? {2 points}





2.c)
Given the code fragment below with nested if else statements, rewrite the code to combine tests into statements with multiple conditionals and reduce the duplication of actions. Paste (or type) the rewritten code into your answers document. {3 points}




if (month == 1)



System.out.println("Winter");



else if (month == 2)



System.out.println("Winter");



else if (month == 3)



System.out.println("Spring");



else if (month == 4)



System.out.println("Spring");



else if (month == 5)



System.out.println("Spring");



else if (month == 6)



System.out.println("Summer");



else if (month == 7)



System.out.println("Summer");



else if (month == 8)



System.out.println("Summer");



else if (month == 9)



System.out.println("Fall");



else if (month == 10)



System.out.println("Fall");



else if (month == 11)



System.out.println("Fall");



else if (month == 12)



System.out.println("Winter");



else



System.out.println("Not a month");




2.c.i)
If the value of month is 7, what will be printed after your new code runs? {1 points}





2.d)
Given the code fragment you just wrote for part 2.c), rewrite the code fragment again to use a switch statement instead of if else statements. Paste (or type) the rewritten code into your answers document. {3 points}





Part 3:
For this problem you are going to use the code you wrote for
Lab2Part2b
and modify how the program gets its input and do some additional actions on the data.




3.a)
Create a new Java application called
XYZ1234Lab3Part3a

and copy the code from your Lab2Part2b program into this new application. If you aren’t happy with your Lab2Part2b, there will be a version of Lab2Part2b posted that you can use for this problem.



In the Lab2Part2b code you read the week’s input values from the user. In this part 3a, you are going to change that code to read the values from an input file. In NetBeans in this project, you will create an input file named
Lab3Part3aInput.txt
. We did file input in class on Feb 20. In the input file you will put 4 lines of data and each line of data will have seven values on it.



In your program create a File variable, connect it to the input file, then connect the input file to the Scanner to use for input. Also declare a boolean variable to use to indicate if the file is found or not. Make sure that you use the try-catch to handle the FileNotFoundException when connecting the File to the Scanner. If the input file is not found, then make sure that the Scanner is connected to the keyboard and make sure the boolean variable value is set to false.



When you are ready to read the data in your code for this part 3a, you will read just the first seven values from the file. After you have the File and Scanner set up, insure that the file was found and then make sure your input statements use the correct Scanner name and then run your program. The program should run the same way as the Lab2Part2b program.



Make sure that all output data is labeled so that a user would know what is represented by each set of data shown in the output. Make sure that your program runs completely without errors and the correct output is produced. Once your program is working correctly, save your working program as
XYZ1234Lab3Part3a.java

and save the input file you created to use with the program.
[Don’t forget to put a note in your answer file for this question reminding the grader to look for the .java file and the input file
Lab3Part3aInput.txt.] {13 points}



Rubric:



Correctly implement the File variable {1 pt}



Correctly connect the File variable to the input file {1 pt}



Correctly implement the try-catch for the FileNotFoundException {2 pts}



Correctly connect the Scanner variable to the File variable {1 pt}



Correctly handle the exception as defined above if the file is not found {2 pts}



Correctly insure that the file exists before trying to read values from the file {2 pts}



Program runs correctly using the first 7 values in the input file {2 pts}



Correct format and acceptable content in input file Lab3Part3aInput.txt {2 pts}




3.b)
Create a new Java application called
XYZ1234Lab3Part3b

and copy the code from your Lab3Part3a program into this new application. In Lab3Part3b, you will now read all the lines of data from the file
Lab3Part3aInput.txt
and process each line of data from the file.



In your code you need to add a loop around the body of the code that will allow your program to read the values from one line, find the average, find the max, and determine if it gets hotter then go back and, if there is more data in the file, read the next seven values and process all of it. At the top of this loop, print a message that a new line of the file is being processed. This loop should continue until there are no more lines in the file. Remember that the TAs may use files with different amounts of data in them do you cannot assume that there are only 4 lines in the file. After all the lines in the file have been processed, print a message saying that all data in the file has been processed.



Make sure that all output data is labeled so that a user would know what is represented by each set of data shown in the output. Make sure that your program runs completely without errors and the correct output is produced. Once your program is working correctly, save your working program as
XYZ1234Lab3Part3b.java

and save the input file you created to use with the program.
[Don’t forget to put a note in your answer file for this question reminding the grader to look for the .java file and the input file
Lab3Part3aInput.txt.] {8 points}



Rubric:



Correctly implement the loop {3 pt}



Correctly test for no more data in the file {3 pts}



Correct output and messages {2 pts}






3.b2)
Take one or more screenshots of the complete output of your program
XYZ1234Lab3Part3b.java
with the input and the program output. Make sure that the output information is understandable to the user. Paste the screenshot(s) into your answers document. {4 pts}




--------------------------------------------------------------------------------------------------------


Suggestions


A. Pay close attention to all requirements on this page, including file names and submission format. Even in cases where the program works correctly, points will be taken off for not following the instructions given on this page (such as wrong file names, wrong compression format for the submitted code, and so on). The reason is that non-compliance with the instructions makes the grading process significantly (and unnecessarily) more time consuming. Contact the instructor or TA if you have any questions.



B. For each new question that requires you to write code, make a new Java project with the exact name that you need to use. When you submit your files the name of the file and name of the class MUST be the same. They are case sensitive. Make sure that they are the same before submitting your assignment. Double check that there are no errors before submission. In case of an error in this situation, 0 points will be awarded.



C. Write your code in small steps. Do NOT try to write 25 lines of code and then start trying to run it. Instead, write a small chunk of code - 5 to 10 lines - then save and run those 10 lines to make sure they work and then continue. If the 5 to 10 lines have any errors, start on the errors closest to the top of the file and fix those first. Once you have made changes to fix ONE error, then run the code again to see if that really did fix the error. Don’t go forward until you have fixed to topmost error. If you can’t figure out how to fix that one – try to comment out the line with the error and then fix the remaining errors in your chunk. Don’t go further until you have fixed what you have so far.



D. If you are working on a project and want to start completely over (which is not always the best idea), copy the “old code” that you do not plan to reuse and save it into a text file in a directory with your other materials for the class. You may discover that you do want to reuse that code and if you have saved it, then you won’t have to rewrite all of it.



Instructions about preparing your lab to turn in (same as Lab1 with some extra suggestions):



How to prepare your assignment to submit

Create a folder for your lab in your CSE 1310 class directory (outside of the Netbeans projects folder) and name the lab folder with LabX, your lastname, then your first initial. [If you don’t have your files organized into folders, I STRONGLY suggest that you start doing this. This is a way to sort the material you keep on your computer and be able to find it later. Check the internet for suggestions on how to do this type of organization.)



As an example, if your name is Happy Camper, then your Lab 1 folder name is Lab1CamperH. Save your CamperHLab1answers file in this folder. Next save all the .java files for the assignment in the folder. Below is how to do that. Once you have put all the needed .java files in your folder, zip the folder to compress it before submitting it (see info further down with submission details).



Saving .java files out of NetBeans

Go to the .java file (the program) in NetBeans that you want to save. Make sure the program is open in the main window and is the program you are looking at. You should see the window tab highlighted for the program you want. For example, if you are saving the .java file for Lab1Part5 you will see the window tab for that file will say lab1part5/Lab1Part5. Make sure your cursor clicks in the editing window of that file.



From that window, go to the File menu and select Save As. This will bring up the directory where the file is currently stored (lab1part5 in NetBeansProjects). Navigate from the current directory to your new folder and save the file in the new folder. This will make a copy of the program in your new folder. You will notice now in NetBeans that the tab on the window of the program has changed. The tab will now say Lab1CamperH/Lab1Part5. This shows that you have made a copy and stored it in the new directory. You should close this window in NetBeans.



NOTE: If you have closed a project in NetBeans and want to go back to it to edit it some more, you can go to the project by going to the Projects tab either in the pane or on the left margin of the window. Then select the folder of the project you want. Open the folder and select Source Packages (might say
src), then select the name of the class (this is the same as the file) you want, then click on the .java file. This will open the program again in the editing window for you to keep working on.



TIP: If you open the .java file that you saved in your folder (Lab1CamperH/Lab1Part5) you can edit it in NetBeans but NetBeans CANNOT run it. This is because NetBeans expects all the files to be in its own folder. What will happen is that you will make changes but when NetBeans runs, it will run the copy that is stored in NetBeans not the copy from your folder so none of your edits will be executed. This is why you should always go back to the files in NetBeans to keep working.



JAVA FILE NOTE: Only the .java files and the answers file should be included in your zipped folder. Do NOT include the entire NetBeans project. You will be penalized if you save the project instead of just the .java file.



How to submit

The assignment should be submitted via Canvas. Submit a ZIPPED directory/folder called lab1LastnameI.zip where “LastnameI” should be replaced with your last name followed by your first initial, ex. my filename would be lab1TiernanC.zip . The file must be ZIPped (not RAR). No other forms of compression accepted. (Contact the instructor or TA if you do not know how to produce .zip files). The zipped directory should contain your answers document and all the Java code files (task1.java, etc).



Summary of submission steps


To create a zipped directory called lab1LastnameI.zip, follow these steps:


1. Create a folder called lab1LastnameI.


2. Copy to that folder all your solutions (your answers file, all your Java files, and any .txt files that were required).


3. Zip that folder. On some Windows systems, you can zip a folder by right-clicking on the folder, and then selecting Send to->Compressed (zipped) folder. On Mac, go to the parent directory of your Lab# directory in a Finder window, click on your lab#LastnameI folder, then select Compress from the list of file actions (under the gear icon).


4. Submit your zipped folder through your Canvas account at the lab assignment. You click on the name of the assignment and then it goes to the screen where you can upload your zipped file. (Assignments are only accepted through Canvas.)


5. Check that what you uploaded is exactly what you want by going into Canvas and then downloading the assignment you just uploaded, unzipping the folder, and checking the contents to make sure that everything you want to be there is present.



Submission checklist (slightly updated for Lab 2)


• Did you answer all of the questions in the lab assignment, putting your answers in your answers document, and did you create all the required .java program files and text files, if any?


· Do all your .java program files run without errors in Netbeans?


· Do all your .java program files run and give correct answers in Netbeans?


• Did you create the answers file with your name and UTA ID in the file, and your answers to non-programming tasks?


· Did you include all the needed screenshots for output making sure that each screenshot was large enough to be read but small enough to keep the answers document from becoming huge?


• Did you put all of the files you created into one folder called lab#LastnameI ?


• Did you zip that folder into a file called lab#LastnameI.zip?


• Did you upload the zipped file to Canvas before the due date and time?


· Did you check what you uploaded to Canvas to make sure it has the desired material in it?


Mar 19, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here