Hand Crafting your own program PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Hand Crafting your own program


1
Hand Crafting your own program
  • By Eric Davis
  • for CS103

2
How 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.

3
General 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
4
How 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

5
An 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.
6
Reading 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.

7
What 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.

8
Our 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.

9
Our 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.

10
Starting 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.
11
Taking 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.

12
Our 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() ...
13
Crunch, 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.

14
The return of the program...
public class Product ... productOfUsers
user1user2user3 ...
15
Show 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.

16
The 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.
17
There 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.

18
More 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.

19
More 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
Write a Comment
User Comments (0)
About PowerShow.com