First, either create a new project (see question 2) or open an existing project (see question 3). Then simply click on the icon of a running man. Cafe compiles and then runs the program in the project. Notice that if you make any changes to your source code and then rerun the program, Cafe will automatically save your program file before recompiling.
First, type your Java code into the edit window and save it in a .java file on the a: drive. (It doesn't have to be complete; the file just has to exist.) Note that your filename (just the part before the dot) must agree with the program (class) name. Now, select "New" from the "Project" menu. You should now see the ProjectExpress window. There are three steps to perform:
After Cafe is done creating your project, you should see the project name appear in the blue title bar at the top.
Select "Open" from the "Project" menu. Make sure that the a: drive is selected. Double click on the project name.
ccj is a package which comes with our textbook, "Computing Concepts with Java Essentials". It sets up an easy-to-use interface based on Java APIs. You can achieve the same without the package, but to make life easier, for the whole semester, we are going to use this interface. For every Java program you write, put import ccj.*; at the very top.
Links to the files in the ccj package can be found under "Useful Links" on the home page. Download all these files to a separate, previously created, folder named ccj. (Note that the folder name ccj is all lowercase.)
When you compile your Java program, the compiler will look for this folder in the same folder as your source code, so your .java file must be at the same "level" as the ccj folder. Do not save your .java files inside the ccj folder!
The basic structure of all Java programs is the same, so it will save some typing time if you create a template that you can just edit and rename whenever you want to start a new program. Here's a template file that you can download:
We are running Symantic Cafe Version 1.8 in the UITS labs. It may not be possible to purchase this same version anymore. There is a Visual product available at the bookstore, version 3.0, for about $110. The interface is substantially changed.
We do not recommend that you use MS J++. It's complicated and feature-laden and difficult for beginners.
Another possibility is to install JDK, which is freely available from Sun at http://java.sun.com/products/jdk/1.1/. (For Mac users, follow the link to "other ports" from this page.) Be aware that this not a integrated environment, like Cafe. You'll need to use some editor to type in your Java programs and save them in an ASCII file. (You can use the editor that comes with J++ here). Then, in two separate steps, you'll feed this file into the JDK application to compile the program into a .class file, and then execute the bytecode in the resulting .class file.
Unfortunately, we will not be able to advise you on any problems you encounter installing or using any of these products.
You will receive a letter grade for the entire lab. Correctly fulfilling the basic requirements of the assignment earns you a grade of B.
To receive a grade of B+ or better, your programs must be well documented (contain insightful comments), have a consistent and readable layout (e.g. indentation, blank lines), and use variables with meaningful names. They must also incorporate a solution that is straightfoward and efficient.
In general, avoid single letter variable names like x and y. Names like pricePerGallon and numQuarters are far better. Stick with our naming conventions, i.e. all variable names should begin with a lowercase letter.
Make use of blank lines to separate logically equivalent blocks of code. But, do not put a blank line between every single statement! That just makes the whole program harder to read.
This is because of a problem with Cafe on windows. You will need to add the line
Console.in.readLine();
at the end of your main method to keep this from happening.
See the template file Template.java to see
how this is done. NOTE: sometimes you may need two of
these for your program to work correctly.
Nothing is wrong and there is nothing you need to do to fix it! Since a double variable is a container of a fixed size, the computer cannot represent all real values exactly.
For example, you probably already know that the number one-third, when written as a decimal, repeats infinitely (0.3333333333333333...). It is impossible to represent the quantity one-third as a decimal with a fixed number of digits. So, the trailing digits are truncated at some point and you get a close, but not exact, approximation to the number one-third.
You might still ask, "OK, but why do I see this effect with terminating decimals, like the number 87.89? Can't the computer represent this number exactly? After all, it doesn't repeat infinitely."
Numbers in the computer are represented in binary; a base two number system, not a base 10 number system. Some decimal numbers which have a terminating decimal representation in base 10, like 87.89, have a very long or infinite binary representation.
Please note that this discussion of representations is given only as an explanation of the phenomenon. You are not responsible for this, and we will not be concerning ourselves with such details in this class.
Sure thing. The user input is given in green italics.
Enter the number of gallons of gas in the tank: 15.3
Enter the fuel efficiency in miles per gallon: 23.9
Enter the price of gas per gallon: 1.19
You will be able to travel 365.67 miles till you run out of gas.
You will pay $4.979079497907949 to drive 100 miles.
Do not worry about the fact that the dollar amount $4.979079497907949 is given with such precision. Also, you do not have to use the same exact wording in your prompts and your output.
You just need to calculate how much it costs to go 100 miles. You don't have to worry about whether or not there is enough gas in the car to get that far.
Sure thing. The user input is given in green italics.
Enter the amount due: 5.68
Great, now enter the amount received: 10.00
You will receive the following in change:
dollars: 4
quarters: 1
dimes: 0
nickels: 1
pennies: 2
It's up to you. In general, your output should be complete, clearly labeled, and readable. The exact format is your decision. So, you can print out the total amount due back to the customer if you like. The only thing you must display are the different amounts of each monetary unit to be returned.
This may be because you have not properly converted the change into pennies properly. You will need to explicitly convert the whole expression into integer, not just the balance. So something like the following will work:
int integerBalance = (int)(doubleBalance * 100);
Notice the last pair of parentheses.
This is because of floating point error in computers. Basic idea is this. When you subtract one double from another, the result is not absolutely accurate. Subtracting 10 from 10.01 may give you something like 0.009999987 or similar. When you multiply this by 100 and make it into an int, you get 0 instead of a 1.
To get around this, do the multiplication by 100 and casting to int before you calculate the balance, so that the subtraction is on integers, not doubles.