MAHARISHI INTERNATIONAL UNIVERSITY 1971-1995 M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature.

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

1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright © Recursive GCD Demo public class.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Programming Methodology (1). Implementing Methods main.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
 Setter methods ◦ public methods that set the value of instance variables to a value specified by the call to the argument ◦ Setter methods do not violate.
MAHARISHI INTERNATIONAL UNIVERSITY M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature.
MAHARISHI INTERNATIONAL UNIVERSITY M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature.
MAHARISHI INTERNATIONAL UNIVERSITY M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature.
MAHARISHI INTERNATIONAL UNIVERSITY M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature.
MAHARISHI INTERNATIONAL UNIVERSITY M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature.
LAB 10.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
CMSC 132 Week2 Lab1 Comparable vs Comparator clone() finalize()
Introduction to Objects A way to create our own types.
Programming Progamz pls. Importance VERY IMPORTANT.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Java operatoriai sandbolts/operators.html
Object-Oriented Programming Simple Stack Implementation.
Indentation & Readability. What does this program do? public class Hello { public static void main ( String[] args ) { //display initial message System.out.println(
Lab 1 Logbook ADT. OVERVIEW A monthly logbook consists of a set of entries, one for each day of the month.
State Design Pattern. Behavioral Pattern Allows object to alter its behavior when internal state changes Uses Polymorphism to define different behaviors.
Take out a piece of paper and PEN. The quiz starts ONE minute after the tardy bell rings. You will have 45 – 90 seconds per question. Determine the output.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Library Books exercise cosc The classes Library class +ArrayList -ArrayList + Library() +initializeDefaultCategories():void +displayAllCategories():void.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Output Programs These slides will present a variety of small programs. Each program has a compound condition which uses the Boolean Logic that was introduced.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
David Stotts Computer Science Department UNC Chapel Hill.
Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Staples are our staple Building upon our solution.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Advanced Programming TA Session 2
using System; namespace Demo01 { class Program
Sum of natural numbers class SumOfNaturalNumbers {
Interface.
Abstract Class, Interface, Package
Java operatoriai
Something about Java Introduction to Problem Solving and Programming 1.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
ATS Application Programming: Java Programming
Functions Used to write code only once Can use parameters.
Computing Adjusted Quiz Total Score
null, true, and false are also reserved.
TO COMPLETE THE FOLLOWING:
Introduction to Java Programming
Interfaces and Constructors
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
Chapter 11: More on the Implications of Inheritance
Code Animation Examples
Recursive GCD Demo public class Euclid {
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Java Programming with Multiple Classes
if-else if (condition) { statements1 } else { statements2
Scope of variables class scopeofvars {
while while (condition) { statements }
Recursion Method calling itself (circular definition)
Midterm 2 Review Lecture 23.
Topics OOP Review Inheritance Review Abstract Classes
Midterm 2 Review Lecture 23.
State Design Pattern.
Presentation transcript:

MAHARISHI INTERNATIONAL UNIVERSITY M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature

Lab 5: State Pattern

State : Example without state pattern public class Application { static public void main(String args[]) { Person p=new Person(); p.setState("hungry"); p.talk(); p.setState("happy"); p.talk(); p.setState("sleepy"); p.talk(); p.setState("dirty"); p.talk(); } public class Person { private String state; public void setState(String st){ state=st; } public void talk(){ if (state.equals("hungry")) System.out.println("I want food"); else if (state.equals("sleepy")) System.out.println("I want to sleep"); else if (state.equals("dirty")) System.out.println("I need a bath"); else if (state.equals("happy")) System.out.println("I feel good"); }

State : Example with state pattern public class Application { static public void main(String args[]) { Person p=new HungryPerson(); p.talk(); p=new HappyPerson(); p.talk(); p=new SleepyPerson(); p.talk(); p=new DirtyPerson(); p.talk(); } public interface Person { public void talk(); } public class DirtyPerson implements Person{ public void talk(){ System.out.println("I need a bath"); } public class SleepyPerson implements Person{ public void talk(){ System.out.println("I want to sleep"); } public class HappyPerson implements Person{ public void talk(){ System.out.println("I feel good"); } public class HungryPerson implements Person{ public void talk(){ System.out.println("I want food"); }

Shift car simulation speedshift x=0park 0<x<51 5<x<102 10<x<303 30<x<554 x>555 State : Lab

State : Lab without pattern public class Car{ public int changeSpeed(int speed){ if (speed == 0) { return 0; } else { if (speed > 0 && speed < 5) { return 1; } else { if (speed > 5 && speed < 10) { return 2; } else { if (speed > 10 && speed < 30) { return 3; } else { if (speed > 30 && speed < 55) { return 4; } else { if (speed > 55 ) { return 5; } return 0; }