AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

Laboratory 4.  This algorithm reads two numbers, A and B and prints the largest value and the samllerest value.  [Input data values] Read(A,B)  [Determine.
Chapter 2 - Introduction to Java Applications
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2008.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
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.
IC211 Lecture 8 I/O: Command Line And JOptionPane.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2010.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Review. By the end of today you should be able to- Know how to use args Know how to use the JOptionPane Know how to convert a String to a number.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
1/23: Java Modular Components Identifiers: naming requirements for Java variables & objects Stepping out of the MS-DOS window: The Java Modular Component:
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
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.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
7/31: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
Review for Nested loops & Math class methods & User defined methods.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing a Line of Text 2.2.1Compiling and Executing.
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
AP Java 10/4/2016.
2.5 Another Java Application: Adding Integers
Section 64 – Manipulating Data Using Methods – Java Swing
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to Java Applications
Truth tables: Ways to organize results of Boolean expressions.
Chapter 2 - Introduction to Java Applications
AP Java 10/4/2016.
Truth tables: Ways to organize results of Boolean expressions.
Object Oriented Programming
CS431 ws99 Half Text Half Graphics
Chapter 2 - Introduction to Java Applications
Computer Science I: Get out your notes.
JOptionPane class.
F II 2. Simple Java Programs Objectives
Random Numbers while loop
Presentation transcript:

AP Java 10/1/2015

public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for (int count = 1; count < 10; count++) for (int count = 1; count < 10; count++) { roll = (int)(6*Math.random())+1; roll = (int)(6*Math.random())+1; System.out.print(roll); System.out.print(roll); } } // end main } // end main} What do you think this program does? Type it in BlueJ and test it.

Math.random(); Returns a double value in the range 0<=Math.random() < 1. Returns a double value in the range 0<=Math.random() < 1. You can change its type from double to int by type casting. You can change its type from double to int by type casting. roll = (int)(6*Math.random())+1; roll = (int)(6*Math.random())+1;

Random Practice Describe the range of random values generated from the following. Describe the range of random values generated from the following. int one = (int)(6*Math.random()) + 1; int one = (int)(6*Math.random()) + 1; int roll = (int)(10*Math.random()) + 6; int roll = (int)(10*Math.random()) + 6; int roll = (int)(20*Math.random()) - 3; int roll = (int)(20*Math.random()) - 3; int roll = 2* ((int)(6*Math.random())) + 10; int roll = 2* ((int)(6*Math.random())) + 10; int roll = 2* ((int)(6*Math.random())) + 1; int roll = 2* ((int)(6*Math.random())) + 1; double roll = Math.random() + 1; double roll = Math.random() + 1; double roll = 6*Math.random() + 1; double roll = 6*Math.random() + 1;

More Random Practice Write the code needed to generate the following ranges of random numbers. Write the code needed to generate the following ranges of random numbers. 2 to 7 2 to 7 -5 to 5 -5 to 5 50 to to to 26 even 10 to 26 even A pair of 20 sided dice A pair of 20 sided dice Double values from 1 to 5 Double values from 1 to 5 Double values from 4 to 10 Double values from 4 to 10 Double values from -5 to 5 Double values from -5 to 5

Program options Flip a coin 100 times, show the total number of heads and tails AND which occurred the most often. Flip a coin 100 times, show the total number of heads and tails AND which occurred the most often. Roll a pair of 6 sided dice 100 times. Find out which occurs most often, 3 or 11. Show how often each of these rolls occur and which occurs most often. Roll a pair of 6 sided dice 100 times. Find out which occurs most often, 3 or 11. Show how often each of these rolls occur and which occurs most often. Widget walk. Put a widget (any graphic, circle, dot,…) on the screen. Have it move randomly for 100 steps. Widget walk. Put a widget (any graphic, circle, dot,…) on the screen. Have it move randomly for 100 steps. Use Swing to create a GUI for this program. Push: Look up JOptionPane in Java Docs and incorporate a method not described in today’s presentation.

Java Swing 101 Basic Input and Output using Java Swing.

GUI 101 with javax.swing // Fig. 3.17: Dialog1.java // Printing multiple lines in dialog box. import javax.swing.JOptionPane; public class Dialog1 { public static void main( String args[] ) public static void main( String args[] ) { // display a dialog with the message // display a dialog with the message JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); } // end main } // end main } // end class Dialog1 Look at help files for JOptionPane

Input with swing // Fig. 3.18: NameDialog.java // Basic input with a dialog box. import javax.swing.JOptionPane; public class NameDialog { public static void main( String args[] ) public static void main( String args[] ) { // prompt user to enter name // prompt user to enter name String name = String name = JOptionPane.showInputDialog( "What is your name?" ); JOptionPane.showInputDialog( "What is your name?" ); // create the message // create the message String message = String message = String.format( "Welcome, %s, to Java Programming!", name ); String.format( "Welcome, %s, to Java Programming!", name ); // display the message to welcome the user by name // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); JOptionPane.showMessageDialog( null, message ); } // end main } // end main } // end class NameDialog

Swing input stuff It only enters a string, not a double or int. It only enters a string, not a double or int. So to enter numbers you will need to convert them to integers or doubles So to enter numbers you will need to convert them to integers or doubles To convert a String into an integer. To convert a String into an integer. int answer=Integer.parseInt(name); int answer=Integer.parseInt(name); To convert to a double: To convert to a double: double answer=Double.parseDouble(name); double answer=Double.parseDouble(name);

import javax.swing.JOptionPane; public class NameDialog { public static void main( String [] args) public static void main( String [] args) { // prompt user to enter name String name = String name = JOptionPane.showInputDialog( "What is your name?" ); JOptionPane.showInputDialog( "What is your name?" ); //Prompt for thei age //Prompt for thei age String ageString = String ageString = JOptionPane.showInputDialog( "What is your age?" ); JOptionPane.showInputDialog( "What is your age?" ); int age = Integer.parseInt(ageString); int age = Integer.parseInt(ageString); // create the message // create the message String message = String message = String.format( "Welcome, %s, to Java Programming! \n %d", name, age ); String.format( "Welcome, %s, to Java Programming! \n %d", name, age ); // display the message to welcome the user by name // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); JOptionPane.showMessageDialog( null, message ); } // end main } // end main } // end class NameDialog String For int, Use %f for double

Swing Program Tree height calculator: ◦Input the distance you are from a tree and the angle you look up to the top of the tree. ◦Output: The height of the tree. ◦Calculation: tree height = the height of your eyes + (Distance from the tree) * tan(angle) ◦Look up the Math.tan() method to determine if the angle is in degrees or radians. ◦Push: Research to find out how to use this information to estimate the number of ‘board- foot’ volume of the standing tree.