Ahlers Michael Lab 04
Ahlers Michael Lab 04
Note: Complete the lab individually. You can get help from your group members but it is very
important that each student complete the lab themselves using Netbeans. You want to be totally
comfortable with creating java projects in Netbeans.
Learning Goals:
how java’s numeric types, (int, and double), interact with the simple arithmetic operators
(multiplication, addition, modulo, etc.).
code several of our previous pseudocode programs in java!
Take a break and check with your fellow learners. Was everyone able to create the project
files correctly? (If you are taking the course online, be sure to watch the support videos
before you do this lab!)
int:
You can set these to simple integer values 1 5, 13, 24, etc.
double: (The first two should be decimal values with a fractional part: i.e. 1.5, 3.14 etc. The
others will be calculated.)
Note that we do not include the type of variables i.e. int, double, boolean, etc in the name of the
variables. I’m having you do that here so you can observe how the typing effects the arithmetic
operations.
Now provide output statements using your variables for each of these arithmetic operations for
both the int and the double types:
Example:
intSum = intOperandA + intOperandB;
System.out.println(“The sum using ints of “ + intOperandA + “ “ + intOperandB + “ is “ +
intSum);
Do this with the int variables for each of the operations (+ sum, * product, - difference, /
quotient, and % modulo. Notice how we include a space before and after each operator to
increase readability. Get in the habit of typing the code this way.
Now, execute (that is ‘run’) your program. Right click on your Variables.java file in the project
view and choose Run File. Alternatively, you can run the program by clicking on the green
arrow on the top menu bar. You can also run the program using the [shift] + F6 shortcut.
Size the output window so you can see the results of the println statements for these expressions
and grab a screen shot of your desktop that shows this. Alternatively, and what often works
better for me, copy the output window as text and paste it into this document. Be sure when you
do these screen shots or output copies that they clearly show me the work I asked you to do.
[Windows: Alt + printScrn key puts image of desktop in clipboard just use past to insert it here
in to this document.
Did any of these results surprise you? (If not, take another look at the result of int division)
OK complete this part by creating declarations and initializing each of these variables.
Choose meaningful names of your own for your variables and use camel case:
Example:
the cost of my lunch: double myLunchCost = 12.50;
(If you want, you can print these out but I’ll make that optional so you can finish the lab
during the session.)
Get another screen shot or copy of your output window making sure I can read all your
variable declarations.
Part II: Now code these programs. Since we can’t do java input yet, just set the input value as a
literal value in your code. I’ll leave it to you to choose the values. Be sure to use clear and
meaningful variable names with appropriate types! Try to pick values that make it easy for you
to visually confirm the results of your program outputs.
Here is an example:
We did an example where the user enters a number and the program doubles it. Here is the code
that I would put within the main() class of my Netbeans project:
int myFavNum = 23; // Later we would input this but today we simulate that
int doubleNum = 0;
doubleNum = myFavNum * 2;
Create a new Netbeans Project and java main class for each using the specified file names.
Given the price of a purchase the program computes and outputs a 5% sales tax. Be sure
to use output statements that clearly display the price and the computed sales tax. (No
conditions here, the sales tax is always the same rate.)
Screenshots or output copy here from Task 1 (Be sure to include a readable shot of your
programs output for each of these tasks):
Task 2 (3 pts):
Project: YearlyMaintenanceCost
File: YearlyMaintenanceCost.java
A program that uses home maintenance costs for each of the four seasons and returns the
total yearly maintenance costs. Again, be clear in the output by echoing the values for the
seasonal costs when you display the combined total.
A program that starts with a credit card balance of $5000 and then calculates the interest
at a rate of 17%. Assume the user does not make any payments. Display the interest due
after one month and again after two months.
A program that takes a integer (java int) and uses the modulo function to indicate if the
number is odd or even. Since we can’t do input in java yet, just hard code the value for the
input as a variable instantiation.
(Instead of inputing the value which we can’t do yet, set the value in the code.
int numToExamine = 2;
// program will then determine that since 2 mod 2 is 0 that numToExamine is Even.
)
Screenshots or output copy here for Task 4:
PLEASE READ CAREFULLY. NOW THAT WE ARE USING JAVA YOU WILL HAVE
TO SUBMIT ALL THE WORK.
Make sure that you saved your screen shots or output copy in this document and that your file is
correctly named: Lastname_Firstname_Lab_04.docx using your name. Create a new
compressed *.zip archive folder. (Don’t give me any other type of archive, it will be returned to
you ungraded!) Name it: Lastname_Firstname_Lab_04.zip using your name. Place a copy of
each of your entire Netbeans project folder(s) and a copy of this doc file within your .zip archive.
When you submit your lab, upload the complete archive as well as the separate copy of this MS
Word file which will be used for grading your lab. This is very important. If you submit the
word file, I can insert my feedback to you directly in it.
Although I won’t run your code this time, I ask that you give me your entire project every time
so I can run your code or examine it if I need to for grading. If you don’t follow the submission
directions, your work will not be graded.
Use the Canvas Assignment mechanism to submit your two files.
For the Extra credit resubmit both files using the extra credit submission link.
Every assignment for the rest of the course will follow these submission guidelines. Please
get it right!