Day 23 5.1 Programming with Methods. The “do” loop The “do” loop is a type of while loop that only does one iteration (does one loop at least) It is hardly.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
{ int fun = 99; } Scope refers to whether the variable is seen within a certain context Any variable defined inside of curly brackets, only exists within.
Fundamentals of Software Development 1Slide 1 Review: Why start with WordGames? You wrote your first lines of code in this course in WordGames.You wrote.
Java Syntax. Basic Output public class test1 { public static void main(String[] args) { System.out.println("Hello"); }
Important Notes Pay attention to file names!!! You must name them what we tell you to. mkdir – make a folder (make the ones we tell you to) rm – remove.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST.
 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.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Constructors. Defining Constructors  A constructor is a special kind of method that is designed to perform initializations, such as giving values to.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
UML Basics & Access Modifier
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
“I am the way and the truth and the life. No one comes to the Father except through me” John 14:6.
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: Ms Mihaela Cocea (from ) Room London Knowledge Lab Emerald.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
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.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
The 9 th and 10 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/2/2012 and 10/3/2012 -Review loop structures and improve CheckISBN and CourseAverage.
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: (from ) Ms Mihaela Cocea Room London Knowledge Lab Emerald.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things.
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 +
Jeopardy Print Me Which loop? Call Me Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it.
Method Examples CS 139 Algorithm Development 10/06/2008.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
(Dreaded) Quiz 2 Next Monday.
Sophomore Scholars Java
Chapter 2 Clarifications
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Arrays in Classes and Methods
using System; namespace Demo01 { class Program
5.1 Programming with Methods
“I am the way and the truth and the life
Conditionals and Loops
Repetition.
Something about Java Introduction to Problem Solving and Programming 1.
BIT115: Introduction to Programming
Building Java Programs
Building Java Programs
Building Java Programs
Michele Weigle - COMP 14 - Spr 04
חלק ה שימוש במציין שלם לערך תווי
Java Language Basics.
Building Java Programs
The Basics of Recursion
Building Java Programs
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Building Java Programs
Building Java Programs
More on iterations using
Optional Topic: User Input with Scanner
Presentation transcript:

Day Programming with Methods

The “do” loop The “do” loop is a type of while loop that only does one iteration (does one loop at least) It is hardly ever used

Let’s write the Oracle class UML Class Diagram: Oracle - oldAnswer = “The answer is in your heart.”: String - newAnswer: String - question: String + dialog(): void + answerOne(): void + seekAdvice(): void

import java.util.*; public class Day_23_ChatterBox { … private instance variables

public void dialog() { String ans; Scanner s = new Scanner(System.in); do { answerOne(); // we’ll program this later System.out.println(“Do you wish to ask another question?”); ans = s.next(); } while (ans.equalsIgnoreCase(“yes)); System.out.println(“The oracle will now rest.”); }

private void answerOne() { System.out.println(“I am the oracle.”); System.out.println(“I will answer any one-line question.”); System.out.println(“What is your question?”); Scanner s = new Scanner(System.in); question = keyboard.nextLine(); seekAdvice(); System.out.println(“You asked the question:”); System.out.println(question); System.out.println(“Now, here is my answer:”); System.out.println(oldAnswer); update(); }

private void seekAdvice() { System.out.println(“Hmm, I need some help on that.”); System.out.println(“Please give me one line of advice.”); Scanner keyboard = new Scanner(System.in); newAnswer = keyboard.nextLine(); System.out.println(“Thank you. That helped a lot.”); }

private void update() { oldAnswer = newAnswer; }

public class OracleDemo { public static void main(String [] args) { Oracle delphi = new Oracle(); delphi.dialog(); } }

Do homework on wiki to prepare for test