CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another.

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

Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
CS 121 – Intro to Programming:Java - Lecture 9 Announcements Two new Owl assignments up - they’re a bit more challenging. Programming assignment 5 is due.
Building Java Programs Chapter 5
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;
CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
Applications in Java Towson University *Ref:
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Lecture 2: Static Methods, if statements, homework uploader.
1 CSC 201: Computer Programming I B. S. Afolabi. Introduction  3 unit course  2 hours of lecture/week Thursdays 4.00pm – 6.00pm Mondays 4.00pm – 6.00pm.
CSC 204 Programming I Loop I The while statement.
Some Uses of Probability Randomized algorithms –for CS in general –for games and robotics in particular Testing Simulation Solving probabilistic problems.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
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,
CS 121 – Intro to Programming:Java - Lecture 7 Announcements A new Owl assignment is available. Programming assignment 4 is due on Thursday - hand in on.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-4: Static Methods and Fields.
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.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
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.
CS 121 – Intro to Programming:Java - Lecture 5 Announcements Course home page: Owl due Thursday at 11; another one up today. Third programming assignment.
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.
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
CS 121 – Intro to Programming:Java - Lecture 8 Announcements Current Owl assignment is due Wednesday. Next Owl assignment - arrays I - will be up by tomorrow.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
CS 121 – Intro to Programming:Java - Lecture 12 Announcements New Owl assignment up soon (today?) For this week: read Ch 8, sections 0 -3 Programming assignment.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
1 Building Java Programs Chapter 5 Lecture 11: Random Numbers reading: 5.1, 5.6.
CS 121 – Intro to Programming:Java - Lecture 11 Announcements Inheritance Owl assignment due 11/30 at 11 am Programming assignment six due Friday 12/3.
CompSci 230 S Programming Techniques
Values vs. References Lecture 13.
Chapter 5 The if Statement
Building Java Programs
Methods Chapter 6.
Building Java Programs
Computer Programming Methodology Input and While Loop
Midterm Review Problems
Building Java Programs
Building Java Programs
Building Java Programs
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
An Introduction to Java – Part I, language basics
Java so far Week 7.
Building Java Programs
Building Java Programs
Unit 7 - Short-Circuit Evaluation - De Morgan’s Laws - The switch statement - Type-casting in Java - Using Math.random()
Building Java Programs
Building Java Programs
Presentation transcript:

CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another one up today. Second programming assignment now due, next assignment up today, due next Thursday Read for next week MidTerm: Monday, 10/18, , Thompson 102,106 Office hours - now posted; held in LGRT 220 I will accept late programming assignments on Thursday - but this is it: beginning with #3, late rules will be in effect

Random numbers Java has a Random class (in the java.util package) It produces pseudo-random numbers Random r = new Random(); vs Random r = new Random(303); r.nextInt(); vs r.nextInt(1000 r.nextDouble() -> produces a random value between 0 and 1.0

import java.util.Random; public class RandomTester{ public static void main(String[] args) { Random r1 = new Random(202); Random r2 = new Random(303); System.out.println(" from r1: " + r1.nextInt()); System.out.println(" from r2: " + r2.nextInt()); } } œ from r1: œ from r2:

A real use for random numbers (more or less..) We know that √2 = ~ This says that if I choose 1000 numbers between 1 and 2, about 414 should fall below √2 -- the others should be above this value. We can actually use this (probabilistic) technique to compute this value (unimportant) and other values (important!) Here’s the code… Note: if r is a Random object, the statement val = r.nextDouble(); copies a random value between 0 and 1 into val. To get a random value between 1 and 2, try (1 + val); for a random value between 13 and 14: (13 + val)

import java.util.Random; public class RootTwo{ public static void main(String[] args){ Random r = new Random(); int ct = 0; double val; for(int j = 0; j < 1000; j++){ val = r.nextDouble(); if ((1 + val) * (1 + val) < 2) ct++; } System.out.println("Square root of 2 is~ " + (1 + (double)ct/1000)); System.out.println(“Using Math class: “ + Math.sqrt(2)); } } jGRASP exec: java RootTwo œœßœSquare root of 2 is~ Using Math class:

An aside: sqrt(num) doesn’t really belong to a particular object -- in a way it’s too basic. Instead, sqrt is a class method or static method. No object is needed - you just associate it with the class: Math.sqrt(someNum); same with Math.max(a,b)

The game of Craps The basic game of craps is very simple. On the first roll of two dice (the come-out roll), the shooter wins by rolling either a 7 or 11 (a natural). Rolling craps (2, 3, or 12) loses. Any other number (4, 5, 6, 8, 9, or 10) is called the point. Now to win, the point number must be rolled before a 7. If a 7 is rolled before the point number, the shooter loses; if the point comes up first, the shooter wins.

import java.util.*; class CrapsTester { public static void main(String args[]) { public int gameCount = ; Craps1 C= new Craps1(); int count = 0; for (int i = 0; i < gameCount; i++) if (C.crapsRun()) count++; System.out.println((float)count / gameCount); } }

// The Craps1 class has one attribute - the random // object r. It has three methods: throwDie, throwDice, // and crapsRun import java.util.Random; public class Craps1 { Random r = new Random(); public int throwDie(){ return (1 +r.nextInt(6));} public int throwDice(){ return (throwDie() + throwDie());}

public boolean crapsRun() // returns true if player wins { int point = 0; int cur; cur = throwDice(); switch(cur) {{ case 7: case 11: return(true); case 2: case 3: case 12: return(false); default: point = cur; } while (true) { cur = throwDice(); if (cur == point) return(true); else if (cur == 7) return(false);} }

public boolean crapsRun2() { int point = 0; int cur; boolean done = false; boolean result= false; cur = throwDice(); switch(cur){ case 7: case 11: {done = true; result = true; break;} case 2: case 3: case 12: {done = true; result = false; break;} default: point = cur; } if (done) return result; else while (true) { cur = throwDice(); if (cur == point) return(true); else if (cur == 7) return(false);}}

public class Test{ public static void main(String[] args){ int num = ; int splitCount = 0; while (num > 1){ num = num/2; splitCount++; } System.out.println("number of splits: " + splitCount); } } ----jGRASP exec: java Test œœßœnumber of splits: 18

import element.*; public class Test{ public static void main(String[] args){ ConsoleWindow c = new ConsoleWindow(); c.out.println("Enter some large integer "); int num = c.input.readInt(); int splitCount = 0; while (num > 1){ c.out.println("number " + num); num = num/2; splitCount++; } c.out.println(); // a blank line in the console c.out.println("number of splits: " + splitCount); } }