Shlomo Hershkop 20071 Basics overview. Shlomo Hershkop 20072 Basic Review - Overview Practice coding Practice coding finger guessing game finger guessing.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Building Java Programs
Written by: Dr. JJ Shepherd
Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while.
Arrays part 2 Applications & such. Returning an array from a method A method can return an array, just like it can return any other kind of variable;
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Games and Simulations O-O Programming in Java The Walker School
CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies.
Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Applications in Java Towson University *Ref:
Writing Classes You have already used classes –String, Random, Scanner, Math, Graphics, etc –To use a class: import the class or the package containing.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
MOM! Phineas and Ferb are … Aims:
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
Best Practices. Contents Bad Practices Good Practices.
Implementation of the Hangman Game in C++
A Simple Quiz: Ask User Functions. By Lana Dyck under the direction of Professor Susan Rodger Duke University June 2009, added Part 2 July 2011.
Code reading skills LEVEL GAME.  Skeleton has error messages.  Notice the red lines on right slider. Click… you’ll go to an error.  pieces = levels.getPieces();
10-Nov-15 Java Object Oriented Programming What is it?
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SEEM Java – Basic Introduction, Classes and Objects.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Creating a GUI Class An example of class design using inheritance and interfaces.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Written by: Dr. JJ Shepherd
Week 9 - Friday.  What did we talk about last time?  Static method practice  Finished the Game of Life.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Introduction to TouchDevelop Lesson 3 – Comments & Lists Created by S. Johnson
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Objective of the lesson Use Blockly to make a dice for Snakes and Ladders All of you will: – Make an image which displays when you press a button Most.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Building Java Programs
Conditions and Ifs BIS1523 – Lecture 8.
Principles of Software Development
Outline Boolean Expressions The if Statement Comparing Data
Truth tables: Ways to organize results of Boolean expressions.
Loops CIS 40 – Introduction to Programming in Python
Introduction to TouchDevelop
Alorithms4 Bank Owner.
Truth tables: Ways to organize results of Boolean expressions.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
More on Creating Classes
Random Numbers while loop
What to expect this week
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

Shlomo Hershkop Basics overview

Shlomo Hershkop Basic Review - Overview Practice coding Practice coding finger guessing game finger guessing game Useful classes Useful classes Vector class Vector class Random Random Practice Practice Rolling dice game Rolling dice game Slot machine simulation Slot machine simulation Iterators Iterators

Shlomo Hershkop Reminder Lets just review some of the basics and get to some code Lets just review some of the basics and get to some code

Shlomo Hershkop control You can control a code block (commands between braces) You can control a code block (commands between braces) if condition statement if condition statement while loop statement while loop statement

Shlomo Hershkop Exceptions Can look for problems by surrounding your code with try and catch blocks Can look for problems by surrounding your code with try and catch blocks The Exception class, is the most general exception, will always catch everything The Exception class, is the most general exception, will always catch everything

Shlomo Hershkop random Many times in programming would like to get a random number Many times in programming would like to get a random number Can ask the user for it Can ask the user for it Use a built in library which produces pseudo random numbers Use a built in library which produces pseudo random numbers

Shlomo Hershkop Math.random() Math.random() So what is this ? So what is this ?

Shlomo Hershkop Math.random() Math.random() Math class Math class It’s a static method since we don’t create an instance of the math class to get to it It’s a static method since we don’t create an instance of the math class to get to it

Shlomo Hershkop Next Lets write a small program Lets write a small program Will want to simulate a game, say tossing a few dice Will want to simulate a game, say tossing a few dice Say you have 2 users, who ever has more points wins! Say you have 2 users, who ever has more points wins!

Shlomo Hershkop Eclipse 1. Start Eclipse 2. Start a new project (right click in project explorer) 3. Double click, right click on default package and start a new class 4. Call it DiceTest 1. Check off that you want comments 2. Check off you want a main

Shlomo Hershkop Next Create a class called Dice Create a class called Dice Check off you want comments Check off you want comments Should have two members: Should have two members: private final int MAX = 8;//8 sided dice private final int MAX = 8;//8 sided dice private int faceValue;//the face of the die private int faceValue;//the face of the die Create empty constructor Create empty constructor Will be initializing the die, choose any number less than the MAX Will be initializing the die, choose any number less than the MAX

Shlomo Hershkop More stuff Need to add a method Need to add a method roll() roll() This will simulate a dice roll This will simulate a dice roll How to write this ? Any ideas ? How to write this ? Any ideas ?

Shlomo Hershkop public void roll () { public void roll () { faceValue = (int)(Math.random() * MAX +1) faceValue = (int)(Math.random() * MAX +1) notice the cast …. ?? notice the cast …. ?? Why the +1 Why the +1

Shlomo Hershkop next Need to add accessor methods to get the face value Need to add accessor methods to get the face value Also lets define a toString method to allow us to print it out Also lets define a toString method to allow us to print it out

Shlomo Hershkop public int getFace(){ public int getFace(){ return faceValue; } public String toString(){ public String toString(){ return “face: “ + faceValue; }

Shlomo Hershkop One more Lets add a way to set the dice face Lets add a way to set the dice face Important to make sure that it isn’t more than max!! Important to make sure that it isn’t more than max!!

Shlomo Hershkop code public void setFace(int val) throws Exception { public void setFace(int val) throws Exception { if(val > MAX){ if(val > MAX){ throw new Exception(“too high value”); throw new Exception(“too high value”);} faceValue = val; }

Shlomo Hershkop Back to main Ok how would main look like ? Ok how would main look like ? We want to toss the dice twice for each user and add up their results to see who is the winner We want to toss the dice twice for each user and add up their results to see who is the winner Start coding… Start coding…

Shlomo Hershkop Dice d1,d2; int user1,user2; d1 = new Dice(), d2 = new Dice(); d1.roll(); d2.roll(); user1 = d1.getFace() + d2.getFace(); //roll again for user 2 d1.roll(); d2.roll(); user2 = d1.getFace() + d2.getFace(); //now compare if( user1 > user2) { System.out.println(“User 1 has won with “ + user1); System.out.println(“User 1 has won with “ + user1); } else if (user2 > user1) { System.out.println(“User 2 has won with “ + user2); } else { System.out.println(“It was tied at “ + user1); }

Shlomo Hershkop Make sure it compiles correctly Make sure it compiles correctly Now run it correctly Now run it correctly Now print out both dice at users to see how each user received the points they did Now print out both dice at users to see how each user received the points they did

Shlomo Hershkop Next In your main, call the setFace method and get it to compile In your main, call the setFace method and get it to compile

Shlomo Hershkop Moving on How would you adopt the code to run over say 5 dice without having to create 5 variables ?? How would you adopt the code to run over say 5 dice without having to create 5 variables ?? Please discuss it with who ever is sitting next to you Please discuss it with who ever is sitting next to you

Shlomo Hershkop solution Work with dice arrays Work with dice arrays Dice []game = new Dice[5]; Dice []game = new Dice[5]; for( int i=0; i < 5; i++){ for( int i=0; i < 5; i++){ game[i] = new Dice(); } Same for rolling and adding results Same for rolling and adding results

Shlomo Hershkop Back to theory Vector class Vector class Iterators Iterators

Shlomo Hershkop Vector Class Arrays allow you to hold a specific amount of things Arrays allow you to hold a specific amount of things But need to know ahead of time how many But need to know ahead of time how many Would like to have an array which don’t need to know how many items will hold Would like to have an array which don’t need to know how many items will hold

Shlomo Hershkop Vector Vector vec = new Vector(); Vector vec = new Vector(); vec.add(10); vec.add(10); vec.add(23); vec.add(23); etc etc Then can say something like Then can say something like vec.size(); vec.size(); vec.elementAt(0); vec.elementAt(0); vec.remove(2); vec.remove(2);

Shlomo Hershkop Iterator Iterator is an object which allows you to step through (and maybe modify) a collection of objects in some order without knowing the underlying representation. Iterator is an object which allows you to step through (and maybe modify) a collection of objects in some order without knowing the underlying representation. Allows loose coupling between collections and users. Allows loose coupling between collections and users. Simplest example: Simplest example: We want to walk through the vector, but don’t want to depend on the size when we start We want to walk through the vector, but don’t want to depend on the size when we start Imagine someone might be adding things into the vector as we are looking at it Imagine someone might be adding things into the vector as we are looking at it

Shlomo Hershkop Iterator Simple Simple Can call a method called Can call a method called hasNext() hasNext() Tell us if there is something else Tell us if there is something else next() next() Get the next item Get the next item

Shlomo Hershkop Vector Iterator Iterator ourIter = somevec.iterator(); Iterator ourIter = somevec.iterator(); while( ourIter.hasNext() ){ while( ourIter.hasNext() ){ Object obj = ourIter.next(); Object obj = ourIter.next(); So something with obj So something with obj }

Shlomo Hershkop More code Lets have some fun Lets have some fun Imagine you have a slot machine Imagine you have a slot machine Say you get a random number when you play the machine Say you get a random number when you play the machine Only get payoff if you hit a prime number Only get payoff if you hit a prime number Say 0 – 100 Say 0 – 100 What do we need to do to get this working ? What do we need to do to get this working ?

Shlomo Hershkop Useful method private boolean isPrime(int p) { private boolean isPrime(int p) { int i; int i; for(i=2; i<(p/2); i++) { for(i=2; i<(p/2); i++) { if ( i*(p/i) == p ) if ( i*(p/i) == p )return(false); } return(true); return(true); }

Shlomo Hershkop next Code the main class Code the main class SlotMachineGame SlotMachineGame Include main Include main Should define a max number Should define a max number Should have a way to roll the slot machine Should have a way to roll the slot machine Check if prime, if yes return payment (say 5) Check if prime, if yes return payment (say 5) Else return 0 (nothing) Else return 0 (nothing)

Shlomo Hershkop Question Can you write the program so that if you play the game 50 times in a row (say it costs you a dollar to play) Can you write the program so that if you play the game 50 times in a row (say it costs you a dollar to play) Will I come out ahead at the end ?? Will I come out ahead at the end ?? What about playing 40 times in a row ? What about playing 40 times in a row ?

Shlomo Hershkop Thanks and hoped you had fun Thanks and hoped you had fun