|
Lab1d
You have seen
addition, multiplication and subtraction in the c labs.
There are 2 more integer operations. Let's look at them now
Division - Copy,
paste, compile and run Use 8 and 4 as the integers first time, use 8
and 3 the second time. What is happening here?????
//
integer operations
import
javax.swing.*;
public
class Lab1d1 { public static void
main(String[] args)
{
String number1 = "";
String number2 = "";
number1 =
JOptionPane.showInputDialog("Enter the first integer");
number2=
JOptionPane.showInputDialog("Enter the second
integer");
// declare
integers int n1=
0, n2 = 0;
// change
Strings to integers n1 =
Integer.parseInt(number1);
n2 =
Integer.parseInt(number2);
int answer1 = n1/n2; //
figure out the answer
JOptionPane.showMessageDialog(null,number1+"
/ "+number2+ " = "+answer1);
System.exit(0);
} } |
This is an
example of Integer Division. RULE: When you
divide 2 integers, the answer is always an integer.
|
It is NOT rounded off. You get the
quotient - the answer to a division problem. the
remainder is thrown away.
Try it with
9 and 5 - see if you get these result. |
|
There is an
integer operation to find the remainder. It is called "mod" for modulus
operation. It is an operation on some calculators.
Add this code
before the system exit( 0), compile and run with 9 and
5.
Java uses
the % sysmbol to ask for the remainder.
int answer2 = n1 % n2;
JOptionPane.showMessageDialog(null,number1+" mod "+number2+ " =
"+answer2); |
Mod is used
extensively in games with spinners, dice or deck of cards
|
Arithmetic Operators
for Java
These are the arithmetic
operations built into Java for integer (int) numbers. They are +
(addition), -
(subtraction), *
(multiplication), /
(division), and %
(modulus)
|
Operator |
Use |
Description |
|
+
|
a +
b
|
Add a
and b
|
|
-
|
a - b
|
Subtract b
from a
|
|
*
|
a *
b
|
Multiply a
by b
|
|
/
|
a /
b
|
Divide a
by b
|
|
%
|
a % b
|
Compute the remainder of dividing a
by b
|
|
-
|
-a
|
How to negate a
(take the opposite) |
|
|
Decimal number in java
There are 2 primitive data
types for decimal numbers. Decimals are called
floating point numbers
Floating
Point Primitive Data Types
|
Type
|
Size
|
Range
|
| float |
32 bits |
-3.4E+38 to +3.4E+38 |
| double |
64 bits |
-1.7E+308 to 1.7E+308
|
We will use the double for our decimals
Copy, paste, compile and run
this program with 9 and 5,
12.4 and
1.3 ,
8 and 2.3 It works with integers and
doubles. By the way, what happens if you say 5 divided by 0 ?
//
doubles
import
javax.swing.*;
public
class Lab1d2 { public static void
main(String[] args)
{ String
number1 = ""; String number2 = "";
number1 =
JOptionPane.showInputDialog("Enter the first number ");
number2=
JOptionPane.showInputDialog("Enter the second number
");
// declare doubles
double n1= 0, n2 = 0;
// change Strings to doubles
n1 =
Double.parseDouble(number1);
n2 =
Double.parseDouble(number2);
double answer1 = n1/n2;
JOptionPane.showMessageDialog(null,number1+"
/ "+number2+ " = "+answer1);
System.exit(0);
} } |
|
|
Agreement
Everything must
agree:
If you are using decimals,
use double and Double.parseDouble If you are using integers use
int and Integer.parseInt
If you mix
ints and doubles using the operations
( +, -, *, /, % ) ,
the answer will always be a double
So, why not just use
doubles all the time??? Doubles are tricky. |
|
Tricky doubles
Most of the times we will use
integers in CS1. There are times when we need doubles. Doubles are not as
easy to use as you will see in this example. Strange things happen.
Look at the code to see if you understand what is
happening. then copy, paste and run it.
Run it with the numbers 5 for
nickels and 8 for dimes - looks good -
Right??? Wrong! - run it with 4
nickels and 6 dimes - it doesn't really look like money with
the 0
missing run
it with 5 nickels and 7 dimes
YUK!!!! - totally
weird!!!!
Doubles will do that to you.
Rule for doubles:
When you use doubles, you have to control the
number of decimal places
|
// doubles
can cause troubles
import
javax.swing.*;
public class Lab1d3 {
public static void main (String[]
args)
{
String num1 = "";
String num2= "";
num1 =
JOptionPane.showInputDialog("How many nickles do you have?");
num2=
JOptionPane.showInputDialog("How many dimes do you
have");
// declare integers
int n1= 0, n2 = 0;
// change Strings to
integers n1 =
Integer.parseInt(num1); n2 =
Integer.parseInt(num2);
// formulas
double total = (n1*0.05) +
(n2*0.10); // value of the coins
// all parentheses
use is exactly the same as algebra
int sum = n1+n2; // for the total
number of coins
JOptionPane.showMessageDialog(null,"You
have "+sum+"
coins\n"+ "they
are worth $"+total);
System.exit(0);
}
} |
|
|
How to
control decimal places (round off)
Copy, paste and run this sample. The new
Stuff is in Red
We will discuss what is happening below
this block
|
//
controlling number of decimal places in doubles import
javax.swing.*; import java.text.*; // Decimal
Formatting comes from this import
public
class Lab1d4 { public static void main (String[]
args) { DecimalFormat df2 = new
DecimalFormat("0.00"); DecimalFormat df1
= new
DecimalFormat("0.0");
String
num = "";
num =
JOptionPane.showInputDialog("Enter a decimal number ");
double
n; n = Double.parseDouble(num);
String ans = df2.format(n); //takes the decimal number, rounds
//it
to 0.00 and saves it as a
String
JOptionPane.showMessageDialog(null,num+"
Rounded to two places is "+ ans
); System.exit(0); }
}
|
Try your program with
numbers
like: 134.5678 9 2.5
-2.532
1. This line is calling up the
decimalformat class with the
object
DecimalFormat import
java.text.*;
2. These lines are creating 2
instances of the DecimalFormat
Object DecimalFormat df2 = new
DecimalFormat("0.00"); DecimalFormat
df1 = new DecimalFormat("0.0");
"0.00" means 2 decimal
places "0.0" means 1 decimal place
I use the variable names df1 and
df2. df1 for
decimalformat variable for 1 place,
df2 for
decimalformat variable for 2 places
3. When you format a decimal to a
certain number of decimal places you change it back into a String
so you can never, never do math with that number
unless you parse it again. That is this line
does. String ans
= df2.format(num);
We open a String memory location called
ans, num must be a double
( or an
integer) format
rounds answer off to 2 places and saves it as a String
4. Add this code before the
System.exit(0) to see how the rounding to 1 decimal
place works
String
ans2 = df1.format(n); JOptionPane.showMessageDialog(null,num+"
Rounded to one place is "+ ans2
); |
|
|
We are now going
to "fix" Lab1d3 so that the decimals look good
1. add the import
line.
2. add the
DecimalFormat for 2 places at the top of the main method
3.
total is the only variable that is a double and needs formatting.
After the formula line - add this line String total2 =
df2.format(total);
4. Change
total in the
showMessageDialog to
total2 - try it now with 5 nickles, 7 dimes
New Stuff today:
1. Integer division and
integer mod operation
2. When do you need a
doubles? How to declare a
double double n =
0;
3. Parse a String to get a
double n =
Double.parseDouble(num);
4. Steps to Round off a
double --- import line ---declare the DecimalFormat object ---
use the object to convert a decimal rounded off to a String
|
|
Assignment
|
Lab1d5 - 40
pts
Lab1d4 would be a good template for
this Lab.
On the right you see a sample run
for this lab when you enter 6 as the radius.
give answer before
rounding and after rounding to 2 decimal places.
Use 3.14159 for
pi.
formula area of a circle
= 3.14159 *radius * radius
|
|
|
Lab1d6 - 40
pts
On the right you see a sample run
for this lab. Feel free to change the event - ie car wash,
etc. You pick the template that you want. None of the samples
is perfect.
Enter the amount of money and the
number of people.
Show the answer after the number is
rounded off for money
|
|
|
Lab1d7 - 20
pts
This lab uses only
integers. There is no rounding off. It uses integer
operations.
Lab1d1 will help you.
Here is how I got the
answer

|
|
|