AP Java 10/4/2016.

Slides:



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

Chapter 2 - Introduction to Java Applications
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
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.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2008.
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.
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
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.
 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.
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.
习 题 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.
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.
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.
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.
 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.
Creating and Using Class Methods. Definition Class Object.
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.
AP Java Java’s version of Repeat until.
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.
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.
3 Introduction to Classes and Objects.
CS 201 Lecture 7: John Hurley Summer 2012 Cal State LA.
Introduction to Classes and Objects
John Hurley CS201 Cal State LA
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
2.5 Another Java Application: Adding Integers
Section 64 – Manipulating Data Using Methods – Java Swing
Chapter 3 Java Input/Output.
Chapter 2 - Introduction to Java Applications
CS18000: Problem Solving and Object-Oriented Programming
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.
Chapter 3 Input/Output.
int [] scores = new int [10];
Object Oriented Programming
Truth tables: Ways to organize results of Boolean expressions.
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
int [] scores = new int [10];
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

AP Java 10/4/2016

Learning Objectives Be able to use Java’s Swing class to make windows applications Be able to convert from String to int and double. Be able to use Math.random() and type casting to generate ranges of random numbers.

string a = "infinity"; 1. ) int b = a. length() - 4;. b = 2 string a = "infinity"; 1.) int b = a.length() - 4; b = 2.) int c = a.indexOf("ty") + b; c = 3.) boolean d = !a.equals("infinity"); d = 4.) String e = a.substring(4); e = 5.) String f = a.substring(1, 8); f = 6.) boolean g = a.equals("infInity"); g = 7.) int h = a.indexOf("z"); h = 8.) int i = a.length() - b/g; i = 9.) int j = a.substring(2); j = 10.) int k = a.indexOf("ini") * c; k =

Basic Input and Output using Java Swing.

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

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[] ) // prompt user to enter name String name = JOptionPane.showInputDialog( "What is your name?" ); // create the message String message = String.format( "Welcome, %s, to Java Programming!", name ); // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); } // end main } // end class NameDialog Input with swing

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

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

Swing Program Tree height calculator: Resisting Change 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. Will you need any other information? Resisting Change Input: The resistance in Ohms of two resistors wired in parallel (r1 and r2). Output: The net resistance. NetResistance = r1*r2 / (r1 + r2) Push: Be able to find the resistance given three resisters connected in parallel. Push: Be able to find the resistance of n resistors connected in parallel. Let the user enter the number of resisters.

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

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

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

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

Use Swing to create a GUI for this program. Program options Use Swing to create a GUI for this program. 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. Widget walk. Put a widget (any graphic, circle, dot,…) on the screen. Have it move randomly for 100 steps. Push: Look up JOptionPane in Java Docs and incorporate a method not described in today’s presentation.