Computer Programming1 Computer Science 1 Computer Programming.

Slides:



Advertisements
Similar presentations
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Advertisements

 Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
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.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
LAB 10.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Java Programming: From the Ground Up
Starting Out with Java: From Control Structures through Objects
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Dialog Boxes.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
Java Review if Online Time For loop Quiz on Thursday.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Introduction to Computers and Programming Lecture 7:
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 5.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
JOptionPane. Import javax.swing.JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Loops, Part II IT108 George Mason University. Indefinite Loop Don’t always have access to the number of iterations ahead of time If a condition (user-response,
Chapter 2 Clarifications
CSC111 Quick Revision.
OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS
Repetition-Counter control Loop
Repetition.
Data types, Expressions and assignment, Input from User
Something about Java Introduction to Problem Solving and Programming 1.
While Statement.
Java Fix a program that has if Online time for Monday’s Program
Chapter 4 - Control Structures: Part 1
Starting Out with Java: From Control Structures through Objects
Building Java Programs
Random Numbers while loop
Presentation transcript:

Computer Programming1 Computer Science 1 Computer Programming

2 Variables Variables are definred as follows Variable typeVariable nameValue int x= 5; this is saying “x holds a value of 5” doubley= 2.4; y String subject= “prog”; subject x prog

Computer Programming3 Printing variables System.out.print(x); This prints the value of x, i.e. 5 System.out.print(y); This prints the value of y, i.e. 2.4 System.out.print(subject); This prints the value of subject, i.e. prog

Computer Programming4 We can also declare a variable and initialize it later int x; x =5; Declaring a variable and assigning a value is a statement. Remember all statements end with ;

Computer Programming5 If statements if statements take the following format: if(condition){ statements }// end if statement Note that the body of the if statement is indented

Computer Programming6 if Example public class ifTest{ public static void main(String args[]){ int age = 19; if (age >=18){ System.out.print(“You can vote”); }// end if statement System.exit(0); }

Computer Programming7 if else int age = 19; if(age >=18) Sytem.out.print(“You can vote”); else Sytem.out.print(“You cannot vote”);

Computer Programming8 Braces are needed around the statements in the if when there is more than one line, e.g. if(age >=18){ System.out.println("You can vote"); System.out.print(“Congratulations"); } else{ System.out.println("You cannot vote"); System.out.print(“Sorry!"); }

Computer Programming9 if else if int grade = 55; if (grade >= 80) System.out.print(“Distinction”); else if (grade >= 50) System.out.print(“Pass”); else System.out.print(“Fail”);

Computer Programming10 Inputting data We will look at two ways of Inputting data: 1: JOptionPanes 2: From the DOS prompt

Computer Programming11 JOptionPanes We first need to import the JOptionPane library at the very top of the file: import javax.swing.JOptionPane; Taking an input: Declare a string variable String name; name = JOptionPane.showInputDialog(“Enter name”); System.out.print(name); //prints name to DOS screen

Computer Programming12 JOptionPanes and Numbers When using JOptionPanes to take a number we need to variables, a String and an int String input; int number; This is because everything we enter into a JOptionPane is a string. Therefore if we enter a number (e.g. 5) we need to change it from a string value (“5”) to a number value (5).

Computer Programming13 import javax.swing.JOptionPane; public class Number{ public static void main(String args[]){ String input; int number; input = JOptionPane.showInputDialog(“Enter number”); //convert the string to an int number = Integer.parseInt(input); //print out number System.out.print(“Number is “ + number); System.exit(0); }//end main }//end class

Computer Programming14 Using a JOptionPane to display data JOptionPane.showMessageDialog(null,”The number is “ + number, “Results”,JOptionPane.INFORMATION_MESSAGE); First agruement is always null

Computer Programming15 Entering a number at the command prompt If we are taking a number from the user at the DOS screen we need to import the library: import java.util.Scanner; The only variable needed is an int. int number;

Computer Programming16 import java.util.Scanner; public class Age{ public static void main(String args[]){ Scanner input = new Scanner(System.in); int age; //prompt the user for an input System.out.print(“Enter your age”); age = input.nextInt(); System.out.print(“your age is “ + age); System.exit(0); }

Computer Programming17 For loops When we need to do something a number of times we can use a for loop. E.g. take in 3 numbers and add them up. Without a for loop the code would look like this: System.out.print(“Enter number 1”); number = input.nextInt(); total += number; System.out.print(“Enter number 2”); number = input.nextInt(); total += number; System.out.print(“Enter number 3”); number = input.nextInt(); total += number; System.out.print(“the total is “+total);

Computer Programming18 There is a lot of repetition in this code. However, if we were to use a for loop: int i, total = 0, number; String input; for(i = 1; i<=3 ;i++){ input = JOptionPane.showInputDialog(“enter number”); number = Integer.parseInt(input); total = total + number; }//end for System.out.print(“The total is “+ total);

Computer Programming19 Initialisation condition incrementation for (i=1 ; i <=3; i++){ statements } i starts with a value 1 The condition in the for loop is checked (is I (1) less than or equal to 3). If it is the statements are executed. After the statements are executed i is incremented(1 is added to i) This continues until the condition is no longer met, i.e. until I is no longer less than or equal to 3.

Computer Programming20 While loops A while loop takes the form: while (condition){ statements }

Computer Programming21 While loop examples 1)print values from 1 to 10 i= 1;//initialize i while ( i <=10){ System.out.println(i); i++;// increment i }// end while

Computer Programming22 Take in 5 grades using a while loop import javax.swing.JOptionPane; class grades{ public static void main(String args[]){ String input; int total =0, average, grade, i= 0; //start while loop to take grades while (i<5){ //take in an input input = JOptionPane.showInputDialog("Enter a grade"); //convert input to an int grade = Integer.parseInt(input); //add grade to total total += grade; //increment i i++; }//end while //get average average = total/5; //display results JOptionPane.showMessageDialog(null, "The total of the grades is "+ total + "\nThe average is "+ average ); }//end main }//end class

Computer Programming23 E.g. 2 While a non-digit is entered into a JOptionPane, give the user an error message and get the user to re-enter the input. //take an input from the user input = JOptionPane.showInputDialog(“Enter Number”); //give an error message and take the input again if non-digits entered while(!input.matches(“\\d+”)){ //show an error message JOptionPane.showMessageDialog(null, “Numbers only”); //take in input again input = JOptionPane.showInputDialog(“Enter Number”); }//end while //convert input to an int Number = Integer.parseInt(input);

Computer Programming24 While loop using a sentinel //take unknown amount of grades form a user //and get total of grades and average import javax.swing.JOptionPane; class sentinelTest { public static void main (String args[]){ String input; int grade, total = 0, counter =0; //take input from user input = JOptionPane.showInputDialog("enter a grade, -1 to quit"); //convert input to an int grade = Integer.parseInt(input); //loop to add to total and get another grade until -1 is entered while (grade != -1){ counter++; total += grade; input = JOptionPane.showInputDialog("enter a grade, -1 to quit"); //convert input to an int grade = Integer.parseInt(input); }//end while //check if -1 was entered first if (counter == 0){ JOptionPane.showMessageDialog(null,"No grades entered"); System.exit(0); } //if grades where entered display the results else { JOptionPane.showMessageDialog(null, "The total of the grades is "+ total + "\nThe average is "+ (total/counter) ); } }//end main method }//end class