begins with // and is placed at the end of or immediately before a line of code to clarify the line
multiline comment
provides clarification as well as important information about the program. It may extend over several lines and it begins with /* and ends with */
compiling a program
translates it from Java language statements into Java bytecode
running a program
uses a Java Virtual Machine (JVM) to interpret and execute the bytecode
source code file
a program file written in Java language statements
bytecode file
a file that can be understood by the JVM
syntax
the set of rules that determine whether a particular statement is correctly formulated
semantics
the meaning of a program statement β that is, what action the statement takes
Syntax error
an incorrectly formulated statement that cannot be read by the Java compiler
semantic error
an error in the logic of the program that will cause it to run incorrectly, but will not be detected by the compiler. The program may still run, but will not produce the desired results
data
ways of representing information needed to run the program
methods
sections of code that manipulate information and perform particular actions
variable
a memory location in which a datum, such as an integer or a character, may be stored
algorithm
a step-by-step description of the solution to a problem
pseudocode
a hybrid between English and Java code that does not pay attention to the Java syntax. It makes translating a program into Java code easier
Java code
Text that follows Java syntax and can be compiled by the JVM if written correctly
method definition
the task a particular method is written to perform
method invocation
the calling of a particular method to perform its designated task
Suppose you have a Java program stored in a file named Test.java. Describe the compilation and execution process for this program, naming any other files that would be created.
Suppose N is 5 and M is 3. What value would be reported by the following pseudocode algorithm? In general, what quantity does this algorithm calculate?
0. Write 0 on a piece of paper.
1. If M equals 0, report what's on the paper and stop.
2. Add N to the quantity written on the paper.
3. Subtract 1 from M.
4. Go to step 1.
Puzzle Problem: You are given two different length ropes that have the characteristic that they both take exactly one hour to burn. However, neither rope burns at a constant rate. Some sections of the ropes burn very fast; other sections burn very slowly. All you have to work with is a box of matches and the two ropes. Describe an algorithm that uses the ropes and the matches to calculate when exactly 45 minutes have elapsed.
Light both ends of the first string and one end of the second string at the same time.
---
When the first string is finished burning, light the unlit end of the second string.
---
When the second string is finished burning exactly 45 minutes will have passed.
---
Light both ends of the first string at the same time. #distractor
---
Light one end of the second string. #distractor
---
When the first string is half of itβs original length, light both ends of the second string at the same time. #distractor
Puzzle Problem: A polar bear that lives right at the North Pole can walk due south for one hour, due east for one hour, and due north for one hour, and end up right back where it started. Is it possible to do this anywhere else on earth? Explain.
Yes.
---
Start an hour north from a latitude that has a circumference that will take one hour to traverse.
---
Walk an hour south.
---
Walk due east for an hour, going completely around the circumference, so youβll be at the same place.
---
Walk an hour north.
---
No, the North Pole is the only place where this can be done. #distractor
Puzzle Problem: Lewis Carroll, the author of Alice in Wonderland, used the following puzzle to entertain his guests: A captive monarch weighing 195 pounds, her youngest child weighing 90 pounds, and her eldest weighing 165 pounds, were trapped in a very high tower. Outside their window was a pulley and rope with a basket fastened on each end. They managed to escape by using the baskets and a 75-pound weight they found in the tower. How did they do it? The problem is that anytime the difference in weight between the two baskets is more than 15 pounds, someone might get hurt. Describe an algorithm that gets them down safely.
Lower the weight in the basket. (75/0)
---
Lower the son in the basket. (90/75)
---
Lower the weight in the basket again. (75/0)
---
Put the son with the weight in the basket at the bottom. (0/165)
---
Lower the daughter. (165/165)
---
Lower the weight in the basket a third time. (75/0)
---
Lower the son in the basket again. (90/75)
---
Put the daughter with the son in the basket at the bottom. (0/255)
---
Lower the queen and weight in the basket. (270/255)
---
Keep the weight in the basket. (0/75)
---
Lower the son in the basket a third time. (90/75)
---
Lower the weight in the basket a fourth time. (75/0)
---
Put the son with the weight in the basket at the bottom again. (0/165)
---
Lower the daughter again. (165/165)
---
Lower the weight in the basket a fifth time. (75/0)
---
Lower the son in the basket a fourth time. (90/75)
---
Stay clear of the weight falling as everyone is out of the tower!
Puzzle Problem: Hereβs another Carroll favorite: A farmer needs to cross a river with a fox, a goose, and a bag of corn. Thereβs a rowboat that will hold the farmer and one other passenger. The problem is that the fox will eat the goose if they are left alone on the river bank, and the goose will eat the corn if they are left alone on the river bank. Write an algorithm that describes how they got all across without any losses. One last constraint is that the animals donβt like to stay still; so, prioritize getting the animals to the other side before the corn, if possible.
Farmer and goose row across.
---
Farmer rows back.
---
Farmer and fox row across.
---
Farmer and goose row back.
---
Farmer and corn row across.
---
Farmer rows back again.
---
Farmer and goose row across again.
---
Farmer and fox row back. #distractor
---
Farmer and fox row across again. #distractor
---
Farmer and corn row back. #distractor
---
Farmer and corn row across again. #distractor
Puzzle Problem: Have you heard this one? A farmer lent the mechanic next door a 40-pound weight. Unfortunately, the mechanic dropped the weight and it broke into four pieces. The good news is that, according to the mechanic, it is still possible to use the four pieces to weigh any quantity between one 40 pounds on a balance scale. How much did each of the four pieces weigh? Enter in order of increasing weight, the value of each weight fragment in pounds.
Suppose your younger sibling asks you to show them how to use a pocket calculator so that they can calculate their homework average in their science course. Describe an algorithm that they can use to find the average of 10 homework grades.
Input the grade for the next homework assignment.
---
Type the plus key.
---
If there are more grades to average, go to the beginning.
---
Type the equal key.
---
Type the division key.
---
Input 10.
---
Type the equal key a second time.
---
Read the value on the screen, Thatβs the average.
---
Type the minus key. #distractor
---
Type the times key. #distractor
---
Type the clear key. #distractor
A Caesar cipher is a secret code in which each letter of the alphabet is shifted by N letters to the right, with the letters at the end of the alphabet wrapping around to the beginning. For example, if N is 1, when we shift each letter to the right, the word daze would be written as ebaf. Note that the z has wrapped around to the beginning of the alphabet. Select each of the below algorithms that can be used to create a Caesar encoded message with a shift of 5.
Shift each letter of the message to the right by 5.
Yes, since letters are represented numerically, shifting to the right is equivalent to adding 5 to the value and subtracting 26 if the number is above 26.
Repeat 5 times,
Shift each letter of the message to the right by 1
Yes, this can be done with repetition or a loop.
Shift each letter of the message to the left by 21.
Yes, shifting to the left by 21 (subtract 21 and add 26 if the result is below 1) is the same as shifting to the right by 5
Shift each letter of the message to the right by 7.
No, that will shift too mucn.
Repeat 3 times,
Shift each letter of the message to the left by 7
Yes, this combines a loop, shifting to the left, and the fact that 21 can be factored into 3 and 7.
Shift each letter of the message to the right by
3.
Try again, this will only work if the encoding is a left shift of 3 or a right shift of 23.
Shift each letter of the message to the left by
3.
Try again, this will only work if the encoding is a right shift of 3 or a left shift of 23.
Repeat until the message has real words,
Shift each letter of the message to the left by 1
Yes, this is a brute force solution that stops as soon as it finds a reasonable decoding.
1. Select the smallest encoded word
2. let the shift amount be 0
3. Shift each letter of the message to the left by 1
4. increment the shift amount
5. if the smallest word is not a real word, goto 3
6. Shift the encoded phrase to the left by the shift amount
7. if the encoded phrase isn't only real words, goto 3
Yes, this is a brute force solution, with an optimization to search the smallest word as a first guess, then stops as soon as it finds a reasonable decoding.
Suppose youβre talking to your younger sibling on the phone and they want you to calculate their homework average. All you have to work with is a piece of chalk and a very small chalkboardβbig enough to write one four-digit number. Whatβs more, although your younger sibling knows how to read numbers, they donβt know how to count very well so they canβt tell you how many grades there are. All they can do is read the numbers to you. Describe an algorithm that will calculate the correct average under these conditions.
Divide the chalkboard into two boxes.
---
Write a 0 in each box.
---
Look at chalkboard. (Loop start)
---
Get a grade from your sibling.
---
Add it to the number in the first box.
---
Add 1 to the number in the second box.
---
If there are more grades, go to the "Look at chalkboard" step
---
Divide the number in the first box by the number in the second box.
---
Tell that number to your sibling.
---
Multiply it by the number in the first box. #distractor
---
Subtract the number in the second box from the number in the first box. #distractor
Design and implement a class to represent a geometric rectangle with a given length and width, such that it is capable of calculating the area and the perimeter of the rectangle. A main method for the class is below that should work once your class is fully implemented. (Note the tests will only work if you make all of your variables and return types as double.
Define a Java class, called Patterns, modeled after OldMacDonald, that will print the following patterns of asterisks, one after the other heading down the page:
Challenge: Define a class that represents a Temperature object. It should store the current temperature in an instance variable of type double, and it should have a public constructor Temperature(double t) that initializes the instance variable, and one public method, getTemp(), which returns the value of the instance variable. Use the Riddle class as a model.
Challenge: Define a class named TaxWhiz that computes the sales tax for a purchase. It should store the current tax rate as an instance variable. Following the model of the Riddle class, you can initialize the rate using a TaxWhiz() constructor. This class should have one public method, calcTax(double purchase), which will return a double, whose value is purchases times the tax rate. For example, if the tax rate is 4 percent, 0.04, and the purchase is $100, then calcTax() should return 4.0.
Draw a UML class diagram representing the following class: The name of the class is Circle. It has one attribute, a radius that is represented by a double value. It has one operation, calculateArea(), which returns a double. Its attributes should be designated as private and its method as public.
To represent a triangle we need attributes for each of its three sides and operations to create a triangle, calculate its area, and calculate its perimeter. Draw a UML diagram to represent this triangle.