Download presentation
Presentation is loading. Please wait.
Published byHannah Barton Modified over 9 years ago
1
Hand Crafting your own program By Eric Davis for CS103
2
How to make your own programs n General format of a program. n How to read a problem. n What variables do I want? n Take in user input n Crunch and compute n Spit the answer out.
3
General Format of a Program Can be repeated
4
How to read a problem. n When reading a problem, try to extract the information you need and disregard the other stuff. n 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 n What it says –Three Numbers –Product n What it might mean –Need to have three number variables –Output the multiplication of the three numbers.
7
What variables do I want? n Every program needs some variables. Fit the variables to the problem. If you need to store numbers, you will need int s or double s, probably. If you need to store words or letters, you will need String s or char s.
8
Our example continued n 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.
9
Our example continued n 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 double s, we will probably want this also to be a double.
10
Starting our program public class Product { public static void main(String []arg) { double user1=0, user2=0, user3=0; double productOfUsers=0; } Required Header Stuff Variable Declarations Good idea to initialize.
11
Taking in user input n Comprised of two parts: –Prompting the user for input. –Taking in the input n 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(“Enter second number:”); user2 = SavitchIn.readLineDouble(); System.out.println(“Enter last number:”); user3 = SavitchIn.readLineDouble();... }
13
Crunch, compute, calculate n Once we have the information that we need, we can start making calculations. n In our example, we now have all the data to make the product.
14
The return of the program... public class Product {... productOfUsers = user1*user2*user3;... }
15
Show the answer n 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. n 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);... } Can be on separate lines, just don’t break up lines in the middle of a string. Concatenate
17
There can be more than one... n 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: n 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: n 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.