The University of Texas at Arlington Loops and Arrays Programming Project

User Generated

xricbegre

Programming

The University of Texas at Arlington

Description

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
The grades are:
Grade | letter
Grade counts:
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:
Enter the number of students in the class: 7
The grades are:
67
72
93
5
9
54
82
Grade | letter
   67 | D
   72 | C
   93 | A
    5 | F
    9 | F
   54 | F
   82 | B
Grade counts:
A: 1
B: 1
C: 1
D: 1
F: 3

---------- Sample run 2:
Enter the number of students in the class: 10
The grades are:
67
72
93
5
9
54
82
42
84
98
Grade | letter
   67 | D
   72 | C
   93 | A
    5 | F
    9 | F
   54 | F
   82 | B
   42 | F
   84 | B
   98 | A
Grade counts:
A: 2
B: 2
C: 1
D: 1
F: 4
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);
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...)
Amount of money to be given: 478
How many different bill values do you want? N = 5
Enter 5 values in decreasing order (from largest to smallest) to be used as bill values
bills[0] = 100
bills[1] = 50
bills[2] = 10
bills[3] = 5
bills[4] = 1
You entered bills: [100, 50, 10, 5, 1]

Here are your bills:
4 bills of value 100 
1 bills of value 50 
2 bills of value 10 
1 bills of value 5 
3 bills of value 1 

Here is your receipt:
| Bill value   | 100|  50|  10|   5|   1|
-----------------------------------------
| Num of bills |   4|   1|   2|   1|   3|
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...)
Amount of money to be given: 478
How many different bill values do you want? N = 3
Enter 3 values in decreasing order (from largest to smallest) to be used as bill values
bills[0] = 100
bills[1] = 25
bills[2] = 1
You entered bills: [100, 25, 1]

Here are your bills:
4 bills of value 100 
3 bills of value 25 
3 bills of value 1 

Here is your receipt:
| Bill value   | 100|  25|   1|
-------------------------------
| Num of bills |   4|   3|   3|
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...)
Amount of money to be given: 15
How many different bill values do you want? N = 5
Enter 5 values in decreasing order (from largest to smallest) to be used as bill values
bills[0] = 100
bills[1] = 33
bills[2] = 20
bills[3] = 10
bills[4] = 1
You entered bills: [100, 33, 20, 10, 1]

Here are your bills:
0 bills of value 100 
0 bills of value 33 
0 bills of value 20 
1 bills of value 10 
5 bills of value 1 

Here is your receipt:
| Bill value   | 100|  33|  20|  10|   1|
-----------------------------------------
| Num of bills |   0|   0|   0|   1|   5|

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:
This program will generate all the substrings of a given word, in increasing order of substring length.

Please enter a word or q to quit: elephant
e       , l       , e       , p       , h       , a       , n       , t       , 
el      , le      , ep      , ph      , ha      , an      , nt      , 
ele     , lep     , eph     , pha     , han     , ant     , 
elep    , leph    , epha    , phan    , hant    , 
eleph   , lepha   , ephan   , phant   , 
elepha  , lephan  , ephant  , 
elephan , lephant , 
elephant, 
Please enter a word or q to quit: cat
c  , a  , t  , 
ca , at , 
cat, 
Please enter a word or q to quit: start
s    , t    , a    , r    , t    , 
st   , ta   , ar   , rt   , 
sta  , tar  , art  , 
star , tart , 
start, 
Please enter a word or q to quit: resume
r     , e     , s     , u     , m     , e     , 
re    , es    , su    , um    , me    , 
res   , esu   , sum   , ume   , 
resu  , esum  , sume  , 
resum , esume , 
resume, 
Please enter a word or q to quit: q
Bye
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";
        int N = word.length();
    	String formatStr= "|%-" + N + "s |";  // building a formatting string that reserves as many spaces as the word length
    	System.out.printf(formatStr, "c");
    	System.out.println();
    	System.out.printf(formatStr, "co");
    	System.out.println();
    	System.out.printf(formatStr, "cool");
    	System.out.println();
    	
----------------------------- Sample run:
This program will generate all the substrings of a given word, in increasing order of substring length.

Please enter a word or q to quit: cat
-------------------
| c   | a   | t   |
-------------------
| ca  | at  |
-------------
| cat |
-------

Please enter a word or q to quit: elephant
-----------------------------------------------------------------------------------------
| e        | l        | e        | p        | h        | a        | n        | t        |
-----------------------------------------------------------------------------------------
| el       | le       | ep       | ph       | ha       | an       | nt       |
------------------------------------------------------------------------------
| ele      | lep      | eph      | pha      | han      | ant      |
-------------------------------------------------------------------
| elep     | leph     | epha     | phan     | hant     |
--------------------------------------------------------
| eleph    | lepha    | ephan    | phant    |
---------------------------------------------
| elepha   | lephan   | ephant   |
----------------------------------
| elephan  | lephant  |
-----------------------
| elephant |
------------

Please enter a word or q to quit: barn
-----------------------------
| b    | a    | r    | n    |
-----------------------------
| ba   | ar   | rn   |
----------------------
| bar  | arn  |
---------------
| barn |
--------

Please enter a word or q to quit: x
-----
| x |
-----

Please enter a word or q to quit: by
-----------
| b  | y  |
-----------
| by |
------

Please enter a word or q to quit: soon
-----------------------------
| s    | o    | o    | n    |
-----------------------------
| so   | oo   | on   |
----------------------
| soo  | oon  |
---------------
| soon |
--------

Please enter a word or q to quit: q
Bye

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

The soluti...


Anonymous
Nice! Really impressed with the quality.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Content

Related Tags