Title: Hand Crafting your own program
1Hand Crafting your own program
2How to make your own programs
- General format of a program.
- How to read a problem.
- What variables do I want?
- Take in user input
- Crunch and compute
- Spit the answer out.
3General Format of a Program
ltAny Required Headersgt ltVariable
Declarationsgt ltGet User Inputgt ltMake
Computations on Inputgt ltOutput Resultsgt ltAny
Required Closing stuffgt
Can be repeated
4How to read a problem.
- When reading a problem, try to extract the
information you need and disregard the other
stuff. - Identifying the important elements of the problem
is the most important step in creating a correct
computer program
5An example problem
- Write a program that reads in three numbers and
output the product of the numbers. - Identify the important words.
- --------------------------------------------------
-----
Write a program that reads in three numbers and
output the product of the numbers.
6Reading the problem
- What it says
- Three Numbers
- Product
- What it might mean
- Need to have three number variables
- Output the multiplication of the three numbers.
7What variables do I want?
- Every program needs some variables.
- Fit the variables to the problem. If you need to
store numbers, you will need ints or doubles,
probably. If you need to store words or letters,
you will need Strings or chars.
8Our example continued
- We need to take in three numbers, so well
probably need three number variables. - The problem didnt say if they were integers or
real, so we want to probably go with most
general- double.
9Our example continued
- The problem also mentioned that we need to output
the product, which is again a number variable. - Since our product will be calculated from the
three doubles, we will probably want this also to
be a double.
10Starting our program
Required Header Stuff
public class Product public static void
main(String arg) double user10, user20,
user30 double productOfUsers0
Variable Declarations
Good idea to initialize.
11Taking in user input
- Comprised of two parts
- Prompting the user for input.
- Taking in the input
- Often this process is repeated multiple times, to
take in multiple pieces of data. It will be in
our example so that we can take in three numbers.
12Our program taking form...
public class Product ... System.out.println(
Enter first number) user1
SavitchIn.readLineDouble() System.out.println(E
nter second number) user2
SavitchIn.readLineDouble() System.out.println(E
nter last number) user3 SavitchIn.readLineDo
uble() ...
13Crunch, compute, calculate
- Once we have the information that we need, we can
start making calculations. - In our example, we now have all the data to make
the product.
14The return of the program...
public class Product ... productOfUsers
user1user2user3 ...
15Show the answer
- Now that the computer has made the calculations
it needs to, we need to have it give the answer
to us humans in some meaningful format. - Usually need to concatenate (glue) some text
strings and some variables together.
16The end of the program
Public class Product ... System.out.println(
The Product is productOfUsers) ...
Concatenate
Can be on separate lines, just dont break up
lines in the middle of a string.
17There can be more than one...
- The solution we created is not the only way of
solving the problem. It can be done with
different algorithms, with fewer variables, etc.
This is just a good general outline of how to
make your own program.
18More examples to try
- Create a program that asks a user for a number of
characters(up to 8) and then takes in that number
of characters from the user(1 per line). The
program then outputs the characters in reverse
order.
19More Examples to try
- Read in a 4-digit number and output each digit on
a separate line (Hint, modulo and division). An
example would be - Enter a number 1324
- 1
- 3
- 2
- 4