programming final
TRANSCRIPT
![Page 1: Programming Final](https://reader036.vdocuments.net/reader036/viewer/2022092700/5870df981a28abcf288b469f/html5/thumbnails/1.jpg)
# John Boggs
# Menu-Driven Program
# Yamnel Serra assisted me with the proper code for this program.
# Display the menu.
print '1. Sales Prediction.'
print '2. Kilometer Converter.'
print '3. Book Club points.'
print '4. Calories Burned.'
print '5. Feet to Inches.'
print '6. Speeding Violation Calculator.'
print '7. Lottery Number generator.'
print '8. Sum of Numbers.' print '9.
Backward String.' print '10. Pet
Information.' print
# Prompt the user for a selection.
menu_selection = (raw_input('Select from the menu: '))
while menu_selection.isalpha() ==
True:
print ('Please try again.')
menu_selection = raw_input('Enter only numbers: ')
menu_selection = int(menu_selection)
# Validate the menu selection. while
menu_selection < 1 or menu_selection > 10:
print ('Please try again.')
menu_selection = raw_input('Enter a number 1-10: ')
# Perform the selected operation.
if menu_selection == 1: #
Calculate total profit.
PROFIT_RATE = 0.23
sales = float(raw_input("What is the projected amount of sales? "))
profit = sales * PROFIT_RATE
print("The total profit will be: " + str(profit))
if menu_selection ==
2:
# Convert kilometers to miles. kilometers = (input("How
many kilometers did you travel? ")) miles=kilometers*0.6214
print("You have traveled " + str(miles) + " miles")
if menu_selection == 3:
# Calculate award points.
def
award_points(books):
if books == 0:
return(0) elif books
![Page 2: Programming Final](https://reader036.vdocuments.net/reader036/viewer/2022092700/5870df981a28abcf288b469f/html5/thumbnails/2.jpg)
== 1: return(5)
elif books == 2:
return(15) elif
books == 3:
return(30) else:
return(60)
books = int(raw_input("How many books did you purchase this
month?
"))
points = award_points(books)
print("You have earned " + str(points) + " points.")
if menu_selection == 4: #
Calculate calories burned.
print("How many minutes did you spend on the treadmill?")
calories = int(raw_input("Please enter 10, 15, 20, 25, or 30 ")) *
3.9 print("You burned " + str(calories) + "
calories.")
if menu_selection == 5:
# Convert feet to inches
print("This program will convert feet to inches.")
feet = float(raw_input("Please enter the number of feet: "))
def feetToInches(feet):
NUMBER_INCHES = 12
return(feet * NUMBER_INCHES)
def main(): inches =
feetToInches(feet)
print(str(feet) + " feet is equal to " + str(inches) + "
inches.") main() if menu_selection == 6 :
# Calculate speed.
print("Are you driving a safe speed?")
def safeSpeed(s,
sl): if s <= sl:
print("You are a safe driver!")
elif s > sl:
overSpeed = s - sl
print("Slow down before you crash!" "You are going",
overSpeed, "MPH over the speed limit!")
limit = int(raw_input("Please enter the speed limit: "))
while 20 > limit or limit > 70:
print("The speed limit should be at least 20, but not more than
70.") limit = int(raw_input("Please enter the speed limit
again: "))
speed = int(raw_input("Please enter your speed: "))
safeSpeed(speed, limit)
![Page 3: Programming Final](https://reader036.vdocuments.net/reader036/viewer/2022092700/5870df981a28abcf288b469f/html5/thumbnails/3.jpg)
if menu_selection ==
7:
# Display a winning lottery number
import random
number = [1,2,3,4,5,6,7]
for i in
range(7):
number[i] = random.randint(0, 9)
output = "Your winning lottery number is: "
for i in range
(len(number)):
output = output + str(number[i]) + ","
output = output[:-1]
print(output) if
menu_selection == 8: #
Calculate sum of numbers
try: with
open("numbers.dat") as file:
lst = [] for line in file:
lst.append(int(line))
print(sum(lst))
except
ValueError:
pass
if menu_selection ==
9:
# Display a word backwards.
txt = raw_input("Please enter a word: ")
print ()
print ("This is the backwards text")
print ()
print (''.join((txt[i] for i in range(len(txt)-1, -1, -1))))
if menu_selection == 10:
#Display pet information
class Pet(object): def
__init__(self,**kwargs):
self.__dict__.update(kwargs)
def __repr__(self):
return ('Pet(' +
', '.join('%s=%r' % (name, item)
for name,item in self.__dict__.items()) +
')' )
![Page 4: Programming Final](https://reader036.vdocuments.net/reader036/viewer/2022092700/5870df981a28abcf288b469f/html5/thumbnails/4.jpg)
def main(): prompt = 'Please enter
your pets %s: ' mypet =
Pet(name=raw_input(prompt % 'name'),
kind=raw_input(prompt % 'kind'),
age=int(raw_input(prompt % 'age'))
)
print mypet
main()
John Boggs
Test Average program from the Language Companion in Visual Basic (Code):
![Page 5: Programming Final](https://reader036.vdocuments.net/reader036/viewer/2022092700/5870df981a28abcf288b469f/html5/thumbnails/5.jpg)
Test Average program from the Language Companion in Visual Basic (Running Program):
![Page 6: Programming Final](https://reader036.vdocuments.net/reader036/viewer/2022092700/5870df981a28abcf288b469f/html5/thumbnails/6.jpg)
Program 2-2 Sales Prediction (Code):
![Page 7: Programming Final](https://reader036.vdocuments.net/reader036/viewer/2022092700/5870df981a28abcf288b469f/html5/thumbnails/7.jpg)
Program 2-2 Sales Prediction (Running Program):
Program 2-5 from the Java Language Companion:
Code:
import java.util.Scanner;
public class GetInput
{
public static void main(String[]args)
{
Scanner keyboard = new Scanner(System.in);
String name;
double payRate;
int hours;
System.out.print("Enter your name: ");
name = keyboard.nextLine();
System.out.print("Enter your hourly pay rate: ");
payRate = keyboard.nextDouble();
System.out.print("Enter the number of hours worked: ");
hours = keyboard.nextInt();
System.out.println("Here are the values that you entered: ");
System.out.println(name);
![Page 8: Programming Final](https://reader036.vdocuments.net/reader036/viewer/2022092700/5870df981a28abcf288b469f/html5/thumbnails/8.jpg)
System.out.println(payRate);
System.out.println(hours);
}
}
Output:
Enter your name: John
Enter your hourly pay rate: 40
Enter the number of hours worked: 40
Here are the values that you entered:
John
40.0
40
Program 5-2: Calories Burned
Code:
import java.util.Scanner;
public class CalcTotal
{
public static void main(String[]args)
{
Scanner keyboard = new Scanner(System.in);
double calories;
int treadmillMinutes;
final int MAX_VALUE = 30;
for (treadmillMinutes = 10; treadmillMinutes <= MAX_VALUE;
treadmillMinutes = treadmillMinutes + 5)
{
calories = treadmillMinutes * 3.9;
System.out.println("You burned " + calories + "
calories.");
}
}
} Output:
You burned 39.0 calories.
You burned 58.5 calories.
You burned 78.0 calories.
You burned 97.5 calories.
You burned 117.0 calories.