In today's lecture, we try to solve a hairy problem and demonstrate its solution in the Java programming language. We will create and edit our program using jEdit, compile the program from the command prompt using javac, and run the program from the command prompt using the java application launcher. Along the way, you will learn about types, declaring variables, and a model for representing variables in computer memory. You get a chance to apply what you learn to a second problem.
The first practice contest for the York Programming Team is today from 12:30 to 3:00pm in room 1006 of CSE.
The 2010 ACM-ICPC World Finals will be held in Harbin, China.
Estimate the number of hairs on an adult human head.
Assuming that a human head is approximately spherical:
Variables | Values |
diameter | 17 cm |
fraction of head covered by hair | 0.5 |
area covered by hair | 0.5 × π × 17cm × 17cm ≅ 454 square cm |
number of hairs per cm | 15 |
number of hairs per square cm | 15 × 15 = 225 hairs per square cm |
number of hairs on head | 454 × 225 = 102150 |
Several Internet sources state that the average number of hairs on a human head is on the order of 100000.
Solving small problems like estimating the number of hairs on a human head is easy to do in Java. The solution involves only a few numeric variables and some simple arithmetic.
Start jEdit if you have not already done so.
In 1020, your programs will always consist of a single file. The file will define a Java class. For now, you can think of a class as being the smallest building block of a Java program.
You need to give the class a name
to declare a class. The name (or identifier) should be descriptive of the
problem you are trying to solve. We will use the name
HairsOnHead
for our solution. Type the following
into jEdit:
public class HairsOnHead { }
Save the file with the name HairsOnHead.java
in your working folder.
public class HairsOnHead
is called the class headerHairsOnHead
is
a different identifier than Hairsonhead
{
and closing brace }
line up
vertically with the p
.java
main
MethodEvery Java program starts executing in a method called main
.
A method is simply a group of Java statements that have been collected
into one unit that can be re-used over and over again. In 1020, the only
method that you will have to create is the main
method;
however, you will use methods that other programmers have created.
Add the main
method to the HairsOnHead
class
as shown below:
public class HairsOnHead { public static void main(String[] args) { } }
Save the file.
main
method shown above is the
one that is most commonly used
diameter
VariableRecall that our worked solution used a variable to describe the diameter of a human head. The diameter is a numeric value. Let's assume that we're only interested in integer values for the diameter.
In Java, we must declare a variable before we can use it.
Declaring a variable means specifying its type and
its name. For integer numeric values we can use the
type called int
.
Declare a variable of type int
named
diameter
by typing in the following code:
public class HairsOnHead { public static void main(String[] args) { int diameter; } }
int diameter;
Mean?The statement int diameter;
indicates that we want
to associate the name diameter
with a chunk of
memory that holds an int
value. A variable is
the chunk of memory that is reserved for it.
A simple model of computer memory is a column of identical boxes numbered sequentially starting at zero.
0 | ||
1 | ||
2 | ||
¦ | ||
¦ | ||
100 | ||
101 | ||
102 | ||
103 | ||
104 | ||
¦ | ||
¦ | ||
500 |
Each box holds a fixed amount called a byte of memory.
It requires 4 bytes to hold an int
in Java.
The statement int diameter;
reserves a block
of 4 bytes and labels the block with the identifier
diameter
.
0 | ||
1 | ||
2 | ||
¦ | ||
¦ | ||
diameter ⇒ | 100 | |
104 | ||
¦ | ||
¦ | ||
500 |
diameter
Now that we have declared a variable named diameter
we need to assign it a value. Because the type of diameter
is int
, we can only assign it integer values.
In Java, the equals symbol =
is used to assign values to
a variable. The symbol =
is called the assignment
operator.
Update your code to assign a value to diameter
:
public class HairsOnHead { public static void main(String[] args) { int diameter; diameter = 17; } }
diameter = 17;
means
"the variable named diameter is assigned the value of 17"
int
literal
diameter = 17;
Mean?The statement diameter = 17;
means that you want
to assign the value of 17
to the block of memory (the variable)
with the identifier diameter
.
0 | ||
1 | ||
2 | ||
¦ | ||
¦ | ||
diameter ⇒ | 100 | 17 |
104 | ||
¦ | ||
¦ | ||
500 |
We say that the variable at address 100
has the type int
,
the name diameter
, and the value 17
.
The Java compiler keeps track of all of this information inside of its
symbol table.
fractionCovered
VariableNow we need a variable that describes the fraction of a head
that is covered by hair; this is a real-valued numeric variable.
In Java, a suitable type for representing real-valued numeric
quantities is called double
.
If you know the value of a variable when you want to declare
the variable, you can declare and assign a value to the variable
in one statement. Declare and assign the value of 0.5 to a
variable of type double
named fractionCovered
as shown below:
public class HairsOnHead { public static void main(String[] args) { int diameter; diameter = 17; double fractionCovered = 0.5; } }
double fractionCovered = 0.5;
means
"the variable of type double named fractionCovered is assigned the value of 0.5"
double
literal; the following are also
double
literals with the value of 0.5
0.5d
5e-1
or 5.e-1
or 5.0e-1
(Java notation for 5 × 10-1)5E-1
or 5.E-1
or 5.0E-1
(Java notation for 5 × 10-1)areaCovered
VariableWe can now calculate the area of a head that is covered by hair. The calculation requires the multiplication of some variables we have already declared and assigned. We will declare a variable to hold the value of the calculated area.
In Java, multiplying variables of type int
and
double
is done using the *
symbol. Declare
and assign a variable of type double
named
areaCovered
as shown below:
public class HairsOnHead { public static void main(String[] args) { int diameter; diameter = 17; double fractionCovered = 0.5; double areaCovered = fractionCovered * 3.1415 * diameter * diameter; } }
areaCovered
the
value on the right-hand-side (RHS) of the assignment
operator is evaluated first
fractionCovered * 3.1415 * diameter * diameter
is evaluated first and then assigned to areaCovered
You have now seen all of the techniques you need to complete the solution. Complete the solution as shown below:
public class HairsOnHead { public static void main(String[] args) { int diameter; diameter = 17; double fractionCovered = 0.5; double areaCovered = fractionCovered * 3.1415 * diameter * diameter; int linearDensity = 15; int areaDensity = linearDensity * linearDensity; double numberOfHairs = areaCovered * areaDensity; } }
double
to hold the result; you will see the reason
for doing so in Day 03Each variable of type double
requires 8 bytes of memory.
The complete memory diagram for our main
method is shown
below:
0 | ||
1 | ||
2 | ||
¦ | ||
¦ | ||
diameter ⇒ | 100 | 17 |
fractionCovered ⇒ | 104 | 0.5 |
areaCovered ⇒ | 112 | 453.94675 |
linearDensity ⇒ | 120 | 15 |
areaDensity ⇒ | 124 | 225 |
numberOfHairs ⇒ | 128 | 102138.01875 |
¦ | ||
¦ | ||
500 |
Our program is correct the way it is currently written, but we cannot see the answer to the problem. Java provides a way to send output to a console window. Output the answer by adding the code as shown below:
public class HairsOnHead { public static void main(String[] args) { int diameter; diameter = 17; double fractionCovered = 0.5; double areaCovered = fractionCovered * 3.1415 * diameter * diameter; int linearDensity = 15; int areaDensity = linearDensity * linearDensity; double numberOfHairs = areaCovered * areaDensity; System.out.print("The number of hairs on a human head is "); System.out.println(numberOfHairs); } }
Save the file.
You must compile your program before it can be run. The Java compiler translates your source code program into instructions that the Java virtual machine can understand.
To compile your program, start the Command Prompt program.
Make sure your source code file is in the folder by typing
dir
followed by the Enter key.
Compile your program using the Java compiler
by typing javac HairsOnHead.java
followed by the Enter key.
Run your program using the Java program launcher
by typing java HairsOnHead
followed by the Enter key.
Write a Java program to solve the following problem:
Suppose that 100000 people are tested for an uncommon disease that afflicts approximately 1 in every 5000 people. The medical test is not perfect. There is a 95% chance that the test will come back positive if the person actually has the disease. There is a 1% chance that the test will come back positive if the person does not have the disease.
What percentage of people who test positive for the disease actually have the disease?
main
method?