Unit 5: Day 4
Tasks
55.1 Arrays Example 5 - Guitar Store
- See code below
- Includes 2 CHANGE/UPDATE algorithms
- Uses the index value to look up a product
55.2 AP Concepts
- Discuss the following AP Concepts and relate them to our past examples
- AAP-1.D.1 Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation.
- AAP-1.D.2 Data abstractions manage complexity in programs by giving a collection of data a name without referencing the specific details of the representation.
- AAP-1.D.3 Data abstractions can be created using lists.
- AAP-1.D.4 Developing a data abstraction to implement in a program can result in a program that is easier to develop and maintain.
- AAP-1.D.5 Data abstractions often contain different types of elements.
- AAP-1.D.6 The use of lists allows multiple related items to be treated as a single value. Lists are referred to by different names, such as array, depending on the programming language.
Ex 5 Guitar Store
let products = ["guitar", "pick", "strings", "drums", "drum sticks"];
let prices = [350, 0.50, 15.50, 800, 25];
let choice;
let howmany = 0;
function setup() {
let sketch = createCanvas(800, 600);
sketch.parent("myP5canvas");
background(200, 50, 50);
//Change All Algorithm - Add GST to all prices
for (let i = 0; i < products.length; i++) {
prices[i] *= 1.05;
}
//Change Algorithm - Place only products over
//$200 on sale
for (let i = 0; i < products.length; i++) {
if (prices[i] > 200) {
prices[i] *= 0.8;
}
}
}//end setup
function draw() {
textSize(14)
fill(200, 50, 50);
rect(0, 0, width, 260);
fill(255, 255, 0);
for (let i = 0; i < products.length; i++) {
text(i + ". " + products[i] + " $" + prices[i], 50, (100 + i * 20));
}
text("Press the letter b to Buy Now!", 50, 250);
}//end draw
function keyPressed() {
if (key === 'b') {
choice = window.prompt("Type the number of the product you wish to purchase.")
howmany = window.prompt("How many do you wish to buy")
background(200, 50, 50);
text("Your purchase totals: $" + (howmany * prices[choice]), 50, 280);
}
}
55.3 Lists Assign 1 - Superheroes
- Instructions for this assignment can be found in the folder: 1.6 Year 1 - AP Extensions
- This assignment is an extension of the Superheroes example started last class
Continue to Unit 5: Day 5 »