hand crafting your own program by eric davis for cs103

19
Hand Crafting your own program By Eric Davis for CS103

Upload: hannah-barton

Post on 18-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Hand Crafting your own program By Eric Davis for CS103

Hand Crafting your own program

By Eric Davis

for CS103

Page 2: Hand Crafting your own program By Eric Davis for CS103

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.

Page 3: Hand Crafting your own program By Eric Davis for CS103

General Format of a Program

<Any Required Headers><Variable Declarations>

<Get User Input><Make Computations on Input><Output Results>

<Any Required Closing stuff>

Can be repeated

Page 4: Hand Crafting your own program By Eric Davis for CS103

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

Page 5: Hand Crafting your own program By Eric Davis for CS103

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.

Page 6: Hand Crafting your own program By Eric Davis for CS103

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.

Page 7: Hand Crafting your own program By Eric Davis for CS103

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.

Page 8: Hand Crafting your own program By Eric Davis for CS103

Our example continued

We need to take in three numbers, so we’ll probably need three number variables.

The problem didn’t say if they were integers or real, so we want to probably go with most general- double.

Page 9: Hand Crafting your own program By Eric Davis for CS103

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.

Page 10: Hand Crafting your own program By Eric Davis for CS103

Starting our programpublic class Product{

public static void main(String []arg){

double user1=0, user2=0, user3=0;double productOfUsers=0;

}}

Required Header Stuff

Variable DeclarationsGood idea toinitialize.

Page 11: Hand Crafting your own program By Eric Davis for CS103

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.

Page 12: Hand Crafting your own program By Eric Davis for CS103

Our program taking form...

public class Product{

...

System.out.println(“Enter first number:”);user1 = SavitchIn.readLineDouble();System.out.println(“Enter second number:”);user2 = SavitchIn.readLineDouble();System.out.println(“Enter last number:”);user3 = SavitchIn.readLineDouble();

...}

Page 13: Hand Crafting your own program By Eric Davis for CS103

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.

Page 14: Hand Crafting your own program By Eric Davis for CS103

The return of the program...public class Product{

...

productOfUsers = user1*user2*user3;

...

}

Page 15: Hand Crafting your own program By Eric Davis for CS103

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.

Page 16: Hand Crafting your own program By Eric Davis for CS103

The end of the program

Public class Product{

...

System.out.println(“The Product is “ + productOfUsers);

...

}

Can be on separate lines, just don’t break up lines in the middle of a string.

Concatenate

Page 17: Hand Crafting your own program By Eric Davis for CS103

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.

Page 18: Hand Crafting your own program By Eric Davis for CS103

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.

Page 19: Hand Crafting your own program By Eric Davis for CS103

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