|
Calculations:
Prerequisites: Read carefully Chapter 2 of your text. Be able to cut, paste, fix, and run Java programs from this lesson.
At the end of this lesson you should:
- Understand how an assignment statement works
- Know and be able to use the basic Arithmatic operators in Java
- Predict output for Java programs using assignment statements
- Understand the Order of Operations and the use of parenthesis
- Produce psuedocode for an arithmetic problem
- Code a Java program using calculations and no control structures.
Let’s take a quick review of the assignment statement. If num1 and num2 are declared integers (int num1; int num2), then the following statement: num1 = num2 + 1 should be read as: Take the right side of the equation, figure it out, and place the result in the container on the left side of the = sign. In this case, add 1 to the value in num2 and place the result in num1. Notice that this is NOT what you think of with the mathematical version on =. In reality, it would be better to have an assignment statement that looked like this: num1 ß num2 + 1; But…. We do not have an easy arrow symbol on the keyboard.
OK.. Now we are ready to write any program that involves input, output, add (and what adding allows us to do), and store. In effect, if the program works from the top to the bottom with no jumping around in the instructions, and the only input and output we need is text or numbers, we can at this point in our Java knowledge, write ANY program we want!
Open a new project as Calcuations and add a new Applet. Name the .java file Calc. Erase the default code and paste this into Calc.java:
import java.awt.*;
import java.applet.*;
public class Calc extends Applet
{
int a = 3;
int b = 4;
double c = 3.5;
public void paint(Graphics g)
{
a = b + (int)c - a;
c = a * b + 1;
b++;
b = b/a+1;
c = a/b;
a--;
g.drawString("a=" + a + " b=" + b + " c=" + c,10,10);
}
}
This program illustrates the use of the assignment statement. Read in your text about the ++ and – statements. Notice that we had to use (int) to add a double and place it in an int variable. Would we have to use (double) before an int variable if we wanted to place it in a double container? Read in your text about the other math operators. Notice that the % operator works only for integers. It is the remainder if you divide. For example, to find 14 % 3 you would divide 3 into 14. It goes in 4 times. Throw the 4 away, you are worried about the left over. In this case, there are 2 left over, and that is the answer. IE: 14 / 3 is 4 with a remainder of 2. The answer is 2. For integer /, you only get the whole number. If num1 and num2 are integers, num1 was assigned a 14 and num2 was assigned a 3, then num1/num2 would equal 4 and would always be an integer. For doubles, the / operator returns a decimal value, so if num1 and num2 were defined as doubles, then num1/num2 would be 4.666666 and would have to be placed in a double variable. Here is another program. Before you run it, predict what the output will be. Run it to see if you were correct. If not, find your error!
import java.awt.*;
import java.applet.*;
public class Calc extends Applet
{
double a = 1.5;
int b = 4;
double c = 3.5;
public void paint(Graphics g)
{
a = 7 % b / 2;
c = a + c * 2;
b = (int) a / b;
b = (int)a +1;
c = a/b;
b++;
g.drawString("a=" + a + " b=" + b + " c=" + c,10,10);
}
}
Note you may need to look at the order of operations section of your textbook. You will need to be able to flawlessly predict the output of programs such as this. Here is your challenge: Make a program with the same outline as the one above. Use 2 integers and 1 double variable (you can use a, b, and c). Write 6 assignment statements where you change the values of the variables. Make sure it works (are you dividing by 0?). Use no more than 3 operators per statement and no more than 14 total. Make sure no variable ever gets above 10. See if you can stump your fellow students! Email your program to everyone in the class via group email. We will have a contest to see who could make the ‘hardest’ working program to successfully predict.
Other Operators:
Besides the normal operators ( +, -, /, *, %, ++, and --) Java gives you another shortcut. Look at the following statement: a = a * 2; Here we are taking a, doubling it and placing it back into the variable a. Java also allows this to be writing as: a *=2; a=a+1 could be: a+=1; a /=7 would be the same as a = a / 7; This is a shortcut I would not encourage you to use when you write code, but you need to be able to read it. Why is it there? It allows you to save a couple of key presses. If computer programmers are anything, they are lazy about hitting keys.. The price for this energy savings, however, is pretty steep. First, it can be confusing. Most importantly, the more ways you have for doing the same task in a language the more difficult it is to learn and maintain the language. For example, a = a + 1, a++, and a + = 2, all do exactly the same thing. You have to learn all 3.. What for?
A good programming language should be Orthoganal, that is, have only 1 way to do any particular task. Java is NOT Orthoganal. Why? First, it comes from C and C++, which were not Orthoganal. The second reason is that there is a difference between a ‘power user’ and a novice or beginning user. The power user uses Java every day. These added ways of doing the same thing are nice for them, because they are just another tool in their toolset and give them, as Tim would say, MORE POWER, Argh,Argh.. Not everyone IS a power user, though. The other problem is that the more ways you allow a programmer to do something, the more ways they WILL do something. This variance makes maintaining the program much more difficult. Think of how much easier it would be if every programmer had to write essentially the same program to solve a given task. For our part, we will try very hard to follow the KISS method of programming: KEEP IT SIMPLE, STUPID… Simple has its advantages, even for the power hungry!
Review the order of operations section of your text. Make sure you use parenthesis so that the user does not HAVE to know this order to be able to figure out what you are doing. For example, write: a = b + ((a * 7) / 2) instead of a = b + a * 7 / 2, even though they do the same thing.
Pseudocode:
Now we can get to the heart of programming! The fundamental skill is to be able to describe HOW to solve a problem. After you can do this talk, it is usually much simpler to translate your algorithm into Java (or any other language, for that matter). Further, this translation process will get MUCH easier as you get more proficient in Java. How do you get good at describing HOW to solve a problem? Like any skill, it takes PRACTICE. Try the following: Using English only, write a set of instructions that:
- Describe precisely how to do long division
- Describe precisely how to add fractions (What is a common denominator?)
- Describe precisely how to get to your house
- Describe precisely how to tie your shoes
- (make up your own tasks and email them to classmates)
Now try this.. After you write up your instructions for how to do your task (an algorithm), give it to a classmate, your study group, or the whole class and see if they can follow your reasoning. Here is the real test, however. See if they can interpret it BADLY. See if they can find a way of interpreting your instructions that does NOT lead to the solution to the problem.. In other words, TRY to be obtuse. This is one of the major problems with programming. We as programmers know what we WANT the computer to do, but the computer is obtuse. It does not always interpret things the way we mean them. We have to write instructions carefully so they must be interpreted correctly. I want you to email each other with both challenges to write pseudocode and with solutions of those challenges. This, again, is the basis of programming, only without having to know any Java at all!
Assignment: Write a Java application to input how many kilometer a person has traveled. Your program will output the number of Miles, Yards, Feet, and Inches that would equal that number of kilometers. Use the following conversion only: 1 kilometer = 3280.8 feet. Use a constant to hold this conversion value. If you add up the inches, feet, yards, and miles in your answer the total should equal exactly the same distance as the kilometers entered by the user. Use the comments and documentation standards discussed in the book. Use no control structures (if, while,..). Start with pseudocode. How would you do this on paper? You will put the pseudocode, code, and the output screen in your portfolio.
|