Hand Crafting your own program By Eric Davis for CS103.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

Computer and Programming
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Hello, world! Dissect HelloWorld.java Compile it Run it.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
LAB 10.
Computer Programming Lab(4).
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Java Programming: From the Ground Up
Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Variables, Input, and Output. Challenge: ● Ask the user his or her name, and repeat that name to the user ● Pause video and try.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Building Java Programs
Lecture 2: Operations and Data Types
Building Java Programs
SELECTION STATEMENTS (2)
Building Java Programs
Class Examples.
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Chapter 2 Programming Basics.
Java for Beginners University Greenwich Computing At School DASCO
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Variables and Computer Memory
Unit 3: Variables in Java
Building Java Programs Chapter 2
Data Types and Maths Programming Guides.
Random Numbers while loop
Building Java Programs
Building Java Programs
More on iterations using
Presentation transcript:

Hand Crafting your own program By Eric Davis for CS103

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.

General Format of a Program Can be repeated

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

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.

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.

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.

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.

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.

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.

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.

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();... }

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.

The return of the program... public class Product {... productOfUsers = user1*user2*user3;... }

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.

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

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.

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.

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: