topics loops arrays of int formatted printing

Task 1

File: HW6_Arrays1.java

Topics: Arrays (easy-medium difficulty based on what was covered in class), methods, formatted printing (easy), random generated data.

Objective: Use an array of integers (printing, processing based on individual values in the array). Write a method to fit a given signature (to match the existing method call in
main). See an example of code that generates random integers in a given range.

Copy/paste the program from HW6_Arrays1.java. It is an incomplete program. It uses a method to create an array with random integers between 0 and 100. We will assume these random numbers are grades. Here is a sample run of this program:

Enter the number of students in the class: 7_x000D_
The grades are:_x000D_
Grade | letter_x000D_
Grade counts:_x000D_

Note that the program did generate 7 numbers and stored them in the
grades array in
main (see
grades = getGrades(N);), but they are not printed anywhere.

You need to write code to :

  1. iterate through the array and print each grade in the array. The printed grades must look the same way as in the sample run.
  2. iterate through the array and print print each grade and next to it the corresponding Letter grade based on the standard thresholds: A≥90, 90>B≥80, 80>C≥70, 70>D≥60, F≥0.
  3. count how many of each letter grade there are (how many of the grades would be an A, B,C,D or F) and after that print the counts of each. (You may be able to do this counting in the loop where you print both the grade and the letter, since at that point you know the letter for that grade.

Below is a sample run of how the complete program should behave after you write your code:

---------- Sample run 1:_x000D_
Enter the number of students in the class: 7_x000D_
The grades are:_x000D_
67_x000D_
72_x000D_
93_x000D_
5_x000D_
9_x000D_
54_x000D_
82_x000D_
Grade | letter_x000D_
   67 | D_x000D_
   72 | C_x000D_
   93 | A_x000D_
    5 | F_x000D_
    9 | F_x000D_
   54 | F_x000D_
   82 | B_x000D_
Grade counts:_x000D_
A: 1_x000D_
B: 1_x000D_
C: 1_x000D_
D: 1_x000D_
F: 3_x000D_
_x000D_
---------- Sample run 2:_x000D_
Enter the number of students in the class: 10_x000D_
The grades are:_x000D_
67_x000D_
72_x000D_
93_x000D_
5_x000D_
9_x000D_
54_x000D_
82_x000D_
42_x000D_
84_x000D_
98_x000D_
Grade | letter_x000D_
   67 | D_x000D_
   72 | C_x000D_
   93 | A_x000D_
    5 | F_x000D_
    9 | F_x000D_
   54 | F_x000D_
   82 | B_x000D_
   42 | F_x000D_
   84 | B_x000D_
   98 | A_x000D_
Grade counts:_x000D_
A: 2_x000D_
B: 2_x000D_
C: 1_x000D_
D: 1_x000D_
F: 4_x000D_

Understanding the given code: As you see in the sample runs and you will observe in your own runs, the ‘random’ numbers that are generated, come in the same sequence every time you run the program (e.g. when run with 10 grades, the first 7 are the same as for the run with 7 grades). That happens because the Random generator was given a ‘seed’ number (in this case, 0) in line:

Random rGen = new Random(0);_x000D_

If you want to experiment, remove the 0 (and do not put any other number instead of it). You will get different numbers. However, for the program you submit, keep the 0. That will make all programs have the same behavior and makes grading easier.


Task 2

File: HW6_BillsArr.java
Objective: Practice arrays, extend previous homework work, formatted printing .

Assume you need to write a component of the software for an ATM. The user enters the amount and the ATM has to give bills that total to that amount. We will assume the ATM has unlimited (or sufficiently large) number of each bill. Your program has to calculate how many of each bill the ATM should give. In order to make the program more flexible, your program should also read the number of different values for bills and the actual values. See sample runs below. Directions:

  1. Given starting code: HW6_BillsArr.java. This code compiles and runs.
  2. In order to guide your work, starting code with complete code for main is given. Copy/paste the program from HW6_BillsArr.java. You only need to:
    -write the methods that main calls and
    -uncomment the call to that method in main.
    Make sure the methods you write have the correct signature/header in order to work with the given call in main as is. You should NOT change main in any way other than uncommenting //getBills,//printBillCount(bills, amount); and //printFormatedReceipt(bills, amount);.
  3. Look up the solution for HW3_Bills. You can use your solution or the instructor’s solution in any way you want. The program here is generalizing that solution (or part of it). In particular the work is hardcoded there for some hardcoded values of bills. Here you need to store the values of bills in an array and instead of copy/paste the same work for different bills, put it in a loop so that each iteration of the loop will process one more bill.
  4. For this program there is a Scanner object called kb in main. But it will not be visible in any other method. In order to read the bill values from the user, you will need to create another Scanner object in the method that needs to read from the user. That may seem weird, but it is fine. It works.
  5. getBills reads N bills from the user, saves them in an array (with the highest bill value sorted at index 0) and returns that array.
    You can assume the user gives the bill values in decreasing order and that they will enter as many bill values as needed.
  6. printBillCount – calculates and prints the number of bills of each value to be given. FOcus on the logic for getting the correct bill count here.
  7. printFormatedReceipt method prints the bill value and bill count on 4 reserved spaces.
    Hint: to produce the line of dashes use a string that you build. You can start with as many dashes as needed under the leftmost column. Next, for each iteration of the loop that prints one more bill value, add as many dashes as needed to go under that.
    Since you should already know how to compute the count of bills, for this method you can focus on the formated printing. (You will still need to recalculate the count of bills since this method does not share data with the printBillCount method.)

Reflection question (on limitations of this program): if there is no bill of value 1, can you still give bills to cover any possible amount? E.g. imagine you have only bills of values 10 and 3. Can you think of an amount that you cannot cover exactly using these bills?

Welcome to 1310 ATM. We never run out of money or bills of certain values (unlike stores run out of toilet paper...)_x000D_
Amount of money to be given: 478_x000D_
How many different bill values do you want? N = 5_x000D_
Enter 5 values in decreasing order (from largest to smallest) to be used as bill values_x000D_
bills[0] = 100_x000D_
bills[1] = 50_x000D_
bills[2] = 10_x000D_
bills[3] = 5_x000D_
bills[4] = 1_x000D_
You entered bills: [100, 50, 10, 5, 1]_x000D_
_x000D_
Here are your bills:_x000D_
4 bills of value 100 _x000D_
1 bills of value 50 _x000D_
2 bills of value 10 _x000D_
1 bills of value 5 _x000D_
3 bills of value 1 _x000D_
_x000D_
Here is your receipt:_x000D_
| Bill value   | 100|  50|  10|   5|   1|_x000D_
-----------------------------------------_x000D_
| Num of bills |   4|   1|   2|   1|   3|_x000D_

Sample run 2:

Welcome to 1310 ATM. We never run out of money or bills of certain values (unlike stores run out of toilet paper...)_x000D_
Amount of money to be given: 478_x000D_
How many different bill values do you want? N = 3_x000D_
Enter 3 values in decreasing order (from largest to smallest) to be used as bill values_x000D_
bills[0] = 100_x000D_
bills[1] = 25_x000D_
bills[2] = 1_x000D_
You entered bills: [100, 25, 1]_x000D_
_x000D_
Here are your bills:_x000D_
4 bills of value 100 _x000D_
3 bills of value 25 _x000D_
3 bills of value 1 _x000D_
_x000D_
Here is your receipt:_x000D_
| Bill value   | 100|  25|   1|_x000D_
-------------------------------_x000D_
| Num of bills |   4|   3|   3|_x000D_

Sample run 3 (see any weird choices of bills like 33)

Welcome to 1310 ATM. We never run out of money or bills of certain values (unlike stores run out of toilet paper...)_x000D_
Amount of money to be given: 15_x000D_
How many different bill values do you want? N = 5_x000D_
Enter 5 values in decreasing order (from largest to smallest) to be used as bill values_x000D_
bills[0] = 100_x000D_
bills[1] = 33_x000D_
bills[2] = 20_x000D_
bills[3] = 10_x000D_
bills[4] = 1_x000D_
You entered bills: [100, 33, 20, 10, 1]_x000D_
_x000D_
Here are your bills:_x000D_
0 bills of value 100 _x000D_
0 bills of value 33 _x000D_
0 bills of value 20 _x000D_
1 bills of value 10 _x000D_
5 bills of value 1 _x000D_
_x000D_
Here is your receipt:_x000D_
| Bill value   | 100|  33|  20|  10|   1|_x000D_
-----------------------------------------_x000D_
| Num of bills |   0|   0|   0|   1|   5|_x000D_

EXTRA 1: Can you SAVE the calculated number of bills for each bill value, and reuse it (instead of recalculating them) for
printFormatedReceipt?


Task 3 – Print all substrings

File: HW6_Substrings.java

(Adapted from book problem P4.12, page 190)

Objective: complex decomposition of problem (non-trivial loop setup), string processing, ‘new look’ at formatted printing:
build the formatting string based on
runtime data.

a) (20 points) Copy/paste the program from HW6_Substrings.java and implement the printSubstrings method. It will take as argument one string and print all its substrings, by length as shown in the sample run below. Hints:

  1. Use nested loops to produce the indexes needed to generate the substrings you want. With those indexes, use the substring method to create the substring.
  2. First try to produce the substrings on a specific row. (See the pattern of the indexes that give all the substrings of some length,L.)
  3. Next, see how you can use an outer loop to make the above code (that produces one row) repeat.
  4. First focus on producing the substrings and then on printing the lines around them.
  5. Use formatted printing to reserve as many spaces as the word length and align them to the left. See the sample code for part b) for an example of how to build the formatting string to reserve as many spaces as the word length.
----------------------------- Sample run:_x000D_
This program will generate all the substrings of a given word, in increasing order of substring length._x000D_
_x000D_
Please enter a word or q to quit: elephant_x000D_
e       , l       , e       , p       , h       , a       , n       , t       , _x000D_
el      , le      , ep      , ph      , ha      , an      , nt      , _x000D_
ele     , lep     , eph     , pha     , han     , ant     , _x000D_
elep    , leph    , epha    , phan    , hant    , _x000D_
eleph   , lepha   , ephan   , phant   , _x000D_
elepha  , lephan  , ephant  , _x000D_
elephan , lephant , _x000D_
elephant, _x000D_
Please enter a word or q to quit: cat_x000D_
c  , a  , t  , _x000D_
ca , at , _x000D_
cat, _x000D_
Please enter a word or q to quit: start_x000D_
s    , t    , a    , r    , t    , _x000D_
st   , ta   , ar   , rt   , _x000D_
sta  , tar  , art  , _x000D_
star , tart , _x000D_
start, _x000D_
Please enter a word or q to quit: resume_x000D_
r     , e     , s     , u     , m     , e     , _x000D_
re    , es    , su    , um    , me    , _x000D_
res   , esu   , sum   , ume   , _x000D_
resu  , esum  , sume  , _x000D_
resum , esume , _x000D_
resume, _x000D_
Please enter a word or q to quit: q_x000D_
Bye_x000D_

b) (10 points) Print the substrings like a table as shown below.

Note: if you implemented both part a and part b correct, you can submit just part b) and you get credit for part a) as well (assuming your code is correct).

  1. Use string formatting to make the cells aligned. In particular each cell is as wide as the length of the string plus 2. See the code below for how to do this.
  2. You must match the sample output completely (same number of spaces, dashed lines..).
  3. In order to be able to produce this formatting, you must BUILD the formatting string. For example, for “cat”, instead of hardcoding, “%3s”, you must find the length of string and use string concatenation. That way, when a longer string is given, it will build the formatting string with the correct string length. See what this code does:
        String word = "cool";_x000D_
        int N = word.length();_x000D_
    	String formatStr= "|%-" + N + "s |";  // building a formatting string that reserves as many spaces as the word length_x000D_
    	System.out.printf(formatStr, "c");_x000D_
    	System.out.println();_x000D_
    	System.out.printf(formatStr, "co");_x000D_
    	System.out.println();_x000D_
    	System.out.printf(formatStr, "cool");_x000D_
    	System.out.println();_x000D_
    	
----------------------------- Sample run:_x000D_
This program will generate all the substrings of a given word, in increasing order of substring length._x000D_
_x000D_
Please enter a word or q to quit: cat_x000D_
-------------------_x000D_
| c   | a   | t   |_x000D_
-------------------_x000D_
| ca  | at  |_x000D_
-------------_x000D_
| cat |_x000D_
-------_x000D_
_x000D_
Please enter a word or q to quit: elephant_x000D_
-----------------------------------------------------------------------------------------_x000D_
| e        | l        | e        | p        | h        | a        | n        | t        |_x000D_
-----------------------------------------------------------------------------------------_x000D_
| el       | le       | ep       | ph       | ha       | an       | nt       |_x000D_
------------------------------------------------------------------------------_x000D_
| ele      | lep      | eph      | pha      | han      | ant      |_x000D_
-------------------------------------------------------------------_x000D_
| elep     | leph     | epha     | phan     | hant     |_x000D_
--------------------------------------------------------_x000D_
| eleph    | lepha    | ephan    | phant    |_x000D_
---------------------------------------------_x000D_
| elepha   | lephan   | ephant   |_x000D_
----------------------------------_x000D_
| elephan  | lephant  |_x000D_
-----------------------_x000D_
| elephant |_x000D_
------------_x000D_
_x000D_
Please enter a word or q to quit: barn_x000D_
-----------------------------_x000D_
| b    | a    | r    | n    |_x000D_
-----------------------------_x000D_
| ba   | ar   | rn   |_x000D_
----------------------_x000D_
| bar  | arn  |_x000D_
---------------_x000D_
| barn |_x000D_
--------_x000D_
_x000D_
Please enter a word or q to quit: x_x000D_
-----_x000D_
| x |_x000D_
-----_x000D_
_x000D_
Please enter a word or q to quit: by_x000D_
-----------_x000D_
| b  | y  |_x000D_
-----------_x000D_
| by |_x000D_
------_x000D_
_x000D_
Please enter a word or q to quit: soon_x000D_
-----------------------------_x000D_
| s    | o    | o    | n    |_x000D_
-----------------------------_x000D_
| so   | oo   | on   |_x000D_
----------------------_x000D_
| soo  | oon  |_x000D_
---------------_x000D_
| soon |_x000D_
--------_x000D_
_x000D_
Please enter a word or q to quit: q_x000D_
Bye

#write essay #research paper #blog writing #article writing #academic writer #reflective paper #essay pro #types of essays #write my essay #reflective essay #paper writer #essay writing service #essay writer free #essay helper #write my paper #assignment writer #write my essay for me #write an essay for me #uk essay #thesis writer #dissertation writing services #writing a research paper #academic essay #dissertation help #easy essay #do my essay #paper writing service #buy essay #essay writing help #essay service #dissertation writing #online essay writer #write my paper for me #types of essay writing #essay writing website #write my essay for free #reflective report #type my essay #thesis writing services #write paper for me #research paper writing service #essay paper #professional essay writers #write my essay online #essay help online #write my research paper #dissertation writing help #websites that write papers for you for free #write my essay for me cheap #pay someone to write my paper #pay someone to write my research paper #Essaywriting #Academicwriting #Assignmenthelp #Nursingassignment #Nursinghomework #Psychologyassignment #Physicsassignment #Philosophyassignment #Religionassignment #History #Writing #writingtips #Students #universityassignment #onlinewriting #savvyessaywriters #onlineprowriters #assignmentcollection #excelsiorwriters #writinghub #study #exclusivewritings #myassignmentgeek #expertwriters #art #transcription #grammer #college #highschool #StudentsHelpingStudents #studentshirt #StudentShoe #StudentShoes #studentshoponline #studentshopping #studentshouse #StudentShoutout #studentshowcase2017 #StudentsHub #studentsieuczy #StudentsIn #studentsinberlin #studentsinbusiness #StudentsInDubai #studentsininternational