| CS 199 | Willamette University | Spring, 2019 |
|
Programming in PHP, Databases with MySQL, and Web Applications |
||
Consider this example from your textbook page 30:
$roll = rand(1, 6);
echo "You rolled a $roll";
if ($roll == 6) {
echo "You win this round!";
}
else {
echo "Sorry, you didn't win this round, better luck next time!";
}
echo "Thanks for playing";
Let's change the game in the following way:
Below we have an example of nested conditional statements:
$grades = rand(40,100);
if($grades >= 60){
if($grades >= 90){
echo "Congratulations! You Aced it!";
}
else{
echo "You passed";
}
}
else {
echo "See me after class";
}
Describe how the program will run when $grades has the value of 87.
Expand upon this snippet of code to have it output different messages for letter grades A-D using conditional statements with the cutoffs: 60, 70, 80, 90.
Based on your past experience with forms,
Refresher from geometry: In order for three numbers to identify the length of sides in a triangle, the sum of any two must be greater than the third:
a + b > c a + c > c b + c > a
Below is a refresher of this concept.
Example A: Consider
For these values, each of the triangle equations are satisfied, so this is a triangle.
Example B: Consider
Here, it is not true that $a + $b > $c, so this is not a triangle.
With this background, write a program to determine whether or not a triangle can be formed , given three sides.
$a = rand(2,20); $b = rand(2,20); $c = rand(2,20);
We have some other operators that help us use more complex conditions.
| Logical Operators | Function | Description |
|---|---|---|
| a && b | and | Both condition a and condition b must be true |
| a || b | or | Either condition a, condition b, or both conditions and b are true |
| a != b | not equal | Value a is not equal to condition b |
| Condition in English | Equivalent Condition in PHP |
|---|---|
| variable $a is between 3 and 10, inclusive | ((3 <= $a) && ($a <= 10)) |
| variable $a is NOT between 3 and 10, inclusive | ( ! ((3 <= $a) && ($a <= 10))) |
| variable $a is less than 3 or greater than 10 (equivalent to test $a is not in the interval) | (($a < 3) || ($a > 10)) |
Driver's insurance can be pricey, especially for young people. Fortunately for college-age students, each year it gets cheaper until your mid 30's, and insurers also offer 'good grades' discounts.
The table below lists some fictional insurance rates based on age:
| Age Group | Average Rate |
|---|---|
| 16-19 | $2,999 |
| 20-24 | $2,040 |
| 25-29 | $1,707 |
| 30-34 | $1,591 |
Assume that the average discount for good grades is 18%, so students who qualify pay 82% of the price listed for their age range,
Checking Prerequisites
Output might be
Success! You can register for CS 495W
or
Sorry, you have not fulfilled the prerequisites for this course
ENVS-120, Social Systems and the Environment, is only offered to first-year and second-year students who are environmental science or archaeology majors. Edit your code from part a. in order to fit the new parameters:
$class is firstYear or secondYear
In digital photography and on Web pages, pictures are divided into pixels, as discussed in the first reading and lab on cascading style sheets. In this setting, colors are seperated into red, green, and blue values, each in the range from 0 to 255.
(0,0,0) is black, white is (255,255,255), (255,0,0) is pure red etc. Here is a table to help you get an understanding of the kinds of colors you can make:
| Color Category | Swatch | RGB Value |
|---|---|---|
| Full Red | (255, 0, 0) | |
| Orange | (234, 137, 0) | |
| Yellow | (234, 234, 0) | |
| Full Green | (0, 255, 0) | |
| Full Blue | (0, 0, 255) | |
| Indigo | (33, 0, 149) | |
| Violet | (117, 0, 153) | |
| Dark red | (106, 0, 0) | |
| White | (255, 255, 255) | |
| Black | (0, 0, 0) |
As you can see, you can customize your color very specifically through using RGB. For more information this website can be helpful:
With this concept of RGB in mind, we can make each of these three values a variable:
$red = rand(0,255); $green = rand(0,255); $blue = rand(0,255);
If we take an example RGB combo (230, 15, 20), its distance from red, rgb(255,0,0), may be found considering the distance of the red, green, and blue values:
255-100 = 155 (red distance) 0-15 = -15 (green distance) 0-20 = -20 (blue distance)
Then these results are squared results and added to get a distance (actually a distance squared): (155)2 + (-15)2 + (-20)2 = 24650
Comparing these computations against red (rgb(255,0,0)), green (rgb(0,255,0)), and blue (rgb(0,0,255)) will indicate what primary color is closest to the given color.
Comparing colors:
(10,40,200)
(127, 127, 127)
255, 10, 200)
(0, 164, 117)
(89, 190, 82)
(15, 15, 200)
Both the previous lab and this lab have provided experience with various forms of if statements and conditions.
Concepts:
Time for reflection:
|
created 4 February 2019 by Zhen Zhen McMahon modest editing 9 February 2019 by Zhen Zhen McMahon and Henry M. Walker |
|