Convert to JavaScript
Java Conversion/Assignment 1/Assignment 1 Finished Look.PNG
Java Conversion/Assignment 1/Assignment 1.docx
This program is object-based, so in the <head> section of the
HTML file, you create a constructor method for a MovieOrder class
that contains variables for the movie title and movie price. The
constructor method also sets the price to $3.99 or $4.99, depending
on the delivery type requested. Therefore, this method needs to
use a selection structure. It should also call a method for displaying
information about an order.
In the <body> section of the HTML file, your program prompts the
user to enter a number for the movie:
• 1 to order Star Wars
• 2 to order E.T.
• 3 to order Raiders of the Lost Ark
• If the user enters a different number, an “Invalid choice” message
is displayed, and the movie title field is set to an empty string.
(Data validation is discussed in more detail in Chapter 9.)
• If you want to put a line break in a JavaScript prompt, you need to
use a special “newline” character that’s different from the HTML
tag because the prompt string is processed by JavaScript, not
HTML. Declare a constant called NL, set it to "\n", and use it in the
prompt. Here’s the JavaScript code for this prompt:
// Get movie choice from user
choice = prompt("Enter 1 to order STAR WARS," + NL
+ " 2 for E.T., or" + NL
+ " 3 for RAIDERS OF THE LOST ARK",ES);
Based on the numbers entered, the program determines the movie
name by using a selection structure. Next, the program asks the user
for the delivery type:
• S to stream the movie
• D to download the movie for later viewing
Next, the program calls the constructor method to create an object
for the movie order, sending it arguments for the movie title and the
delivery type. Then it calls the method for displaying information
about the order.
Start with a class diagram (see Figure 5-14). The constructor takes
two arguments (for the movie title and delivery type).
MovieOrder
String movieName
Numeric moviePrice
MovieOrder (String name, String deliv)
displayInfo()
Figure 5-14 The class diagram for the MovieOrder class
Now you’re ready to create the pseudocode for the MovieOrder class
and the algorithm for prompting the user, creating the order, and dis-
playing the information. Remember to add blank lines after the code
section in each step.
1. Open a new document in Notepad, and save it as
MovieOrderClass.txt. Enter the documentation lines:
// Program name: Movie Order Class
// Purpose: Create an order from MovieOrder class
// Author: Paul Addison
// Date last modifi ed: 01-Sep-2011
2. Start the class definition and declare the class variables:
Class MovieOrder
// Variables
String movieName // title of movie
Numeric moviePrice // price of movie
3. Enter the constructor method and the method to display
information, and then end the class definition:
// Constructor method for movie order
Constructor Method MovieOrder(String name,
String deliv)
movieName = name
If deliv == "S" Then
moviePrice = 3.99
Else
moviePrice = 4.99
End If
End Method
// Method to display information about order
Method displayInfo()
Display "Movie name: " + movieName
Display "Price: " + moviePrice
End Method
End Class
4. Next, start the pseudocode section for creating a MovieOrder
object by declaring variables and welcoming the user:
Start
// Variables
Numeric choice // # of movie selection
String movie // name of movie
String deliveryType // delivery type (S or D)
// Welcome the user
Display "Welcome to MovieStream!"
5. Get the movie choice and delivery type from the user:
// Get movie choice from user
Display "Enter 1 to order STAR WARS,"
Display " 2 for E.T., or"
Display " 3 for RAIDERS OF THE LOST ARK"
Input choice
// Determine title
If choice == 1 Then
movie = "Star Wars"
Else If choice == 2 Then
movie = "E.T."
Else If choice == 3 Then
movie = "Raiders of the Lost Ark"
Else
movie = ""
Display "Invalid choice."
End If
// Get delivery type from user
Display "Enter S to stream movie"
Display "or D to download: "
Input deliveryType
6. Call the constructor method to create the object and the
method to display information:
// Call constructor method to create order
// Call method to display movie name and price
MovieOrder myOrder = new MovieOrder
(movie, deliveryType)
myOrder.displayInfo()
7. Thank the user and end the program:
// Thank the user
Display "Thank you!"
Stop
8. Save the file again.
Now try converting this pseudocode to JavaScript. Open a new docu-
ment in Notepad, and save it as MovieOrderClass.html. Enter the
JavaScript code, and refer back to the instructions given at the begin-
ning of this Object Lesson if you need a reminder about using the
NL constant in the prompt. When you’re finished, save the file again,
and open it in a browser for testing. Enter 3 for the movie selec-
tion and D for the delivery type. Your browser page should look like
Figure 5-15. If your program doesn’t run correctly, compare it with
MovieOrderClass-solution.html in your student data files.
Java Conversion/Assignment 1/MovieOrderClass2.html
MovieOrder String movieName Numeric moviePrice MovieOrder (String name, String deliv) displayInfo() 1 to order Star Wars 2 to order E.T. 3 to order Raiders of the Lost ArkJava Conversion/Assignment 2/Assignment 2 Finished Look.PNG
Java Conversion/Assignment 2/Assignment 2.docx
Jacqui wants you to enter the daily totals for each employee in a pro-
gram that computes and determines the following statistics:
• Total coupon purchases attributed to each employee
• Name of the employee with the highest coupon purchase total
• Total coupon purchases for all employees
The promotion might be repeated, so she doesn’t want you to lock in
the number of employees. However, you can specify that it’s a five-
day promotion. Here are some guidelines for your program:
• You don’t need to retain every daily amount; just add it to the total
for an employee. You need to set each employee’s total to 0 before
entering the daily amounts.
• You don’t need to retain every employee’s name, just the one with
the highest total.
• You need an outer loop for employees and an inner loop for the
five days of the promotion.
• You don’t know the number of employees, so use a sentinel value
for the outer loop. Remember that you need a priming prompt
and a prompt at the end of the loop. In addition, you need to ask
for the employee’s name before the daily amounts, so the sentinel
should be a string value that can’t be confused with a name. Q for
“quit” will work.
• You know the number of iterations for the inner loop, so a For loop
is suitable.
Discussion: First, you create your algorithm in pseudocode, and then
convert it to JavaScript and test it. Start with the IPO method:
• What outputs are requested?
• Total coupon purchases attributed to each employee (numeric):
empTot
• Name of the employee with the highest sales (string): maxName
• Total coupon sales for all employees (numeric): grandTot
• What inputs do you have available?
• Employee name (string): empName
• Daily coupon sales amounts (numeric): dailyAmt
• What processing is required?
• Add daily amounts to total for each employee.
• After each employee’s five amounts have been entered, display
the employee’s name and total.
• Keep track of the highest total by any employee (numeric):
maxTot. To do this, initialize a variable for the maximum to 0,
compare any new value with it, and replace the variable’s value
with the new value if it’s higher.
• If the employee’s total is the highest so far, retain this amount
and the employee’s name.
• After the numbers for all employees have been entered, display
the grand total and the name of the employee with the highest
total.
1. Open a new document in Notepad, and save it as
frozenRainbowPromotion.txt. Your pseudocode should look
something like the following:
// Program name: Frozen Rainbow Promotion
// Purpose: Compute stats for coupon purchases
// attributed to employees for a 5-day period
// Author: Paul Addison
// Date last modifi ed: 01-Sep-2011
Start
// Declare variables
Declare String empName
// employee name
Declare String maxName
// employee with high total
Declare Numeric dailyAmt
// daily employee amounts
Declare Numeric empTot
// total employee purchases
Declare Numeric maxTot = 0
// highest employee total
Declare Numeric grandTot = 0
// total of all purchases
Declare Numeric dayIndex
// loop index for weekdays
// Display program header,
// prompt for fi rst employee name
Display "Frozen Rainbow Promotion Program"
Display "Enter employee's name or Q to quit: "
Input empName
// Start the outer loop,
// and set employee's total to 0
While empName != "Q"
empTot = 0
// Start the inner loop,
// prompt for daily amounts
// Add each daily amount to total
For dayIndex = 1 to 5
Display "Enter the amount for day: " +
dayIndex
Input dailyAmt
empTot = empTot + dailyAmt
End For
// Display employee's name and total
// Compare total with max total
// If higher,
// replace max total and employee's name
// Add total to grand total
Display empName + ": total is " + empTot
If empTot > maxTot Then
maxTot = empTot
maxName = empName
End If
grandTot = grandTot + empTot
// Prompt for next employee's name, end loop
Display "Enter employee's name or Q to quit: "
Input empName
End While
// Display grand total and display
// name and amount of highest employee
Display "Grand total of coupon purchases: "
+ grandTot
Display "Employee with highest amount: "
+ maxName
Display "Highest amount: " + maxTot
// Thank the user and exit the program
Display "Frozen Rainbow thanks you!"
Stop
2. Now convert the pseudocode to JavaScript. Save the file as
frozenRainbowPromotion.html, and open it in a browser.
Enter the following data:
• First employee: Mandy Lifeboats. Daily amounts: 15, 19,
22, 10, 20
• Second employee: Saul Teasnacks. Daily amounts: 17, 12,
28, 15, 19
• For the third employee, enter Q to quit the program. Your
output should look similar to Figure 6-5. If your program
doesn’t produce the correct output, compare it with frozen-
RainbowPromotion-solution.html in your student data files.
In this Programmer’s Workshop, you have used a loop to accumulate
amounts and determined the maximum amount by comparing each
total with the current maximum. In the Object Lesson, you learn the
difference between variables that each object gets and variables that
occur only once for the class.