Simonefleek / Zybooks

Random Number
0 stars 0 forks source link

Arrays #28

Open Simonefleek opened 2 months ago

Simonefleek commented 2 months ago

7.11 Java example: Annual salary tax rate calculation with arrays

Arrays are useful to process tabular information. Income taxes are based on annual salary, usually with a tiered approach. Below is an example of a simple tax table:

Annual Salary Tax Rate 0 to 20000 10% Above 20000 to 50000 20% Above 50000 to 100000 30% Above 100000 40%

The below program uses an array salaryBase to hold the cutoffs for each salary level and a parallel array taxBase that has the corresponding tax rate.

Run the program and enter annual salaries of 40000 and 60000, then enter 0. Modify the program to use two parallel arrays named annualSalaries and taxesToPay, each with 10 elements. Array annualSalaries holds up to 10 annual salaries entered; array taxesToPay holds up to 10 corresponding amounts of taxes to pay for those annual salaries. Print the total annual salaries and taxes to pay after all input has been processed. Run the program again with the same annual salary numbers as above. Challenge: Modify the program from the previous step to use a 2-dimensional array of 10 elements named salariesAndTaxes instead of two one-dimensional parallel arrays. The 2D array's first column will hold the salaries, the second the taxes to pay for each salary. Note: The calculation is inaccurate to how taxes are formally assessed and is a simplification for educational purposes only.

Simonefleek commented 2 months ago

Sometimes a programmer wishes to maintain a list of items, like a grocery list, or a course roster. An ArrayList is an ordered list of reference type items that comes with Java. Each item in an ArrayList is known as an element. The statement import java.util.ArrayList; enables use of an ArrayList.

The declaration

ArrayList vals = new ArrayList() creates reference variable vals that refers to a new ArrayList object consisting of Integer objects. The ArrayList list size can grow to contain the desired elements. ArrayList does not support primitive types like int, but rather reference types like Integer. A common error among beginners is to declare an ArrayList of a primitive type like int, as in ArrayList myVals, yielding a compilation error: "unexpected type, found : int, required: reference."

ArrayList valsList = new ArrayList();

//Creating space for 3 Integers valsList.add(31); valsList.add(41); valsList.add(59);

System.out.println(valsList.get(1));

// Setting the value of existing elements valsList.set(1, 119);

System.out.println(valsList.get(1));

add() add(element) Create space for and add the element at the end of the list.

// List originally empty valsList.add(31); // List now: 31 valsList.add(41); // List now: 31 4

Simonefleek commented 2 months ago

get() get(index) Returns the element at the specified list location known as the index. Indices start at 0.
// List originally: 31 41 59. Assume x is an int. x = valsList.get(0); // Assigns 31 to x x = valsList.get(1); // Assigns 41 x = valsList.get(2); // Assigns 59 x = valsList.get(3); // Error: No such element

set() set(index, element) Replaces the element at the specified position in this list with the specified element. // List originally: 31 41 59 valsList.set(1, 119); // List now 31 119 59

size() size() Returns the number of list elements.
// List originally: 31 41 59. Assume x is an int. x = valsList.size(); // Assigns x with 3